Debian Codegraph @

www/search

raw
1 #!/usr/bin/tclsh.docsrc
2 #### Import of wapp.tcl
3 # Copyright (c) 2017 D. Richard Hipp
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the Simplified BSD License (also
7 # known as the "2-Clause License" or "FreeBSD License".)
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but without any warranty; without even the implied warranty of
11 # merchantability or fitness for a particular purpose.
12 #
13 #---------------------------------------------------------------------------
14 #
15 # Design rules:
16 #
17 # (1) All identifiers in the global namespace begin with "wapp"
18 #
19 # (2) Indentifiers intended for internal use only begin with "wappInt"
20 #
21 package require Tcl 8.6
22
23 # Add text to the end of the HTTP reply. No interpretation or transformation
24 # of the text is performs. The argument should be enclosed within {...}
25 #
26 proc wapp {txt} {
27 global wapp
28 dict append wapp .reply $txt
29 }
30
31 # Add text to the page under construction. Do no escaping on the text.
32 #
33 # Though "unsafe" in general, there are uses for this kind of thing.
34 # For example, if you want to return the complete, unmodified content of
35 # a file:
36 #
37 # set fd [open content.html rb]
38 # wapp-unsafe [read $fd]
39 # close $fd
40 #
41 # You could do the same thing using ordinary "wapp" instead of "wapp-unsafe".
42 # The difference is that wapp-safety-check will complain about the misuse
43 # of "wapp", but it assumes that the person who write "wapp-unsafe" understands
44 # the risks.
45 #
46 # Though occasionally necessary, the use of this interface should be minimized.
47 #
48 proc wapp-unsafe {txt} {
49 global wapp
50 dict append wapp .reply $txt
51 }
52
53 # Add text to the end of the reply under construction. The following
54 # substitutions are made:
55 #
56 # %html(...) Escape text for inclusion in HTML
57 # %url(...) Escape text for use as a URL
58 # %qp(...) Escape text for use as a URI query parameter
59 # %string(...) Escape text for use within a JSON string
60 # %unsafe(...) No transformations of the text
61 #
62 # The substitutions above terminate at the first ")" character. If the
63 # text of the TCL string in ... contains ")" characters itself, use instead:
64 #
65 # %html%(...)%
66 # %url%(...)%
67 # %qp%(...)%
68 # %string%(...)%
69 # %unsafe%(...)%
70 #
71 # In other words, use "%(...)%" instead of "(...)" to include the TCL string
72 # to substitute.
73 #
74 # The %unsafe substitution should be avoided whenever possible, obviously.
75 # In addition to the substitutions above, the text also does backslash
76 # escapes.
77 #
78 # The wapp-trim proc works the same as wapp-subst except that it also removes
79 # whitespace from the left margin, so that the generated HTML/CSS/Javascript
80 # does not appear to be indented when delivered to the client web browser.
81 #
82 if {$tcl_version>=8.7} {
83 proc wapp-subst {txt} {
84 global wapp
85 regsub -all -command \
86 {%(html|url|qp|string|unsafe){1,1}?(|%)\((.+)\)\2} $txt wappInt-enc txt
87 dict append wapp .reply [subst -novariables -nocommand $txt]
88 }
89 proc wapp-trim {txt} {
90 global wapp
91 regsub -all {\n\s+} [string trim $txt] \n txt
92 regsub -all -command \
93 {%(html|url|qp|string|unsafe){1,1}?(|%)\((.+)\)\2} $txt wappInt-enc txt
94 dict append wapp .reply [subst -novariables -nocommand $txt]
95 }
96 proc wappInt-enc {all mode nu1 txt} {
97 return [uplevel 2 "wappInt-enc-$mode \"$txt\""]
98 }
99 } else {
100 proc wapp-subst {txt} {
101 global wapp
102 regsub -all {%(html|url|qp|string|unsafe){1,1}?(|%)\((.+)\)\2} $txt \
103 {[wappInt-enc-\1 "\3"]} txt
104 dict append wapp .reply [uplevel 1 [list subst -novariables $txt]]
105 }
106 proc wapp-trim {txt} {
107 global wapp
108 regsub -all {\n\s+} [string trim $txt] \n txt
109 regsub -all {%(html|url|qp|string|unsafe){1,1}?(|%)\((.+)\)\2} $txt \
110 {[wappInt-enc-\1 "\3"]} txt
111 dict append wapp .reply [uplevel 1 [list subst -novariables $txt]]
112 }
113 }
114
115 # There must be a wappInt-enc-NAME routine for each possible substitution
116 # in wapp-subst. Thus there are routines for "html", "url", "qp", and "unsafe".
117 #
118 # wappInt-enc-html Escape text so that it is safe to use in the
119 # body of an HTML document.
120 #
121 # wappInt-enc-url Escape text so that it is safe to pass as an
122 # argument to href= and src= attributes in HTML.
123 #
124 # wappInt-enc-qp Escape text so that it is safe to use as the
125 # value of a query parameter in a URL or in
126 # post data or in a cookie.
127 #
128 # wappInt-enc-string Escape ", ', \, and < for using inside of a
129 # javascript string literal. The < character
130 # is escaped to prevent "</script>" from causing
131 # problems in embedded javascript.
132 #
133 # wappInt-enc-unsafe Perform no encoding at all. Unsafe.
134 #
135 proc wappInt-enc-html {txt} {
136 return [string map {& &amp; < &lt; > &gt; \" &quot; \\ &#92;} $txt]
137 }
138 proc wappInt-enc-unsafe {txt} {
139 return $txt
140 }
141 proc wappInt-enc-url {s} {
142 if {[regsub -all {[^-{}\\@~?=#_.:/a-zA-Z0-9]} $s {[wappInt-%HHchar {&}]} s]} {
143 set s [subst -novar -noback $s]
144 }
145 if {[regsub -all {[\\{}]} $s {[wappInt-%HHchar \\&]} s]} {
146 set s [subst -novar -noback $s]
147 }
148 return $s
149 }
150 proc wappInt-enc-qp {s} {
151 if {[regsub -all {[^-{}\\_.a-zA-Z0-9]} $s {[wappInt-%HHchar {&}]} s]} {
152 set s [subst -novar -noback $s]
153 }
154 if {[regsub -all {[\\{}]} $s {[wappInt-%HHchar \\&]} s]} {
155 set s [subst -novar -noback $s]
156 }
157 return $s
158 }
159 proc wappInt-enc-string {s} {
160 return [string map {\\ \\\\ \" \\\" ' \\' < \\u003c \n \\n \r \\r
161 \f \\f \t \\t \x01 \\u0001 \x02 \\u0002 \x03 \\u0003
162 \x04 \\u0004 \x05 \\u0005 \x06 \\u0006 \x07 \\u0007
163 \x0b \\u000b \x0e \\u000e \x0f \\u000f \x10 \\u0010
164 \x11 \\u0011 \x12 \\u0012 \x13 \\u0013 \x14 \\u0014
165 \x15 \\u0015 \x16 \\u0016 \x17 \\u0017 \x18 \\u0018
166 \x19 \\u0019 \x1a \\u001a \x1b \\u001b \x1c \\u001c
167 \x1d \\u001d \x1e \\u001e \x1f \\u001f} $s]
168 }
169
170 # This is a helper routine for wappInt-enc-url and wappInt-enc-qp. It returns
171 # an appropriate %HH encoding for the single character c. If c is a unicode
172 # character, then this routine might return multiple bytes: %HH%HH%HH
173 #
174 proc wappInt-%HHchar {c} {
175 if {$c==" "} {return +}
176 return [regsub -all .. [binary encode hex [encoding convertto utf-8 $c]] {%&}]
177 }
178
179
180 # Undo the www-url-encoded format.
181 #
182 # HT: This code stolen from ncgi.tcl
183 #
184 proc wappInt-decode-url {str} {
185 set str [string map [list + { } "\\" "\\\\" \[ \\\[ \] \\\]] $str]
186 regsub -all -- \
187 {%([Ee][A-Fa-f0-9])%([89ABab][A-Fa-f0-9])%([89ABab][A-Fa-f0-9])} \
188 $str {[encoding convertfrom utf-8 [binary decode hex \1\2\3]]} str
189 regsub -all -- \
190 {%([CDcd][A-Fa-f0-9])%([89ABab][A-Fa-f0-9])} \
191 $str {[encoding convertfrom utf-8 [binary decode hex \1\2]]} str
192 regsub -all -- {%([0-7][A-Fa-f0-9])} $str {\\u00\1} str
193 return [subst -novar $str]
194 }
195
196 # Reset the document back to an empty string.
197 #
198 proc wapp-reset {} {
199 global wapp
200 dict set wapp .reply {}
201 }
202
203 # Change the mime-type of the result document.
204 #
205 proc wapp-mimetype {x} {
206 global wapp
207 dict set wapp .mimetype $x
208 }
209
210 # Change the reply code.
211 #
212 proc wapp-reply-code {x} {
213 global wapp
214 dict set wapp .reply-code $x
215 }
216
217 # Set a cookie
218 #
219 proc wapp-set-cookie {name value} {
220 global wapp
221 dict lappend wapp .new-cookies $name $value
222 }
223
224 # Unset a cookie
225 #
226 proc wapp-clear-cookie {name} {
227 wapp-set-cookie $name {}
228 }
229
230 # Add extra entries to the reply header
231 #
232 proc wapp-reply-extra {name value} {
233 global wapp
234 dict lappend wapp .reply-extra $name $value
235 }
236
237 # Specifies how the web-page under construction should be cached.
238 # The argument should be one of:
239 #
240 # no-cache
241 # max-age=N (for some integer number of seconds, N)
242 # private,max-age=N
243 #
244 proc wapp-cache-control {x} {
245 wapp-reply-extra Cache-Control $x
246 }
247
248 # Redirect to a different web page
249 #
250 proc wapp-redirect {uri} {
251 wapp-reply-code {307 Redirect}
252 wapp-reply-extra Location $uri
253 }
254
255 # Return the value of a wapp parameter
256 #
257 proc wapp-param {name {dflt {}}} {
258 global wapp
259 if {![dict exists $wapp $name]} {return $dflt}
260 return [dict get $wapp $name]
261 }
262
263 # Return true if a and only if the wapp parameter $name exists
264 #
265 proc wapp-param-exists {name} {
266 global wapp
267 return [dict exists $wapp $name]
268 }
269
270 # Set the value of a wapp parameter
271 #
272 proc wapp-set-param {name value} {
273 global wapp
274 dict set wapp $name $value
275 }
276
277 # Return all parameter names that match the GLOB pattern, or all
278 # names if the GLOB pattern is omitted.
279 #
280 proc wapp-param-list {{glob {*}}} {
281 global wapp
282 return [dict keys $wapp $glob]
283 }
284
285 # By default, Wapp does not decode query parameters and POST parameters
286 # for cross-origin requests. This is a security restriction, designed to
287 # help prevent cross-site request forgery (CSRF) attacks.
288 #
289 # As a consequence of this restriction, URLs for sites generated by Wapp
290 # that contain query parameters will not work as URLs found in other
291 # websites. You cannot create a link from a second website into a Wapp
292 # website if the link contains query planner, by default.
293 #
294 # Of course, it is sometimes desirable to allow query parameters on external
295 # links. For URLs for which this is safe, the application should invoke
296 # wapp-allow-xorigin-params. This procedure tells Wapp that it is safe to
297 # go ahead and decode the query parameters even for cross-site requests.
298 #
299 # In other words, for Wapp security is the default setting. Individual pages
300 # need to actively disable the cross-site request security if those pages
301 # are safe for cross-site access.
302 #
303 proc wapp-allow-xorigin-params {} {
304 global wapp
305 if {![dict exists $wapp .qp] && ![dict get $wapp SAME_ORIGIN]} {
306 wappInt-decode-query-params
307 }
308 }
309
310 # Set the content-security-policy.
311 #
312 # The default content-security-policy is very strict: "default-src 'self'"
313 # The default policy prohibits the use of in-line javascript or CSS.
314 #
315 # Provide an alternative CSP as the argument. Or use "off" to disable
316 # the CSP completely.
317 #
318 proc wapp-content-security-policy {val} {
319 global wapp
320 if {$val=="off"} {
321 dict unset wapp .csp
322 } else {
323 dict set wapp .csp $val
324 }
325 }
326
327 # Examine the bodys of all procedures in this program looking for
328 # unsafe calls to various Wapp interfaces. Return a text string
329 # containing warnings. Return an empty string if all is ok.
330 #
331 # This routine is advisory only. It misses some constructs that are
332 # dangerous and flags others that are safe.
333 #
334 proc wapp-safety-check {} {
335 set res {}
336 foreach p [info command] {
337 set ln 0
338 foreach x [split [info body $p] \n] {
339 incr ln
340 if {[regexp {^[ \t]*wapp[ \t]+([^\n]+)} $x all tail]
341 && [string index $tail 0]!="\173"
342 && [regexp {[[$]} $tail]
343 } {
344 append res "$p:$ln: unsafe \"wapp\" call: \"[string trim $x]\"\n"
345 }
346 if {[regexp {^[ \t]*wapp-(subst|trim)[ \t]+[^\173]} $x all cx]} {
347 append res "$p:$ln: unsafe \"wapp-$cx\" call: \"[string trim $x]\"\n"
348 }
349 }
350 }
351 return $res
352 }
353
354 # Return a string that descripts the current environment. Applications
355 # might find this useful for debugging.
356 #
357 proc wapp-debug-env {} {
358 global wapp
359 set out {}
360 foreach var [lsort [dict keys $wapp]] {
361 if {[string index $var 0]=="."} continue
362 append out "$var = [list [dict get $wapp $var]]\n"
363 }
364 append out "\[pwd\] = [list [pwd]]\n"
365 return $out
366 }
367
368 # Tracing function for each HTTP request. This is overridden by wapp-start
369 # if tracing is enabled.
370 #
371 proc wappInt-trace {} {}
372
373 # Start up a listening socket. Arrange to invoke wappInt-new-connection
374 # for each inbound HTTP connection.
375 #
376 # port Listen on this TCP port. 0 means to select a port
377 # that is not currently in use
378 #
379 # wappmode One of "scgi", "remote-scgi", "server", or "local".
380 #
381 # fromip If not {}, then reject all requests from IP addresses
382 # other than $fromip
383 #
384 proc wappInt-start-listener {port wappmode fromip} {
385 if {[string match *scgi $wappmode]} {
386 set type SCGI
387 set server [list wappInt-new-connection \
388 wappInt-scgi-readable $wappmode $fromip]
389 } else {
390 set type HTTP
391 set server [list wappInt-new-connection \
392 wappInt-http-readable $wappmode $fromip]
393 }
394 if {$wappmode=="local" || $wappmode=="scgi"} {
395 set x [socket -server $server -myaddr 127.0.0.1 $port]
396 } else {
397 set x [socket -server $server $port]
398 }
399 set coninfo [chan configure $x -sockname]
400 set port [lindex $coninfo 2]
401 if {$wappmode=="local"} {
402 wappInt-start-browser http://127.0.0.1:$port/
403 } elseif {$fromip!=""} {
404 puts "Listening for $type requests on TCP port $port from IP $fromip"
405 } else {
406 puts "Listening for $type requests on TCP port $port"
407 }
408 }
409
410 # Start a web-browser and point it at $URL
411 #
412 proc wappInt-start-browser {url} {
413 global tcl_platform
414 if {$tcl_platform(platform)=="windows"} {
415 exec cmd /c start $url &
416 } elseif {$tcl_platform(os)=="Darwin"} {
417 exec open $url &
418 } elseif {[catch {exec xdg-open $url}]} {
419 exec firefox $url &
420 }
421 }
422
423 # This routine is a "socket -server" callback. The $chan, $ip, and $port
424 # arguments are added by the socket command.
425 #
426 # Arrange to invoke $callback when content is available on the new socket.
427 # The $callback will process inbound HTTP or SCGI content. Reject the
428 # request if $fromip is not an empty string and does not match $ip.
429 #
430 proc wappInt-new-connection {callback wappmode fromip chan ip port} {
431 upvar #0 wappInt-$chan W
432 if {$fromip!="" && ![string match $fromip $ip]} {
433 close $chan
434 return
435 }
436 set W [dict create REMOTE_ADDR $ip REMOTE_PORT $port WAPP_MODE $wappmode \
437 .header {}]
438 fconfigure $chan -blocking 0 -translation binary
439 fileevent $chan readable [list $callback $chan]
440 }
441
442 # Close an input channel
443 #
444 proc wappInt-close-channel {chan} {
445 if {$chan=="stdout"} {
446 # This happens after completing a CGI request
447 exit 0
448 } else {
449 unset ::wappInt-$chan
450 close $chan
451 }
452 }
453
454 # Process new text received on an inbound HTTP request
455 #
456 proc wappInt-http-readable {chan} {
457 if {[catch [list wappInt-http-readable-unsafe $chan] msg]} {
458 puts stderr "$msg\n$::errorInfo"
459 wappInt-close-channel $chan
460 }
461 }
462 proc wappInt-http-readable-unsafe {chan} {
463 upvar #0 wappInt-$chan W wapp wapp
464 if {![dict exists $W .toread]} {
465 # If the .toread key is not set, that means we are still reading
466 # the header
467 set line [string trimright [gets $chan]]
468 set n [string length $line]
469 if {$n>0} {
470 if {[dict get $W .header]=="" || [regexp {^\s+} $line]} {
471 dict append W .header $line
472 } else {
473 dict append W .header \n$line
474 }
475 if {[string length [dict get $W .header]]>100000} {
476 error "HTTP request header too big - possible DOS attack"
477 }
478 } elseif {$n==0} {
479 # We have reached the blank line that terminates the header.
480 global argv0
481 if {[info exists ::argv0]} {
482 set a0 [file normalize $argv0]
483 } else {
484 set a0 /
485 }
486 dict set W SCRIPT_FILENAME $a0
487 dict set W DOCUMENT_ROOT [file dir $a0]
488 if {[wappInt-parse-header $chan]} {
489 catch {close $chan}
490 return
491 }
492 set len 0
493 if {[dict exists $W CONTENT_LENGTH]} {
494 set len [dict get $W CONTENT_LENGTH]
495 }
496 if {$len>0} {
497 # Still need to read the query content
498 dict set W .toread $len
499 } else {
500 # There is no query content, so handle the request immediately
501 set wapp $W
502 wappInt-handle-request $chan
503 }
504 }
505 } else {
506 # If .toread is set, that means we are reading the query content.
507 # Continue reading until .toread reaches zero.
508 set got [read $chan [dict get $W .toread]]
509 dict append W CONTENT $got
510 dict set W .toread [expr {[dict get $W .toread]-[string length $got]}]
511 if {[dict get $W .toread]<=0} {
512 # Handle the request as soon as all the query content is received
513 set wapp $W
514 wappInt-handle-request $chan
515 }
516 }
517 }
518
519 # Decode the HTTP request header.
520 #
521 # This routine is always running inside of a [catch], so if
522 # any problems arise, simply raise an error.
523 #
524 proc wappInt-parse-header {chan} {
525 upvar #0 wappInt-$chan W
526 set hdr [split [dict get $W .header] \n]
527 if {$hdr==""} {return 1}
528 set req [lindex $hdr 0]
529 dict set W REQUEST_METHOD [set method [lindex $req 0]]
530 if {[lsearch {GET HEAD POST} $method]<0} {
531 error "unsupported request method: \"[dict get $W REQUEST_METHOD]\""
532 }
533 set uri [lindex $req 1]
534 set split_uri [split $uri ?]
535 set uri0 [lindex $split_uri 0]
536 if {![regexp {^/[-.a-z0-9_/]*$} $uri0]} {
537 error "invalid request uri: \"$uri0\""
538 }
539 dict set W REQUEST_URI $uri0
540 dict set W PATH_INFO $uri0
541 set uri1 [lindex $split_uri 1]
542 dict set W QUERY_STRING $uri1
543 set n [llength $hdr]
544 for {set i 1} {$i<$n} {incr i} {
545 set x [lindex $hdr $i]
546 if {![regexp {^(.+): +(.*)$} $x all name value]} {
547 error "invalid header line: \"$x\""
548 }
549 set name [string toupper $name]
550 switch -- $name {
551 REFERER {set name HTTP_REFERER}
552 USER-AGENT {set name HTTP_USER_AGENT}
553 CONTENT-LENGTH {set name CONTENT_LENGTH}
554 CONTENT-TYPE {set name CONTENT_TYPE}
555 HOST {set name HTTP_HOST}
556 COOKIE {set name HTTP_COOKIE}
557 ACCEPT-ENCODING {set name HTTP_ACCEPT_ENCODING}
558 default {set name .hdr:$name}
559 }
560 dict set W $name $value
561 }
562 return 0
563 }
564
565 # Decode the QUERY_STRING parameters from a GET request or the
566 # application/x-www-form-urlencoded CONTENT from a POST request.
567 #
568 # This routine sets the ".qp" element of the ::wapp dict as a signal
569 # that query parameters have already been decoded.
570 #
571 proc wappInt-decode-query-params {} {
572 global wapp
573 dict set wapp .qp 1
574 if {[dict exists $wapp QUERY_STRING]} {
575 foreach qterm [split [dict get $wapp QUERY_STRING] &] {
576 set qsplit [split $qterm =]
577 set nm [lindex $qsplit 0]
578 if {[regexp {^[a-z][a-z0-9]*$} $nm]} {
579 dict set wapp $nm [wappInt-decode-url [lindex $qsplit 1]]
580 }
581 }
582 }
583 if {[dict exists $wapp CONTENT_TYPE] && [dict exists $wapp CONTENT]} {
584 set ctype [dict get $wapp CONTENT_TYPE]
585 if {$ctype=="application/x-www-form-urlencoded"} {
586 foreach qterm [split [string trim [dict get $wapp CONTENT]] &] {
587 set qsplit [split $qterm =]
588 set nm [lindex $qsplit 0]
589 if {[regexp {^[a-z][-a-z0-9_]*$} $nm]} {
590 dict set wapp $nm [wappInt-decode-url [lindex $qsplit 1]]
591 }
592 }
593 } elseif {[string match multipart/form-data* $ctype]} {
594 regexp {^(.*?)\r\n(.*)$} [dict get $wapp CONTENT] all divider body
595 set ndiv [string length $divider]
596 while {[string length $body]} {
597 set idx [string first $divider $body]
598 set unit [string range $body 0 [expr {$idx-3}]]
599 set body [string range $body [expr {$idx+$ndiv+2}] end]
600 if {[regexp {^Content-Disposition: form-data; (.*?)\r\n\r\n(.*)$} \
601 $unit unit hdr content]} {
602 if {[regexp {name="(.*)"; filename="(.*)"\r\nContent-Type: (.*?)$}\
603 $hdr hr name filename mimetype]} {
604 dict set wapp $name.filename \
605 [string map [list \\\" \" \\\\ \\] $filename]
606 dict set wapp $name.mimetype $mimetype
607 dict set wapp $name.content $content
608 } elseif {[regexp {name="(.*)"} $hdr hr name]} {
609 dict set wapp $name $content
610 }
611 }
612 }
613 }
614 }
615 }
616
617 # Invoke application-supplied methods to generate a reply to
618 # a single HTTP request.
619 #
620 # This routine uses the global variable ::wapp and so must not be nested.
621 # It must run to completion before the next instance runs. If a recursive
622 # instances of this routine starts while another is running, the the
623 # recursive instance is added to a queue to be invoked after the current
624 # instance finishes. Yes, this means that WAPP IS SINGLE THREADED. Only
625 # a single page rendering instance my be running at a time. There can
626 # be multiple HTTP requests inbound at once, but only one my be processed
627 # at a time once the request is full read and parsed.
628 #
629 set wappIntPending {}
630 set wappIntLock 0
631 proc wappInt-handle-request {chan} {
632 global wappIntPending wappIntLock
633 fileevent $chan readable {}
634 if {$wappIntLock} {
635 # Another instance of request is already running, so defer this one
636 lappend wappIntPending [list wappInt-handle-request $chan]
637 return
638 }
639 set wappIntLock 1
640 catch [list wappInt-handle-request-unsafe $chan]
641 set wappIntLock 0
642 if {[llength $wappIntPending]>0} {
643 # If there are deferred requests, then launch the oldest one
644 after idle [lindex $wappIntPending 0]
645 set wappIntPending [lrange $wappIntPending 1 end]
646 }
647 }
648 proc wappInt-handle-request-unsafe {chan} {
649 global wapp
650 dict set wapp .reply {}
651 dict set wapp .mimetype {text/html; charset=utf-8}
652 dict set wapp .reply-code {200 Ok}
653 dict set wapp .csp {default-src 'self'}
654
655 # Set up additional CGI environment values
656 #
657 if {![dict exists $wapp HTTP_HOST]} {
658 dict set wapp BASE_URL {}
659 } elseif {[dict exists $wapp HTTPS]} {
660 dict set wapp BASE_URL https://[dict get $wapp HTTP_HOST]
661 } else {
662 dict set wapp BASE_URL http://[dict get $wapp HTTP_HOST]
663 }
664 if {![dict exists $wapp REQUEST_URI]} {
665 dict set wapp REQUEST_URI /
666 } elseif {[regsub {\?.*} [dict get $wapp REQUEST_URI] {} newR]} {
667 # Some servers (ex: nginx) append the query parameters to REQUEST_URI.
668 # These need to be stripped off
669 dict set wapp REQUEST_URI $newR
670 }
671 if {[dict exists $wapp SCRIPT_NAME]} {
672 dict append wapp BASE_URL [dict get $wapp SCRIPT_NAME]
673 } else {
674 dict set wapp SCRIPT_NAME {}
675 }
676 if {![dict exists $wapp PATH_INFO]} {
677 # If PATH_INFO is missing (ex: nginx) then construct it
678 set URI [dict get $wapp REQUEST_URI]
679 set skip [string length [dict get $wapp SCRIPT_NAME]]
680 dict set wapp PATH_INFO [string range $URI $skip end]
681 }
682 if {[regexp {^/([^/]+)(.*)$} [dict get $wapp PATH_INFO] all head tail]} {
683 dict set wapp PATH_HEAD $head
684 dict set wapp PATH_TAIL [string trimleft $tail /]
685 } else {
686 dict set wapp PATH_INFO {}
687 dict set wapp PATH_HEAD {}
688 dict set wapp PATH_TAIL {}
689 }
690 dict set wapp SELF_URL [dict get $wapp BASE_URL]/[dict get $wapp PATH_HEAD]
691
692 # Parse query parameters from the query string, the cookies, and
693 # POST data
694 #
695 if {[dict exists $wapp HTTP_COOKIE]} {
696 foreach qterm [split [dict get $wapp HTTP_COOKIE] {;}] {
697 set qsplit [split [string trim $qterm] =]
698 set nm [lindex $qsplit 0]
699 if {[regexp {^[a-z][-a-z0-9_]*$} $nm]} {
700 dict set wapp $nm [wappInt-decode-url [lindex $qsplit 1]]
701 }
702 }
703 }
704 set same_origin 0
705 if {[dict exists $wapp HTTP_REFERER]} {
706 set referer [dict get $wapp HTTP_REFERER]
707 set base [dict get $wapp BASE_URL]
708 if {$referer==$base || [string match $base/* $referer]} {
709 set same_origin 1
710 }
711 }
712 dict set wapp SAME_ORIGIN $same_origin
713 if {$same_origin} {
714 wappInt-decode-query-params
715 }
716
717 # Invoke the application-defined handler procedure for this page
718 # request. If an error occurs while running that procedure, generate
719 # an HTTP reply that contains the error message.
720 #
721 wapp-before-dispatch-hook
722 wappInt-trace
723 set mname [dict get $wapp PATH_HEAD]
724 if {[catch {
725 if {$mname!="" && [llength [info command wapp-page-$mname]]>0} {
726 wapp-page-$mname
727 } else {
728 wapp-default
729 }
730 } msg]} {
731 if {[wapp-param WAPP_MODE]=="local" || [wapp-param WAPP_MODE]=="server"} {
732 puts "ERROR: $::errorInfo"
733 }
734 wapp-reset
735 wapp-reply-code "500 Internal Server Error"
736 wapp-mimetype text/html
737 wapp-trim {
738 <h1>Wapp Application Error</h1>
739 <pre>%html($::errorInfo)</pre>
740 }
741 dict unset wapp .new-cookies
742 }
743 wapp-before-reply-hook
744
745 # Transmit the HTTP reply
746 #
747 if {$chan=="stdout"} {
748 puts $chan "Status: [dict get $wapp .reply-code]\r"
749 } else {
750 puts $chan "HTTP/1.1 [dict get $wapp .reply-code]\r"
751 puts $chan "Server: wapp\r"
752 puts $chan "Connection: close\r"
753 }
754 if {[dict exists $wapp .reply-extra]} {
755 foreach {name value} [dict get $wapp .reply-extra] {
756 puts $chan "$name: $value\r"
757 }
758 }
759 if {[dict exists $wapp .csp]} {
760 puts $chan "Content-Security-Policy: [dict get $wapp .csp]\r"
761 }
762 set mimetype [dict get $wapp .mimetype]
763 puts $chan "Content-Type: $mimetype\r"
764 if {[dict exists $wapp .new-cookies]} {
765 foreach {nm val} [dict get $wapp .new-cookies] {
766 if {[regexp {^[a-z][-a-z0-9_]*$} $nm]} {
767 if {$val==""} {
768 puts $chan "Set-Cookie: $nm=; HttpOnly; Path=/; Max-Age=1\r"
769 } else {
770 set val [wappInt-enc-url $val]
771 puts $chan "Set-Cookie: $nm=$val; HttpOnly; Path=/\r"
772 }
773 }
774 }
775 }
776 if {[string match text/* $mimetype]} {
777 set reply [encoding convertto utf-8 [dict get $wapp .reply]]
778 if {[regexp {\ygzip\y} [wapp-param HTTP_ACCEPT_ENCODING]]} {
779 catch {
780 set x [zlib gzip $reply]
781 set reply $x
782 puts $chan "Content-Encoding: gzip\r"
783 }
784 }
785 } else {
786 set reply [dict get $wapp .reply]
787 }
788 puts $chan "Content-Length: [string length $reply]\r"
789 puts $chan \r
790 puts -nonewline $chan $reply
791 flush $chan
792 wappInt-close-channel $chan
793 }
794
795 # This routine runs just prior to request-handler dispatch. The
796 # default implementation is a no-op, but applications can override
797 # to do additional transformations or checks.
798 #
799 proc wapp-before-dispatch-hook {} {return}
800
801 # This routine runs after the request-handler dispatch and just
802 # before the reply is generated. The default implementation is
803 # a no-op, but applications can override to do validation and security
804 # checks on the reply, such as verifying that no sensitive information
805 # such as an API key or password is accidentally included in the
806 # reply text.
807 #
808 proc wapp-before-reply-hook {} {return}
809
810 # Process a single CGI request
811 #
812 proc wappInt-handle-cgi-request {} {
813 global wapp env
814 foreach key [array names env {[A-Z]*}] {dict set wapp $key $env($key)}
815 set len 0
816 if {[dict exists $wapp CONTENT_LENGTH]} {
817 set len [dict get $wapp CONTENT_LENGTH]
818 }
819 if {$len>0} {
820 fconfigure stdin -translation binary
821 dict set wapp CONTENT [read stdin $len]
822 }
823 dict set wapp WAPP_MODE cgi
824 fconfigure stdout -translation binary
825 wappInt-handle-request-unsafe stdout
826 }
827
828 # Process new text received on an inbound SCGI request
829 #
830 proc wappInt-scgi-readable {chan} {
831 if {[catch [list wappInt-scgi-readable-unsafe $chan] msg]} {
832 puts stderr "$msg\n$::errorInfo"
833 wappInt-close-channel $chan
834 }
835 }
836 proc wappInt-scgi-readable-unsafe {chan} {
837 upvar #0 wappInt-$chan W wapp wapp
838 if {![dict exists $W .toread]} {
839 # If the .toread key is not set, that means we are still reading
840 # the header.
841 #
842 # An SGI header is short. This implementation assumes the entire
843 # header is available all at once.
844 #
845 dict set W .remove_addr [dict get $W REMOTE_ADDR]
846 set req [read $chan 15]
847 set n [string length $req]
848 scan $req %d:%s len hdr
849 incr len [string length "$len:,"]
850 append hdr [read $chan [expr {$len-15}]]
851 foreach {nm val} [split $hdr \000] {
852 if {$nm==","} break
853 dict set W $nm $val
854 }
855 set len 0
856 if {[dict exists $W CONTENT_LENGTH]} {
857 set len [dict get $W CONTENT_LENGTH]
858 }
859 if {$len>0} {
860 # Still need to read the query content
861 dict set W .toread $len
862 } else {
863 # There is no query content, so handle the request immediately
864 dict set W SERVER_ADDR [dict get $W .remove_addr]
865 set wapp $W
866 wappInt-handle-request $chan
867 }
868 } else {
869 # If .toread is set, that means we are reading the query content.
870 # Continue reading until .toread reaches zero.
871 set got [read $chan [dict get $W .toread]]
872 dict append W CONTENT $got
873 dict set W .toread [expr {[dict get $W .toread]-[string length $got]}]
874 if {[dict get $W .toread]<=0} {
875 # Handle the request as soon as all the query content is received
876 dict set W SERVER_ADDR [dict get $W .remove_addr]
877 set wapp $W
878 wappInt-handle-request $chan
879 }
880 }
881 }
882
883 # Start up the wapp framework. Parameters are a list passed as the
884 # single argument.
885 #
886 # -server $PORT Listen for HTTP requests on this TCP port $PORT
887 #
888 # -local $PORT Listen for HTTP requests on 127.0.0.1:$PORT
889 #
890 # -scgi $PORT Listen for SCGI requests on 127.0.0.1:$PORT
891 #
892 # -remote-scgi $PORT Listen for SCGI requests on TCP port $PORT
893 #
894 # -cgi Handle a single CGI request
895 #
896 # With no arguments, the behavior is called "auto". In "auto" mode,
897 # if the GATEWAY_INTERFACE environment variable indicates CGI, then run
898 # as CGI. Otherwise, start an HTTP server bound to the loopback address
899 # only, on an arbitrary TCP port, and automatically launch a web browser
900 # on that TCP port.
901 #
902 # Additional options:
903 #
904 # -fromip GLOB Reject any incoming request where the remote
905 # IP address does not match the GLOB pattern. This
906 # value defaults to '127.0.0.1' for -local and -scgi.
907 #
908 # -nowait Do not wait in the event loop. Return immediately
909 # after all event handlers are established.
910 #
911 # -trace "puts" each request URL as it is handled, for
912 # debugging
913 #
914 # -lint Run wapp-safety-check on the application instead
915 # of running the application itself
916 #
917 # -Dvar=value Set TCL global variable "var" to "value"
918 #
919 #
920 proc wapp-start {arglist} {
921 global env
922 set mode auto
923 set port 0
924 set nowait 0
925 set fromip {}
926 set n [llength $arglist]
927 for {set i 0} {$i<$n} {incr i} {
928 set term [lindex $arglist $i]
929 if {[string match --* $term]} {set term [string range $term 1 end]}
930 switch -glob -- $term {
931 -server {
932 incr i;
933 set mode "server"
934 set port [lindex $arglist $i]
935 }
936 -local {
937 incr i;
938 set mode "local"
939 set fromip 127.0.0.1
940 set port [lindex $arglist $i]
941 }
942 -scgi {
943 incr i;
944 set mode "scgi"
945 set fromip 127.0.0.1
946 set port [lindex $arglist $i]
947 }
948 -remote-scgi {
949 incr i;
950 set mode "remote-scgi"
951 set port [lindex $arglist $i]
952 }
953 -cgi {
954 set mode "cgi"
955 }
956 -fromip {
957 incr i
958 set fromip [lindex $arglist $i]
959 }
960 -nowait {
961 set nowait 1
962 }
963 -trace {
964 proc wappInt-trace {} {
965 set q [wapp-param QUERY_STRING]
966 set uri [wapp-param BASE_URL][wapp-param PATH_INFO]
967 if {$q!=""} {append uri ?$q}
968 puts $uri
969 }
970 }
971 -lint {
972 set res [wapp-safety-check]
973 if {$res!=""} {
974 puts "Potential problems in this code:"
975 puts $res
976 exit 1
977 } else {
978 exit
979 }
980 }
981 -D*=* {
982 if {[regexp {^.D([^=]+)=(.*)$} $term all var val]} {
983 set ::$var $val
984 }
985 }
986 default {
987 error "unknown option: $term"
988 }
989 }
990 }
991 if {$mode=="auto"} {
992 if {[info exists env(GATEWAY_INTERFACE)]
993 && [string match CGI/1.* $env(GATEWAY_INTERFACE)]} {
994 set mode cgi
995 } else {
996 set mode local
997 }
998 }
999 if {$mode=="cgi"} {
1000 wappInt-handle-cgi-request
1001 } else {
1002 wappInt-start-listener $port $mode $fromip
1003 if {!$nowait} {
1004 vwait ::forever
1005 }
1006 }
1007 }
1008
1009 # Call this version 1.0
1010 package provide wapp 1.0
1011
1012 #### End of wapp.tcl
1013
1014 # Generate all header content for the output document
1015 #
1016 proc search_header {} {
1017 wapp {
1018 <!DOCTYPE html>
1019 <html><head>
1020 <meta name="viewport" content="width=device-width, initial-scale=1.0">
1021 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
1022 <link href="sqlite.css" rel="stylesheet">
1023 <title>Search SQLite Documentation</title>
1024 <!-- path= -->
1025 </head>
1026 <body>
1027 <div class=nosearch>
1028 <a href="index.html">
1029 <img class="logo" src="images/sqlite370_banner.svg" alt="SQLite" border="0">
1030 </a>
1031 <div><!-- IE hack to prevent disappearing logo --></div>
1032 <div class="tagline desktoponly">
1033 Small. Fast. Reliable.<br>Choose any three.
1034 </div>
1035 <div class="menu mainmenu">
1036 <ul>
1037 <li><a href="index.html">Home</a>
1038 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
1039 <li class='wideonly'><a href='about.html'>About</a>
1040 <li class='desktoponly'><a href="docs.html">Documentation</a>
1041 <li class='desktoponly'><a href="download.html">Download</a>
1042 <li class='wideonly'><a href='copyright.html'>License</a>
1043 <li class='desktoponly'><a href="support.html">Support</a>
1044 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
1045 <li class='search' id='search_menubutton'>
1046 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
1047 </ul>
1048 </div>
1049 <div class="menu submenu" id="submenu">
1050 <ul>
1051 <li><a href='about.html'>About</a>
1052 <li><a href='docs.html'>Documentation</a>
1053 <li><a href='download.html'>Download</a>
1054 <li><a href='support.html'>Support</a>
1055 <li><a href='prosupport.html'>Purchase</a>
1056 </ul>
1057 </div>
1058 <div class="searchmenu" id="searchmenu">
1059 <form method="GET" action="search">
1060 <select name="s" id="searchtype">
1061 <option value="d">Search Documentation</option>
1062 <option value="c">Search Changelog</option>
1063 </select>
1064 <input type="text" name="q" id="searchbox" value="">
1065 <input type="submit" value="Go">
1066 </form>
1067 </div>
1068 </div>
1069 <script>
1070 function toggle_div(nm) {
1071 var w = document.getElementById(nm);
1072 if( w.style.display=="block" ){
1073 w.style.display = "none";
1074 }else{
1075 w.style.display = "block";
1076 }
1077 }
1078 function toggle_search() {
1079 var w = document.getElementById("searchmenu");
1080 if( w.style.display=="block" ){
1081 w.style.display = "none";
1082 } else {
1083 w.style.display = "block";
1084 setTimeout(function(){
1085 document.getElementById("searchbox").focus()
1086 }, 30);
1087 }
1088 }
1089 function div_off(nm){document.getElementById(nm).style.display="none";}
1090 window.onbeforeunload = function(e){div_off("submenu");}
1091 /* Disable the Search feature if we are not operating from CGI, since */
1092 /* Search is accomplished using CGI and will not work without it. */
1093 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
1094 document.getElementById("search_menubutton").style.display = "none";
1095 }
1096 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
1097 function hideorshow(btn,obj){
1098 var x = document.getElementById(obj);
1099 var b = document.getElementById(btn);
1100 if( x.style.display!='none' ){
1101 x.style.display = 'none';
1102 b.innerHTML='show';
1103 }else{
1104 x.style.display = '';
1105 b.innerHTML='hide';
1106 }
1107 return false;
1108 }
1109 var antiRobot = 0;
1110 function antiRobotGo(){
1111 if( antiRobot!=3 ) return;
1112 antiRobot = 7;
1113 var j = document.getElementById("mtimelink");
1114 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
1115 }
1116 function antiRobotDefense(){
1117 document.body.onmousedown=function(){
1118 antiRobot |= 2;
1119 antiRobotGo();
1120 document.body.onmousedown=null;
1121 }
1122 document.body.onmousemove=function(){
1123 antiRobot |= 2;
1124 antiRobotGo();
1125 document.body.onmousemove=null;
1126 }
1127 setTimeout(function(){
1128 antiRobot |= 1;
1129 antiRobotGo();
1130 }, 100)
1131 antiRobotGo();
1132 }
1133 antiRobotDefense();
1134 </script>
1135 }
1136 }
1137
1138 #-------------------------------------------------------------------------
1139 # Add an entry to the log database for the current query. Which
1140 # returns $nRes results.
1141 #
1142 proc search_add_log_entry {nRes} {
1143 if {[wapp-param-exists donotlog]} return
1144 sqlite3 db2 [file dir [wapp-param SCRIPT_FILENAME]]/search.d/searchlog.db
1145 db2 timeout 10000
1146 set ip [wapp-param REMOTE_ADDR]
1147 set query [wapp-param q]
1148 db2 eval {
1149 PRAGMA synchronous=OFF;
1150 PRAGMA journal_mode=OFF;
1151 BEGIN;
1152 CREATE TABLE IF NOT EXISTS log(
1153 ip, -- IP query was made from
1154 query, -- Fts5 query string
1155 nres, -- Number of results
1156 timestamp DEFAULT CURRENT_TIMESTAMP
1157 );
1158 INSERT INTO log(ip, query, nres) VALUES($ip, $query, $nRes);
1159 COMMIT;
1160 }
1161 db2 close
1162 }
1163
1164 #-------------------------------------------------------------------------
1165 # This command is similar to the builtin Tcl [time] command, except that
1166 # it only ever runs the supplied script once. Also, instead of returning
1167 # a string like "xxx microseconds per iteration", it returns "x.yy ms" or
1168 # "x.yy s", depending on the magnitude of the time spent running the
1169 # command. For example:
1170 #
1171 # % ttime {after 1500}
1172 # 1.50 s
1173 # % ttime {after 45}
1174 # 45.02 ms
1175 #
1176 proc ttime {script} {
1177 set t [lindex [time [list uplevel $script]] 0]
1178 if {$t>1000000} { return [format "%.2f s" [expr {$t/1000000.0}]] }
1179 return [format "%.2f ms" [expr {$t/1000.0}]]
1180 }
1181
1182 #-----------------------------------------------------------------------
1183 # Do a search of the change log
1184 #
1185 proc searchchanges {} {
1186 set q [wapp-param q]
1187 if {$q==""} {return {}}
1188 if {[regexp -all \x22 $q] % 2} { append q \x22 }
1189 set x ""
1190 foreach word [split $q " "] {
1191 append x " \"[string map [list "\"" "\"\""] $word]\""
1192 }
1193 set q [string trim $x]
1194 regsub -all {[^a-zA-Z0-9_]} $q { } q
1195 set q [string trim $q]
1196 if {$q==""} {return {}}
1197 set open {<span style="background-color:#d9f2e6">}
1198 set close {</span>}
1199 set query {
1200 SELECT url, version, idx, highlight(change, 3, $open, $close) AS text
1201 FROM change($q) ORDER BY rowid ASC
1202 }
1203 wapp-trim {
1204 <p>Change log entries mentioning: <b>%html($q)</b>
1205 <table border=0>
1206 }
1207 set s2 "style=\"margin-top:0\""
1208 set s1 "style=\"font-size:larger; text-align:left\" class=nounderline"
1209 set prev ""
1210 db eval $query {
1211 if {$prev!=$version} {
1212 wapp-trim {
1213 <tr> <td %unsafe($s1) valign=top> <a href='%url($url)'>%html($version)</a>
1214 <td> <ul %unsafe($s2)>
1215 }
1216 set prev $version
1217 }
1218 wapp-subst {<li value=%html($idx)> (%html($idx)) %unsafe($text)\n}
1219 }
1220 wapp-trim {
1221 </table>
1222 <center><p>You can also see the <a href=changes.html>entire
1223 changelog as a single page</a> if you wish.</center>
1224 }
1225 }
1226
1227 #-----------------------------------------------------------------------
1228 # Do a search over all documentation other than the change log
1229 #
1230 proc searchresults {} {
1231 set q [wapp-param q]
1232 if {$q==""} {return ""}
1233
1234 # Count the '"' characters in $::A(q). If there is an odd number of
1235 # occurrences, add a " to the end of the query so that fts5 can parse
1236 # it without error.
1237 if {[regexp -all \x22 $q] % 2} { append q \x22 }
1238
1239 # Set iStart to the index of the first result to display. Results are
1240 # indexed starting at zero from most to least relevant.
1241 #
1242 set iStart 0
1243 catch {set iStart [expr {[wapp-param i 0]*10}]}
1244
1245 # Grab a list of rowid results.
1246 #
1247 set sql {
1248 SELECT rowid FROM page WHERE page MATCH $q
1249 ORDER BY srank(page) DESC,
1250 rank * COALESCE(
1251 (SELECT percent FROM weight WHERE id=page.rowid), 100
1252 );
1253 }
1254 if {[catch { set lRowid [db eval $sql] }]} {
1255 set x ""
1256 foreach word [split $q " "] {
1257 append x " \"[string map [list "\"" "\"\""] $word]\""
1258 }
1259 set q [string trim $x]
1260 set lRowid [db eval $sql]
1261 }
1262
1263 set lRes [list]
1264 foreach rowid $lRowid {
1265 if {$rowid > 1000} {
1266 set parent [expr $rowid / 1000]
1267 lappend subsections($parent) $rowid
1268 } else {
1269 lappend lRes $rowid
1270 }
1271 }
1272
1273 set nRes [llength $lRes]
1274 set lRes [lrange $lRes $iStart [expr $iStart+9]]
1275
1276 # Add an entry to the log database.
1277 #
1278 search_add_log_entry $nRes
1279
1280 # If there are no results, return a message to that effect.
1281 #
1282 if {[llength $lRes] == 0} {
1283 wapp-subst {<p>No Results for: <b>%html($q)</b>\n}
1284 }
1285
1286 # HTML markup used to highlight keywords within FTS5 generated snippets.
1287 #
1288 set open {<span style="background-color:#d9f2e6">}
1289 set close {</span>}
1290 set ellipsis {<b>&nbsp;...&nbsp;</b>}
1291
1292 # Grab the required data
1293 #
1294 db eval [string map [list %LIST% [join $lRowid ,]] {
1295 SELECT
1296 rowid AS parentid,
1297 snippet(page, 0, $open, $close, $ellipsis, 6) AS s_apis,
1298 snippet(page, 2, $open, $close, '', 40) AS s_title1,
1299 snippet(page, 3, $open, $close, $ellipsis, 40) AS s_title2,
1300 snippet(page, 4, $open, $close, $ellipsis, 40) AS s_content,
1301 url, rank
1302 FROM page($q)
1303 WHERE rowid IN (%LIST%)
1304 }] X {
1305 foreach k [array names X] { set data($X(parentid),$k) [set X($k)] }
1306 }
1307
1308 set i1 [expr {$iStart+1}]
1309 set i2 [expr {($nRes < $iStart+10) ? $nRes : $iStart+10}]
1310 wapp-trim {
1311 <table border=0>
1312 <p>Search results %html($i1)..%html($i2) of %html($nRes) for: <b>%html($q)</b>
1313 }
1314
1315 foreach rowid $lRes {
1316
1317 foreach a {parentid s_apis s_title1 s_content url rank} {
1318 set $a $data($rowid,$a)
1319 }
1320
1321 if {[info exists subsections($parentid)]} {
1322 set childid [lindex $subsections($parentid) 0]
1323 set link $data($childid,url)
1324 set hdr $data($childid,s_title2)
1325
1326 if {$hdr==""} {
1327 set s_content ""
1328 } else {
1329 set s_content [subst {
1330 <b><a style=color:#044a64 href=$link>$hdr</a></b>
1331 }]
1332 }
1333
1334 append s_content " $data($childid,s_content)"
1335 }
1336
1337 wapp-trim {<tr>
1338 <td valign=top style="line-height:150%">
1339 <div style="white-space:wrap;font-size:larger" class=nounderline>
1340 <a href="%url($url)">%unsafe($s_title1)</a>
1341 <div style="float:right;font-size:smaller;color:#BBB">(%url($url))</div>
1342 </div>
1343 <div style="margin-left: 10ex; font:larger monospace">%unsafe($s_apis)</div>
1344 <div style="margin-left: 4ex; margin-bottom:1.5em">
1345 %unsafe($s_content)
1346 </div>
1347 </td>
1348 }
1349 }
1350 wapp-subst {</table>\n}
1351
1352
1353 # If the query returned more than 10 results, add up to 10 links to
1354 # each set of 10 results (first link to results 1-10, second to 11-20,
1355 # third to 21-30, as required).
1356 #
1357 if {$nRes>10} {
1358 set s(0) {border:solid #044a64 1px;padding:1ex;margin:1ex;line-height:300%;}
1359 set s(1) "$s(0);background:#044a64;color:white"
1360 wapp-subst {<center><p>\n}
1361 for {set i 0} {$i < 10 && ($i*10)<$nRes} {incr i} {
1362 set style $s([expr {($iStart/10)==$i}])
1363 wapp-trim {
1364 <a style="%html($style)"
1365 href="search?q=%qp($q)&i=%qp($i)">%html([expr $i+1])</a>
1366 }
1367 }
1368 wapp-subst {</center>\n}
1369 }
1370 }
1371
1372 # This is the main entry point into the search result page generator
1373 #
1374 proc wapp-default {} {
1375 wapp-content-security-policy {default-src 'self' 'unsafe-inline'}
1376 wapp-allow-xorigin-params
1377 if {[wapp-param-exists env]} {
1378 search_header
1379 wapp-trim {
1380 <h1>Environment Dump For Debugging</h1>
1381 <pre>%html([wapp-debug-env])</pre>
1382 }
1383 return
1384 }
1385
1386 # When running using the built-in webserver in Wapp (in other words,
1387 # when not running as CGI) any filename that contains a "." loads
1388 # directly from the filesystem.
1389 if {[wapp-param WAPP_MODE]!="cgi"
1390 && [string match *.* [wapp-param PATH_INFO]]
1391 } {
1392 set altfile [file dir [wapp-param SCRIPT_FILENAME]][wapp-param PATH_INFO]
1393 set fd [open $altfile rb]
1394 fconfigure $fd -translation binary
1395 wapp-unsafe [read $fd]
1396 close $fd
1397 switch -glob -- $altfile {
1398 *.html {
1399 wapp-mimetype text/html
1400 }
1401 *.css {
1402 wapp-mimetype text/css
1403 }
1404 *.gif {
1405 wapp-mimetype image/gif
1406 }
1407 }
1408 return
1409 }
1410
1411 search_header
1412 sqlite3 db [file dir [wapp-param SCRIPT_FILENAME]]/search.d/search.db
1413 set searchType [wapp-param s d]
1414 if {$searchType=="c"} {
1415 set cmd searchchanges
1416 } else {
1417 set cmd searchresults
1418 }
1419 db transaction {
1420 set t [ttime {$cmd}]
1421 }
1422 wapp-trim {
1423 <center>
1424 <p>Page generated by <a href='fts5.html'>FTS5</a> in about %html($t).
1425 </center>
1426 <script>
1427 window.addEventListener('load', function() {
1428 var w = document.getElementById("searchmenu");
1429 w.style.display = "block";
1430 w = document.getElementById("searchtype");
1431 w.value = "%string($searchType)"
1432 setTimeout(function(){
1433 var s = document.getElementById("searchbox");
1434 s.value = "%string([wapp-param q])"
1435 s.focus();
1436 s.select();
1437 }, 30);
1438 });
1439 </script>
1440 }
1441 }
1442 wapp-start $argv