Debian Codegraph @

tests/httpd

raw
1 # -*- tcl -*-
2 #
3 # The httpd_ procedures implement a stub http server.
4 #
5 # Copyright (c) 1997-1998 Sun Microsystems, Inc.
6 # Copyright (c) 1999-2000 Scriptics Corporation
7 #
8 # See the file "license.terms" for information on usage and redistribution
9 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10
11 #set httpLog 1
12
13 # Do not use [info hostname].
14 # Name resolution is often a problem on OSX; not focus of HTTP package anyway.
15 # Also a problem on other platforms for http-4.14 (test with bad port number).
16 set HOST localhost
17
18 proc httpd_init {{port 8015}} {
19 socket -server httpdAccept $port
20 }
21 proc httpd_log {args} {
22 global httpLog
23 if {[info exists httpLog] && $httpLog} {
24 puts stderr "httpd: [join $args { }]"
25 }
26 }
27 array set httpdErrors {
28 204 {No Content}
29 400 {Bad Request}
30 401 {Authorization Required}
31 404 {Not Found}
32 503 {Service Unavailable}
33 504 {Service Temporarily Unavailable}
34 }
35
36 proc httpdError {sock code args} {
37 global httpdErrors
38 puts $sock "$code $httpdErrors($code)"
39 httpd_log "error: [join $args { }]"
40 }
41 proc httpdAccept {newsock ipaddr port} {
42 global httpd
43 upvar #0 httpd$newsock data
44
45 fconfigure $newsock -blocking 0 -translation {auto crlf}
46 httpd_log $newsock Connect $ipaddr $port
47 set data(ipaddr) $ipaddr
48 fileevent $newsock readable [list httpdRead $newsock]
49 }
50
51 # read data from a client request
52
53 proc httpdRead { sock } {
54 upvar #0 httpd$sock data
55
56 if {[eof $sock]} {
57 set readCount -1
58 } elseif {![info exists data(state)]} {
59
60 # Read the protocol line and parse out the URL and query
61
62 set readCount [gets $sock line]
63 if {[regexp {(POST|GET|HEAD) ([^?]+)\??([^ ]*) HTTP/(1.[01])} $line \
64 -> data(proto) data(url) data(query) data(httpversion)]} {
65 set data(state) mime
66 httpd_log $sock Query $line
67 if {[regexp {(?:^|[\?&])delay=([^&]+)} $data(query) {} val]} {
68 fileevent $sock readable {}
69 after $val [list fileevent $sock readable [list httpdRead $sock]]
70 }
71 } else {
72 httpdError $sock 400
73 httpd_log $sock Error "bad first line:$line"
74 httpdSockDone $sock
75 }
76 return
77 } elseif {$data(state) == "mime"} {
78
79 # Read the HTTP headers
80
81 set readCount [gets $sock line]
82 if {[regexp {^([^:]+):(.*)$} $line -> key val]} {
83 lappend data(meta) $key [string trim $val]
84 }
85
86 } elseif {$data(state) == "query"} {
87
88 # Read the query data
89
90 if {![info exists data(length_orig)]} {
91 set data(length_orig) $data(length)
92 }
93 set line [read $sock $data(length)]
94 set readCount [string length $line]
95 incr data(length) -$readCount
96 }
97
98 # string compare $readCount 0 maps -1 to -1, 0 to 0, and > 0 to 1
99
100 set state [string compare $readCount 0],$data(state),$data(proto)
101 httpd_log $sock $state
102 switch -- $state {
103 -1,mime,HEAD -
104 -1,mime,GET -
105 -1,mime,POST {
106 # gets would block
107 return
108 }
109 0,mime,HEAD -
110 0,mime,GET -
111 0,query,POST {
112 # Empty line at end of headers,
113 # or eof after query data
114 httpdRespond $sock
115 }
116 0,mime,POST {
117 # Empty line between headers and query data
118 if {![info exists data(mime,content-length)]} {
119 httpd_log $sock Error "No Content-Length for POST"
120 httpdError $sock 400
121 httpdSockDone $sock
122 } else {
123 set data(state) query
124 set data(length) $data(mime,content-length)
125
126 # Special case to simulate servers that respond
127 # without reading the post data.
128
129 if {[string match *droppost* $data(url)]} {
130 fileevent $sock readable {}
131 httpdRespond $sock
132 }
133 }
134 }
135 1,mime,HEAD -
136 1,mime,POST -
137 1,mime,GET {
138 # A line of HTTP headers
139 if {[regexp {([^:]+):[ ]*(.*)} $line dummy key value]} {
140 set data(mime,[string tolower $key]) $value
141 }
142 }
143 -1,query,POST {
144 httpd_log $sock Error "unexpected eof on <$data(url)> request"
145 httpdError $sock 400
146 httpdSockDone $sock
147 }
148 1,query,POST {
149 append data(query) $line
150 if {$data(length) <= 0} {
151 set data(length) $data(length_orig)
152 httpdRespond $sock
153 }
154 }
155 default {
156 if {[eof $sock]} {
157 httpd_log $sock Error "unexpected eof on <$data(url)> request"
158 } else {
159 httpd_log $sock Error "unhandled state <$state> fetching <$data(url)>"
160 }
161 httpdError $sock 404
162 httpdSockDone $sock
163 }
164 }
165 }
166 proc httpdSockDone { sock } {
167 upvar #0 httpd$sock data
168 unset data
169 catch {close $sock}
170 }
171
172 # Respond to the query.
173
174 proc httpdRespond { sock } {
175 global httpd bindata port
176 upvar #0 httpd$sock data
177
178 switch -glob -- $data(url) {
179 *binary* {
180 set html "$bindata${::HOST}:$port$data(url)"
181 set type application/octet-stream
182 }
183 *xml* {
184 set html [encoding convertto utf-8 "<test>\u1234</test>"]
185 set type "application/xml;charset=UTF-8"
186 }
187 *post* {
188 set html "Got [string length $data(query)] bytes"
189 set type text/plain
190 }
191 *headers* {
192 set html ""
193 set type text/plain
194 foreach {key value} $data(meta) {
195 append html [list $key $value] "\n"
196 }
197 set html [string trim $html]
198 }
199 default {
200 set type text/html
201
202 set html "<html><head><title>HTTP/1.0 TEST</title></head><body>
203 <h1>Hello, World!</h1>
204 <h2>$data(proto) $data(url)</h2>
205 "
206 if {[info exists data(query)] && [string length $data(query)]} {
207 append html "<h2>Query</h2>\n<dl>\n"
208 foreach {key value} [split $data(query) &=] {
209 append html "<dt>$key<dd>$value\n"
210 if {$key == "timeout"} {
211 after $value ;# pause
212 }
213 }
214 append html </dl>\n
215 }
216 append html </body></html>
217 }
218 }
219
220 # Catch errors from premature client closes
221
222 catch {
223 if {$data(proto) == "HEAD"} {
224 puts $sock "HTTP/1.0 200 OK"
225 } else {
226 # Split the response to test for [Bug 26245326]
227 puts -nonewline $sock "HT"
228 flush $sock
229 puts $sock "TP/1.0 200 Data follows"
230 }
231 puts $sock "Date: [clock format [clock seconds] \
232 -format {%a, %d %b %Y %H:%M:%S %Z}]"
233 puts $sock "Content-Type: $type"
234 puts $sock "Content-Length: [string length $html]"
235 foreach {key val} $data(meta) {
236 if {[string match "X-*" $key]} {
237 puts $sock "$key: $val"
238 }
239 }
240 puts $sock ""
241 flush $sock
242 if {$data(proto) != "HEAD"} {
243 fconfigure $sock -translation binary
244 puts -nonewline $sock $html
245 }
246 }
247 httpd_log $sock Done ""
248 httpdSockDone $sock
249 }