os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/httpd
Update contrib.
     2 # The httpd_ procedures implement a stub http server.
 
     4 # Copyright (c) 1997-1998 Sun Microsystems, Inc.
 
     5 # Copyright (c) 1999-2000 Scriptics Corporation
 
     7 # See the file "license.terms" for information on usage and redistribution
 
     8 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 
    10 # SCCS: @(#) httpd 1.2 98/02/20 14:51:59
 
    14 proc httpd_init {{port 8015}} {
 
    15     socket -server httpdAccept $port
 
    17 proc httpd_log {args} {
 
    19     if {[info exists httpLog] && $httpLog} {
 
    20 	puts stderr "httpd: [join $args { }]"
 
    23 array set httpdErrors {
 
    26     401 {Authorization Required}
 
    28     503 {Service Unavailable}
 
    29     504 {Service Temporarily Unavailable}
 
    32 proc httpdError {sock code args} {
 
    34     puts $sock "$code $httpdErrors($code)"
 
    35     httpd_log "error: [join $args { }]"
 
    37 proc httpdAccept {newsock ipaddr port} {
 
    39     upvar #0 httpd$newsock data
 
    41     fconfigure $newsock -blocking 0 -translation {auto crlf}
 
    42     httpd_log $newsock Connect $ipaddr $port
 
    43     set data(ipaddr) $ipaddr
 
    44     fileevent $newsock readable [list httpdRead $newsock]
 
    47 # read data from a client request
 
    49 proc httpdRead { sock } {
 
    50     upvar #0 httpd$sock data
 
    54     } elseif {![info exists data(state)]} {
 
    56 	# Read the protocol line and parse out the URL and query
 
    58 	set readCount [gets $sock line]
 
    59 	if [regexp {(POST|GET|HEAD) ([^?]+)\??([^ ]*) HTTP/(1.[01])} \
 
    60 		$line x data(proto) data(url) data(query) data(httpversion)] {
 
    62 	    httpd_log $sock Query $line
 
    65 	    httpd_log $sock Error "bad first line:$line"
 
    69     } elseif {$data(state) == "mime"} {
 
    71 	# Read the HTTP headers
 
    73 	set readCount [gets $sock line]
 
    74     } elseif {$data(state) == "query"} {
 
    78 	if {![info exists data(length_orig)]} {
 
    79 	    set data(length_orig) $data(length)
 
    81 	set line [read $sock $data(length)]
 
    82 	set readCount [string length $line]
 
    83 	incr data(length) -$readCount
 
    86     # string compare $readCount 0 maps -1 to -1, 0 to 0, and > 0 to 1
 
    88     set state [string compare $readCount 0],$data(state),$data(proto)
 
    89     httpd_log $sock $state
 
   100 	    # Empty line at end of headers,
 
   101 	    # or eof after query data
 
   105 	    # Empty line between headers and query data
 
   106 	    if {![info exists data(mime,content-length)]} {
 
   107 		httpd_log $sock Error "No Content-Length for POST"
 
   111 		set data(state) query
 
   112 		set data(length) $data(mime,content-length)
 
   114 		# Special case to simulate servers that respond
 
   115 		# without reading the post data.
 
   117 		if {[string match *droppost* $data(url)]} {
 
   118 		    fileevent $sock readable {}
 
   126 	    # A line of HTTP headers
 
   127 	    if {[regexp {([^:]+):[ 	]*(.*)}  $line dummy key value]} {
 
   128 		set data(mime,[string tolower $key]) $value
 
   132 	    httpd_log $sock Error "unexpected eof on <$data(url)> request"
 
   137 	    append data(query) $line
 
   138 	    if {$data(length) <= 0} {
 
   139 		set data(length) $data(length_orig)
 
   145 		httpd_log $sock Error "unexpected eof on <$data(url)> request"
 
   147 		httpd_log $sock Error "unhandled state <$state> fetching <$data(url)>"
 
   154 proc httpdSockDone { sock } {
 
   155     upvar #0 httpd$sock data
 
   160 # Respond to the query.
 
   162 proc httpdRespond { sock } {
 
   163     global httpd bindata port
 
   164     upvar #0 httpd$sock data
 
   166     switch -glob -- $data(url) {
 
   168 	    set html "$bindata[info hostname]:$port$data(url)"
 
   169 	    set type application/octet-stream
 
   172 	    set html "Got [string length $data(query)] bytes"
 
   178 	    set html "<html><head><title>HTTP/1.0 TEST</title></head><body>
 
   179 <h1>Hello, World!</h1>
 
   180 <h2>$data(proto) $data(url)</h2>
 
   182 	    if {[info exists data(query)] && [string length $data(query)]} {
 
   183 		append html "<h2>Query</h2>\n<dl>\n"
 
   184 		foreach {key value} [split $data(query) &=] {
 
   185 		    append html "<dt>$key<dd>$value\n"
 
   186 		    if {$key == "timeout"} {
 
   187 			after $value	;# pause
 
   192 	    append html </body></html>
 
   196     # Catch errors from premature client closes
 
   199 	if {$data(proto) == "HEAD"} {
 
   200 	    puts $sock "HTTP/1.0 200 OK"
 
   202 	    puts $sock "HTTP/1.0 200 Data follows"
 
   204 	puts $sock "Date: [clock format [clock clicks]]"
 
   205 	puts $sock "Content-Type: $type"
 
   206 	puts $sock "Content-Length: [string length $html]"
 
   209 	if {$data(proto) != "HEAD"} {
 
   210 	    fconfigure $sock -translation binary
 
   211 	    puts -nonewline $sock $html
 
   214     httpd_log $sock Done ""