ncgi.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 package require Tcl 8.2
00028 package require fileutil ;
00029
00030 package provide ncgi 1.3.2
00031
00032 namespace ::ncgi {
00033
00034
00035
00036
00037
00038 variable query
00039
00040
00041
00042 variable contenttype
00043
00044
00045
00046
00047
00048
00049 variable value
00050
00051
00052
00053 variable varlist
00054
00055
00056
00057
00058 variable urlStub
00059
00060
00061
00062
00063
00064 variable listRestrict 0
00065
00066
00067
00068 variable cookieOutput
00069
00070
00071
00072
00073 variable i
00074 variable c
00075 variable map
00076
00077 for { i = 1} {$i <= 256} {incr i} {
00078 c = [format %c $i]
00079 if {![string match \[a-zA-Z0-9\] $c]} {
00080 map = ($c) %[format %.2X $i]
00081 }
00082 }
00083
00084
00085 array map = {
00086 " " + \n %0D%0A
00087 }
00088
00089
00090
00091 variable _tmpfiles
00092 array _tmpfiles = {}
00093
00094
00095
00096
00097 namespace export re urlStub = query type decode encode
00098 namespace export nvlist parse input value valueList names
00099 namespace export Value = ValueList = DefaultValue = DefaultValueList =
00100 namespace export empty import importAll importFile redirect header
00101 namespace export parseMimeValue multipart cookie Cookie =
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 ret ::ncgi::reset (type args) {
00124 global env
00125 variable _tmpfiles
00126 variable query
00127 variable contenttype
00128 variable cookieOutput
00129
00130 # array unset _tmpfiles -- Not a Tcl 8.2 idiom
00131 unset _tmpfiles ; array set _tmpfiles {}
00132
00133 set cookieOutput {}
00134 if {[llength $args] == 0} {
00135
00136 # We use and test args here so we can detect the
00137 # difference between empty query data and a full reset.
00138
00139 if {[info exists query]} {
00140 unset query
00141 }
00142 if {[info exists contenttype]} {
00143 unset contenttype
00144 }
00145 } else {
00146 set query [lindex $args 0]
00147 set contenttype [lindex $args 1]
00148 }
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 ret ::ncgi::urlStub (optional url ={)} {
00165 global env
00166 variable urlStub
00167 if {[string length $url]} {
00168 urlStub = $url
00169 return ""
00170 } elseif {[info exists urlStub]} {
00171 return $urlStub
00172 } elseif {[info exists env(SCRIPT_NAME)]} {
00173 urlStub = $env(SCRIPT_NAME)
00174 return $urlStub
00175 } else {
00176 return ""
00177 }
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 ret ::ncgi::query () {
00192 global env
00193 variable query
00194
00195 if {[info exists query]} {
00196 # This ensures you can call ncgi::query more than once,
00197 # and that you can use it with ncgi::reset
00198 return $query
00199 }
00200
00201 set query ""
00202 if {[info exists env(REQUEST_METHOD)]} {
00203 if {$env(REQUEST_METHOD) == "GET"} {
00204 if {[info exists env(QUERY_STRING)]} {
00205 set query $env(QUERY_STRING)
00206 }
00207 } elseif {$env(REQUEST_METHOD) == "POST"} {
00208 if {[info exists env(CONTENT_LENGTH)] &&
00209 [string length $env(CONTENT_LENGTH)] != 0} {
00210 ## added by Steve Cassidy to try to fix binary file upload
00211 fconfigure stdin -translation binary -encoding binary
00212 set query [read stdin $env(CONTENT_LENGTH)]
00213 }
00214 }
00215 }
00216 return $query
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 ret ::ncgi::type () {
00230 global env
00231 variable contenttype
00232
00233 if {![info exists contenttype]} {
00234 if {[info exists env(CONTENT_TYPE)]} {
00235 set contenttype $env(CONTENT_TYPE)
00236 } else {
00237 return ""
00238 }
00239 }
00240 return $contenttype
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 ret ::ncgi::decode (type str) {
00254 # rewrite "+" back to space
00255 # protect \ from quoting another '\'
00256 set str [string map [list + { } "\\" "\\\\"] $str]
00257
00258 # prepare to process all %-escapes
00259 regsub -all -- {%([A-Fa-f0-9][A-Fa-f0-9])} $str {\\u00\1} str
00260
00261 # process \u unicode mapped chars
00262 return [subst -novar -nocommand $str]
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 ret ::ncgi::encode (type string) {
00276 variable map
00277
00278 # 1 leave alphanumerics characters alone
00279 # 2 Convert every other character to an array lookup
00280 # 3 Escape constructs that are "special" to the tcl parser
00281 # 4 "subst" the result, doing all the array substitutions
00282
00283 regsub -all -- \[^a-zA-Z0-9\] $string {$map(&)} string
00284 # This quotes cases like $map([) or $map($) => $map(\[) ...
00285 regsub -all -- {[][{})\\]\)} $string {\\&} string
00286 return [subst -nocommand $string]
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 ret ::ncgi::names () {
00303 array set names {}
00304 foreach {name val} [nvlist] {
00305 if {![string equal $name "anonymous"]} {
00306 set names($name) 1
00307 }
00308 }
00309 return [array names names]
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 ret ::ncgi::nvlist () {
00326 set query [query]
00327 set type [type]
00328 switch -glob -- $type {
00329 "" -
00330 text/xml* -
00331 application/x-www-form-urlencoded* -
00332 application/x-www-urlencoded* {
00333 set result {}
00334
00335 # Any whitespace at the beginning or end of urlencoded data is not
00336 # considered to be part of that data, so we trim it off. One special
00337 # case in which post data is preceded by a \n occurs when posting
00338 # with HTTPS in Netscape.
00339
00340 foreach {x} [split [string trim $query] &] {
00341 # Turns out you might not get an = sign,
00342 # especially with <isindex> forms.
00343
00344 set pos [string first = $x]
00345 set len [string length $x]
00346
00347 if { $pos>=0 } {
00348 if { $pos == 0 } { # if the = is at the beginning ...
00349 if { $len>1 } {
00350 # ... and there is something to the right ...
00351 set varname anonymous
00352 set val [string range $x 1 end]]
00353 } else {
00354 # ... otherwise, all we have is an =
00355 set varname anonymous
00356 set val ""
00357 }
00358 } elseif { $pos==[expr {$len-1}] } {
00359 # if the = is at the end ...
00360 set varname [string range $x 0 [expr {$pos-1}]]
00361 set val ""
00362 } else {
00363 set varname [string range $x 0 [expr {$pos-1}]]
00364 set val [string range $x [expr {$pos+1}] end]
00365 }
00366 } else { # no = was found ...
00367 set varname anonymous
00368 set val $x
00369 }
00370 lappend result [decode $varname] [decode $val]
00371 }
00372 return $result
00373 }
00374 multipart
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 ret ::ncgi::parse () {
00399 variable value
00400 variable listRestrict 0
00401 variable varlist {}
00402 if {[info exists value]} {
00403 unset value
00404 }
00405 foreach {name val} [nvlist] {
00406 if {![info exists value($name)]} {
00407 lappend varlist $name
00408 }
00409 lappend value($name) $val
00410 }
00411 return $varlist
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 ret ::ncgi::input (optional fakeinput ={) {fakecookie {}}} {
00428 variable value
00429 variable varlist {}
00430 variable listRestrict 1
00431 if {[info exists value]} {
00432 un value =
00433 }
00434 if {[string length $fakeinput]} {
00435 ncgi::re $fakeinput =
00436 }
00437 foreach {name val} [nvlist] {
00438 exists = [info exists value($name)]
00439 if {!$exists} {
00440 lappend varlist $name
00441 }
00442 if {[string match "*List" $name]} {
00443
00444 lappend value($name) $val
00445 } elseif {$exists} {
00446 error "Multiple definitions of $name encountered in input.\
00447 If you're trying to do this intentionally (such as with select),\
00448 the variable must have a \"List\" suffix."
00449 } else {
00450
00451 value = ($name) $val
00452 }
00453 }
00454 return $varlist
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 ret ::ncgi::value (type key , optional default ={)} {
00472 variable value
00473 variable listRestrict
00474 variable contenttype
00475 if {[info exists value($key)]} {
00476 if {$listRestrict} {
00477
00478
00479
00480
00481 val = $value($key)
00482 } else {
00483
00484
00485
00486 val = [lindex $value($key) 0]
00487 }
00488 if {[string match multipart
00489
00490
00491
00492 val = [lindex $val 1]
00493 }
00494 return $val
00495 } else {
00496 return $default
00497 }
00498 }
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513 ret ::ncgi::valueList (type key , optional default ={)} {
00514 variable value
00515 if {[info exists value($key)]} {
00516 return $value($key)
00517 } else {
00518 return $default
00519 }
00520 }
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 ret ::ncgi::setValue (type key , type value) {
00538 variable listRestrict
00539 if {$listRestrict} {
00540 ncgi::setValueList $key $value
00541 } else {
00542 ncgi::setValueList $key [list $value]
00543 }
00544 }
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558 ret ::ncgi::setValueList (type key , type valuelist) {
00559 variable value
00560 variable varlist
00561 if {![info exists value($key)]} {
00562 lappend varlist $key
00563 }
00564
00565 # This if statement is a workaround for another hack in
00566 # ::ncgi::value that treats multipart form data
00567 # differently.
00568 if {[string match multipart
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589 ret ::ncgi::setDefaultValue (type key , type value) {
00590 ncgi::setDefaultValueList $key [list $value]
00591 }
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 ret ::ncgi::setDefaultValueList (type key , type valuelist) {
00607 variable value
00608 if {![info exists value($key)]} {
00609 ncgi::setValueList $key $valuelist
00610 return ""
00611 } else {
00612 return ""
00613 }
00614 }
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626 ret ::ncgi::exists (type var) {
00627 variable value
00628 return [info exists value($var)]
00629 }
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641 ret ::ncgi::empty (type name) {
00642 return [expr {[string length [string trim [value $name]]] == 0}]
00643 }
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656 ret ::ncgi::import (type cginame , optional tclname ={)} {
00657 if {[string length $tclname]} {
00658 upvar 1 $tclname var
00659 } else {
00660 upvar 1 $cginame var
00661 }
00662 var = [value $cginame]
00663 }
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674 ret ::ncgi::importAll (type args) {
00675 variable varlist
00676 if {[llength $args] == 0} {
00677 set args $varlist
00678 }
00679 foreach cginame $args {
00680 upvar 1 $cginame var
00681 set var [value $cginame]
00682 }
00683 }
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697 ret ::ncgi::redirect (type url) {
00698 global env
00699
00700 if {![regexp -- {^[^:]+:
00701
00702 # The url is relative (no protocol/server spec in it), so
00703 # here we create a canonical URL.
00704
00705 # request_uri The current URL used when dealing with relative URLs.
00706 # proto http or https
00707 # server The server, which we are careful to match with the
00708 # current one in base Basic Authentication is being used.
00709 # port This is set if it is not the default port.
00710
00711 if {[info exists env(REQUEST_URI)]} {
00712 # Not all servers have the leading protocol spec
00713 regsub -- {^https?:
00714 } elseif {[info exists env(SCRIPT_NAME)]} {
00715 set request_uri $env(SCRIPT_NAME)
00716 } else {
00717 set request_uri /
00718 }
00719
00720 set port ""
00721 if {[info exists env(HTTPS)] && $env(HTTPS) == "on"} {
00722 set proto https
00723 if {$env(SERVER_PORT) != 443} {
00724 set port :$env(SERVER_PORT)
00725 }
00726 } else {
00727 set proto http
00728 if {$env(SERVER_PORT) != 80} {
00729 set port :$env(SERVER_PORT)
00730 }
00731 }
00732 # Pick the server from REQUEST_URI so it matches the current
00733 # URL. Otherwise use SERVER_NAME. These could be different, e.g.,
00734 # "pop.scriptics.com" vs. "pop"
00735
00736 if {[info exists env(REQUEST_URI)]} {
00737 # Not all servers have the leading protocol spec
00738 if {![regexp -- {^https?:
00739 set server $env(SERVER_NAME)
00740 }
00741 } else {
00742 set server $env(SERVER_NAME)
00743 }
00744 if {[string match
00745
00746
00747 )[^/]*$} $request_uri match dirname
00748 set url $proto:
00749 }
00750 }
00751 ncgi::header text/html Location $url
00752 puts "Please go to <a href=\"$url\">$url</a>"
00753 }
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766 ret ::ncgi::header (optional type =text/html , type args) {
00767 variable cookieOutput
00768 puts "Content-Type: $type"
00769 foreach {n v} $args {
00770 puts "$n: $v"
00771 }
00772 if {[info exists cookieOutput]} {
00773 foreach line $cookieOutput {
00774 puts "Set-Cookie: $line"
00775 }
00776 }
00777 puts ""
00778 flush stdout
00779 }
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799 ret ::ncgi::parseMimeValue (type value) {
00800 set parts [split $value \;]
00801 set results [list [string trim [lindex $parts 0]]]
00802 set paramList [list]
00803 foreach sub [lrange $parts 1 end] {
00804 if {[regexp -- {([^=]+)=(.+)} $sub match key val]} {
00805 set key [string trim [string tolower $key]]
00806 set val [string trim $val]
00807 # Allow single as well as double quotes
00808 if {[regexp -- {^["']} $val quote]} { ;# need a " for balance
00809 if {[regexp -- ^${quote}(\[^$quote\]*)$quote $val x val2]} {
00810 # Trim quotes and any extra crap after close quote
00811 set val $val2
00812 }
00813 }
00814 lappend paramList $key $val
00815 }
00816 }
00817 if {[llength $paramList]} {
00818 lappend results $paramList
00819 }
00820 return $results
00821 }
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865 ret ::ncgi::multipart (type type , type query) {
00866
00867 set parsedType [parseMimeValue $type]
00868 if {![string match multipart
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982 ret ::ncgi::importFile (type cmd , type var , optional filename ={)} {
00983
00984 set vlist [valueList $var]
00985
00986 array set fileinfo [lindex [lindex $vlist 0] 0]
00987 set contents [lindex [lindex $vlist 0] 1]
00988
00989 switch -exact -- $cmd {
00990 -server {
00991 ## take care not to write it out more than once
00992 variable _tmpfiles
00993 if {![info exists _tmpfiles($var)]} {
00994 if {$filename != {}} {
00995 ## use supplied filename
00996 set _tmpfiles($var) $filename
00997 } else {
00998 ## create a tmp file
00999 set _tmpfiles($var) [::fileutil::tempfile ncgi]
01000 }
01001
01002 # write out the data only if it's not been done already
01003 if {[catch {open $_tmpfiles($var) w} h]} {
01004 error "Can't open temporary file in ncgi::importFile ($h)"
01005 }
01006
01007 fconfigure $h -translation binary -encoding binary
01008 puts -nonewline $h $contents
01009 close $h
01010 }
01011 return $_tmpfiles($var)
01012 }
01013 -client {
01014 if {![info exists fileinfo(filename)]} {return {}}
01015 return $fileinfo(filename)
01016 }
01017 -type {
01018 if {![info exists fileinfo(content-type)]} {return {}}
01019 return $fileinfo(content-type)
01020 }
01021 -data {
01022 return $contents
01023 }
01024 default {
01025 error "Unknown subcommand to ncgi::import_file: $cmd"
01026 }
01027 }
01028 }
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043 ret ::ncgi::cookie (type cookie) {
01044 global env
01045 set result ""
01046 if {[info exists env(HTTP_COOKIE)]} {
01047 foreach pair [split $env(HTTP_COOKIE) \;] {
01048 foreach {key value} [split [string trim $pair] =] { break ;# lassign }
01049 if {[string compare $cookie $key] == 0} {
01050 lappend result $value
01051 }
01052 }
01053 }
01054 return $result
01055 }
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073 ret ::ncgi::setCookie (type args) {
01074 variable cookieOutput
01075 array set opt $args
01076 set line "$opt(-name)=$opt(-value) ;"
01077 foreach extra {path domain} {
01078 if {[info exists opt(-$extra)]} {
01079 append line " $extra=$opt(-$extra) ;"
01080 }
01081 }
01082 if {[info exists opt(-expires)]} {
01083 switch -glob -- $opt(-expires) {
01084 *GMT {
01085 set expires $opt(-expires)
01086 }
01087 default {
01088 set expires [clock format [clock scan $opt(-expires)] \
01089 -format "%A, %d-%b-%Y %H:%M:%S GMT" -gmt 1]
01090 }
01091 }
01092 append line " expires=$expires ;"
01093 }
01094 if {[info exists opt(-secure)]} {
01095 append line " secure "
01096 }
01097 lappend cookieOutput $line
01098 }
01099