Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | SCGI processing now working on Nginx. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
788f9d011879b838f9763632f6cdaf0c |
User & Date: | drh 2017-12-13 15:09:30.548 |
Context
2017-12-13
| ||
15:25 | Add a "package provide" to the main script. Further documentation updates. (check-in: 97b3ea1716 user: drh tags: trunk) | |
15:09 | SCGI processing now working on Nginx. (check-in: 788f9d0118 user: drh tags: trunk) | |
13:00 | Refactor the command-line argument. Update documentation accordingly. (check-in: 69cae4edc3 user: drh tags: trunk) | |
Changes
Changes to test01.tcl.
︙ | ︙ | |||
9 10 11 12 13 14 15 16 17 18 19 20 21 22 | wapp "<h1>Hello, World!</h1>\n" wapp "<ol>" wapp-unsafe "<li><p><a href='$R/env'>Wapp Environment</a></p>\n" wapp-unsafe "<li><p><a href='$B/fullenv'>Full Environment</a>\n" wapp-unsafe "<li><p><a href='$B/lint'>Lint</a>\n" wapp-unsafe "<li><p><a href='$B/errorout'>Deliberate error</a>\n" wapp "</ol>" } proc wapp-page-env {} { global wapp wapp-set-cookie env-cookie simple wapp "<h1>Wapp Environment</h1>\n" wapp-unsafe "<form method='GET' action='[dict get $wapp SELF_URL]'>\n" wapp "<input type='checkbox' name='showhdr'" | > > > | 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | wapp "<h1>Hello, World!</h1>\n" wapp "<ol>" wapp-unsafe "<li><p><a href='$R/env'>Wapp Environment</a></p>\n" wapp-unsafe "<li><p><a href='$B/fullenv'>Full Environment</a>\n" wapp-unsafe "<li><p><a href='$B/lint'>Lint</a>\n" wapp-unsafe "<li><p><a href='$B/errorout'>Deliberate error</a>\n" wapp "</ol>" if {[dict exists $wapp showenv]} { wapp-page-env } } proc wapp-page-env {} { global wapp wapp-set-cookie env-cookie simple wapp "<h1>Wapp Environment</h1>\n" wapp-unsafe "<form method='GET' action='[dict get $wapp SELF_URL]'>\n" wapp "<input type='checkbox' name='showhdr'" |
︙ | ︙ | |||
41 42 43 44 45 46 47 | wapp "<h1>Wapp Full Environment</h1>\n" wapp-unsafe "<form method='POST' action='[dict get $wapp SELF_URL]'>\n" wapp "<input type='checkbox' name='var1'" if {[dict exists $wapp showhdr]} { wapp " checked" } wapp "> Var1\n" | | > > | 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | wapp "<h1>Wapp Full Environment</h1>\n" wapp-unsafe "<form method='POST' action='[dict get $wapp SELF_URL]'>\n" wapp "<input type='checkbox' name='var1'" if {[dict exists $wapp showhdr]} { wapp " checked" } wapp "> Var1\n" wapp "<input type='submit' name='s1' value='Go'>\n" wapp "<input type='hidden' name='hidden-parameter-1' " wapp "value='the long value / of ?$ hidden-1..<hi>'>\n" wapp "</form>" wapp "<pre>\n" foreach var [lsort [dict keys $wapp]] { if {$var==".reply"} continue wapp-escape-html "$var = [list [dict get $wapp $var]]\n\n" } wapp "</pre>" |
︙ | ︙ |
Changes to wapp.tcl.
︙ | ︙ | |||
141 142 143 144 145 146 147 | && [info exists env(GATEWAY_INTERFACE)] && $env(GATEWAY_INTERFACE)=="CGI/1.0") || $mode=="cgi" } { wappInt-handle-cgi-request return } | | > > | | | > > > > > > > | | | | | | | | | | 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | && [info exists env(GATEWAY_INTERFACE)] && $env(GATEWAY_INTERFACE)=="CGI/1.0") || $mode=="cgi" } { wappInt-handle-cgi-request return } if {$mode=="scgi"} { wappInt-start-listener $port 1 0 1 } elseif {$mode=="server"} { wappInt-start-listener $port 0 0 0 } else { wappInt-start-listener $port 1 1 0 } vwait ::forever } # Start up a listening socket. Arrange to invoke wappInt-new-connection # for each inbound HTTP connection. # # localonly - If true, listen on 127.0.0.1 only # # browser - If true, launch a web browser pointing to the new server # proc wappInt-start-listener {port localonly browser scgi} { if {$scgi} { set type SCGI set server [list wappInt-new-connection wappInt-scgi-readable] } else { set type HTTP set server [list wappInt-new-connection wappInt-http-readable] } if {$localonly} { set x [socket -server $server -myaddr 127.0.0.1 $port] } else { set x [socket -server $server $port] } set coninfo [chan configure $x -sockname] set port [lindex $coninfo 2] if {$browser} { wappInt-start-browser http://127.0.0.1:$port/ } else { puts "Listening for $type requests on TCP port $port" } } # Start a web-browser and point it at $URL # proc wappInt-start-browser {url} { global tcl_platform if {$tcl_platform(platform)=="windows"} { exec cmd /c start $url & } elseif {$tcl_platform(os)=="Darwin"} { exec open $url & } elseif {[catch {exec xdg-open $url}]} { exec firefox $url & } } # Accept a new inbound HTTP request # proc wappInt-new-connection {callback chan ip port} { upvar #0 wappInt-$chan W set W [dict create REMOTE_ADDR $ip REMOTE_PORT $port .header {}] fconfigure $chan -blocking 0 -translation binary fileevent $chan readable [list $callback $chan] } # Close an input channel # proc wappInt-close-channel {chan} { if {$chan=="stdout"} { # This happens after completing a CGI request exit 0 } else { unset ::wappInt-$chan close $chan } } # Process new text received on an inbound HTTP request # proc wappInt-http-readable {chan} { if {[catch [list wappInt-http-readable-unsafe $chan] msg]} { puts stderr "$msg\n$::errorInfo" wappInt-close-channel $chan } } proc wappInt-http-readable-unsafe {chan} { upvar #0 wappInt-$chan W wapp wapp if {![dict exists $W .toread]} { # If the .toread key is not set, that means we are still reading # the header set line [string trimright [gets $chan]] set n [string length $line] if {$n>0} { |
︙ | ︙ | |||
316 317 318 319 320 321 322 | global wapp dict set wapp .reply {} dict set wapp .mimetype {text/html; charset=utf-8} dict set wapp .reply-code {200 Ok} # Set up additional CGI environment values # | < < < < < < < < < < < < < > > > > > > > > > > > > > > > > > > > > > | 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | global wapp dict set wapp .reply {} dict set wapp .mimetype {text/html; charset=utf-8} dict set wapp .reply-code {200 Ok} # Set up additional CGI environment values # if {![dict exists $wapp HTTP_HOST]} { dict set wapp BASE_URL {} } elseif {[dict exists $wapp HTTPS]} { dict set wapp BASE_URL https://[dict get $wapp HTTP_HOST] } else { dict set wapp BASE_URL http://[dict get $wapp HTTP_HOST] } if {![dict exists $wapp REQUEST_URI]} { dict set wapp REQUEST_URI / } elseif {[regsub {\?.*} [dict get $wapp REQUEST_URI] {} newR]} { # Some servers (ex: nginx) append the query parameters to REQUEST_URI. # These need to be stripped off dict set wapp REQUEST_URI $newR } if {[dict exists $wapp SCRIPT_NAME]} { dict append wapp BASE_URL [dict get $wapp SCRIPT_NAME] } else { dict set wapp SCRIPT_NAME {} } if {![dict exists $wapp PATH_INFO]} { # If PATH_INFO is missing (ex: nginx) the construct it set URI [dict get $wapp REQUEST_URI] set skip [string length [dict get $wapp SCRIPT_NAME]] dict set wapp PATH_INFO [string range $URI $skip end] } if {[regexp {^/([^/]+)(.*)$} [dict get $wapp PATH_INFO] all head tail]} { dict set wapp PATH_HEAD $head dict set wapp PATH_TAIL [string trimleft $tail /] } else { dict set wapp PATH_INFO {} dict set wapp PATH_HEAD {} dict set wapp PATH_TAIL {} } dict set wapp SELF_URL [dict get $wapp BASE_URL]/[dict get $wapp PATH_HEAD] # Parse query parameters from the query string, the cookies, and # POST data # if {[dict exists $wapp HTTP_COOKIE]} { |
︙ | ︙ | |||
376 377 378 379 380 381 382 | && [dict exists $wapp CONTENT] && [dict exists $wapp HTTP_REFERER] && [string match [dict get $wapp BASE_URL]/* [dict get $wapp HTTP_REFERER]] } { foreach qterm [split [string trim [dict get $wapp CONTENT]] &] { set qsplit [split $qterm =] set nm [lindex $qsplit 0] | | | 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | && [dict exists $wapp CONTENT] && [dict exists $wapp HTTP_REFERER] && [string match [dict get $wapp BASE_URL]/* [dict get $wapp HTTP_REFERER]] } { foreach qterm [split [string trim [dict get $wapp CONTENT]] &] { set qsplit [split $qterm =] set nm [lindex $qsplit 0] if {[regexp {^[a-z][-a-z0-9_]*$} $nm]} { dict set wapp $nm [wappInt-url-decode [lindex $qsplit 1]] } } } # To-Do: Perhaps add support for multipart/form-data decoding. # Alternatively, perhaps multipart/form-data decoding can be done # by application code using a separate helper function, like |
︙ | ︙ | |||
524 525 526 527 528 529 530 | } if {$len>0} { fconfigure stdin -translation binary dict set wapp CONTENT [read stdin $len] } wappInt-handle-request stdout 1 } | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 | } if {$len>0} { fconfigure stdin -translation binary dict set wapp CONTENT [read stdin $len] } wappInt-handle-request stdout 1 } # Process new text received on an inbound SCGI request # proc wappInt-scgi-readable {chan} { if {[catch [list wappInt-scgi-readable-unsafe $chan] msg]} { puts stderr "$msg\n$::errorInfo" wappInt-close-channel $chan } } proc wappInt-scgi-readable-unsafe {chan} { upvar #0 wappInt-$chan W wapp wapp if {![dict exists $W .toread]} { # If the .toread key is not set, that means we are still reading # the header. # # An SGI header is short. This implementation assumes the entire # header is available all at once. # set req [read $chan 15] set n [string length $req] scan $req %d:%s len hdr incr len [string length "$len:,"] append hdr [read $chan [expr {$len-15}]] foreach {nm val} [split $hdr \000] { if {$nm==","} break dict set W $nm $val } set len 0 if {[dict exists $W CONTENT_LENGTH]} { set len [dict get $W CONTENT_LENGTH] } if {$len>0} { # Still need to read the query content dict set W .toread $len } else { # There is no query content, so handle the request immediately set wapp $W wappInt-handle-request $chan 0 } } else { # If .toread is set, that means we are reading the query content. # Continue reading until .toread reaches zero. set got [read $chan [dict get $W .toread]] dict append W CONTENT $got dict set W .toread [expr {[dict get $W .toread]-[string length $got]}] if {[dict get $W .toread]<=0} { # Handle the request as soon as all the query content is received set wapp $W wappInt-handle-request $chan 0 } } } |