Merge lp:~pedronis/ubuntu-push/http13client into lp:ubuntu-push
- http13client
- Merge into trunk
Proposed by
Samuele Pedroni
Status: | Merged |
---|---|
Approved by: | Samuele Pedroni |
Approved revision: | 92 |
Merged at revision: | 82 |
Proposed branch: | lp:~pedronis/ubuntu-push/http13client |
Merge into: | lp:ubuntu-push |
Diff against target: |
18096 lines (+17900/-0) 39 files modified
http13client/LICENSE (+27/-0) http13client/Makefile (+37/-0) http13client/_patches/empty_server.patch (+4719/-0) http13client/_patches/fix_code.patch (+739/-0) http13client/_patches/fix_status.patch (+118/-0) http13client/_patches/fix_tests.patch (+1603/-0) http13client/_patches/no_keepalive.patch (+11/-0) http13client/_patches/no_serve_test_unsupported_bench.patch (+46/-0) http13client/_patches/sync_pool.Rpatch (+172/-0) http13client/_patches/tweak_doc_go.patch (+43/-0) http13client/_using.txt (+5/-0) http13client/chunked.go (+203/-0) http13client/chunked_test.go (+159/-0) http13client/client.go (+480/-0) http13client/client_test.go (+879/-0) http13client/cookie.go (+313/-0) http13client/cookie_test.go (+304/-0) http13client/doc.go (+56/-0) http13client/export_test.go (+62/-0) http13client/header.go (+49/-0) http13client/header_test.go (+213/-0) http13client/lex.go (+96/-0) http13client/lex_test.go (+31/-0) http13client/npn_test.go (+119/-0) http13client/proxy_test.go (+81/-0) http13client/readrequest_test.go (+332/-0) http13client/request.go (+865/-0) http13client/request_test.go (+571/-0) http13client/requestwrite_test.go (+566/-0) http13client/response.go (+244/-0) http13client/response_test.go (+629/-0) http13client/responsewrite_test.go (+111/-0) http13client/serve_test.go (+58/-0) http13client/server.go (+68/-0) http13client/transfer.go (+699/-0) http13client/transfer_test.go (+64/-0) http13client/transport.go (+1137/-0) http13client/transport_test.go (+1894/-0) http13client/z_last_test.go (+97/-0) |
To merge this branch: | bzr merge lp:~pedronis/ubuntu-push/http13client |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
John Lenton (community) | Approve | ||
Review via email: mp+211919@code.launchpad.net |
Commit message
vendor the go 1.3 development net/http bits which properly/better support timeouts for requests; see _using.txt and Makefile for the recipe in http13client
Description of the change
vendor the go 1.3 development net/http bits which properly/better support timeouts for requests,
added as a http13client package directory (package still named http, though typically it would be imported as http13)
_using.txt records the starting point,
the Makefile and _patches directory captures the required massaging
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added directory 'http13client' | |||
2 | === added file 'http13client/LICENSE' | |||
3 | --- http13client/LICENSE 1970-01-01 00:00:00 +0000 | |||
4 | +++ http13client/LICENSE 2014-03-20 12:26:55 +0000 | |||
5 | @@ -0,0 +1,27 @@ | |||
6 | 1 | Copyright (c) 2012 The Go Authors. All rights reserved. | ||
7 | 2 | |||
8 | 3 | Redistribution and use in source and binary forms, with or without | ||
9 | 4 | modification, are permitted provided that the following conditions are | ||
10 | 5 | met: | ||
11 | 6 | |||
12 | 7 | * Redistributions of source code must retain the above copyright | ||
13 | 8 | notice, this list of conditions and the following disclaimer. | ||
14 | 9 | * Redistributions in binary form must reproduce the above | ||
15 | 10 | copyright notice, this list of conditions and the following disclaimer | ||
16 | 11 | in the documentation and/or other materials provided with the | ||
17 | 12 | distribution. | ||
18 | 13 | * Neither the name of Google Inc. nor the names of its | ||
19 | 14 | contributors may be used to endorse or promote products derived from | ||
20 | 15 | this software without specific prior written permission. | ||
21 | 16 | |||
22 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
23 | 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
24 | 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
25 | 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
26 | 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
27 | 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
28 | 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
29 | 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
30 | 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
31 | 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
32 | 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
33 | 0 | 28 | ||
34 | === added file 'http13client/Makefile' | |||
35 | --- http13client/Makefile 1970-01-01 00:00:00 +0000 | |||
36 | +++ http13client/Makefile 2014-03-20 12:26:55 +0000 | |||
37 | @@ -0,0 +1,37 @@ | |||
38 | 1 | # help massage and extract from go development 1.3 net/http the client bits | ||
39 | 2 | |||
40 | 3 | grab: | ||
41 | 4 | cp $(GOCHECKOUT)/src/pkg/net/http/*.go . | ||
42 | 5 | cp $(GOCHECKOUT)/LICENSE . | ||
43 | 6 | mkdir -p httptest | ||
44 | 7 | mkdir -p httputil | ||
45 | 8 | mkdir -p testdata | ||
46 | 9 | cp $(GOCHECKOUT)/src/pkg/net/http/httptest/*.go httptest | ||
47 | 10 | cp $(GOCHECKOUT)/src/pkg/net//http/httputil/*.go httputil | ||
48 | 11 | cp $(GOCHECKOUT)/src/pkg/net/http/testdata/* testdata | ||
49 | 12 | hg -R $(GOCHECKOUT) summary > _using.txt | ||
50 | 13 | |||
51 | 14 | full-prepare: | ||
52 | 15 | patch -R -p5 < _patches/sync_pool.Rpatch | ||
53 | 16 | patch -p1 < _patches/no_keepalive.patch | ||
54 | 17 | sed -i -e 's+"net/http"+"launchpad.net/ubuntu-push/http13client"+' *.go httptest/*.go httputil/*.go | ||
55 | 18 | sed -i -e 's+"net/http/+"launchpad.net/ubuntu-push/http13client/+' *.go httptest/*.go httputil/*.go | ||
56 | 19 | patch -p1 < _patches/no_serve_test_unsupported_bench.patch | ||
57 | 20 | |||
58 | 21 | prune: | ||
59 | 22 | rm -rf example_test.go filetransport*.go fs*.go race.go range_test.go \ | ||
60 | 23 | sniff*.go httptest httputil testdata triv.go jar.go status.go | ||
61 | 24 | sed -i -e 's+"launchpad.net/ubuntu-push/http13client/+"net/http/+' *.go | ||
62 | 25 | |||
63 | 26 | fix: | ||
64 | 27 | patch -p1 < _patches/empty_server.patch | ||
65 | 28 | patch -p1 < _patches/fix_tests.patch | ||
66 | 29 | patch -p1 < _patches/fix_code.patch | ||
67 | 30 | patch -p1 < _patches/fix_status.patch | ||
68 | 31 | patch -p1 < _patches/tweak_doc_go.patch | ||
69 | 32 | go fmt | ||
70 | 33 | |||
71 | 34 | wipe: | ||
72 | 35 | rm -rf *.go httptest httputil testdata | ||
73 | 36 | |||
74 | 37 | .PHONY: grab full-prepare prune fix wipe | ||
75 | 0 | 38 | ||
76 | === added directory 'http13client/_patches' | |||
77 | === added file 'http13client/_patches/empty_server.patch' | |||
78 | --- http13client/_patches/empty_server.patch 1970-01-01 00:00:00 +0000 | |||
79 | +++ http13client/_patches/empty_server.patch 2014-03-20 12:26:55 +0000 | |||
80 | @@ -0,0 +1,4719 @@ | |||
81 | 1 | === modified file 'http13client/serve_test.go' | ||
82 | 2 | --- http13client/serve_test.go 2014-03-19 21:38:56 +0000 | ||
83 | 3 | +++ http13client/serve_test.go 2014-03-19 22:27:37 +0000 | ||
84 | 4 | @@ -2,60 +2,15 @@ | ||
85 | 5 | // Use of this source code is governed by a BSD-style | ||
86 | 6 | // license that can be found in the LICENSE file. | ||
87 | 7 | |||
88 | 8 | -// End-to-end serving tests | ||
89 | 9 | - | ||
90 | 10 | package http_test | ||
91 | 11 | |||
92 | 12 | import ( | ||
93 | 13 | - "bufio" | ||
94 | 14 | - "bytes" | ||
95 | 15 | - "crypto/tls" | ||
96 | 16 | - "errors" | ||
97 | 17 | - "fmt" | ||
98 | 18 | "io" | ||
99 | 19 | - "io/ioutil" | ||
100 | 20 | - "log" | ||
101 | 21 | "net" | ||
102 | 22 | - . "launchpad.net/ubuntu-push/http13client" | ||
103 | 23 | - "net/http/httptest" | ||
104 | 24 | - "net/http/httputil" | ||
105 | 25 | - "net/url" | ||
106 | 26 | - "os" | ||
107 | 27 | - "os/exec" | ||
108 | 28 | - "reflect" | ||
109 | 29 | - "runtime" | ||
110 | 30 | - "strconv" | ||
111 | 31 | - "strings" | ||
112 | 32 | - "sync" | ||
113 | 33 | - "sync/atomic" | ||
114 | 34 | - "syscall" | ||
115 | 35 | - "testing" | ||
116 | 36 | "time" | ||
117 | 37 | ) | ||
118 | 38 | |||
119 | 39 | type dummyAddr string | ||
120 | 40 | -type oneConnListener struct { | ||
121 | 41 | - conn net.Conn | ||
122 | 42 | -} | ||
123 | 43 | - | ||
124 | 44 | -func (l *oneConnListener) Accept() (c net.Conn, err error) { | ||
125 | 45 | - c = l.conn | ||
126 | 46 | - if c == nil { | ||
127 | 47 | - err = io.EOF | ||
128 | 48 | - return | ||
129 | 49 | - } | ||
130 | 50 | - err = nil | ||
131 | 51 | - l.conn = nil | ||
132 | 52 | - return | ||
133 | 53 | -} | ||
134 | 54 | - | ||
135 | 55 | -func (l *oneConnListener) Close() error { | ||
136 | 56 | - return nil | ||
137 | 57 | -} | ||
138 | 58 | - | ||
139 | 59 | -func (l *oneConnListener) Addr() net.Addr { | ||
140 | 60 | - return dummyAddr("test-address") | ||
141 | 61 | -} | ||
142 | 62 | |||
143 | 63 | func (a dummyAddr) Network() string { | ||
144 | 64 | return string(a) | ||
145 | 65 | @@ -93,1289 +48,6 @@ | ||
146 | 66 | return nil | ||
147 | 67 | } | ||
148 | 68 | |||
149 | 69 | -type testConn struct { | ||
150 | 70 | - readBuf bytes.Buffer | ||
151 | 71 | - writeBuf bytes.Buffer | ||
152 | 72 | - closec chan bool // if non-nil, send value to it on close | ||
153 | 73 | - noopConn | ||
154 | 74 | -} | ||
155 | 75 | - | ||
156 | 76 | -func (c *testConn) Read(b []byte) (int, error) { | ||
157 | 77 | - return c.readBuf.Read(b) | ||
158 | 78 | -} | ||
159 | 79 | - | ||
160 | 80 | -func (c *testConn) Write(b []byte) (int, error) { | ||
161 | 81 | - return c.writeBuf.Write(b) | ||
162 | 82 | -} | ||
163 | 83 | - | ||
164 | 84 | -func (c *testConn) Close() error { | ||
165 | 85 | - select { | ||
166 | 86 | - case c.closec <- true: | ||
167 | 87 | - default: | ||
168 | 88 | - } | ||
169 | 89 | - return nil | ||
170 | 90 | -} | ||
171 | 91 | - | ||
172 | 92 | -// reqBytes treats req as a request (with \n delimiters) and returns it with \r\n delimiters, | ||
173 | 93 | -// ending in \r\n\r\n | ||
174 | 94 | -func reqBytes(req string) []byte { | ||
175 | 95 | - return []byte(strings.Replace(strings.TrimSpace(req), "\n", "\r\n", -1) + "\r\n\r\n") | ||
176 | 96 | -} | ||
177 | 97 | - | ||
178 | 98 | -type handlerTest struct { | ||
179 | 99 | - handler Handler | ||
180 | 100 | -} | ||
181 | 101 | - | ||
182 | 102 | -func newHandlerTest(h Handler) handlerTest { | ||
183 | 103 | - return handlerTest{h} | ||
184 | 104 | -} | ||
185 | 105 | - | ||
186 | 106 | -func (ht handlerTest) rawResponse(req string) string { | ||
187 | 107 | - reqb := reqBytes(req) | ||
188 | 108 | - var output bytes.Buffer | ||
189 | 109 | - conn := &rwTestConn{ | ||
190 | 110 | - Reader: bytes.NewReader(reqb), | ||
191 | 111 | - Writer: &output, | ||
192 | 112 | - closec: make(chan bool, 1), | ||
193 | 113 | - } | ||
194 | 114 | - ln := &oneConnListener{conn: conn} | ||
195 | 115 | - go Serve(ln, ht.handler) | ||
196 | 116 | - <-conn.closec | ||
197 | 117 | - return output.String() | ||
198 | 118 | -} | ||
199 | 119 | - | ||
200 | 120 | -func TestConsumingBodyOnNextConn(t *testing.T) { | ||
201 | 121 | - conn := new(testConn) | ||
202 | 122 | - for i := 0; i < 2; i++ { | ||
203 | 123 | - conn.readBuf.Write([]byte( | ||
204 | 124 | - "POST / HTTP/1.1\r\n" + | ||
205 | 125 | - "Host: test\r\n" + | ||
206 | 126 | - "Content-Length: 11\r\n" + | ||
207 | 127 | - "\r\n" + | ||
208 | 128 | - "foo=1&bar=1")) | ||
209 | 129 | - } | ||
210 | 130 | - | ||
211 | 131 | - reqNum := 0 | ||
212 | 132 | - ch := make(chan *Request) | ||
213 | 133 | - servech := make(chan error) | ||
214 | 134 | - listener := &oneConnListener{conn} | ||
215 | 135 | - handler := func(res ResponseWriter, req *Request) { | ||
216 | 136 | - reqNum++ | ||
217 | 137 | - ch <- req | ||
218 | 138 | - } | ||
219 | 139 | - | ||
220 | 140 | - go func() { | ||
221 | 141 | - servech <- Serve(listener, HandlerFunc(handler)) | ||
222 | 142 | - }() | ||
223 | 143 | - | ||
224 | 144 | - var req *Request | ||
225 | 145 | - req = <-ch | ||
226 | 146 | - if req == nil { | ||
227 | 147 | - t.Fatal("Got nil first request.") | ||
228 | 148 | - } | ||
229 | 149 | - if req.Method != "POST" { | ||
230 | 150 | - t.Errorf("For request #1's method, got %q; expected %q", | ||
231 | 151 | - req.Method, "POST") | ||
232 | 152 | - } | ||
233 | 153 | - | ||
234 | 154 | - req = <-ch | ||
235 | 155 | - if req == nil { | ||
236 | 156 | - t.Fatal("Got nil first request.") | ||
237 | 157 | - } | ||
238 | 158 | - if req.Method != "POST" { | ||
239 | 159 | - t.Errorf("For request #2's method, got %q; expected %q", | ||
240 | 160 | - req.Method, "POST") | ||
241 | 161 | - } | ||
242 | 162 | - | ||
243 | 163 | - if serveerr := <-servech; serveerr != io.EOF { | ||
244 | 164 | - t.Errorf("Serve returned %q; expected EOF", serveerr) | ||
245 | 165 | - } | ||
246 | 166 | -} | ||
247 | 167 | - | ||
248 | 168 | -type stringHandler string | ||
249 | 169 | - | ||
250 | 170 | -func (s stringHandler) ServeHTTP(w ResponseWriter, r *Request) { | ||
251 | 171 | - w.Header().Set("Result", string(s)) | ||
252 | 172 | -} | ||
253 | 173 | - | ||
254 | 174 | -var handlers = []struct { | ||
255 | 175 | - pattern string | ||
256 | 176 | - msg string | ||
257 | 177 | -}{ | ||
258 | 178 | - {"/", "Default"}, | ||
259 | 179 | - {"/someDir/", "someDir"}, | ||
260 | 180 | - {"someHost.com/someDir/", "someHost.com/someDir"}, | ||
261 | 181 | -} | ||
262 | 182 | - | ||
263 | 183 | -var vtests = []struct { | ||
264 | 184 | - url string | ||
265 | 185 | - expected string | ||
266 | 186 | -}{ | ||
267 | 187 | - {"http://localhost/someDir/apage", "someDir"}, | ||
268 | 188 | - {"http://localhost/otherDir/apage", "Default"}, | ||
269 | 189 | - {"http://someHost.com/someDir/apage", "someHost.com/someDir"}, | ||
270 | 190 | - {"http://otherHost.com/someDir/apage", "someDir"}, | ||
271 | 191 | - {"http://otherHost.com/aDir/apage", "Default"}, | ||
272 | 192 | - // redirections for trees | ||
273 | 193 | - {"http://localhost/someDir", "/someDir/"}, | ||
274 | 194 | - {"http://someHost.com/someDir", "/someDir/"}, | ||
275 | 195 | -} | ||
276 | 196 | - | ||
277 | 197 | -func TestHostHandlers(t *testing.T) { | ||
278 | 198 | - defer afterTest(t) | ||
279 | 199 | - mux := NewServeMux() | ||
280 | 200 | - for _, h := range handlers { | ||
281 | 201 | - mux.Handle(h.pattern, stringHandler(h.msg)) | ||
282 | 202 | - } | ||
283 | 203 | - ts := httptest.NewServer(mux) | ||
284 | 204 | - defer ts.Close() | ||
285 | 205 | - | ||
286 | 206 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
287 | 207 | - if err != nil { | ||
288 | 208 | - t.Fatal(err) | ||
289 | 209 | - } | ||
290 | 210 | - defer conn.Close() | ||
291 | 211 | - cc := httputil.NewClientConn(conn, nil) | ||
292 | 212 | - for _, vt := range vtests { | ||
293 | 213 | - var r *Response | ||
294 | 214 | - var req Request | ||
295 | 215 | - if req.URL, err = url.Parse(vt.url); err != nil { | ||
296 | 216 | - t.Errorf("cannot parse url: %v", err) | ||
297 | 217 | - continue | ||
298 | 218 | - } | ||
299 | 219 | - if err := cc.Write(&req); err != nil { | ||
300 | 220 | - t.Errorf("writing request: %v", err) | ||
301 | 221 | - continue | ||
302 | 222 | - } | ||
303 | 223 | - r, err := cc.Read(&req) | ||
304 | 224 | - if err != nil { | ||
305 | 225 | - t.Errorf("reading response: %v", err) | ||
306 | 226 | - continue | ||
307 | 227 | - } | ||
308 | 228 | - switch r.StatusCode { | ||
309 | 229 | - case StatusOK: | ||
310 | 230 | - s := r.Header.Get("Result") | ||
311 | 231 | - if s != vt.expected { | ||
312 | 232 | - t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected) | ||
313 | 233 | - } | ||
314 | 234 | - case StatusMovedPermanently: | ||
315 | 235 | - s := r.Header.Get("Location") | ||
316 | 236 | - if s != vt.expected { | ||
317 | 237 | - t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected) | ||
318 | 238 | - } | ||
319 | 239 | - default: | ||
320 | 240 | - t.Errorf("Get(%q) unhandled status code %d", vt.url, r.StatusCode) | ||
321 | 241 | - } | ||
322 | 242 | - } | ||
323 | 243 | -} | ||
324 | 244 | - | ||
325 | 245 | -var serveMuxRegister = []struct { | ||
326 | 246 | - pattern string | ||
327 | 247 | - h Handler | ||
328 | 248 | -}{ | ||
329 | 249 | - {"/dir/", serve(200)}, | ||
330 | 250 | - {"/search", serve(201)}, | ||
331 | 251 | - {"codesearch.google.com/search", serve(202)}, | ||
332 | 252 | - {"codesearch.google.com/", serve(203)}, | ||
333 | 253 | - {"example.com/", HandlerFunc(checkQueryStringHandler)}, | ||
334 | 254 | -} | ||
335 | 255 | - | ||
336 | 256 | -// serve returns a handler that sends a response with the given code. | ||
337 | 257 | -func serve(code int) HandlerFunc { | ||
338 | 258 | - return func(w ResponseWriter, r *Request) { | ||
339 | 259 | - w.WriteHeader(code) | ||
340 | 260 | - } | ||
341 | 261 | -} | ||
342 | 262 | - | ||
343 | 263 | -// checkQueryStringHandler checks if r.URL.RawQuery has the same value | ||
344 | 264 | -// as the URL excluding the scheme and the query string and sends 200 | ||
345 | 265 | -// response code if it is, 500 otherwise. | ||
346 | 266 | -func checkQueryStringHandler(w ResponseWriter, r *Request) { | ||
347 | 267 | - u := *r.URL | ||
348 | 268 | - u.Scheme = "http" | ||
349 | 269 | - u.Host = r.Host | ||
350 | 270 | - u.RawQuery = "" | ||
351 | 271 | - if "http://"+r.URL.RawQuery == u.String() { | ||
352 | 272 | - w.WriteHeader(200) | ||
353 | 273 | - } else { | ||
354 | 274 | - w.WriteHeader(500) | ||
355 | 275 | - } | ||
356 | 276 | -} | ||
357 | 277 | - | ||
358 | 278 | -var serveMuxTests = []struct { | ||
359 | 279 | - method string | ||
360 | 280 | - host string | ||
361 | 281 | - path string | ||
362 | 282 | - code int | ||
363 | 283 | - pattern string | ||
364 | 284 | -}{ | ||
365 | 285 | - {"GET", "google.com", "/", 404, ""}, | ||
366 | 286 | - {"GET", "google.com", "/dir", 301, "/dir/"}, | ||
367 | 287 | - {"GET", "google.com", "/dir/", 200, "/dir/"}, | ||
368 | 288 | - {"GET", "google.com", "/dir/file", 200, "/dir/"}, | ||
369 | 289 | - {"GET", "google.com", "/search", 201, "/search"}, | ||
370 | 290 | - {"GET", "google.com", "/search/", 404, ""}, | ||
371 | 291 | - {"GET", "google.com", "/search/foo", 404, ""}, | ||
372 | 292 | - {"GET", "codesearch.google.com", "/search", 202, "codesearch.google.com/search"}, | ||
373 | 293 | - {"GET", "codesearch.google.com", "/search/", 203, "codesearch.google.com/"}, | ||
374 | 294 | - {"GET", "codesearch.google.com", "/search/foo", 203, "codesearch.google.com/"}, | ||
375 | 295 | - {"GET", "codesearch.google.com", "/", 203, "codesearch.google.com/"}, | ||
376 | 296 | - {"GET", "images.google.com", "/search", 201, "/search"}, | ||
377 | 297 | - {"GET", "images.google.com", "/search/", 404, ""}, | ||
378 | 298 | - {"GET", "images.google.com", "/search/foo", 404, ""}, | ||
379 | 299 | - {"GET", "google.com", "/../search", 301, "/search"}, | ||
380 | 300 | - {"GET", "google.com", "/dir/..", 301, ""}, | ||
381 | 301 | - {"GET", "google.com", "/dir/..", 301, ""}, | ||
382 | 302 | - {"GET", "google.com", "/dir/./file", 301, "/dir/"}, | ||
383 | 303 | - | ||
384 | 304 | - // The /foo -> /foo/ redirect applies to CONNECT requests | ||
385 | 305 | - // but the path canonicalization does not. | ||
386 | 306 | - {"CONNECT", "google.com", "/dir", 301, "/dir/"}, | ||
387 | 307 | - {"CONNECT", "google.com", "/../search", 404, ""}, | ||
388 | 308 | - {"CONNECT", "google.com", "/dir/..", 200, "/dir/"}, | ||
389 | 309 | - {"CONNECT", "google.com", "/dir/..", 200, "/dir/"}, | ||
390 | 310 | - {"CONNECT", "google.com", "/dir/./file", 200, "/dir/"}, | ||
391 | 311 | -} | ||
392 | 312 | - | ||
393 | 313 | -func TestServeMuxHandler(t *testing.T) { | ||
394 | 314 | - mux := NewServeMux() | ||
395 | 315 | - for _, e := range serveMuxRegister { | ||
396 | 316 | - mux.Handle(e.pattern, e.h) | ||
397 | 317 | - } | ||
398 | 318 | - | ||
399 | 319 | - for _, tt := range serveMuxTests { | ||
400 | 320 | - r := &Request{ | ||
401 | 321 | - Method: tt.method, | ||
402 | 322 | - Host: tt.host, | ||
403 | 323 | - URL: &url.URL{ | ||
404 | 324 | - Path: tt.path, | ||
405 | 325 | - }, | ||
406 | 326 | - } | ||
407 | 327 | - h, pattern := mux.Handler(r) | ||
408 | 328 | - rr := httptest.NewRecorder() | ||
409 | 329 | - h.ServeHTTP(rr, r) | ||
410 | 330 | - if pattern != tt.pattern || rr.Code != tt.code { | ||
411 | 331 | - t.Errorf("%s %s %s = %d, %q, want %d, %q", tt.method, tt.host, tt.path, rr.Code, pattern, tt.code, tt.pattern) | ||
412 | 332 | - } | ||
413 | 333 | - } | ||
414 | 334 | -} | ||
415 | 335 | - | ||
416 | 336 | -var serveMuxTests2 = []struct { | ||
417 | 337 | - method string | ||
418 | 338 | - host string | ||
419 | 339 | - url string | ||
420 | 340 | - code int | ||
421 | 341 | - redirOk bool | ||
422 | 342 | -}{ | ||
423 | 343 | - {"GET", "google.com", "/", 404, false}, | ||
424 | 344 | - {"GET", "example.com", "/test/?example.com/test/", 200, false}, | ||
425 | 345 | - {"GET", "example.com", "test/?example.com/test/", 200, true}, | ||
426 | 346 | -} | ||
427 | 347 | - | ||
428 | 348 | -// TestServeMuxHandlerRedirects tests that automatic redirects generated by | ||
429 | 349 | -// mux.Handler() shouldn't clear the request's query string. | ||
430 | 350 | -func TestServeMuxHandlerRedirects(t *testing.T) { | ||
431 | 351 | - mux := NewServeMux() | ||
432 | 352 | - for _, e := range serveMuxRegister { | ||
433 | 353 | - mux.Handle(e.pattern, e.h) | ||
434 | 354 | - } | ||
435 | 355 | - | ||
436 | 356 | - for _, tt := range serveMuxTests2 { | ||
437 | 357 | - tries := 1 | ||
438 | 358 | - turl := tt.url | ||
439 | 359 | - for tries > 0 { | ||
440 | 360 | - u, e := url.Parse(turl) | ||
441 | 361 | - if e != nil { | ||
442 | 362 | - t.Fatal(e) | ||
443 | 363 | - } | ||
444 | 364 | - r := &Request{ | ||
445 | 365 | - Method: tt.method, | ||
446 | 366 | - Host: tt.host, | ||
447 | 367 | - URL: u, | ||
448 | 368 | - } | ||
449 | 369 | - h, _ := mux.Handler(r) | ||
450 | 370 | - rr := httptest.NewRecorder() | ||
451 | 371 | - h.ServeHTTP(rr, r) | ||
452 | 372 | - if rr.Code != 301 { | ||
453 | 373 | - if rr.Code != tt.code { | ||
454 | 374 | - t.Errorf("%s %s %s = %d, want %d", tt.method, tt.host, tt.url, rr.Code, tt.code) | ||
455 | 375 | - } | ||
456 | 376 | - break | ||
457 | 377 | - } | ||
458 | 378 | - if !tt.redirOk { | ||
459 | 379 | - t.Errorf("%s %s %s, unexpected redirect", tt.method, tt.host, tt.url) | ||
460 | 380 | - break | ||
461 | 381 | - } | ||
462 | 382 | - turl = rr.HeaderMap.Get("Location") | ||
463 | 383 | - tries-- | ||
464 | 384 | - } | ||
465 | 385 | - if tries < 0 { | ||
466 | 386 | - t.Errorf("%s %s %s, too many redirects", tt.method, tt.host, tt.url) | ||
467 | 387 | - } | ||
468 | 388 | - } | ||
469 | 389 | -} | ||
470 | 390 | - | ||
471 | 391 | -// Tests for http://code.google.com/p/go/issues/detail?id=900 | ||
472 | 392 | -func TestMuxRedirectLeadingSlashes(t *testing.T) { | ||
473 | 393 | - paths := []string{"//foo.txt", "///foo.txt", "/../../foo.txt"} | ||
474 | 394 | - for _, path := range paths { | ||
475 | 395 | - req, err := ReadRequest(bufio.NewReader(strings.NewReader("GET " + path + " HTTP/1.1\r\nHost: test\r\n\r\n"))) | ||
476 | 396 | - if err != nil { | ||
477 | 397 | - t.Errorf("%s", err) | ||
478 | 398 | - } | ||
479 | 399 | - mux := NewServeMux() | ||
480 | 400 | - resp := httptest.NewRecorder() | ||
481 | 401 | - | ||
482 | 402 | - mux.ServeHTTP(resp, req) | ||
483 | 403 | - | ||
484 | 404 | - if loc, expected := resp.Header().Get("Location"), "/foo.txt"; loc != expected { | ||
485 | 405 | - t.Errorf("Expected Location header set to %q; got %q", expected, loc) | ||
486 | 406 | - return | ||
487 | 407 | - } | ||
488 | 408 | - | ||
489 | 409 | - if code, expected := resp.Code, StatusMovedPermanently; code != expected { | ||
490 | 410 | - t.Errorf("Expected response code of StatusMovedPermanently; got %d", code) | ||
491 | 411 | - return | ||
492 | 412 | - } | ||
493 | 413 | - } | ||
494 | 414 | -} | ||
495 | 415 | - | ||
496 | 416 | -func TestServerTimeouts(t *testing.T) { | ||
497 | 417 | - if runtime.GOOS == "plan9" { | ||
498 | 418 | - t.Skip("skipping test; see http://golang.org/issue/7237") | ||
499 | 419 | - } | ||
500 | 420 | - defer afterTest(t) | ||
501 | 421 | - reqNum := 0 | ||
502 | 422 | - ts := httptest.NewUnstartedServer(HandlerFunc(func(res ResponseWriter, req *Request) { | ||
503 | 423 | - reqNum++ | ||
504 | 424 | - fmt.Fprintf(res, "req=%d", reqNum) | ||
505 | 425 | - })) | ||
506 | 426 | - ts.Config.ReadTimeout = 250 * time.Millisecond | ||
507 | 427 | - ts.Config.WriteTimeout = 250 * time.Millisecond | ||
508 | 428 | - ts.Start() | ||
509 | 429 | - defer ts.Close() | ||
510 | 430 | - | ||
511 | 431 | - // Hit the HTTP server successfully. | ||
512 | 432 | - tr := &Transport{DisableKeepAlives: true} // they interfere with this test | ||
513 | 433 | - defer tr.CloseIdleConnections() | ||
514 | 434 | - c := &Client{Transport: tr} | ||
515 | 435 | - r, err := c.Get(ts.URL) | ||
516 | 436 | - if err != nil { | ||
517 | 437 | - t.Fatalf("http Get #1: %v", err) | ||
518 | 438 | - } | ||
519 | 439 | - got, _ := ioutil.ReadAll(r.Body) | ||
520 | 440 | - expected := "req=1" | ||
521 | 441 | - if string(got) != expected { | ||
522 | 442 | - t.Errorf("Unexpected response for request #1; got %q; expected %q", | ||
523 | 443 | - string(got), expected) | ||
524 | 444 | - } | ||
525 | 445 | - | ||
526 | 446 | - // Slow client that should timeout. | ||
527 | 447 | - t1 := time.Now() | ||
528 | 448 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
529 | 449 | - if err != nil { | ||
530 | 450 | - t.Fatalf("Dial: %v", err) | ||
531 | 451 | - } | ||
532 | 452 | - buf := make([]byte, 1) | ||
533 | 453 | - n, err := conn.Read(buf) | ||
534 | 454 | - latency := time.Since(t1) | ||
535 | 455 | - if n != 0 || err != io.EOF { | ||
536 | 456 | - t.Errorf("Read = %v, %v, wanted %v, %v", n, err, 0, io.EOF) | ||
537 | 457 | - } | ||
538 | 458 | - if latency < 200*time.Millisecond /* fudge from 250 ms above */ { | ||
539 | 459 | - t.Errorf("got EOF after %s, want >= %s", latency, 200*time.Millisecond) | ||
540 | 460 | - } | ||
541 | 461 | - | ||
542 | 462 | - // Hit the HTTP server successfully again, verifying that the | ||
543 | 463 | - // previous slow connection didn't run our handler. (that we | ||
544 | 464 | - // get "req=2", not "req=3") | ||
545 | 465 | - r, err = Get(ts.URL) | ||
546 | 466 | - if err != nil { | ||
547 | 467 | - t.Fatalf("http Get #2: %v", err) | ||
548 | 468 | - } | ||
549 | 469 | - got, _ = ioutil.ReadAll(r.Body) | ||
550 | 470 | - expected = "req=2" | ||
551 | 471 | - if string(got) != expected { | ||
552 | 472 | - t.Errorf("Get #2 got %q, want %q", string(got), expected) | ||
553 | 473 | - } | ||
554 | 474 | - | ||
555 | 475 | - if !testing.Short() { | ||
556 | 476 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
557 | 477 | - if err != nil { | ||
558 | 478 | - t.Fatalf("Dial: %v", err) | ||
559 | 479 | - } | ||
560 | 480 | - defer conn.Close() | ||
561 | 481 | - go io.Copy(ioutil.Discard, conn) | ||
562 | 482 | - for i := 0; i < 5; i++ { | ||
563 | 483 | - _, err := conn.Write([]byte("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")) | ||
564 | 484 | - if err != nil { | ||
565 | 485 | - t.Fatalf("on write %d: %v", i, err) | ||
566 | 486 | - } | ||
567 | 487 | - time.Sleep(ts.Config.ReadTimeout / 2) | ||
568 | 488 | - } | ||
569 | 489 | - } | ||
570 | 490 | -} | ||
571 | 491 | - | ||
572 | 492 | -// golang.org/issue/4741 -- setting only a write timeout that triggers | ||
573 | 493 | -// shouldn't cause a handler to block forever on reads (next HTTP | ||
574 | 494 | -// request) that will never happen. | ||
575 | 495 | -func TestOnlyWriteTimeout(t *testing.T) { | ||
576 | 496 | - if runtime.GOOS == "plan9" { | ||
577 | 497 | - t.Skip("skipping test; see http://golang.org/issue/7237") | ||
578 | 498 | - } | ||
579 | 499 | - defer afterTest(t) | ||
580 | 500 | - var conn net.Conn | ||
581 | 501 | - var afterTimeoutErrc = make(chan error, 1) | ||
582 | 502 | - ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, req *Request) { | ||
583 | 503 | - buf := make([]byte, 512<<10) | ||
584 | 504 | - _, err := w.Write(buf) | ||
585 | 505 | - if err != nil { | ||
586 | 506 | - t.Errorf("handler Write error: %v", err) | ||
587 | 507 | - return | ||
588 | 508 | - } | ||
589 | 509 | - conn.SetWriteDeadline(time.Now().Add(-30 * time.Second)) | ||
590 | 510 | - _, err = w.Write(buf) | ||
591 | 511 | - afterTimeoutErrc <- err | ||
592 | 512 | - })) | ||
593 | 513 | - ts.Listener = trackLastConnListener{ts.Listener, &conn} | ||
594 | 514 | - ts.Start() | ||
595 | 515 | - defer ts.Close() | ||
596 | 516 | - | ||
597 | 517 | - tr := &Transport{DisableKeepAlives: false} | ||
598 | 518 | - defer tr.CloseIdleConnections() | ||
599 | 519 | - c := &Client{Transport: tr} | ||
600 | 520 | - | ||
601 | 521 | - errc := make(chan error) | ||
602 | 522 | - go func() { | ||
603 | 523 | - res, err := c.Get(ts.URL) | ||
604 | 524 | - if err != nil { | ||
605 | 525 | - errc <- err | ||
606 | 526 | - return | ||
607 | 527 | - } | ||
608 | 528 | - _, err = io.Copy(ioutil.Discard, res.Body) | ||
609 | 529 | - errc <- err | ||
610 | 530 | - }() | ||
611 | 531 | - select { | ||
612 | 532 | - case err := <-errc: | ||
613 | 533 | - if err == nil { | ||
614 | 534 | - t.Errorf("expected an error from Get request") | ||
615 | 535 | - } | ||
616 | 536 | - case <-time.After(5 * time.Second): | ||
617 | 537 | - t.Fatal("timeout waiting for Get error") | ||
618 | 538 | - } | ||
619 | 539 | - if err := <-afterTimeoutErrc; err == nil { | ||
620 | 540 | - t.Error("expected write error after timeout") | ||
621 | 541 | - } | ||
622 | 542 | -} | ||
623 | 543 | - | ||
624 | 544 | -// trackLastConnListener tracks the last net.Conn that was accepted. | ||
625 | 545 | -type trackLastConnListener struct { | ||
626 | 546 | - net.Listener | ||
627 | 547 | - last *net.Conn // destination | ||
628 | 548 | -} | ||
629 | 549 | - | ||
630 | 550 | -func (l trackLastConnListener) Accept() (c net.Conn, err error) { | ||
631 | 551 | - c, err = l.Listener.Accept() | ||
632 | 552 | - *l.last = c | ||
633 | 553 | - return | ||
634 | 554 | -} | ||
635 | 555 | - | ||
636 | 556 | -// TestIdentityResponse verifies that a handler can unset | ||
637 | 557 | -func TestIdentityResponse(t *testing.T) { | ||
638 | 558 | - defer afterTest(t) | ||
639 | 559 | - handler := HandlerFunc(func(rw ResponseWriter, req *Request) { | ||
640 | 560 | - rw.Header().Set("Content-Length", "3") | ||
641 | 561 | - rw.Header().Set("Transfer-Encoding", req.FormValue("te")) | ||
642 | 562 | - switch { | ||
643 | 563 | - case req.FormValue("overwrite") == "1": | ||
644 | 564 | - _, err := rw.Write([]byte("foo TOO LONG")) | ||
645 | 565 | - if err != ErrContentLength { | ||
646 | 566 | - t.Errorf("expected ErrContentLength; got %v", err) | ||
647 | 567 | - } | ||
648 | 568 | - case req.FormValue("underwrite") == "1": | ||
649 | 569 | - rw.Header().Set("Content-Length", "500") | ||
650 | 570 | - rw.Write([]byte("too short")) | ||
651 | 571 | - default: | ||
652 | 572 | - rw.Write([]byte("foo")) | ||
653 | 573 | - } | ||
654 | 574 | - }) | ||
655 | 575 | - | ||
656 | 576 | - ts := httptest.NewServer(handler) | ||
657 | 577 | - defer ts.Close() | ||
658 | 578 | - | ||
659 | 579 | - // Note: this relies on the assumption (which is true) that | ||
660 | 580 | - // Get sends HTTP/1.1 or greater requests. Otherwise the | ||
661 | 581 | - // server wouldn't have the choice to send back chunked | ||
662 | 582 | - // responses. | ||
663 | 583 | - for _, te := range []string{"", "identity"} { | ||
664 | 584 | - url := ts.URL + "/?te=" + te | ||
665 | 585 | - res, err := Get(url) | ||
666 | 586 | - if err != nil { | ||
667 | 587 | - t.Fatalf("error with Get of %s: %v", url, err) | ||
668 | 588 | - } | ||
669 | 589 | - if cl, expected := res.ContentLength, int64(3); cl != expected { | ||
670 | 590 | - t.Errorf("for %s expected res.ContentLength of %d; got %d", url, expected, cl) | ||
671 | 591 | - } | ||
672 | 592 | - if cl, expected := res.Header.Get("Content-Length"), "3"; cl != expected { | ||
673 | 593 | - t.Errorf("for %s expected Content-Length header of %q; got %q", url, expected, cl) | ||
674 | 594 | - } | ||
675 | 595 | - if tl, expected := len(res.TransferEncoding), 0; tl != expected { | ||
676 | 596 | - t.Errorf("for %s expected len(res.TransferEncoding) of %d; got %d (%v)", | ||
677 | 597 | - url, expected, tl, res.TransferEncoding) | ||
678 | 598 | - } | ||
679 | 599 | - res.Body.Close() | ||
680 | 600 | - } | ||
681 | 601 | - | ||
682 | 602 | - // Verify that ErrContentLength is returned | ||
683 | 603 | - url := ts.URL + "/?overwrite=1" | ||
684 | 604 | - res, err := Get(url) | ||
685 | 605 | - if err != nil { | ||
686 | 606 | - t.Fatalf("error with Get of %s: %v", url, err) | ||
687 | 607 | - } | ||
688 | 608 | - res.Body.Close() | ||
689 | 609 | - | ||
690 | 610 | - // Verify that the connection is closed when the declared Content-Length | ||
691 | 611 | - // is larger than what the handler wrote. | ||
692 | 612 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
693 | 613 | - if err != nil { | ||
694 | 614 | - t.Fatalf("error dialing: %v", err) | ||
695 | 615 | - } | ||
696 | 616 | - _, err = conn.Write([]byte("GET /?underwrite=1 HTTP/1.1\r\nHost: foo\r\n\r\n")) | ||
697 | 617 | - if err != nil { | ||
698 | 618 | - t.Fatalf("error writing: %v", err) | ||
699 | 619 | - } | ||
700 | 620 | - | ||
701 | 621 | - // The ReadAll will hang for a failing test, so use a Timer to | ||
702 | 622 | - // fail explicitly. | ||
703 | 623 | - goTimeout(t, 2*time.Second, func() { | ||
704 | 624 | - got, _ := ioutil.ReadAll(conn) | ||
705 | 625 | - expectedSuffix := "\r\n\r\ntoo short" | ||
706 | 626 | - if !strings.HasSuffix(string(got), expectedSuffix) { | ||
707 | 627 | - t.Errorf("Expected output to end with %q; got response body %q", | ||
708 | 628 | - expectedSuffix, string(got)) | ||
709 | 629 | - } | ||
710 | 630 | - }) | ||
711 | 631 | -} | ||
712 | 632 | - | ||
713 | 633 | -func testTCPConnectionCloses(t *testing.T, req string, h Handler) { | ||
714 | 634 | - defer afterTest(t) | ||
715 | 635 | - s := httptest.NewServer(h) | ||
716 | 636 | - defer s.Close() | ||
717 | 637 | - | ||
718 | 638 | - conn, err := net.Dial("tcp", s.Listener.Addr().String()) | ||
719 | 639 | - if err != nil { | ||
720 | 640 | - t.Fatal("dial error:", err) | ||
721 | 641 | - } | ||
722 | 642 | - defer conn.Close() | ||
723 | 643 | - | ||
724 | 644 | - _, err = fmt.Fprint(conn, req) | ||
725 | 645 | - if err != nil { | ||
726 | 646 | - t.Fatal("print error:", err) | ||
727 | 647 | - } | ||
728 | 648 | - | ||
729 | 649 | - r := bufio.NewReader(conn) | ||
730 | 650 | - res, err := ReadResponse(r, &Request{Method: "GET"}) | ||
731 | 651 | - if err != nil { | ||
732 | 652 | - t.Fatal("ReadResponse error:", err) | ||
733 | 653 | - } | ||
734 | 654 | - | ||
735 | 655 | - didReadAll := make(chan bool, 1) | ||
736 | 656 | - go func() { | ||
737 | 657 | - select { | ||
738 | 658 | - case <-time.After(5 * time.Second): | ||
739 | 659 | - t.Error("body not closed after 5s") | ||
740 | 660 | - return | ||
741 | 661 | - case <-didReadAll: | ||
742 | 662 | - } | ||
743 | 663 | - }() | ||
744 | 664 | - | ||
745 | 665 | - _, err = ioutil.ReadAll(r) | ||
746 | 666 | - if err != nil { | ||
747 | 667 | - t.Fatal("read error:", err) | ||
748 | 668 | - } | ||
749 | 669 | - didReadAll <- true | ||
750 | 670 | - | ||
751 | 671 | - if !res.Close { | ||
752 | 672 | - t.Errorf("Response.Close = false; want true") | ||
753 | 673 | - } | ||
754 | 674 | -} | ||
755 | 675 | - | ||
756 | 676 | -// TestServeHTTP10Close verifies that HTTP/1.0 requests won't be kept alive. | ||
757 | 677 | -func TestServeHTTP10Close(t *testing.T) { | ||
758 | 678 | - testTCPConnectionCloses(t, "GET / HTTP/1.0\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) { | ||
759 | 679 | - ServeFile(w, r, "testdata/file") | ||
760 | 680 | - })) | ||
761 | 681 | -} | ||
762 | 682 | - | ||
763 | 683 | -// TestClientCanClose verifies that clients can also force a connection to close. | ||
764 | 684 | -func TestClientCanClose(t *testing.T) { | ||
765 | 685 | - testTCPConnectionCloses(t, "GET / HTTP/1.1\r\nConnection: close\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) { | ||
766 | 686 | - // Nothing. | ||
767 | 687 | - })) | ||
768 | 688 | -} | ||
769 | 689 | - | ||
770 | 690 | -// TestHandlersCanSetConnectionClose verifies that handlers can force a connection to close, | ||
771 | 691 | -// even for HTTP/1.1 requests. | ||
772 | 692 | -func TestHandlersCanSetConnectionClose11(t *testing.T) { | ||
773 | 693 | - testTCPConnectionCloses(t, "GET / HTTP/1.1\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) { | ||
774 | 694 | - w.Header().Set("Connection", "close") | ||
775 | 695 | - })) | ||
776 | 696 | -} | ||
777 | 697 | - | ||
778 | 698 | -func TestHandlersCanSetConnectionClose10(t *testing.T) { | ||
779 | 699 | - testTCPConnectionCloses(t, "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) { | ||
780 | 700 | - w.Header().Set("Connection", "close") | ||
781 | 701 | - })) | ||
782 | 702 | -} | ||
783 | 703 | - | ||
784 | 704 | -func TestSetsRemoteAddr(t *testing.T) { | ||
785 | 705 | - defer afterTest(t) | ||
786 | 706 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
787 | 707 | - fmt.Fprintf(w, "%s", r.RemoteAddr) | ||
788 | 708 | - })) | ||
789 | 709 | - defer ts.Close() | ||
790 | 710 | - | ||
791 | 711 | - res, err := Get(ts.URL) | ||
792 | 712 | - if err != nil { | ||
793 | 713 | - t.Fatalf("Get error: %v", err) | ||
794 | 714 | - } | ||
795 | 715 | - body, err := ioutil.ReadAll(res.Body) | ||
796 | 716 | - if err != nil { | ||
797 | 717 | - t.Fatalf("ReadAll error: %v", err) | ||
798 | 718 | - } | ||
799 | 719 | - ip := string(body) | ||
800 | 720 | - if !strings.HasPrefix(ip, "127.0.0.1:") && !strings.HasPrefix(ip, "[::1]:") { | ||
801 | 721 | - t.Fatalf("Expected local addr; got %q", ip) | ||
802 | 722 | - } | ||
803 | 723 | -} | ||
804 | 724 | - | ||
805 | 725 | -func TestChunkedResponseHeaders(t *testing.T) { | ||
806 | 726 | - defer afterTest(t) | ||
807 | 727 | - log.SetOutput(ioutil.Discard) // is noisy otherwise | ||
808 | 728 | - defer log.SetOutput(os.Stderr) | ||
809 | 729 | - | ||
810 | 730 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
811 | 731 | - w.Header().Set("Content-Length", "intentional gibberish") // we check that this is deleted | ||
812 | 732 | - w.(Flusher).Flush() | ||
813 | 733 | - fmt.Fprintf(w, "I am a chunked response.") | ||
814 | 734 | - })) | ||
815 | 735 | - defer ts.Close() | ||
816 | 736 | - | ||
817 | 737 | - res, err := Get(ts.URL) | ||
818 | 738 | - if err != nil { | ||
819 | 739 | - t.Fatalf("Get error: %v", err) | ||
820 | 740 | - } | ||
821 | 741 | - defer res.Body.Close() | ||
822 | 742 | - if g, e := res.ContentLength, int64(-1); g != e { | ||
823 | 743 | - t.Errorf("expected ContentLength of %d; got %d", e, g) | ||
824 | 744 | - } | ||
825 | 745 | - if g, e := res.TransferEncoding, []string{"chunked"}; !reflect.DeepEqual(g, e) { | ||
826 | 746 | - t.Errorf("expected TransferEncoding of %v; got %v", e, g) | ||
827 | 747 | - } | ||
828 | 748 | - if _, haveCL := res.Header["Content-Length"]; haveCL { | ||
829 | 749 | - t.Errorf("Unexpected Content-Length") | ||
830 | 750 | - } | ||
831 | 751 | -} | ||
832 | 752 | - | ||
833 | 753 | -// Test304Responses verifies that 304s don't declare that they're | ||
834 | 754 | -// chunking in their response headers and aren't allowed to produce | ||
835 | 755 | -// output. | ||
836 | 756 | -func Test304Responses(t *testing.T) { | ||
837 | 757 | - defer afterTest(t) | ||
838 | 758 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
839 | 759 | - w.WriteHeader(StatusNotModified) | ||
840 | 760 | - _, err := w.Write([]byte("illegal body")) | ||
841 | 761 | - if err != ErrBodyNotAllowed { | ||
842 | 762 | - t.Errorf("on Write, expected ErrBodyNotAllowed, got %v", err) | ||
843 | 763 | - } | ||
844 | 764 | - })) | ||
845 | 765 | - defer ts.Close() | ||
846 | 766 | - res, err := Get(ts.URL) | ||
847 | 767 | - if err != nil { | ||
848 | 768 | - t.Error(err) | ||
849 | 769 | - } | ||
850 | 770 | - if len(res.TransferEncoding) > 0 { | ||
851 | 771 | - t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding) | ||
852 | 772 | - } | ||
853 | 773 | - body, err := ioutil.ReadAll(res.Body) | ||
854 | 774 | - if err != nil { | ||
855 | 775 | - t.Error(err) | ||
856 | 776 | - } | ||
857 | 777 | - if len(body) > 0 { | ||
858 | 778 | - t.Errorf("got unexpected body %q", string(body)) | ||
859 | 779 | - } | ||
860 | 780 | -} | ||
861 | 781 | - | ||
862 | 782 | -// TestHeadResponses verifies that all MIME type sniffing and Content-Length | ||
863 | 783 | -// counting of GET requests also happens on HEAD requests. | ||
864 | 784 | -func TestHeadResponses(t *testing.T) { | ||
865 | 785 | - defer afterTest(t) | ||
866 | 786 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
867 | 787 | - _, err := w.Write([]byte("<html>")) | ||
868 | 788 | - if err != nil { | ||
869 | 789 | - t.Errorf("ResponseWriter.Write: %v", err) | ||
870 | 790 | - } | ||
871 | 791 | - | ||
872 | 792 | - // Also exercise the ReaderFrom path | ||
873 | 793 | - _, err = io.Copy(w, strings.NewReader("789a")) | ||
874 | 794 | - if err != nil { | ||
875 | 795 | - t.Errorf("Copy(ResponseWriter, ...): %v", err) | ||
876 | 796 | - } | ||
877 | 797 | - })) | ||
878 | 798 | - defer ts.Close() | ||
879 | 799 | - res, err := Head(ts.URL) | ||
880 | 800 | - if err != nil { | ||
881 | 801 | - t.Error(err) | ||
882 | 802 | - } | ||
883 | 803 | - if len(res.TransferEncoding) > 0 { | ||
884 | 804 | - t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding) | ||
885 | 805 | - } | ||
886 | 806 | - if ct := res.Header.Get("Content-Type"); ct != "text/html; charset=utf-8" { | ||
887 | 807 | - t.Errorf("Content-Type: %q; want text/html; charset=utf-8", ct) | ||
888 | 808 | - } | ||
889 | 809 | - if v := res.ContentLength; v != 10 { | ||
890 | 810 | - t.Errorf("Content-Length: %d; want 10", v) | ||
891 | 811 | - } | ||
892 | 812 | - body, err := ioutil.ReadAll(res.Body) | ||
893 | 813 | - if err != nil { | ||
894 | 814 | - t.Error(err) | ||
895 | 815 | - } | ||
896 | 816 | - if len(body) > 0 { | ||
897 | 817 | - t.Errorf("got unexpected body %q", string(body)) | ||
898 | 818 | - } | ||
899 | 819 | -} | ||
900 | 820 | - | ||
901 | 821 | -func TestTLSHandshakeTimeout(t *testing.T) { | ||
902 | 822 | - if runtime.GOOS == "plan9" { | ||
903 | 823 | - t.Skip("skipping test; see http://golang.org/issue/7237") | ||
904 | 824 | - } | ||
905 | 825 | - defer afterTest(t) | ||
906 | 826 | - ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {})) | ||
907 | 827 | - errc := make(chanWriter, 10) // but only expecting 1 | ||
908 | 828 | - ts.Config.ReadTimeout = 250 * time.Millisecond | ||
909 | 829 | - ts.Config.ErrorLog = log.New(errc, "", 0) | ||
910 | 830 | - ts.StartTLS() | ||
911 | 831 | - defer ts.Close() | ||
912 | 832 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
913 | 833 | - if err != nil { | ||
914 | 834 | - t.Fatalf("Dial: %v", err) | ||
915 | 835 | - } | ||
916 | 836 | - defer conn.Close() | ||
917 | 837 | - goTimeout(t, 10*time.Second, func() { | ||
918 | 838 | - var buf [1]byte | ||
919 | 839 | - n, err := conn.Read(buf[:]) | ||
920 | 840 | - if err == nil || n != 0 { | ||
921 | 841 | - t.Errorf("Read = %d, %v; want an error and no bytes", n, err) | ||
922 | 842 | - } | ||
923 | 843 | - }) | ||
924 | 844 | - select { | ||
925 | 845 | - case v := <-errc: | ||
926 | 846 | - if !strings.Contains(v, "timeout") && !strings.Contains(v, "TLS handshake") { | ||
927 | 847 | - t.Errorf("expected a TLS handshake timeout error; got %q", v) | ||
928 | 848 | - } | ||
929 | 849 | - case <-time.After(5 * time.Second): | ||
930 | 850 | - t.Errorf("timeout waiting for logged error") | ||
931 | 851 | - } | ||
932 | 852 | -} | ||
933 | 853 | - | ||
934 | 854 | -func TestTLSServer(t *testing.T) { | ||
935 | 855 | - defer afterTest(t) | ||
936 | 856 | - ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
937 | 857 | - if r.TLS != nil { | ||
938 | 858 | - w.Header().Set("X-TLS-Set", "true") | ||
939 | 859 | - if r.TLS.HandshakeComplete { | ||
940 | 860 | - w.Header().Set("X-TLS-HandshakeComplete", "true") | ||
941 | 861 | - } | ||
942 | 862 | - } | ||
943 | 863 | - })) | ||
944 | 864 | - ts.Config.ErrorLog = log.New(ioutil.Discard, "", 0) | ||
945 | 865 | - defer ts.Close() | ||
946 | 866 | - | ||
947 | 867 | - // Connect an idle TCP connection to this server before we run | ||
948 | 868 | - // our real tests. This idle connection used to block forever | ||
949 | 869 | - // in the TLS handshake, preventing future connections from | ||
950 | 870 | - // being accepted. It may prevent future accidental blocking | ||
951 | 871 | - // in newConn. | ||
952 | 872 | - idleConn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
953 | 873 | - if err != nil { | ||
954 | 874 | - t.Fatalf("Dial: %v", err) | ||
955 | 875 | - } | ||
956 | 876 | - defer idleConn.Close() | ||
957 | 877 | - goTimeout(t, 10*time.Second, func() { | ||
958 | 878 | - if !strings.HasPrefix(ts.URL, "https://") { | ||
959 | 879 | - t.Errorf("expected test TLS server to start with https://, got %q", ts.URL) | ||
960 | 880 | - return | ||
961 | 881 | - } | ||
962 | 882 | - noVerifyTransport := &Transport{ | ||
963 | 883 | - TLSClientConfig: &tls.Config{ | ||
964 | 884 | - InsecureSkipVerify: true, | ||
965 | 885 | - }, | ||
966 | 886 | - } | ||
967 | 887 | - client := &Client{Transport: noVerifyTransport} | ||
968 | 888 | - res, err := client.Get(ts.URL) | ||
969 | 889 | - if err != nil { | ||
970 | 890 | - t.Error(err) | ||
971 | 891 | - return | ||
972 | 892 | - } | ||
973 | 893 | - if res == nil { | ||
974 | 894 | - t.Errorf("got nil Response") | ||
975 | 895 | - return | ||
976 | 896 | - } | ||
977 | 897 | - defer res.Body.Close() | ||
978 | 898 | - if res.Header.Get("X-TLS-Set") != "true" { | ||
979 | 899 | - t.Errorf("expected X-TLS-Set response header") | ||
980 | 900 | - return | ||
981 | 901 | - } | ||
982 | 902 | - if res.Header.Get("X-TLS-HandshakeComplete") != "true" { | ||
983 | 903 | - t.Errorf("expected X-TLS-HandshakeComplete header") | ||
984 | 904 | - } | ||
985 | 905 | - }) | ||
986 | 906 | -} | ||
987 | 907 | - | ||
988 | 908 | -type serverExpectTest struct { | ||
989 | 909 | - contentLength int // of request body | ||
990 | 910 | - expectation string // e.g. "100-continue" | ||
991 | 911 | - readBody bool // whether handler should read the body (if false, sends StatusUnauthorized) | ||
992 | 912 | - expectedResponse string // expected substring in first line of http response | ||
993 | 913 | -} | ||
994 | 914 | - | ||
995 | 915 | -var serverExpectTests = []serverExpectTest{ | ||
996 | 916 | - // Normal 100-continues, case-insensitive. | ||
997 | 917 | - {100, "100-continue", true, "100 Continue"}, | ||
998 | 918 | - {100, "100-cOntInUE", true, "100 Continue"}, | ||
999 | 919 | - | ||
1000 | 920 | - // No 100-continue. | ||
1001 | 921 | - {100, "", true, "200 OK"}, | ||
1002 | 922 | - | ||
1003 | 923 | - // 100-continue but requesting client to deny us, | ||
1004 | 924 | - // so it never reads the body. | ||
1005 | 925 | - {100, "100-continue", false, "401 Unauthorized"}, | ||
1006 | 926 | - // Likewise without 100-continue: | ||
1007 | 927 | - {100, "", false, "401 Unauthorized"}, | ||
1008 | 928 | - | ||
1009 | 929 | - // Non-standard expectations are failures | ||
1010 | 930 | - {0, "a-pony", false, "417 Expectation Failed"}, | ||
1011 | 931 | - | ||
1012 | 932 | - // Expect-100 requested but no body | ||
1013 | 933 | - {0, "100-continue", true, "400 Bad Request"}, | ||
1014 | 934 | -} | ||
1015 | 935 | - | ||
1016 | 936 | -// Tests that the server responds to the "Expect" request header | ||
1017 | 937 | -// correctly. | ||
1018 | 938 | -func TestServerExpect(t *testing.T) { | ||
1019 | 939 | - defer afterTest(t) | ||
1020 | 940 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
1021 | 941 | - // Note using r.FormValue("readbody") because for POST | ||
1022 | 942 | - // requests that would read from r.Body, which we only | ||
1023 | 943 | - // conditionally want to do. | ||
1024 | 944 | - if strings.Contains(r.URL.RawQuery, "readbody=true") { | ||
1025 | 945 | - ioutil.ReadAll(r.Body) | ||
1026 | 946 | - w.Write([]byte("Hi")) | ||
1027 | 947 | - } else { | ||
1028 | 948 | - w.WriteHeader(StatusUnauthorized) | ||
1029 | 949 | - } | ||
1030 | 950 | - })) | ||
1031 | 951 | - defer ts.Close() | ||
1032 | 952 | - | ||
1033 | 953 | - runTest := func(test serverExpectTest) { | ||
1034 | 954 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
1035 | 955 | - if err != nil { | ||
1036 | 956 | - t.Fatalf("Dial: %v", err) | ||
1037 | 957 | - } | ||
1038 | 958 | - defer conn.Close() | ||
1039 | 959 | - | ||
1040 | 960 | - // Only send the body immediately if we're acting like an HTTP client | ||
1041 | 961 | - // that doesn't send 100-continue expectations. | ||
1042 | 962 | - writeBody := test.contentLength > 0 && strings.ToLower(test.expectation) != "100-continue" | ||
1043 | 963 | - | ||
1044 | 964 | - go func() { | ||
1045 | 965 | - _, err := fmt.Fprintf(conn, "POST /?readbody=%v HTTP/1.1\r\n"+ | ||
1046 | 966 | - "Connection: close\r\n"+ | ||
1047 | 967 | - "Content-Length: %d\r\n"+ | ||
1048 | 968 | - "Expect: %s\r\nHost: foo\r\n\r\n", | ||
1049 | 969 | - test.readBody, test.contentLength, test.expectation) | ||
1050 | 970 | - if err != nil { | ||
1051 | 971 | - t.Errorf("On test %#v, error writing request headers: %v", test, err) | ||
1052 | 972 | - return | ||
1053 | 973 | - } | ||
1054 | 974 | - if writeBody { | ||
1055 | 975 | - body := strings.Repeat("A", test.contentLength) | ||
1056 | 976 | - _, err = fmt.Fprint(conn, body) | ||
1057 | 977 | - if err != nil { | ||
1058 | 978 | - if !test.readBody { | ||
1059 | 979 | - // Server likely already hung up on us. | ||
1060 | 980 | - // See larger comment below. | ||
1061 | 981 | - t.Logf("On test %#v, acceptable error writing request body: %v", test, err) | ||
1062 | 982 | - return | ||
1063 | 983 | - } | ||
1064 | 984 | - t.Errorf("On test %#v, error writing request body: %v", test, err) | ||
1065 | 985 | - } | ||
1066 | 986 | - } | ||
1067 | 987 | - }() | ||
1068 | 988 | - bufr := bufio.NewReader(conn) | ||
1069 | 989 | - line, err := bufr.ReadString('\n') | ||
1070 | 990 | - if err != nil { | ||
1071 | 991 | - if writeBody && !test.readBody { | ||
1072 | 992 | - // This is an acceptable failure due to a possible TCP race: | ||
1073 | 993 | - // We were still writing data and the server hung up on us. A TCP | ||
1074 | 994 | - // implementation may send a RST if our request body data was known | ||
1075 | 995 | - // to be lost, which may trigger our reads to fail. | ||
1076 | 996 | - // See RFC 1122 page 88. | ||
1077 | 997 | - t.Logf("On test %#v, acceptable error from ReadString: %v", test, err) | ||
1078 | 998 | - return | ||
1079 | 999 | - } | ||
1080 | 1000 | - t.Fatalf("On test %#v, ReadString: %v", test, err) | ||
1081 | 1001 | - } | ||
1082 | 1002 | - if !strings.Contains(line, test.expectedResponse) { | ||
1083 | 1003 | - t.Errorf("On test %#v, got first line = %q; want %q", test, line, test.expectedResponse) | ||
1084 | 1004 | - } | ||
1085 | 1005 | - } | ||
1086 | 1006 | - | ||
1087 | 1007 | - for _, test := range serverExpectTests { | ||
1088 | 1008 | - runTest(test) | ||
1089 | 1009 | - } | ||
1090 | 1010 | -} | ||
1091 | 1011 | - | ||
1092 | 1012 | -// Under a ~256KB (maxPostHandlerReadBytes) threshold, the server | ||
1093 | 1013 | -// should consume client request bodies that a handler didn't read. | ||
1094 | 1014 | -func TestServerUnreadRequestBodyLittle(t *testing.T) { | ||
1095 | 1015 | - conn := new(testConn) | ||
1096 | 1016 | - body := strings.Repeat("x", 100<<10) | ||
1097 | 1017 | - conn.readBuf.Write([]byte(fmt.Sprintf( | ||
1098 | 1018 | - "POST / HTTP/1.1\r\n"+ | ||
1099 | 1019 | - "Host: test\r\n"+ | ||
1100 | 1020 | - "Content-Length: %d\r\n"+ | ||
1101 | 1021 | - "\r\n", len(body)))) | ||
1102 | 1022 | - conn.readBuf.Write([]byte(body)) | ||
1103 | 1023 | - | ||
1104 | 1024 | - done := make(chan bool) | ||
1105 | 1025 | - | ||
1106 | 1026 | - ls := &oneConnListener{conn} | ||
1107 | 1027 | - go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) { | ||
1108 | 1028 | - defer close(done) | ||
1109 | 1029 | - if conn.readBuf.Len() < len(body)/2 { | ||
1110 | 1030 | - t.Errorf("on request, read buffer length is %d; expected about 100 KB", conn.readBuf.Len()) | ||
1111 | 1031 | - } | ||
1112 | 1032 | - rw.WriteHeader(200) | ||
1113 | 1033 | - rw.(Flusher).Flush() | ||
1114 | 1034 | - if g, e := conn.readBuf.Len(), 0; g != e { | ||
1115 | 1035 | - t.Errorf("after WriteHeader, read buffer length is %d; want %d", g, e) | ||
1116 | 1036 | - } | ||
1117 | 1037 | - if c := rw.Header().Get("Connection"); c != "" { | ||
1118 | 1038 | - t.Errorf(`Connection header = %q; want ""`, c) | ||
1119 | 1039 | - } | ||
1120 | 1040 | - })) | ||
1121 | 1041 | - <-done | ||
1122 | 1042 | -} | ||
1123 | 1043 | - | ||
1124 | 1044 | -// Over a ~256KB (maxPostHandlerReadBytes) threshold, the server | ||
1125 | 1045 | -// should ignore client request bodies that a handler didn't read | ||
1126 | 1046 | -// and close the connection. | ||
1127 | 1047 | -func TestServerUnreadRequestBodyLarge(t *testing.T) { | ||
1128 | 1048 | - conn := new(testConn) | ||
1129 | 1049 | - body := strings.Repeat("x", 1<<20) | ||
1130 | 1050 | - conn.readBuf.Write([]byte(fmt.Sprintf( | ||
1131 | 1051 | - "POST / HTTP/1.1\r\n"+ | ||
1132 | 1052 | - "Host: test\r\n"+ | ||
1133 | 1053 | - "Content-Length: %d\r\n"+ | ||
1134 | 1054 | - "\r\n", len(body)))) | ||
1135 | 1055 | - conn.readBuf.Write([]byte(body)) | ||
1136 | 1056 | - conn.closec = make(chan bool, 1) | ||
1137 | 1057 | - | ||
1138 | 1058 | - ls := &oneConnListener{conn} | ||
1139 | 1059 | - go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) { | ||
1140 | 1060 | - if conn.readBuf.Len() < len(body)/2 { | ||
1141 | 1061 | - t.Errorf("on request, read buffer length is %d; expected about 1MB", conn.readBuf.Len()) | ||
1142 | 1062 | - } | ||
1143 | 1063 | - rw.WriteHeader(200) | ||
1144 | 1064 | - rw.(Flusher).Flush() | ||
1145 | 1065 | - if conn.readBuf.Len() < len(body)/2 { | ||
1146 | 1066 | - t.Errorf("post-WriteHeader, read buffer length is %d; expected about 1MB", conn.readBuf.Len()) | ||
1147 | 1067 | - } | ||
1148 | 1068 | - })) | ||
1149 | 1069 | - <-conn.closec | ||
1150 | 1070 | - | ||
1151 | 1071 | - if res := conn.writeBuf.String(); !strings.Contains(res, "Connection: close") { | ||
1152 | 1072 | - t.Errorf("Expected a Connection: close header; got response: %s", res) | ||
1153 | 1073 | - } | ||
1154 | 1074 | -} | ||
1155 | 1075 | - | ||
1156 | 1076 | -func TestTimeoutHandler(t *testing.T) { | ||
1157 | 1077 | - defer afterTest(t) | ||
1158 | 1078 | - sendHi := make(chan bool, 1) | ||
1159 | 1079 | - writeErrors := make(chan error, 1) | ||
1160 | 1080 | - sayHi := HandlerFunc(func(w ResponseWriter, r *Request) { | ||
1161 | 1081 | - <-sendHi | ||
1162 | 1082 | - _, werr := w.Write([]byte("hi")) | ||
1163 | 1083 | - writeErrors <- werr | ||
1164 | 1084 | - }) | ||
1165 | 1085 | - timeout := make(chan time.Time, 1) // write to this to force timeouts | ||
1166 | 1086 | - ts := httptest.NewServer(NewTestTimeoutHandler(sayHi, timeout)) | ||
1167 | 1087 | - defer ts.Close() | ||
1168 | 1088 | - | ||
1169 | 1089 | - // Succeed without timing out: | ||
1170 | 1090 | - sendHi <- true | ||
1171 | 1091 | - res, err := Get(ts.URL) | ||
1172 | 1092 | - if err != nil { | ||
1173 | 1093 | - t.Error(err) | ||
1174 | 1094 | - } | ||
1175 | 1095 | - if g, e := res.StatusCode, StatusOK; g != e { | ||
1176 | 1096 | - t.Errorf("got res.StatusCode %d; expected %d", g, e) | ||
1177 | 1097 | - } | ||
1178 | 1098 | - body, _ := ioutil.ReadAll(res.Body) | ||
1179 | 1099 | - if g, e := string(body), "hi"; g != e { | ||
1180 | 1100 | - t.Errorf("got body %q; expected %q", g, e) | ||
1181 | 1101 | - } | ||
1182 | 1102 | - if g := <-writeErrors; g != nil { | ||
1183 | 1103 | - t.Errorf("got unexpected Write error on first request: %v", g) | ||
1184 | 1104 | - } | ||
1185 | 1105 | - | ||
1186 | 1106 | - // Times out: | ||
1187 | 1107 | - timeout <- time.Time{} | ||
1188 | 1108 | - res, err = Get(ts.URL) | ||
1189 | 1109 | - if err != nil { | ||
1190 | 1110 | - t.Error(err) | ||
1191 | 1111 | - } | ||
1192 | 1112 | - if g, e := res.StatusCode, StatusServiceUnavailable; g != e { | ||
1193 | 1113 | - t.Errorf("got res.StatusCode %d; expected %d", g, e) | ||
1194 | 1114 | - } | ||
1195 | 1115 | - body, _ = ioutil.ReadAll(res.Body) | ||
1196 | 1116 | - if !strings.Contains(string(body), "<title>Timeout</title>") { | ||
1197 | 1117 | - t.Errorf("expected timeout body; got %q", string(body)) | ||
1198 | 1118 | - } | ||
1199 | 1119 | - | ||
1200 | 1120 | - // Now make the previously-timed out handler speak again, | ||
1201 | 1121 | - // which verifies the panic is handled: | ||
1202 | 1122 | - sendHi <- true | ||
1203 | 1123 | - if g, e := <-writeErrors, ErrHandlerTimeout; g != e { | ||
1204 | 1124 | - t.Errorf("expected Write error of %v; got %v", e, g) | ||
1205 | 1125 | - } | ||
1206 | 1126 | -} | ||
1207 | 1127 | - | ||
1208 | 1128 | -// Verifies we don't path.Clean() on the wrong parts in redirects. | ||
1209 | 1129 | -func TestRedirectMunging(t *testing.T) { | ||
1210 | 1130 | - req, _ := NewRequest("GET", "http://example.com/", nil) | ||
1211 | 1131 | - | ||
1212 | 1132 | - resp := httptest.NewRecorder() | ||
1213 | 1133 | - Redirect(resp, req, "/foo?next=http://bar.com/", 302) | ||
1214 | 1134 | - if g, e := resp.Header().Get("Location"), "/foo?next=http://bar.com/"; g != e { | ||
1215 | 1135 | - t.Errorf("Location header was %q; want %q", g, e) | ||
1216 | 1136 | - } | ||
1217 | 1137 | - | ||
1218 | 1138 | - resp = httptest.NewRecorder() | ||
1219 | 1139 | - Redirect(resp, req, "http://localhost:8080/_ah/login?continue=http://localhost:8080/", 302) | ||
1220 | 1140 | - if g, e := resp.Header().Get("Location"), "http://localhost:8080/_ah/login?continue=http://localhost:8080/"; g != e { | ||
1221 | 1141 | - t.Errorf("Location header was %q; want %q", g, e) | ||
1222 | 1142 | - } | ||
1223 | 1143 | -} | ||
1224 | 1144 | - | ||
1225 | 1145 | -func TestRedirectBadPath(t *testing.T) { | ||
1226 | 1146 | - // This used to crash. It's not valid input (bad path), but it | ||
1227 | 1147 | - // shouldn't crash. | ||
1228 | 1148 | - rr := httptest.NewRecorder() | ||
1229 | 1149 | - req := &Request{ | ||
1230 | 1150 | - Method: "GET", | ||
1231 | 1151 | - URL: &url.URL{ | ||
1232 | 1152 | - Scheme: "http", | ||
1233 | 1153 | - Path: "not-empty-but-no-leading-slash", // bogus | ||
1234 | 1154 | - }, | ||
1235 | 1155 | - } | ||
1236 | 1156 | - Redirect(rr, req, "", 304) | ||
1237 | 1157 | - if rr.Code != 304 { | ||
1238 | 1158 | - t.Errorf("Code = %d; want 304", rr.Code) | ||
1239 | 1159 | - } | ||
1240 | 1160 | -} | ||
1241 | 1161 | - | ||
1242 | 1162 | -// TestZeroLengthPostAndResponse exercises an optimization done by the Transport: | ||
1243 | 1163 | -// when there is no body (either because the method doesn't permit a body, or an | ||
1244 | 1164 | -// explicit Content-Length of zero is present), then the transport can re-use the | ||
1245 | 1165 | -// connection immediately. But when it re-uses the connection, it typically closes | ||
1246 | 1166 | -// the previous request's body, which is not optimal for zero-lengthed bodies, | ||
1247 | 1167 | -// as the client would then see http.ErrBodyReadAfterClose and not 0, io.EOF. | ||
1248 | 1168 | -func TestZeroLengthPostAndResponse(t *testing.T) { | ||
1249 | 1169 | - defer afterTest(t) | ||
1250 | 1170 | - ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
1251 | 1171 | - all, err := ioutil.ReadAll(r.Body) | ||
1252 | 1172 | - if err != nil { | ||
1253 | 1173 | - t.Fatalf("handler ReadAll: %v", err) | ||
1254 | 1174 | - } | ||
1255 | 1175 | - if len(all) != 0 { | ||
1256 | 1176 | - t.Errorf("handler got %d bytes; expected 0", len(all)) | ||
1257 | 1177 | - } | ||
1258 | 1178 | - rw.Header().Set("Content-Length", "0") | ||
1259 | 1179 | - })) | ||
1260 | 1180 | - defer ts.Close() | ||
1261 | 1181 | - | ||
1262 | 1182 | - req, err := NewRequest("POST", ts.URL, strings.NewReader("")) | ||
1263 | 1183 | - if err != nil { | ||
1264 | 1184 | - t.Fatal(err) | ||
1265 | 1185 | - } | ||
1266 | 1186 | - req.ContentLength = 0 | ||
1267 | 1187 | - | ||
1268 | 1188 | - var resp [5]*Response | ||
1269 | 1189 | - for i := range resp { | ||
1270 | 1190 | - resp[i], err = DefaultClient.Do(req) | ||
1271 | 1191 | - if err != nil { | ||
1272 | 1192 | - t.Fatalf("client post #%d: %v", i, err) | ||
1273 | 1193 | - } | ||
1274 | 1194 | - } | ||
1275 | 1195 | - | ||
1276 | 1196 | - for i := range resp { | ||
1277 | 1197 | - all, err := ioutil.ReadAll(resp[i].Body) | ||
1278 | 1198 | - if err != nil { | ||
1279 | 1199 | - t.Fatalf("req #%d: client ReadAll: %v", i, err) | ||
1280 | 1200 | - } | ||
1281 | 1201 | - if len(all) != 0 { | ||
1282 | 1202 | - t.Errorf("req #%d: client got %d bytes; expected 0", i, len(all)) | ||
1283 | 1203 | - } | ||
1284 | 1204 | - } | ||
1285 | 1205 | -} | ||
1286 | 1206 | - | ||
1287 | 1207 | -func TestHandlerPanicNil(t *testing.T) { | ||
1288 | 1208 | - testHandlerPanic(t, false, nil) | ||
1289 | 1209 | -} | ||
1290 | 1210 | - | ||
1291 | 1211 | -func TestHandlerPanic(t *testing.T) { | ||
1292 | 1212 | - testHandlerPanic(t, false, "intentional death for testing") | ||
1293 | 1213 | -} | ||
1294 | 1214 | - | ||
1295 | 1215 | -func TestHandlerPanicWithHijack(t *testing.T) { | ||
1296 | 1216 | - testHandlerPanic(t, true, "intentional death for testing") | ||
1297 | 1217 | -} | ||
1298 | 1218 | - | ||
1299 | 1219 | -func testHandlerPanic(t *testing.T, withHijack bool, panicValue interface{}) { | ||
1300 | 1220 | - defer afterTest(t) | ||
1301 | 1221 | - // Unlike the other tests that set the log output to ioutil.Discard | ||
1302 | 1222 | - // to quiet the output, this test uses a pipe. The pipe serves three | ||
1303 | 1223 | - // purposes: | ||
1304 | 1224 | - // | ||
1305 | 1225 | - // 1) The log.Print from the http server (generated by the caught | ||
1306 | 1226 | - // panic) will go to the pipe instead of stderr, making the | ||
1307 | 1227 | - // output quiet. | ||
1308 | 1228 | - // | ||
1309 | 1229 | - // 2) We read from the pipe to verify that the handler | ||
1310 | 1230 | - // actually caught the panic and logged something. | ||
1311 | 1231 | - // | ||
1312 | 1232 | - // 3) The blocking Read call prevents this TestHandlerPanic | ||
1313 | 1233 | - // function from exiting before the HTTP server handler | ||
1314 | 1234 | - // finishes crashing. If this text function exited too | ||
1315 | 1235 | - // early (and its defer log.SetOutput(os.Stderr) ran), | ||
1316 | 1236 | - // then the crash output could spill into the next test. | ||
1317 | 1237 | - pr, pw := io.Pipe() | ||
1318 | 1238 | - log.SetOutput(pw) | ||
1319 | 1239 | - defer log.SetOutput(os.Stderr) | ||
1320 | 1240 | - defer pw.Close() | ||
1321 | 1241 | - | ||
1322 | 1242 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
1323 | 1243 | - if withHijack { | ||
1324 | 1244 | - rwc, _, err := w.(Hijacker).Hijack() | ||
1325 | 1245 | - if err != nil { | ||
1326 | 1246 | - t.Logf("unexpected error: %v", err) | ||
1327 | 1247 | - } | ||
1328 | 1248 | - defer rwc.Close() | ||
1329 | 1249 | - } | ||
1330 | 1250 | - panic(panicValue) | ||
1331 | 1251 | - })) | ||
1332 | 1252 | - defer ts.Close() | ||
1333 | 1253 | - | ||
1334 | 1254 | - // Do a blocking read on the log output pipe so its logging | ||
1335 | 1255 | - // doesn't bleed into the next test. But wait only 5 seconds | ||
1336 | 1256 | - // for it. | ||
1337 | 1257 | - done := make(chan bool, 1) | ||
1338 | 1258 | - go func() { | ||
1339 | 1259 | - buf := make([]byte, 4<<10) | ||
1340 | 1260 | - _, err := pr.Read(buf) | ||
1341 | 1261 | - pr.Close() | ||
1342 | 1262 | - if err != nil && err != io.EOF { | ||
1343 | 1263 | - t.Error(err) | ||
1344 | 1264 | - } | ||
1345 | 1265 | - done <- true | ||
1346 | 1266 | - }() | ||
1347 | 1267 | - | ||
1348 | 1268 | - _, err := Get(ts.URL) | ||
1349 | 1269 | - if err == nil { | ||
1350 | 1270 | - t.Logf("expected an error") | ||
1351 | 1271 | - } | ||
1352 | 1272 | - | ||
1353 | 1273 | - if panicValue == nil { | ||
1354 | 1274 | - return | ||
1355 | 1275 | - } | ||
1356 | 1276 | - | ||
1357 | 1277 | - select { | ||
1358 | 1278 | - case <-done: | ||
1359 | 1279 | - return | ||
1360 | 1280 | - case <-time.After(5 * time.Second): | ||
1361 | 1281 | - t.Fatal("expected server handler to log an error") | ||
1362 | 1282 | - } | ||
1363 | 1283 | -} | ||
1364 | 1284 | - | ||
1365 | 1285 | -func TestNoDate(t *testing.T) { | ||
1366 | 1286 | - defer afterTest(t) | ||
1367 | 1287 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
1368 | 1288 | - w.Header()["Date"] = nil | ||
1369 | 1289 | - })) | ||
1370 | 1290 | - defer ts.Close() | ||
1371 | 1291 | - res, err := Get(ts.URL) | ||
1372 | 1292 | - if err != nil { | ||
1373 | 1293 | - t.Fatal(err) | ||
1374 | 1294 | - } | ||
1375 | 1295 | - _, present := res.Header["Date"] | ||
1376 | 1296 | - if present { | ||
1377 | 1297 | - t.Fatalf("Expected no Date header; got %v", res.Header["Date"]) | ||
1378 | 1298 | - } | ||
1379 | 1299 | -} | ||
1380 | 1300 | - | ||
1381 | 1301 | -func TestStripPrefix(t *testing.T) { | ||
1382 | 1302 | - defer afterTest(t) | ||
1383 | 1303 | - h := HandlerFunc(func(w ResponseWriter, r *Request) { | ||
1384 | 1304 | - w.Header().Set("X-Path", r.URL.Path) | ||
1385 | 1305 | - }) | ||
1386 | 1306 | - ts := httptest.NewServer(StripPrefix("/foo", h)) | ||
1387 | 1307 | - defer ts.Close() | ||
1388 | 1308 | - | ||
1389 | 1309 | - res, err := Get(ts.URL + "/foo/bar") | ||
1390 | 1310 | - if err != nil { | ||
1391 | 1311 | - t.Fatal(err) | ||
1392 | 1312 | - } | ||
1393 | 1313 | - if g, e := res.Header.Get("X-Path"), "/bar"; g != e { | ||
1394 | 1314 | - t.Errorf("test 1: got %s, want %s", g, e) | ||
1395 | 1315 | - } | ||
1396 | 1316 | - res.Body.Close() | ||
1397 | 1317 | - | ||
1398 | 1318 | - res, err = Get(ts.URL + "/bar") | ||
1399 | 1319 | - if err != nil { | ||
1400 | 1320 | - t.Fatal(err) | ||
1401 | 1321 | - } | ||
1402 | 1322 | - if g, e := res.StatusCode, 404; g != e { | ||
1403 | 1323 | - t.Errorf("test 2: got status %v, want %v", g, e) | ||
1404 | 1324 | - } | ||
1405 | 1325 | - res.Body.Close() | ||
1406 | 1326 | -} | ||
1407 | 1327 | - | ||
1408 | 1328 | -func TestRequestLimit(t *testing.T) { | ||
1409 | 1329 | - defer afterTest(t) | ||
1410 | 1330 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
1411 | 1331 | - t.Fatalf("didn't expect to get request in Handler") | ||
1412 | 1332 | - })) | ||
1413 | 1333 | - defer ts.Close() | ||
1414 | 1334 | - req, _ := NewRequest("GET", ts.URL, nil) | ||
1415 | 1335 | - var bytesPerHeader = len("header12345: val12345\r\n") | ||
1416 | 1336 | - for i := 0; i < ((DefaultMaxHeaderBytes+4096)/bytesPerHeader)+1; i++ { | ||
1417 | 1337 | - req.Header.Set(fmt.Sprintf("header%05d", i), fmt.Sprintf("val%05d", i)) | ||
1418 | 1338 | - } | ||
1419 | 1339 | - res, err := DefaultClient.Do(req) | ||
1420 | 1340 | - if err != nil { | ||
1421 | 1341 | - // Some HTTP clients may fail on this undefined behavior (server replying and | ||
1422 | 1342 | - // closing the connection while the request is still being written), but | ||
1423 | 1343 | - // we do support it (at least currently), so we expect a response below. | ||
1424 | 1344 | - t.Fatalf("Do: %v", err) | ||
1425 | 1345 | - } | ||
1426 | 1346 | - defer res.Body.Close() | ||
1427 | 1347 | - if res.StatusCode != 413 { | ||
1428 | 1348 | - t.Fatalf("expected 413 response status; got: %d %s", res.StatusCode, res.Status) | ||
1429 | 1349 | - } | ||
1430 | 1350 | -} | ||
1431 | 1351 | - | ||
1432 | 1352 | type neverEnding byte | ||
1433 | 1353 | |||
1434 | 1354 | func (b neverEnding) Read(p []byte) (n int, err error) { | ||
1435 | 1355 | @@ -1384,1344 +56,3 @@ | ||
1436 | 1356 | } | ||
1437 | 1357 | return len(p), nil | ||
1438 | 1358 | } | ||
1439 | 1359 | - | ||
1440 | 1360 | -type countReader struct { | ||
1441 | 1361 | - r io.Reader | ||
1442 | 1362 | - n *int64 | ||
1443 | 1363 | -} | ||
1444 | 1364 | - | ||
1445 | 1365 | -func (cr countReader) Read(p []byte) (n int, err error) { | ||
1446 | 1366 | - n, err = cr.r.Read(p) | ||
1447 | 1367 | - atomic.AddInt64(cr.n, int64(n)) | ||
1448 | 1368 | - return | ||
1449 | 1369 | -} | ||
1450 | 1370 | - | ||
1451 | 1371 | -func TestRequestBodyLimit(t *testing.T) { | ||
1452 | 1372 | - defer afterTest(t) | ||
1453 | 1373 | - const limit = 1 << 20 | ||
1454 | 1374 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
1455 | 1375 | - r.Body = MaxBytesReader(w, r.Body, limit) | ||
1456 | 1376 | - n, err := io.Copy(ioutil.Discard, r.Body) | ||
1457 | 1377 | - if err == nil { | ||
1458 | 1378 | - t.Errorf("expected error from io.Copy") | ||
1459 | 1379 | - } | ||
1460 | 1380 | - if n != limit { | ||
1461 | 1381 | - t.Errorf("io.Copy = %d, want %d", n, limit) | ||
1462 | 1382 | - } | ||
1463 | 1383 | - })) | ||
1464 | 1384 | - defer ts.Close() | ||
1465 | 1385 | - | ||
1466 | 1386 | - nWritten := new(int64) | ||
1467 | 1387 | - req, _ := NewRequest("POST", ts.URL, io.LimitReader(countReader{neverEnding('a'), nWritten}, limit*200)) | ||
1468 | 1388 | - | ||
1469 | 1389 | - // Send the POST, but don't care it succeeds or not. The | ||
1470 | 1390 | - // remote side is going to reply and then close the TCP | ||
1471 | 1391 | - // connection, and HTTP doesn't really define if that's | ||
1472 | 1392 | - // allowed or not. Some HTTP clients will get the response | ||
1473 | 1393 | - // and some (like ours, currently) will complain that the | ||
1474 | 1394 | - // request write failed, without reading the response. | ||
1475 | 1395 | - // | ||
1476 | 1396 | - // But that's okay, since what we're really testing is that | ||
1477 | 1397 | - // the remote side hung up on us before we wrote too much. | ||
1478 | 1398 | - _, _ = DefaultClient.Do(req) | ||
1479 | 1399 | - | ||
1480 | 1400 | - if atomic.LoadInt64(nWritten) > limit*100 { | ||
1481 | 1401 | - t.Errorf("handler restricted the request body to %d bytes, but client managed to write %d", | ||
1482 | 1402 | - limit, nWritten) | ||
1483 | 1403 | - } | ||
1484 | 1404 | -} | ||
1485 | 1405 | - | ||
1486 | 1406 | -// TestClientWriteShutdown tests that if the client shuts down the write | ||
1487 | 1407 | -// side of their TCP connection, the server doesn't send a 400 Bad Request. | ||
1488 | 1408 | -func TestClientWriteShutdown(t *testing.T) { | ||
1489 | 1409 | - if runtime.GOOS == "plan9" { | ||
1490 | 1410 | - t.Skip("skipping test; see http://golang.org/issue/7237") | ||
1491 | 1411 | - } | ||
1492 | 1412 | - defer afterTest(t) | ||
1493 | 1413 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {})) | ||
1494 | 1414 | - defer ts.Close() | ||
1495 | 1415 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
1496 | 1416 | - if err != nil { | ||
1497 | 1417 | - t.Fatalf("Dial: %v", err) | ||
1498 | 1418 | - } | ||
1499 | 1419 | - err = conn.(*net.TCPConn).CloseWrite() | ||
1500 | 1420 | - if err != nil { | ||
1501 | 1421 | - t.Fatalf("Dial: %v", err) | ||
1502 | 1422 | - } | ||
1503 | 1423 | - donec := make(chan bool) | ||
1504 | 1424 | - go func() { | ||
1505 | 1425 | - defer close(donec) | ||
1506 | 1426 | - bs, err := ioutil.ReadAll(conn) | ||
1507 | 1427 | - if err != nil { | ||
1508 | 1428 | - t.Fatalf("ReadAll: %v", err) | ||
1509 | 1429 | - } | ||
1510 | 1430 | - got := string(bs) | ||
1511 | 1431 | - if got != "" { | ||
1512 | 1432 | - t.Errorf("read %q from server; want nothing", got) | ||
1513 | 1433 | - } | ||
1514 | 1434 | - }() | ||
1515 | 1435 | - select { | ||
1516 | 1436 | - case <-donec: | ||
1517 | 1437 | - case <-time.After(10 * time.Second): | ||
1518 | 1438 | - t.Fatalf("timeout") | ||
1519 | 1439 | - } | ||
1520 | 1440 | -} | ||
1521 | 1441 | - | ||
1522 | 1442 | -// Tests that chunked server responses that write 1 byte at a time are | ||
1523 | 1443 | -// buffered before chunk headers are added, not after chunk headers. | ||
1524 | 1444 | -func TestServerBufferedChunking(t *testing.T) { | ||
1525 | 1445 | - conn := new(testConn) | ||
1526 | 1446 | - conn.readBuf.Write([]byte("GET / HTTP/1.1\r\n\r\n")) | ||
1527 | 1447 | - conn.closec = make(chan bool, 1) | ||
1528 | 1448 | - ls := &oneConnListener{conn} | ||
1529 | 1449 | - go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) { | ||
1530 | 1450 | - rw.(Flusher).Flush() // force the Header to be sent, in chunking mode, not counting the length | ||
1531 | 1451 | - rw.Write([]byte{'x'}) | ||
1532 | 1452 | - rw.Write([]byte{'y'}) | ||
1533 | 1453 | - rw.Write([]byte{'z'}) | ||
1534 | 1454 | - })) | ||
1535 | 1455 | - <-conn.closec | ||
1536 | 1456 | - if !bytes.HasSuffix(conn.writeBuf.Bytes(), []byte("\r\n\r\n3\r\nxyz\r\n0\r\n\r\n")) { | ||
1537 | 1457 | - t.Errorf("response didn't end with a single 3 byte 'xyz' chunk; got:\n%q", | ||
1538 | 1458 | - conn.writeBuf.Bytes()) | ||
1539 | 1459 | - } | ||
1540 | 1460 | -} | ||
1541 | 1461 | - | ||
1542 | 1462 | -// Tests that the server flushes its response headers out when it's | ||
1543 | 1463 | -// ignoring the response body and waits a bit before forcefully | ||
1544 | 1464 | -// closing the TCP connection, causing the client to get a RST. | ||
1545 | 1465 | -// See http://golang.org/issue/3595 | ||
1546 | 1466 | -func TestServerGracefulClose(t *testing.T) { | ||
1547 | 1467 | - defer afterTest(t) | ||
1548 | 1468 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
1549 | 1469 | - Error(w, "bye", StatusUnauthorized) | ||
1550 | 1470 | - })) | ||
1551 | 1471 | - defer ts.Close() | ||
1552 | 1472 | - | ||
1553 | 1473 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
1554 | 1474 | - if err != nil { | ||
1555 | 1475 | - t.Fatal(err) | ||
1556 | 1476 | - } | ||
1557 | 1477 | - defer conn.Close() | ||
1558 | 1478 | - const bodySize = 5 << 20 | ||
1559 | 1479 | - req := []byte(fmt.Sprintf("POST / HTTP/1.1\r\nHost: foo.com\r\nContent-Length: %d\r\n\r\n", bodySize)) | ||
1560 | 1480 | - for i := 0; i < bodySize; i++ { | ||
1561 | 1481 | - req = append(req, 'x') | ||
1562 | 1482 | - } | ||
1563 | 1483 | - writeErr := make(chan error) | ||
1564 | 1484 | - go func() { | ||
1565 | 1485 | - _, err := conn.Write(req) | ||
1566 | 1486 | - writeErr <- err | ||
1567 | 1487 | - }() | ||
1568 | 1488 | - br := bufio.NewReader(conn) | ||
1569 | 1489 | - lineNum := 0 | ||
1570 | 1490 | - for { | ||
1571 | 1491 | - line, err := br.ReadString('\n') | ||
1572 | 1492 | - if err == io.EOF { | ||
1573 | 1493 | - break | ||
1574 | 1494 | - } | ||
1575 | 1495 | - if err != nil { | ||
1576 | 1496 | - t.Fatalf("ReadLine: %v", err) | ||
1577 | 1497 | - } | ||
1578 | 1498 | - lineNum++ | ||
1579 | 1499 | - if lineNum == 1 && !strings.Contains(line, "401 Unauthorized") { | ||
1580 | 1500 | - t.Errorf("Response line = %q; want a 401", line) | ||
1581 | 1501 | - } | ||
1582 | 1502 | - } | ||
1583 | 1503 | - // Wait for write to finish. This is a broken pipe on both | ||
1584 | 1504 | - // Darwin and Linux, but checking this isn't the point of | ||
1585 | 1505 | - // the test. | ||
1586 | 1506 | - <-writeErr | ||
1587 | 1507 | -} | ||
1588 | 1508 | - | ||
1589 | 1509 | -func TestCaseSensitiveMethod(t *testing.T) { | ||
1590 | 1510 | - defer afterTest(t) | ||
1591 | 1511 | - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
1592 | 1512 | - if r.Method != "get" { | ||
1593 | 1513 | - t.Errorf(`Got method %q; want "get"`, r.Method) | ||
1594 | 1514 | - } | ||
1595 | 1515 | - })) | ||
1596 | 1516 | - defer ts.Close() | ||
1597 | 1517 | - req, _ := NewRequest("get", ts.URL, nil) | ||
1598 | 1518 | - res, err := DefaultClient.Do(req) | ||
1599 | 1519 | - if err != nil { | ||
1600 | 1520 | - t.Error(err) | ||
1601 | 1521 | - return | ||
1602 | 1522 | - } | ||
1603 | 1523 | - res.Body.Close() | ||
1604 | 1524 | -} | ||
1605 | 1525 | - | ||
1606 | 1526 | -// TestContentLengthZero tests that for both an HTTP/1.0 and HTTP/1.1 | ||
1607 | 1527 | -// request (both keep-alive), when a Handler never writes any | ||
1608 | 1528 | -// response, the net/http package adds a "Content-Length: 0" response | ||
1609 | 1529 | -// header. | ||
1610 | 1530 | -func TestContentLengthZero(t *testing.T) { | ||
1611 | 1531 | - ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {})) | ||
1612 | 1532 | - defer ts.Close() | ||
1613 | 1533 | - | ||
1614 | 1534 | - for _, version := range []string{"HTTP/1.0", "HTTP/1.1"} { | ||
1615 | 1535 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
1616 | 1536 | - if err != nil { | ||
1617 | 1537 | - t.Fatalf("error dialing: %v", err) | ||
1618 | 1538 | - } | ||
1619 | 1539 | - _, err = fmt.Fprintf(conn, "GET / %v\r\nConnection: keep-alive\r\nHost: foo\r\n\r\n", version) | ||
1620 | 1540 | - if err != nil { | ||
1621 | 1541 | - t.Fatalf("error writing: %v", err) | ||
1622 | 1542 | - } | ||
1623 | 1543 | - req, _ := NewRequest("GET", "/", nil) | ||
1624 | 1544 | - res, err := ReadResponse(bufio.NewReader(conn), req) | ||
1625 | 1545 | - if err != nil { | ||
1626 | 1546 | - t.Fatalf("error reading response: %v", err) | ||
1627 | 1547 | - } | ||
1628 | 1548 | - if te := res.TransferEncoding; len(te) > 0 { | ||
1629 | 1549 | - t.Errorf("For version %q, Transfer-Encoding = %q; want none", version, te) | ||
1630 | 1550 | - } | ||
1631 | 1551 | - if cl := res.ContentLength; cl != 0 { | ||
1632 | 1552 | - t.Errorf("For version %q, Content-Length = %v; want 0", version, cl) | ||
1633 | 1553 | - } | ||
1634 | 1554 | - conn.Close() | ||
1635 | 1555 | - } | ||
1636 | 1556 | -} | ||
1637 | 1557 | - | ||
1638 | 1558 | -func TestCloseNotifier(t *testing.T) { | ||
1639 | 1559 | - defer afterTest(t) | ||
1640 | 1560 | - gotReq := make(chan bool, 1) | ||
1641 | 1561 | - sawClose := make(chan bool, 1) | ||
1642 | 1562 | - ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) { | ||
1643 | 1563 | - gotReq <- true | ||
1644 | 1564 | - cc := rw.(CloseNotifier).CloseNotify() | ||
1645 | 1565 | - <-cc | ||
1646 | 1566 | - sawClose <- true | ||
1647 | 1567 | - })) | ||
1648 | 1568 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
1649 | 1569 | - if err != nil { | ||
1650 | 1570 | - t.Fatalf("error dialing: %v", err) | ||
1651 | 1571 | - } | ||
1652 | 1572 | - diec := make(chan bool) | ||
1653 | 1573 | - go func() { | ||
1654 | 1574 | - _, err = fmt.Fprintf(conn, "GET / HTTP/1.1\r\nConnection: keep-alive\r\nHost: foo\r\n\r\n") | ||
1655 | 1575 | - if err != nil { | ||
1656 | 1576 | - t.Fatal(err) | ||
1657 | 1577 | - } | ||
1658 | 1578 | - <-diec | ||
1659 | 1579 | - conn.Close() | ||
1660 | 1580 | - }() | ||
1661 | 1581 | -For: | ||
1662 | 1582 | - for { | ||
1663 | 1583 | - select { | ||
1664 | 1584 | - case <-gotReq: | ||
1665 | 1585 | - diec <- true | ||
1666 | 1586 | - case <-sawClose: | ||
1667 | 1587 | - break For | ||
1668 | 1588 | - case <-time.After(5 * time.Second): | ||
1669 | 1589 | - t.Fatal("timeout") | ||
1670 | 1590 | - } | ||
1671 | 1591 | - } | ||
1672 | 1592 | - ts.Close() | ||
1673 | 1593 | -} | ||
1674 | 1594 | - | ||
1675 | 1595 | -func TestCloseNotifierChanLeak(t *testing.T) { | ||
1676 | 1596 | - defer afterTest(t) | ||
1677 | 1597 | - req := reqBytes("GET / HTTP/1.0\nHost: golang.org") | ||
1678 | 1598 | - for i := 0; i < 20; i++ { | ||
1679 | 1599 | - var output bytes.Buffer | ||
1680 | 1600 | - conn := &rwTestConn{ | ||
1681 | 1601 | - Reader: bytes.NewReader(req), | ||
1682 | 1602 | - Writer: &output, | ||
1683 | 1603 | - closec: make(chan bool, 1), | ||
1684 | 1604 | - } | ||
1685 | 1605 | - ln := &oneConnListener{conn: conn} | ||
1686 | 1606 | - handler := HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
1687 | 1607 | - // Ignore the return value and never read from | ||
1688 | 1608 | - // it, testing that we don't leak goroutines | ||
1689 | 1609 | - // on the sending side: | ||
1690 | 1610 | - _ = rw.(CloseNotifier).CloseNotify() | ||
1691 | 1611 | - }) | ||
1692 | 1612 | - go Serve(ln, handler) | ||
1693 | 1613 | - <-conn.closec | ||
1694 | 1614 | - } | ||
1695 | 1615 | -} | ||
1696 | 1616 | - | ||
1697 | 1617 | -func TestOptions(t *testing.T) { | ||
1698 | 1618 | - uric := make(chan string, 2) // only expect 1, but leave space for 2 | ||
1699 | 1619 | - mux := NewServeMux() | ||
1700 | 1620 | - mux.HandleFunc("/", func(w ResponseWriter, r *Request) { | ||
1701 | 1621 | - uric <- r.RequestURI | ||
1702 | 1622 | - }) | ||
1703 | 1623 | - ts := httptest.NewServer(mux) | ||
1704 | 1624 | - defer ts.Close() | ||
1705 | 1625 | - | ||
1706 | 1626 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
1707 | 1627 | - if err != nil { | ||
1708 | 1628 | - t.Fatal(err) | ||
1709 | 1629 | - } | ||
1710 | 1630 | - defer conn.Close() | ||
1711 | 1631 | - | ||
1712 | 1632 | - // An OPTIONS * request should succeed. | ||
1713 | 1633 | - _, err = conn.Write([]byte("OPTIONS * HTTP/1.1\r\nHost: foo.com\r\n\r\n")) | ||
1714 | 1634 | - if err != nil { | ||
1715 | 1635 | - t.Fatal(err) | ||
1716 | 1636 | - } | ||
1717 | 1637 | - br := bufio.NewReader(conn) | ||
1718 | 1638 | - res, err := ReadResponse(br, &Request{Method: "OPTIONS"}) | ||
1719 | 1639 | - if err != nil { | ||
1720 | 1640 | - t.Fatal(err) | ||
1721 | 1641 | - } | ||
1722 | 1642 | - if res.StatusCode != 200 { | ||
1723 | 1643 | - t.Errorf("Got non-200 response to OPTIONS *: %#v", res) | ||
1724 | 1644 | - } | ||
1725 | 1645 | - | ||
1726 | 1646 | - // A GET * request on a ServeMux should fail. | ||
1727 | 1647 | - _, err = conn.Write([]byte("GET * HTTP/1.1\r\nHost: foo.com\r\n\r\n")) | ||
1728 | 1648 | - if err != nil { | ||
1729 | 1649 | - t.Fatal(err) | ||
1730 | 1650 | - } | ||
1731 | 1651 | - res, err = ReadResponse(br, &Request{Method: "GET"}) | ||
1732 | 1652 | - if err != nil { | ||
1733 | 1653 | - t.Fatal(err) | ||
1734 | 1654 | - } | ||
1735 | 1655 | - if res.StatusCode != 400 { | ||
1736 | 1656 | - t.Errorf("Got non-400 response to GET *: %#v", res) | ||
1737 | 1657 | - } | ||
1738 | 1658 | - | ||
1739 | 1659 | - res, err = Get(ts.URL + "/second") | ||
1740 | 1660 | - if err != nil { | ||
1741 | 1661 | - t.Fatal(err) | ||
1742 | 1662 | - } | ||
1743 | 1663 | - res.Body.Close() | ||
1744 | 1664 | - if got := <-uric; got != "/second" { | ||
1745 | 1665 | - t.Errorf("Handler saw request for %q; want /second", got) | ||
1746 | 1666 | - } | ||
1747 | 1667 | -} | ||
1748 | 1668 | - | ||
1749 | 1669 | -// Tests regarding the ordering of Write, WriteHeader, Header, and | ||
1750 | 1670 | -// Flush calls. In Go 1.0, rw.WriteHeader immediately flushed the | ||
1751 | 1671 | -// (*response).header to the wire. In Go 1.1, the actual wire flush is | ||
1752 | 1672 | -// delayed, so we could maybe tack on a Content-Length and better | ||
1753 | 1673 | -// Content-Type after we see more (or all) of the output. To preserve | ||
1754 | 1674 | -// compatibility with Go 1, we need to be careful to track which | ||
1755 | 1675 | -// headers were live at the time of WriteHeader, so we write the same | ||
1756 | 1676 | -// ones, even if the handler modifies them (~erroneously) after the | ||
1757 | 1677 | -// first Write. | ||
1758 | 1678 | -func TestHeaderToWire(t *testing.T) { | ||
1759 | 1679 | - tests := []struct { | ||
1760 | 1680 | - name string | ||
1761 | 1681 | - handler func(ResponseWriter, *Request) | ||
1762 | 1682 | - check func(output string) error | ||
1763 | 1683 | - }{ | ||
1764 | 1684 | - { | ||
1765 | 1685 | - name: "write without Header", | ||
1766 | 1686 | - handler: func(rw ResponseWriter, r *Request) { | ||
1767 | 1687 | - rw.Write([]byte("hello world")) | ||
1768 | 1688 | - }, | ||
1769 | 1689 | - check: func(got string) error { | ||
1770 | 1690 | - if !strings.Contains(got, "Content-Length:") { | ||
1771 | 1691 | - return errors.New("no content-length") | ||
1772 | 1692 | - } | ||
1773 | 1693 | - if !strings.Contains(got, "Content-Type: text/plain") { | ||
1774 | 1694 | - return errors.New("no content-length") | ||
1775 | 1695 | - } | ||
1776 | 1696 | - return nil | ||
1777 | 1697 | - }, | ||
1778 | 1698 | - }, | ||
1779 | 1699 | - { | ||
1780 | 1700 | - name: "Header mutation before write", | ||
1781 | 1701 | - handler: func(rw ResponseWriter, r *Request) { | ||
1782 | 1702 | - h := rw.Header() | ||
1783 | 1703 | - h.Set("Content-Type", "some/type") | ||
1784 | 1704 | - rw.Write([]byte("hello world")) | ||
1785 | 1705 | - h.Set("Too-Late", "bogus") | ||
1786 | 1706 | - }, | ||
1787 | 1707 | - check: func(got string) error { | ||
1788 | 1708 | - if !strings.Contains(got, "Content-Length:") { | ||
1789 | 1709 | - return errors.New("no content-length") | ||
1790 | 1710 | - } | ||
1791 | 1711 | - if !strings.Contains(got, "Content-Type: some/type") { | ||
1792 | 1712 | - return errors.New("wrong content-type") | ||
1793 | 1713 | - } | ||
1794 | 1714 | - if strings.Contains(got, "Too-Late") { | ||
1795 | 1715 | - return errors.New("don't want too-late header") | ||
1796 | 1716 | - } | ||
1797 | 1717 | - return nil | ||
1798 | 1718 | - }, | ||
1799 | 1719 | - }, | ||
1800 | 1720 | - { | ||
1801 | 1721 | - name: "write then useless Header mutation", | ||
1802 | 1722 | - handler: func(rw ResponseWriter, r *Request) { | ||
1803 | 1723 | - rw.Write([]byte("hello world")) | ||
1804 | 1724 | - rw.Header().Set("Too-Late", "Write already wrote headers") | ||
1805 | 1725 | - }, | ||
1806 | 1726 | - check: func(got string) error { | ||
1807 | 1727 | - if strings.Contains(got, "Too-Late") { | ||
1808 | 1728 | - return errors.New("header appeared from after WriteHeader") | ||
1809 | 1729 | - } | ||
1810 | 1730 | - return nil | ||
1811 | 1731 | - }, | ||
1812 | 1732 | - }, | ||
1813 | 1733 | - { | ||
1814 | 1734 | - name: "flush then write", | ||
1815 | 1735 | - handler: func(rw ResponseWriter, r *Request) { | ||
1816 | 1736 | - rw.(Flusher).Flush() | ||
1817 | 1737 | - rw.Write([]byte("post-flush")) | ||
1818 | 1738 | - rw.Header().Set("Too-Late", "Write already wrote headers") | ||
1819 | 1739 | - }, | ||
1820 | 1740 | - check: func(got string) error { | ||
1821 | 1741 | - if !strings.Contains(got, "Transfer-Encoding: chunked") { | ||
1822 | 1742 | - return errors.New("not chunked") | ||
1823 | 1743 | - } | ||
1824 | 1744 | - if strings.Contains(got, "Too-Late") { | ||
1825 | 1745 | - return errors.New("header appeared from after WriteHeader") | ||
1826 | 1746 | - } | ||
1827 | 1747 | - return nil | ||
1828 | 1748 | - }, | ||
1829 | 1749 | - }, | ||
1830 | 1750 | - { | ||
1831 | 1751 | - name: "header then flush", | ||
1832 | 1752 | - handler: func(rw ResponseWriter, r *Request) { | ||
1833 | 1753 | - rw.Header().Set("Content-Type", "some/type") | ||
1834 | 1754 | - rw.(Flusher).Flush() | ||
1835 | 1755 | - rw.Write([]byte("post-flush")) | ||
1836 | 1756 | - rw.Header().Set("Too-Late", "Write already wrote headers") | ||
1837 | 1757 | - }, | ||
1838 | 1758 | - check: func(got string) error { | ||
1839 | 1759 | - if !strings.Contains(got, "Transfer-Encoding: chunked") { | ||
1840 | 1760 | - return errors.New("not chunked") | ||
1841 | 1761 | - } | ||
1842 | 1762 | - if strings.Contains(got, "Too-Late") { | ||
1843 | 1763 | - return errors.New("header appeared from after WriteHeader") | ||
1844 | 1764 | - } | ||
1845 | 1765 | - if !strings.Contains(got, "Content-Type: some/type") { | ||
1846 | 1766 | - return errors.New("wrong content-length") | ||
1847 | 1767 | - } | ||
1848 | 1768 | - return nil | ||
1849 | 1769 | - }, | ||
1850 | 1770 | - }, | ||
1851 | 1771 | - { | ||
1852 | 1772 | - name: "sniff-on-first-write content-type", | ||
1853 | 1773 | - handler: func(rw ResponseWriter, r *Request) { | ||
1854 | 1774 | - rw.Write([]byte("<html><head></head><body>some html</body></html>")) | ||
1855 | 1775 | - rw.Header().Set("Content-Type", "x/wrong") | ||
1856 | 1776 | - }, | ||
1857 | 1777 | - check: func(got string) error { | ||
1858 | 1778 | - if !strings.Contains(got, "Content-Type: text/html") { | ||
1859 | 1779 | - return errors.New("wrong content-length; want html") | ||
1860 | 1780 | - } | ||
1861 | 1781 | - return nil | ||
1862 | 1782 | - }, | ||
1863 | 1783 | - }, | ||
1864 | 1784 | - { | ||
1865 | 1785 | - name: "explicit content-type wins", | ||
1866 | 1786 | - handler: func(rw ResponseWriter, r *Request) { | ||
1867 | 1787 | - rw.Header().Set("Content-Type", "some/type") | ||
1868 | 1788 | - rw.Write([]byte("<html><head></head><body>some html</body></html>")) | ||
1869 | 1789 | - }, | ||
1870 | 1790 | - check: func(got string) error { | ||
1871 | 1791 | - if !strings.Contains(got, "Content-Type: some/type") { | ||
1872 | 1792 | - return errors.New("wrong content-length; want html") | ||
1873 | 1793 | - } | ||
1874 | 1794 | - return nil | ||
1875 | 1795 | - }, | ||
1876 | 1796 | - }, | ||
1877 | 1797 | - { | ||
1878 | 1798 | - name: "empty handler", | ||
1879 | 1799 | - handler: func(rw ResponseWriter, r *Request) { | ||
1880 | 1800 | - }, | ||
1881 | 1801 | - check: func(got string) error { | ||
1882 | 1802 | - if !strings.Contains(got, "Content-Type: text/plain") { | ||
1883 | 1803 | - return errors.New("wrong content-length; want text/plain") | ||
1884 | 1804 | - } | ||
1885 | 1805 | - if !strings.Contains(got, "Content-Length: 0") { | ||
1886 | 1806 | - return errors.New("want 0 content-length") | ||
1887 | 1807 | - } | ||
1888 | 1808 | - return nil | ||
1889 | 1809 | - }, | ||
1890 | 1810 | - }, | ||
1891 | 1811 | - { | ||
1892 | 1812 | - name: "only Header, no write", | ||
1893 | 1813 | - handler: func(rw ResponseWriter, r *Request) { | ||
1894 | 1814 | - rw.Header().Set("Some-Header", "some-value") | ||
1895 | 1815 | - }, | ||
1896 | 1816 | - check: func(got string) error { | ||
1897 | 1817 | - if !strings.Contains(got, "Some-Header") { | ||
1898 | 1818 | - return errors.New("didn't get header") | ||
1899 | 1819 | - } | ||
1900 | 1820 | - return nil | ||
1901 | 1821 | - }, | ||
1902 | 1822 | - }, | ||
1903 | 1823 | - { | ||
1904 | 1824 | - name: "WriteHeader call", | ||
1905 | 1825 | - handler: func(rw ResponseWriter, r *Request) { | ||
1906 | 1826 | - rw.WriteHeader(404) | ||
1907 | 1827 | - rw.Header().Set("Too-Late", "some-value") | ||
1908 | 1828 | - }, | ||
1909 | 1829 | - check: func(got string) error { | ||
1910 | 1830 | - if !strings.Contains(got, "404") { | ||
1911 | 1831 | - return errors.New("wrong status") | ||
1912 | 1832 | - } | ||
1913 | 1833 | - if strings.Contains(got, "Some-Header") { | ||
1914 | 1834 | - return errors.New("shouldn't have seen Too-Late") | ||
1915 | 1835 | - } | ||
1916 | 1836 | - return nil | ||
1917 | 1837 | - }, | ||
1918 | 1838 | - }, | ||
1919 | 1839 | - } | ||
1920 | 1840 | - for _, tc := range tests { | ||
1921 | 1841 | - ht := newHandlerTest(HandlerFunc(tc.handler)) | ||
1922 | 1842 | - got := ht.rawResponse("GET / HTTP/1.1\nHost: golang.org") | ||
1923 | 1843 | - if err := tc.check(got); err != nil { | ||
1924 | 1844 | - t.Errorf("%s: %v\nGot response:\n%s", tc.name, err, got) | ||
1925 | 1845 | - } | ||
1926 | 1846 | - } | ||
1927 | 1847 | -} | ||
1928 | 1848 | - | ||
1929 | 1849 | -// goTimeout runs f, failing t if f takes more than ns to complete. | ||
1930 | 1850 | -func goTimeout(t *testing.T, d time.Duration, f func()) { | ||
1931 | 1851 | - ch := make(chan bool, 2) | ||
1932 | 1852 | - timer := time.AfterFunc(d, func() { | ||
1933 | 1853 | - t.Errorf("Timeout expired after %v", d) | ||
1934 | 1854 | - ch <- true | ||
1935 | 1855 | - }) | ||
1936 | 1856 | - defer timer.Stop() | ||
1937 | 1857 | - go func() { | ||
1938 | 1858 | - defer func() { ch <- true }() | ||
1939 | 1859 | - f() | ||
1940 | 1860 | - }() | ||
1941 | 1861 | - <-ch | ||
1942 | 1862 | -} | ||
1943 | 1863 | - | ||
1944 | 1864 | -type errorListener struct { | ||
1945 | 1865 | - errs []error | ||
1946 | 1866 | -} | ||
1947 | 1867 | - | ||
1948 | 1868 | -func (l *errorListener) Accept() (c net.Conn, err error) { | ||
1949 | 1869 | - if len(l.errs) == 0 { | ||
1950 | 1870 | - return nil, io.EOF | ||
1951 | 1871 | - } | ||
1952 | 1872 | - err = l.errs[0] | ||
1953 | 1873 | - l.errs = l.errs[1:] | ||
1954 | 1874 | - return | ||
1955 | 1875 | -} | ||
1956 | 1876 | - | ||
1957 | 1877 | -func (l *errorListener) Close() error { | ||
1958 | 1878 | - return nil | ||
1959 | 1879 | -} | ||
1960 | 1880 | - | ||
1961 | 1881 | -func (l *errorListener) Addr() net.Addr { | ||
1962 | 1882 | - return dummyAddr("test-address") | ||
1963 | 1883 | -} | ||
1964 | 1884 | - | ||
1965 | 1885 | -func TestAcceptMaxFds(t *testing.T) { | ||
1966 | 1886 | - log.SetOutput(ioutil.Discard) // is noisy otherwise | ||
1967 | 1887 | - defer log.SetOutput(os.Stderr) | ||
1968 | 1888 | - | ||
1969 | 1889 | - ln := &errorListener{[]error{ | ||
1970 | 1890 | - &net.OpError{ | ||
1971 | 1891 | - Op: "accept", | ||
1972 | 1892 | - Err: syscall.EMFILE, | ||
1973 | 1893 | - }}} | ||
1974 | 1894 | - err := Serve(ln, HandlerFunc(HandlerFunc(func(ResponseWriter, *Request) {}))) | ||
1975 | 1895 | - if err != io.EOF { | ||
1976 | 1896 | - t.Errorf("got error %v, want EOF", err) | ||
1977 | 1897 | - } | ||
1978 | 1898 | -} | ||
1979 | 1899 | - | ||
1980 | 1900 | -func TestWriteAfterHijack(t *testing.T) { | ||
1981 | 1901 | - req := reqBytes("GET / HTTP/1.1\nHost: golang.org") | ||
1982 | 1902 | - var buf bytes.Buffer | ||
1983 | 1903 | - wrotec := make(chan bool, 1) | ||
1984 | 1904 | - conn := &rwTestConn{ | ||
1985 | 1905 | - Reader: bytes.NewReader(req), | ||
1986 | 1906 | - Writer: &buf, | ||
1987 | 1907 | - closec: make(chan bool, 1), | ||
1988 | 1908 | - } | ||
1989 | 1909 | - handler := HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
1990 | 1910 | - conn, bufrw, err := rw.(Hijacker).Hijack() | ||
1991 | 1911 | - if err != nil { | ||
1992 | 1912 | - t.Error(err) | ||
1993 | 1913 | - return | ||
1994 | 1914 | - } | ||
1995 | 1915 | - go func() { | ||
1996 | 1916 | - bufrw.Write([]byte("[hijack-to-bufw]")) | ||
1997 | 1917 | - bufrw.Flush() | ||
1998 | 1918 | - conn.Write([]byte("[hijack-to-conn]")) | ||
1999 | 1919 | - conn.Close() | ||
2000 | 1920 | - wrotec <- true | ||
2001 | 1921 | - }() | ||
2002 | 1922 | - }) | ||
2003 | 1923 | - ln := &oneConnListener{conn: conn} | ||
2004 | 1924 | - go Serve(ln, handler) | ||
2005 | 1925 | - <-conn.closec | ||
2006 | 1926 | - <-wrotec | ||
2007 | 1927 | - if g, w := buf.String(), "[hijack-to-bufw][hijack-to-conn]"; g != w { | ||
2008 | 1928 | - t.Errorf("wrote %q; want %q", g, w) | ||
2009 | 1929 | - } | ||
2010 | 1930 | -} | ||
2011 | 1931 | - | ||
2012 | 1932 | -func TestDoubleHijack(t *testing.T) { | ||
2013 | 1933 | - req := reqBytes("GET / HTTP/1.1\nHost: golang.org") | ||
2014 | 1934 | - var buf bytes.Buffer | ||
2015 | 1935 | - conn := &rwTestConn{ | ||
2016 | 1936 | - Reader: bytes.NewReader(req), | ||
2017 | 1937 | - Writer: &buf, | ||
2018 | 1938 | - closec: make(chan bool, 1), | ||
2019 | 1939 | - } | ||
2020 | 1940 | - handler := HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
2021 | 1941 | - conn, _, err := rw.(Hijacker).Hijack() | ||
2022 | 1942 | - if err != nil { | ||
2023 | 1943 | - t.Error(err) | ||
2024 | 1944 | - return | ||
2025 | 1945 | - } | ||
2026 | 1946 | - _, _, err = rw.(Hijacker).Hijack() | ||
2027 | 1947 | - if err == nil { | ||
2028 | 1948 | - t.Errorf("got err = nil; want err != nil") | ||
2029 | 1949 | - } | ||
2030 | 1950 | - conn.Close() | ||
2031 | 1951 | - }) | ||
2032 | 1952 | - ln := &oneConnListener{conn: conn} | ||
2033 | 1953 | - go Serve(ln, handler) | ||
2034 | 1954 | - <-conn.closec | ||
2035 | 1955 | -} | ||
2036 | 1956 | - | ||
2037 | 1957 | -// http://code.google.com/p/go/issues/detail?id=5955 | ||
2038 | 1958 | -// Note that this does not test the "request too large" | ||
2039 | 1959 | -// exit path from the http server. This is intentional; | ||
2040 | 1960 | -// not sending Connection: close is just a minor wire | ||
2041 | 1961 | -// optimization and is pointless if dealing with a | ||
2042 | 1962 | -// badly behaved client. | ||
2043 | 1963 | -func TestHTTP10ConnectionHeader(t *testing.T) { | ||
2044 | 1964 | - defer afterTest(t) | ||
2045 | 1965 | - | ||
2046 | 1966 | - mux := NewServeMux() | ||
2047 | 1967 | - mux.Handle("/", HandlerFunc(func(resp ResponseWriter, req *Request) {})) | ||
2048 | 1968 | - ts := httptest.NewServer(mux) | ||
2049 | 1969 | - defer ts.Close() | ||
2050 | 1970 | - | ||
2051 | 1971 | - // net/http uses HTTP/1.1 for requests, so write requests manually | ||
2052 | 1972 | - tests := []struct { | ||
2053 | 1973 | - req string // raw http request | ||
2054 | 1974 | - expect []string // expected Connection header(s) | ||
2055 | 1975 | - }{ | ||
2056 | 1976 | - { | ||
2057 | 1977 | - req: "GET / HTTP/1.0\r\n\r\n", | ||
2058 | 1978 | - expect: nil, | ||
2059 | 1979 | - }, | ||
2060 | 1980 | - { | ||
2061 | 1981 | - req: "OPTIONS * HTTP/1.0\r\n\r\n", | ||
2062 | 1982 | - expect: nil, | ||
2063 | 1983 | - }, | ||
2064 | 1984 | - { | ||
2065 | 1985 | - req: "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", | ||
2066 | 1986 | - expect: []string{"keep-alive"}, | ||
2067 | 1987 | - }, | ||
2068 | 1988 | - } | ||
2069 | 1989 | - | ||
2070 | 1990 | - for _, tt := range tests { | ||
2071 | 1991 | - conn, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
2072 | 1992 | - if err != nil { | ||
2073 | 1993 | - t.Fatal("dial err:", err) | ||
2074 | 1994 | - } | ||
2075 | 1995 | - | ||
2076 | 1996 | - _, err = fmt.Fprint(conn, tt.req) | ||
2077 | 1997 | - if err != nil { | ||
2078 | 1998 | - t.Fatal("conn write err:", err) | ||
2079 | 1999 | - } | ||
2080 | 2000 | - | ||
2081 | 2001 | - resp, err := ReadResponse(bufio.NewReader(conn), &Request{Method: "GET"}) | ||
2082 | 2002 | - if err != nil { | ||
2083 | 2003 | - t.Fatal("ReadResponse err:", err) | ||
2084 | 2004 | - } | ||
2085 | 2005 | - conn.Close() | ||
2086 | 2006 | - resp.Body.Close() | ||
2087 | 2007 | - | ||
2088 | 2008 | - got := resp.Header["Connection"] | ||
2089 | 2009 | - if !reflect.DeepEqual(got, tt.expect) { | ||
2090 | 2010 | - t.Errorf("wrong Connection headers for request %q. Got %q expect %q", tt.req, got, tt.expect) | ||
2091 | 2011 | - } | ||
2092 | 2012 | - } | ||
2093 | 2013 | -} | ||
2094 | 2014 | - | ||
2095 | 2015 | -// See golang.org/issue/5660 | ||
2096 | 2016 | -func TestServerReaderFromOrder(t *testing.T) { | ||
2097 | 2017 | - defer afterTest(t) | ||
2098 | 2018 | - pr, pw := io.Pipe() | ||
2099 | 2019 | - const size = 3 << 20 | ||
2100 | 2020 | - ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) { | ||
2101 | 2021 | - rw.Header().Set("Content-Type", "text/plain") // prevent sniffing path | ||
2102 | 2022 | - done := make(chan bool) | ||
2103 | 2023 | - go func() { | ||
2104 | 2024 | - io.Copy(rw, pr) | ||
2105 | 2025 | - close(done) | ||
2106 | 2026 | - }() | ||
2107 | 2027 | - time.Sleep(25 * time.Millisecond) // give Copy a chance to break things | ||
2108 | 2028 | - n, err := io.Copy(ioutil.Discard, req.Body) | ||
2109 | 2029 | - if err != nil { | ||
2110 | 2030 | - t.Errorf("handler Copy: %v", err) | ||
2111 | 2031 | - return | ||
2112 | 2032 | - } | ||
2113 | 2033 | - if n != size { | ||
2114 | 2034 | - t.Errorf("handler Copy = %d; want %d", n, size) | ||
2115 | 2035 | - } | ||
2116 | 2036 | - pw.Write([]byte("hi")) | ||
2117 | 2037 | - pw.Close() | ||
2118 | 2038 | - <-done | ||
2119 | 2039 | - })) | ||
2120 | 2040 | - defer ts.Close() | ||
2121 | 2041 | - | ||
2122 | 2042 | - req, err := NewRequest("POST", ts.URL, io.LimitReader(neverEnding('a'), size)) | ||
2123 | 2043 | - if err != nil { | ||
2124 | 2044 | - t.Fatal(err) | ||
2125 | 2045 | - } | ||
2126 | 2046 | - res, err := DefaultClient.Do(req) | ||
2127 | 2047 | - if err != nil { | ||
2128 | 2048 | - t.Fatal(err) | ||
2129 | 2049 | - } | ||
2130 | 2050 | - all, err := ioutil.ReadAll(res.Body) | ||
2131 | 2051 | - if err != nil { | ||
2132 | 2052 | - t.Fatal(err) | ||
2133 | 2053 | - } | ||
2134 | 2054 | - res.Body.Close() | ||
2135 | 2055 | - if string(all) != "hi" { | ||
2136 | 2056 | - t.Errorf("Body = %q; want hi", all) | ||
2137 | 2057 | - } | ||
2138 | 2058 | -} | ||
2139 | 2059 | - | ||
2140 | 2060 | -// Issue 6157, Issue 6685 | ||
2141 | 2061 | -func TestCodesPreventingContentTypeAndBody(t *testing.T) { | ||
2142 | 2062 | - for _, code := range []int{StatusNotModified, StatusNoContent, StatusContinue} { | ||
2143 | 2063 | - ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
2144 | 2064 | - if r.URL.Path == "/header" { | ||
2145 | 2065 | - w.Header().Set("Content-Length", "123") | ||
2146 | 2066 | - } | ||
2147 | 2067 | - w.WriteHeader(code) | ||
2148 | 2068 | - if r.URL.Path == "/more" { | ||
2149 | 2069 | - w.Write([]byte("stuff")) | ||
2150 | 2070 | - } | ||
2151 | 2071 | - })) | ||
2152 | 2072 | - for _, req := range []string{ | ||
2153 | 2073 | - "GET / HTTP/1.0", | ||
2154 | 2074 | - "GET /header HTTP/1.0", | ||
2155 | 2075 | - "GET /more HTTP/1.0", | ||
2156 | 2076 | - "GET / HTTP/1.1", | ||
2157 | 2077 | - "GET /header HTTP/1.1", | ||
2158 | 2078 | - "GET /more HTTP/1.1", | ||
2159 | 2079 | - } { | ||
2160 | 2080 | - got := ht.rawResponse(req) | ||
2161 | 2081 | - wantStatus := fmt.Sprintf("%d %s", code, StatusText(code)) | ||
2162 | 2082 | - if !strings.Contains(got, wantStatus) { | ||
2163 | 2083 | - t.Errorf("Code %d: Wanted %q Modified for %q: %s", code, req, got) | ||
2164 | 2084 | - } else if strings.Contains(got, "Content-Length") { | ||
2165 | 2085 | - t.Errorf("Code %d: Got a Content-Length from %q: %s", code, req, got) | ||
2166 | 2086 | - } else if strings.Contains(got, "stuff") { | ||
2167 | 2087 | - t.Errorf("Code %d: Response contains a body from %q: %s", code, req, got) | ||
2168 | 2088 | - } | ||
2169 | 2089 | - } | ||
2170 | 2090 | - } | ||
2171 | 2091 | -} | ||
2172 | 2092 | - | ||
2173 | 2093 | -// Issue 6995 | ||
2174 | 2094 | -// A server Handler can receive a Request, and then turn around and | ||
2175 | 2095 | -// give a copy of that Request.Body out to the Transport (e.g. any | ||
2176 | 2096 | -// proxy). So then two people own that Request.Body (both the server | ||
2177 | 2097 | -// and the http client), and both think they can close it on failure. | ||
2178 | 2098 | -// Therefore, all incoming server requests Bodies need to be thread-safe. | ||
2179 | 2099 | -func TestTransportAndServerSharedBodyRace(t *testing.T) { | ||
2180 | 2100 | - defer afterTest(t) | ||
2181 | 2101 | - | ||
2182 | 2102 | - const bodySize = 1 << 20 | ||
2183 | 2103 | - | ||
2184 | 2104 | - unblockBackend := make(chan bool) | ||
2185 | 2105 | - backend := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) { | ||
2186 | 2106 | - io.CopyN(rw, req.Body, bodySize/2) | ||
2187 | 2107 | - <-unblockBackend | ||
2188 | 2108 | - })) | ||
2189 | 2109 | - defer backend.Close() | ||
2190 | 2110 | - | ||
2191 | 2111 | - backendRespc := make(chan *Response, 1) | ||
2192 | 2112 | - proxy := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) { | ||
2193 | 2113 | - if req.RequestURI == "/foo" { | ||
2194 | 2114 | - rw.Write([]byte("bar")) | ||
2195 | 2115 | - return | ||
2196 | 2116 | - } | ||
2197 | 2117 | - req2, _ := NewRequest("POST", backend.URL, req.Body) | ||
2198 | 2118 | - req2.ContentLength = bodySize | ||
2199 | 2119 | - | ||
2200 | 2120 | - bresp, err := DefaultClient.Do(req2) | ||
2201 | 2121 | - if err != nil { | ||
2202 | 2122 | - t.Errorf("Proxy outbound request: %v", err) | ||
2203 | 2123 | - return | ||
2204 | 2124 | - } | ||
2205 | 2125 | - _, err = io.CopyN(ioutil.Discard, bresp.Body, bodySize/4) | ||
2206 | 2126 | - if err != nil { | ||
2207 | 2127 | - t.Errorf("Proxy copy error: %v", err) | ||
2208 | 2128 | - return | ||
2209 | 2129 | - } | ||
2210 | 2130 | - backendRespc <- bresp // to close later | ||
2211 | 2131 | - | ||
2212 | 2132 | - // Try to cause a race: Both the DefaultTransport and the proxy handler's Server | ||
2213 | 2133 | - // will try to read/close req.Body (aka req2.Body) | ||
2214 | 2134 | - DefaultTransport.(*Transport).CancelRequest(req2) | ||
2215 | 2135 | - rw.Write([]byte("OK")) | ||
2216 | 2136 | - })) | ||
2217 | 2137 | - defer proxy.Close() | ||
2218 | 2138 | - | ||
2219 | 2139 | - req, _ := NewRequest("POST", proxy.URL, io.LimitReader(neverEnding('a'), bodySize)) | ||
2220 | 2140 | - res, err := DefaultClient.Do(req) | ||
2221 | 2141 | - if err != nil { | ||
2222 | 2142 | - t.Fatalf("Original request: %v", err) | ||
2223 | 2143 | - } | ||
2224 | 2144 | - | ||
2225 | 2145 | - // Cleanup, so we don't leak goroutines. | ||
2226 | 2146 | - res.Body.Close() | ||
2227 | 2147 | - close(unblockBackend) | ||
2228 | 2148 | - (<-backendRespc).Body.Close() | ||
2229 | 2149 | -} | ||
2230 | 2150 | - | ||
2231 | 2151 | -// Test that a hanging Request.Body.Read from another goroutine can't | ||
2232 | 2152 | -// cause the Handler goroutine's Request.Body.Close to block. | ||
2233 | 2153 | -func TestRequestBodyCloseDoesntBlock(t *testing.T) { | ||
2234 | 2154 | - t.Skipf("Skipping known issue; see golang.org/issue/7121") | ||
2235 | 2155 | - if testing.Short() { | ||
2236 | 2156 | - t.Skip("skipping in -short mode") | ||
2237 | 2157 | - } | ||
2238 | 2158 | - defer afterTest(t) | ||
2239 | 2159 | - | ||
2240 | 2160 | - readErrCh := make(chan error, 1) | ||
2241 | 2161 | - errCh := make(chan error, 2) | ||
2242 | 2162 | - | ||
2243 | 2163 | - server := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) { | ||
2244 | 2164 | - go func(body io.Reader) { | ||
2245 | 2165 | - _, err := body.Read(make([]byte, 100)) | ||
2246 | 2166 | - readErrCh <- err | ||
2247 | 2167 | - }(req.Body) | ||
2248 | 2168 | - time.Sleep(500 * time.Millisecond) | ||
2249 | 2169 | - })) | ||
2250 | 2170 | - defer server.Close() | ||
2251 | 2171 | - | ||
2252 | 2172 | - closeConn := make(chan bool) | ||
2253 | 2173 | - defer close(closeConn) | ||
2254 | 2174 | - go func() { | ||
2255 | 2175 | - conn, err := net.Dial("tcp", server.Listener.Addr().String()) | ||
2256 | 2176 | - if err != nil { | ||
2257 | 2177 | - errCh <- err | ||
2258 | 2178 | - return | ||
2259 | 2179 | - } | ||
2260 | 2180 | - defer conn.Close() | ||
2261 | 2181 | - _, err = conn.Write([]byte("POST / HTTP/1.1\r\nConnection: close\r\nHost: foo\r\nContent-Length: 100000\r\n\r\n")) | ||
2262 | 2182 | - if err != nil { | ||
2263 | 2183 | - errCh <- err | ||
2264 | 2184 | - return | ||
2265 | 2185 | - } | ||
2266 | 2186 | - // And now just block, making the server block on our | ||
2267 | 2187 | - // 100000 bytes of body that will never arrive. | ||
2268 | 2188 | - <-closeConn | ||
2269 | 2189 | - }() | ||
2270 | 2190 | - select { | ||
2271 | 2191 | - case err := <-readErrCh: | ||
2272 | 2192 | - if err == nil { | ||
2273 | 2193 | - t.Error("Read was nil. Expected error.") | ||
2274 | 2194 | - } | ||
2275 | 2195 | - case err := <-errCh: | ||
2276 | 2196 | - t.Error(err) | ||
2277 | 2197 | - case <-time.After(5 * time.Second): | ||
2278 | 2198 | - t.Error("timeout") | ||
2279 | 2199 | - } | ||
2280 | 2200 | -} | ||
2281 | 2201 | - | ||
2282 | 2202 | -func TestResponseWriterWriteStringAllocs(t *testing.T) { | ||
2283 | 2203 | - ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
2284 | 2204 | - if r.URL.Path == "/s" { | ||
2285 | 2205 | - io.WriteString(w, "Hello world") | ||
2286 | 2206 | - } else { | ||
2287 | 2207 | - w.Write([]byte("Hello world")) | ||
2288 | 2208 | - } | ||
2289 | 2209 | - })) | ||
2290 | 2210 | - before := testing.AllocsPerRun(50, func() { ht.rawResponse("GET / HTTP/1.0") }) | ||
2291 | 2211 | - after := testing.AllocsPerRun(50, func() { ht.rawResponse("GET /s HTTP/1.0") }) | ||
2292 | 2212 | - if int(after) >= int(before) { | ||
2293 | 2213 | - t.Errorf("WriteString allocs of %v >= Write allocs of %v", after, before) | ||
2294 | 2214 | - } | ||
2295 | 2215 | -} | ||
2296 | 2216 | - | ||
2297 | 2217 | -func TestAppendTime(t *testing.T) { | ||
2298 | 2218 | - var b [len(TimeFormat)]byte | ||
2299 | 2219 | - t1 := time.Date(2013, 9, 21, 15, 41, 0, 0, time.FixedZone("CEST", 2*60*60)) | ||
2300 | 2220 | - res := ExportAppendTime(b[:0], t1) | ||
2301 | 2221 | - t2, err := ParseTime(string(res)) | ||
2302 | 2222 | - if err != nil { | ||
2303 | 2223 | - t.Fatalf("Error parsing time: %s", err) | ||
2304 | 2224 | - } | ||
2305 | 2225 | - if !t1.Equal(t2) { | ||
2306 | 2226 | - t.Fatalf("Times differ; expected: %v, got %v (%s)", t1, t2, string(res)) | ||
2307 | 2227 | - } | ||
2308 | 2228 | -} | ||
2309 | 2229 | - | ||
2310 | 2230 | -func TestServerConnState(t *testing.T) { | ||
2311 | 2231 | - defer afterTest(t) | ||
2312 | 2232 | - handler := map[string]func(w ResponseWriter, r *Request){ | ||
2313 | 2233 | - "/": func(w ResponseWriter, r *Request) { | ||
2314 | 2234 | - fmt.Fprintf(w, "Hello.") | ||
2315 | 2235 | - }, | ||
2316 | 2236 | - "/close": func(w ResponseWriter, r *Request) { | ||
2317 | 2237 | - w.Header().Set("Connection", "close") | ||
2318 | 2238 | - fmt.Fprintf(w, "Hello.") | ||
2319 | 2239 | - }, | ||
2320 | 2240 | - "/hijack": func(w ResponseWriter, r *Request) { | ||
2321 | 2241 | - c, _, _ := w.(Hijacker).Hijack() | ||
2322 | 2242 | - c.Write([]byte("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nHello.")) | ||
2323 | 2243 | - c.Close() | ||
2324 | 2244 | - }, | ||
2325 | 2245 | - "/hijack-panic": func(w ResponseWriter, r *Request) { | ||
2326 | 2246 | - c, _, _ := w.(Hijacker).Hijack() | ||
2327 | 2247 | - c.Write([]byte("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nHello.")) | ||
2328 | 2248 | - c.Close() | ||
2329 | 2249 | - panic("intentional panic") | ||
2330 | 2250 | - }, | ||
2331 | 2251 | - } | ||
2332 | 2252 | - ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) { | ||
2333 | 2253 | - handler[r.URL.Path](w, r) | ||
2334 | 2254 | - })) | ||
2335 | 2255 | - defer ts.Close() | ||
2336 | 2256 | - | ||
2337 | 2257 | - var mu sync.Mutex // guard stateLog and connID | ||
2338 | 2258 | - var stateLog = map[int][]ConnState{} | ||
2339 | 2259 | - var connID = map[net.Conn]int{} | ||
2340 | 2260 | - | ||
2341 | 2261 | - ts.Config.ErrorLog = log.New(ioutil.Discard, "", 0) | ||
2342 | 2262 | - ts.Config.ConnState = func(c net.Conn, state ConnState) { | ||
2343 | 2263 | - if c == nil { | ||
2344 | 2264 | - t.Error("nil conn seen in state %s", state) | ||
2345 | 2265 | - return | ||
2346 | 2266 | - } | ||
2347 | 2267 | - mu.Lock() | ||
2348 | 2268 | - defer mu.Unlock() | ||
2349 | 2269 | - id, ok := connID[c] | ||
2350 | 2270 | - if !ok { | ||
2351 | 2271 | - id = len(connID) + 1 | ||
2352 | 2272 | - connID[c] = id | ||
2353 | 2273 | - } | ||
2354 | 2274 | - stateLog[id] = append(stateLog[id], state) | ||
2355 | 2275 | - } | ||
2356 | 2276 | - ts.Start() | ||
2357 | 2277 | - | ||
2358 | 2278 | - mustGet(t, ts.URL+"/") | ||
2359 | 2279 | - mustGet(t, ts.URL+"/close") | ||
2360 | 2280 | - | ||
2361 | 2281 | - mustGet(t, ts.URL+"/") | ||
2362 | 2282 | - mustGet(t, ts.URL+"/", "Connection", "close") | ||
2363 | 2283 | - | ||
2364 | 2284 | - mustGet(t, ts.URL+"/hijack") | ||
2365 | 2285 | - mustGet(t, ts.URL+"/hijack-panic") | ||
2366 | 2286 | - | ||
2367 | 2287 | - // New->Closed | ||
2368 | 2288 | - { | ||
2369 | 2289 | - c, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
2370 | 2290 | - if err != nil { | ||
2371 | 2291 | - t.Fatal(err) | ||
2372 | 2292 | - } | ||
2373 | 2293 | - c.Close() | ||
2374 | 2294 | - } | ||
2375 | 2295 | - | ||
2376 | 2296 | - // New->Active->Closed | ||
2377 | 2297 | - { | ||
2378 | 2298 | - c, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
2379 | 2299 | - if err != nil { | ||
2380 | 2300 | - t.Fatal(err) | ||
2381 | 2301 | - } | ||
2382 | 2302 | - if _, err := io.WriteString(c, "BOGUS REQUEST\r\n\r\n"); err != nil { | ||
2383 | 2303 | - t.Fatal(err) | ||
2384 | 2304 | - } | ||
2385 | 2305 | - c.Close() | ||
2386 | 2306 | - } | ||
2387 | 2307 | - | ||
2388 | 2308 | - // New->Idle->Closed | ||
2389 | 2309 | - { | ||
2390 | 2310 | - c, err := net.Dial("tcp", ts.Listener.Addr().String()) | ||
2391 | 2311 | - if err != nil { | ||
2392 | 2312 | - t.Fatal(err) | ||
2393 | 2313 | - } | ||
2394 | 2314 | - if _, err := io.WriteString(c, "GET / HTTP/1.1\r\nHost: foo\r\n\r\n"); err != nil { | ||
2395 | 2315 | - t.Fatal(err) | ||
2396 | 2316 | - } | ||
2397 | 2317 | - res, err := ReadResponse(bufio.NewReader(c), nil) | ||
2398 | 2318 | - if err != nil { | ||
2399 | 2319 | - t.Fatal(err) | ||
2400 | 2320 | - } | ||
2401 | 2321 | - if _, err := io.Copy(ioutil.Discard, res.Body); err != nil { | ||
2402 | 2322 | - t.Fatal(err) | ||
2403 | 2323 | - } | ||
2404 | 2324 | - c.Close() | ||
2405 | 2325 | - } | ||
2406 | 2326 | - | ||
2407 | 2327 | - want := map[int][]ConnState{ | ||
2408 | 2328 | - 1: []ConnState{StateNew, StateActive, StateIdle, StateActive, StateClosed}, | ||
2409 | 2329 | - 2: []ConnState{StateNew, StateActive, StateIdle, StateActive, StateClosed}, | ||
2410 | 2330 | - 3: []ConnState{StateNew, StateActive, StateHijacked}, | ||
2411 | 2331 | - 4: []ConnState{StateNew, StateActive, StateHijacked}, | ||
2412 | 2332 | - 5: []ConnState{StateNew, StateClosed}, | ||
2413 | 2333 | - 6: []ConnState{StateNew, StateActive, StateClosed}, | ||
2414 | 2334 | - 7: []ConnState{StateNew, StateActive, StateIdle, StateClosed}, | ||
2415 | 2335 | - } | ||
2416 | 2336 | - logString := func(m map[int][]ConnState) string { | ||
2417 | 2337 | - var b bytes.Buffer | ||
2418 | 2338 | - for id, l := range m { | ||
2419 | 2339 | - fmt.Fprintf(&b, "Conn %d: ", id) | ||
2420 | 2340 | - for _, s := range l { | ||
2421 | 2341 | - fmt.Fprintf(&b, "%s ", s) | ||
2422 | 2342 | - } | ||
2423 | 2343 | - b.WriteString("\n") | ||
2424 | 2344 | - } | ||
2425 | 2345 | - return b.String() | ||
2426 | 2346 | - } | ||
2427 | 2347 | - | ||
2428 | 2348 | - for i := 0; i < 5; i++ { | ||
2429 | 2349 | - time.Sleep(time.Duration(i) * 50 * time.Millisecond) | ||
2430 | 2350 | - mu.Lock() | ||
2431 | 2351 | - match := reflect.DeepEqual(stateLog, want) | ||
2432 | 2352 | - mu.Unlock() | ||
2433 | 2353 | - if match { | ||
2434 | 2354 | - return | ||
2435 | 2355 | - } | ||
2436 | 2356 | - } | ||
2437 | 2357 | - | ||
2438 | 2358 | - mu.Lock() | ||
2439 | 2359 | - t.Errorf("Unexpected events.\nGot log: %s\n Want: %s\n", logString(stateLog), logString(want)) | ||
2440 | 2360 | - mu.Unlock() | ||
2441 | 2361 | -} | ||
2442 | 2362 | - | ||
2443 | 2363 | -func mustGet(t *testing.T, url string, headers ...string) { | ||
2444 | 2364 | - req, err := NewRequest("GET", url, nil) | ||
2445 | 2365 | - if err != nil { | ||
2446 | 2366 | - t.Fatal(err) | ||
2447 | 2367 | - } | ||
2448 | 2368 | - for len(headers) > 0 { | ||
2449 | 2369 | - req.Header.Add(headers[0], headers[1]) | ||
2450 | 2370 | - headers = headers[2:] | ||
2451 | 2371 | - } | ||
2452 | 2372 | - res, err := DefaultClient.Do(req) | ||
2453 | 2373 | - if err != nil { | ||
2454 | 2374 | - t.Errorf("Error fetching %s: %v", url, err) | ||
2455 | 2375 | - return | ||
2456 | 2376 | - } | ||
2457 | 2377 | - _, err = ioutil.ReadAll(res.Body) | ||
2458 | 2378 | - defer res.Body.Close() | ||
2459 | 2379 | - if err != nil { | ||
2460 | 2380 | - t.Errorf("Error reading %s: %v", url, err) | ||
2461 | 2381 | - } | ||
2462 | 2382 | -} | ||
2463 | 2383 | - | ||
2464 | 2384 | -func TestServerKeepAlivesEnabled(t *testing.T) { | ||
2465 | 2385 | - defer afterTest(t) | ||
2466 | 2386 | - ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {})) | ||
2467 | 2387 | - ts.Config.SetKeepAlivesEnabled(false) | ||
2468 | 2388 | - ts.Start() | ||
2469 | 2389 | - defer ts.Close() | ||
2470 | 2390 | - res, err := Get(ts.URL) | ||
2471 | 2391 | - if err != nil { | ||
2472 | 2392 | - t.Fatal(err) | ||
2473 | 2393 | - } | ||
2474 | 2394 | - defer res.Body.Close() | ||
2475 | 2395 | - if !res.Close { | ||
2476 | 2396 | - t.Errorf("Body.Close == false; want true") | ||
2477 | 2397 | - } | ||
2478 | 2398 | -} | ||
2479 | 2399 | - | ||
2480 | 2400 | -func TestServerConnStateNew(t *testing.T) { | ||
2481 | 2401 | - sawNew := false // if the test is buggy, we'll race on this variable. | ||
2482 | 2402 | - srv := &Server{ | ||
2483 | 2403 | - ConnState: func(c net.Conn, state ConnState) { | ||
2484 | 2404 | - if state == StateNew { | ||
2485 | 2405 | - sawNew = true // testing that this write isn't racy | ||
2486 | 2406 | - } | ||
2487 | 2407 | - }, | ||
2488 | 2408 | - Handler: HandlerFunc(func(w ResponseWriter, r *Request) {}), // irrelevant | ||
2489 | 2409 | - } | ||
2490 | 2410 | - srv.Serve(&oneConnListener{ | ||
2491 | 2411 | - conn: &rwTestConn{ | ||
2492 | 2412 | - Reader: strings.NewReader("GET / HTTP/1.1\r\nHost: foo\r\n\r\n"), | ||
2493 | 2413 | - Writer: ioutil.Discard, | ||
2494 | 2414 | - }, | ||
2495 | 2415 | - }) | ||
2496 | 2416 | - if !sawNew { // testing that this read isn't racy | ||
2497 | 2417 | - t.Error("StateNew not seen") | ||
2498 | 2418 | - } | ||
2499 | 2419 | -} | ||
2500 | 2420 | - | ||
2501 | 2421 | -func BenchmarkClientServer(b *testing.B) { | ||
2502 | 2422 | - b.ReportAllocs() | ||
2503 | 2423 | - b.StopTimer() | ||
2504 | 2424 | - ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
2505 | 2425 | - fmt.Fprintf(rw, "Hello world.\n") | ||
2506 | 2426 | - })) | ||
2507 | 2427 | - defer ts.Close() | ||
2508 | 2428 | - b.StartTimer() | ||
2509 | 2429 | - | ||
2510 | 2430 | - for i := 0; i < b.N; i++ { | ||
2511 | 2431 | - res, err := Get(ts.URL) | ||
2512 | 2432 | - if err != nil { | ||
2513 | 2433 | - b.Fatal("Get:", err) | ||
2514 | 2434 | - } | ||
2515 | 2435 | - all, err := ioutil.ReadAll(res.Body) | ||
2516 | 2436 | - res.Body.Close() | ||
2517 | 2437 | - if err != nil { | ||
2518 | 2438 | - b.Fatal("ReadAll:", err) | ||
2519 | 2439 | - } | ||
2520 | 2440 | - body := string(all) | ||
2521 | 2441 | - if body != "Hello world.\n" { | ||
2522 | 2442 | - b.Fatal("Got body:", body) | ||
2523 | 2443 | - } | ||
2524 | 2444 | - } | ||
2525 | 2445 | - | ||
2526 | 2446 | - b.StopTimer() | ||
2527 | 2447 | -} | ||
2528 | 2448 | - | ||
2529 | 2449 | -// A benchmark for profiling the server without the HTTP client code. | ||
2530 | 2450 | -// The client code runs in a subprocess. | ||
2531 | 2451 | -// | ||
2532 | 2452 | -// For use like: | ||
2533 | 2453 | -// $ go test -c | ||
2534 | 2454 | -// $ ./http.test -test.run=XX -test.bench=BenchmarkServer -test.benchtime=15s -test.cpuprofile=http.prof | ||
2535 | 2455 | -// $ go tool pprof http.test http.prof | ||
2536 | 2456 | -// (pprof) web | ||
2537 | 2457 | -func BenchmarkServer(b *testing.B) { | ||
2538 | 2458 | - b.ReportAllocs() | ||
2539 | 2459 | - // Child process mode; | ||
2540 | 2460 | - if url := os.Getenv("TEST_BENCH_SERVER_URL"); url != "" { | ||
2541 | 2461 | - n, err := strconv.Atoi(os.Getenv("TEST_BENCH_CLIENT_N")) | ||
2542 | 2462 | - if err != nil { | ||
2543 | 2463 | - panic(err) | ||
2544 | 2464 | - } | ||
2545 | 2465 | - for i := 0; i < n; i++ { | ||
2546 | 2466 | - res, err := Get(url) | ||
2547 | 2467 | - if err != nil { | ||
2548 | 2468 | - log.Panicf("Get: %v", err) | ||
2549 | 2469 | - } | ||
2550 | 2470 | - all, err := ioutil.ReadAll(res.Body) | ||
2551 | 2471 | - res.Body.Close() | ||
2552 | 2472 | - if err != nil { | ||
2553 | 2473 | - log.Panicf("ReadAll: %v", err) | ||
2554 | 2474 | - } | ||
2555 | 2475 | - body := string(all) | ||
2556 | 2476 | - if body != "Hello world.\n" { | ||
2557 | 2477 | - log.Panicf("Got body: %q", body) | ||
2558 | 2478 | - } | ||
2559 | 2479 | - } | ||
2560 | 2480 | - os.Exit(0) | ||
2561 | 2481 | - return | ||
2562 | 2482 | - } | ||
2563 | 2483 | - | ||
2564 | 2484 | - var res = []byte("Hello world.\n") | ||
2565 | 2485 | - b.StopTimer() | ||
2566 | 2486 | - ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
2567 | 2487 | - rw.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
2568 | 2488 | - rw.Write(res) | ||
2569 | 2489 | - })) | ||
2570 | 2490 | - defer ts.Close() | ||
2571 | 2491 | - b.StartTimer() | ||
2572 | 2492 | - | ||
2573 | 2493 | - cmd := exec.Command(os.Args[0], "-test.run=XXXX", "-test.bench=BenchmarkServer") | ||
2574 | 2494 | - cmd.Env = append([]string{ | ||
2575 | 2495 | - fmt.Sprintf("TEST_BENCH_CLIENT_N=%d", b.N), | ||
2576 | 2496 | - fmt.Sprintf("TEST_BENCH_SERVER_URL=%s", ts.URL), | ||
2577 | 2497 | - }, os.Environ()...) | ||
2578 | 2498 | - out, err := cmd.CombinedOutput() | ||
2579 | 2499 | - if err != nil { | ||
2580 | 2500 | - b.Errorf("Test failure: %v, with output: %s", err, out) | ||
2581 | 2501 | - } | ||
2582 | 2502 | -} | ||
2583 | 2503 | - | ||
2584 | 2504 | -func BenchmarkServerFakeConnNoKeepAlive(b *testing.B) { | ||
2585 | 2505 | - b.ReportAllocs() | ||
2586 | 2506 | - req := reqBytes(`GET / HTTP/1.0 | ||
2587 | 2507 | -Host: golang.org | ||
2588 | 2508 | -Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 | ||
2589 | 2509 | -User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17 | ||
2590 | 2510 | -Accept-Encoding: gzip,deflate,sdch | ||
2591 | 2511 | -Accept-Language: en-US,en;q=0.8 | ||
2592 | 2512 | -Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 | ||
2593 | 2513 | -`) | ||
2594 | 2514 | - res := []byte("Hello world!\n") | ||
2595 | 2515 | - | ||
2596 | 2516 | - conn := &testConn{ | ||
2597 | 2517 | - // testConn.Close will not push into the channel | ||
2598 | 2518 | - // if it's full. | ||
2599 | 2519 | - closec: make(chan bool, 1), | ||
2600 | 2520 | - } | ||
2601 | 2521 | - handler := HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
2602 | 2522 | - rw.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
2603 | 2523 | - rw.Write(res) | ||
2604 | 2524 | - }) | ||
2605 | 2525 | - ln := new(oneConnListener) | ||
2606 | 2526 | - for i := 0; i < b.N; i++ { | ||
2607 | 2527 | - conn.readBuf.Reset() | ||
2608 | 2528 | - conn.writeBuf.Reset() | ||
2609 | 2529 | - conn.readBuf.Write(req) | ||
2610 | 2530 | - ln.conn = conn | ||
2611 | 2531 | - Serve(ln, handler) | ||
2612 | 2532 | - <-conn.closec | ||
2613 | 2533 | - } | ||
2614 | 2534 | -} | ||
2615 | 2535 | - | ||
2616 | 2536 | -// repeatReader reads content count times, then EOFs. | ||
2617 | 2537 | -type repeatReader struct { | ||
2618 | 2538 | - content []byte | ||
2619 | 2539 | - count int | ||
2620 | 2540 | - off int | ||
2621 | 2541 | -} | ||
2622 | 2542 | - | ||
2623 | 2543 | -func (r *repeatReader) Read(p []byte) (n int, err error) { | ||
2624 | 2544 | - if r.count <= 0 { | ||
2625 | 2545 | - return 0, io.EOF | ||
2626 | 2546 | - } | ||
2627 | 2547 | - n = copy(p, r.content[r.off:]) | ||
2628 | 2548 | - r.off += n | ||
2629 | 2549 | - if r.off == len(r.content) { | ||
2630 | 2550 | - r.count-- | ||
2631 | 2551 | - r.off = 0 | ||
2632 | 2552 | - } | ||
2633 | 2553 | - return | ||
2634 | 2554 | -} | ||
2635 | 2555 | - | ||
2636 | 2556 | -func BenchmarkServerFakeConnWithKeepAlive(b *testing.B) { | ||
2637 | 2557 | - b.ReportAllocs() | ||
2638 | 2558 | - | ||
2639 | 2559 | - req := reqBytes(`GET / HTTP/1.1 | ||
2640 | 2560 | -Host: golang.org | ||
2641 | 2561 | -Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 | ||
2642 | 2562 | -User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17 | ||
2643 | 2563 | -Accept-Encoding: gzip,deflate,sdch | ||
2644 | 2564 | -Accept-Language: en-US,en;q=0.8 | ||
2645 | 2565 | -Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 | ||
2646 | 2566 | -`) | ||
2647 | 2567 | - res := []byte("Hello world!\n") | ||
2648 | 2568 | - | ||
2649 | 2569 | - conn := &rwTestConn{ | ||
2650 | 2570 | - Reader: &repeatReader{content: req, count: b.N}, | ||
2651 | 2571 | - Writer: ioutil.Discard, | ||
2652 | 2572 | - closec: make(chan bool, 1), | ||
2653 | 2573 | - } | ||
2654 | 2574 | - handled := 0 | ||
2655 | 2575 | - handler := HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
2656 | 2576 | - handled++ | ||
2657 | 2577 | - rw.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
2658 | 2578 | - rw.Write(res) | ||
2659 | 2579 | - }) | ||
2660 | 2580 | - ln := &oneConnListener{conn: conn} | ||
2661 | 2581 | - go Serve(ln, handler) | ||
2662 | 2582 | - <-conn.closec | ||
2663 | 2583 | - if b.N != handled { | ||
2664 | 2584 | - b.Errorf("b.N=%d but handled %d", b.N, handled) | ||
2665 | 2585 | - } | ||
2666 | 2586 | -} | ||
2667 | 2587 | - | ||
2668 | 2588 | -// same as above, but representing the most simple possible request | ||
2669 | 2589 | -// and handler. Notably: the handler does not call rw.Header(). | ||
2670 | 2590 | -func BenchmarkServerFakeConnWithKeepAliveLite(b *testing.B) { | ||
2671 | 2591 | - b.ReportAllocs() | ||
2672 | 2592 | - | ||
2673 | 2593 | - req := reqBytes(`GET / HTTP/1.1 | ||
2674 | 2594 | -Host: golang.org | ||
2675 | 2595 | -`) | ||
2676 | 2596 | - res := []byte("Hello world!\n") | ||
2677 | 2597 | - | ||
2678 | 2598 | - conn := &rwTestConn{ | ||
2679 | 2599 | - Reader: &repeatReader{content: req, count: b.N}, | ||
2680 | 2600 | - Writer: ioutil.Discard, | ||
2681 | 2601 | - closec: make(chan bool, 1), | ||
2682 | 2602 | - } | ||
2683 | 2603 | - handled := 0 | ||
2684 | 2604 | - handler := HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
2685 | 2605 | - handled++ | ||
2686 | 2606 | - rw.Write(res) | ||
2687 | 2607 | - }) | ||
2688 | 2608 | - ln := &oneConnListener{conn: conn} | ||
2689 | 2609 | - go Serve(ln, handler) | ||
2690 | 2610 | - <-conn.closec | ||
2691 | 2611 | - if b.N != handled { | ||
2692 | 2612 | - b.Errorf("b.N=%d but handled %d", b.N, handled) | ||
2693 | 2613 | - } | ||
2694 | 2614 | -} | ||
2695 | 2615 | - | ||
2696 | 2616 | -const someResponse = "<html>some response</html>" | ||
2697 | 2617 | - | ||
2698 | 2618 | -// A Response that's just no bigger than 2KB, the buffer-before-chunking threshold. | ||
2699 | 2619 | -var response = bytes.Repeat([]byte(someResponse), 2<<10/len(someResponse)) | ||
2700 | 2620 | - | ||
2701 | 2621 | -// Both Content-Type and Content-Length set. Should be no buffering. | ||
2702 | 2622 | -func BenchmarkServerHandlerTypeLen(b *testing.B) { | ||
2703 | 2623 | - benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) { | ||
2704 | 2624 | - w.Header().Set("Content-Type", "text/html") | ||
2705 | 2625 | - w.Header().Set("Content-Length", strconv.Itoa(len(response))) | ||
2706 | 2626 | - w.Write(response) | ||
2707 | 2627 | - })) | ||
2708 | 2628 | -} | ||
2709 | 2629 | - | ||
2710 | 2630 | -// A Content-Type is set, but no length. No sniffing, but will count the Content-Length. | ||
2711 | 2631 | -func BenchmarkServerHandlerNoLen(b *testing.B) { | ||
2712 | 2632 | - benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) { | ||
2713 | 2633 | - w.Header().Set("Content-Type", "text/html") | ||
2714 | 2634 | - w.Write(response) | ||
2715 | 2635 | - })) | ||
2716 | 2636 | -} | ||
2717 | 2637 | - | ||
2718 | 2638 | -// A Content-Length is set, but the Content-Type will be sniffed. | ||
2719 | 2639 | -func BenchmarkServerHandlerNoType(b *testing.B) { | ||
2720 | 2640 | - benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) { | ||
2721 | 2641 | - w.Header().Set("Content-Length", strconv.Itoa(len(response))) | ||
2722 | 2642 | - w.Write(response) | ||
2723 | 2643 | - })) | ||
2724 | 2644 | -} | ||
2725 | 2645 | - | ||
2726 | 2646 | -// Neither a Content-Type or Content-Length, so sniffed and counted. | ||
2727 | 2647 | -func BenchmarkServerHandlerNoHeader(b *testing.B) { | ||
2728 | 2648 | - benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) { | ||
2729 | 2649 | - w.Write(response) | ||
2730 | 2650 | - })) | ||
2731 | 2651 | -} | ||
2732 | 2652 | - | ||
2733 | 2653 | -func benchmarkHandler(b *testing.B, h Handler) { | ||
2734 | 2654 | - b.ReportAllocs() | ||
2735 | 2655 | - req := reqBytes(`GET / HTTP/1.1 | ||
2736 | 2656 | -Host: golang.org | ||
2737 | 2657 | -`) | ||
2738 | 2658 | - conn := &rwTestConn{ | ||
2739 | 2659 | - Reader: &repeatReader{content: req, count: b.N}, | ||
2740 | 2660 | - Writer: ioutil.Discard, | ||
2741 | 2661 | - closec: make(chan bool, 1), | ||
2742 | 2662 | - } | ||
2743 | 2663 | - handled := 0 | ||
2744 | 2664 | - handler := HandlerFunc(func(rw ResponseWriter, r *Request) { | ||
2745 | 2665 | - handled++ | ||
2746 | 2666 | - h.ServeHTTP(rw, r) | ||
2747 | 2667 | - }) | ||
2748 | 2668 | - ln := &oneConnListener{conn: conn} | ||
2749 | 2669 | - go Serve(ln, handler) | ||
2750 | 2670 | - <-conn.closec | ||
2751 | 2671 | - if b.N != handled { | ||
2752 | 2672 | - b.Errorf("b.N=%d but handled %d", b.N, handled) | ||
2753 | 2673 | - } | ||
2754 | 2674 | -} | ||
2755 | 2675 | - | ||
2756 | 2676 | -func BenchmarkServerHijack(b *testing.B) { | ||
2757 | 2677 | - b.ReportAllocs() | ||
2758 | 2678 | - req := reqBytes(`GET / HTTP/1.1 | ||
2759 | 2679 | -Host: golang.org | ||
2760 | 2680 | -`) | ||
2761 | 2681 | - h := HandlerFunc(func(w ResponseWriter, r *Request) { | ||
2762 | 2682 | - conn, _, err := w.(Hijacker).Hijack() | ||
2763 | 2683 | - if err != nil { | ||
2764 | 2684 | - panic(err) | ||
2765 | 2685 | - } | ||
2766 | 2686 | - conn.Close() | ||
2767 | 2687 | - }) | ||
2768 | 2688 | - conn := &rwTestConn{ | ||
2769 | 2689 | - Writer: ioutil.Discard, | ||
2770 | 2690 | - closec: make(chan bool, 1), | ||
2771 | 2691 | - } | ||
2772 | 2692 | - ln := &oneConnListener{conn: conn} | ||
2773 | 2693 | - for i := 0; i < b.N; i++ { | ||
2774 | 2694 | - conn.Reader = bytes.NewReader(req) | ||
2775 | 2695 | - ln.conn = conn | ||
2776 | 2696 | - Serve(ln, h) | ||
2777 | 2697 | - <-conn.closec | ||
2778 | 2698 | - } | ||
2779 | 2699 | -} | ||
2780 | 2700 | |||
2781 | 2701 | === modified file 'http13client/server.go' | ||
2782 | 2702 | --- http13client/server.go 2014-03-19 20:20:19 +0000 | ||
2783 | 2703 | +++ http13client/server.go 2014-03-19 22:27:37 +0000 | ||
2784 | 2704 | @@ -2,1984 +2,17 @@ | ||
2785 | 2705 | // Use of this source code is governed by a BSD-style | ||
2786 | 2706 | // license that can be found in the LICENSE file. | ||
2787 | 2707 | |||
2788 | 2708 | -// HTTP server. See RFC 2616. | ||
2789 | 2709 | - | ||
2790 | 2710 | package http | ||
2791 | 2711 | |||
2792 | 2712 | import ( | ||
2793 | 2713 | - "bufio" | ||
2794 | 2714 | - "crypto/tls" | ||
2795 | 2715 | - "errors" | ||
2796 | 2716 | "fmt" | ||
2797 | 2717 | "io" | ||
2798 | 2718 | "io/ioutil" | ||
2799 | 2719 | "log" | ||
2800 | 2720 | "net" | ||
2801 | 2721 | - "net/url" | ||
2802 | 2722 | - "os" | ||
2803 | 2723 | - "path" | ||
2804 | 2724 | - "runtime" | ||
2805 | 2725 | - "strconv" | ||
2806 | 2726 | "strings" | ||
2807 | 2727 | "sync" | ||
2808 | 2728 | - "sync/atomic" | ||
2809 | 2729 | - "time" | ||
2810 | 2730 | -) | ||
2811 | 2731 | - | ||
2812 | 2732 | -// Errors introduced by the HTTP server. | ||
2813 | 2733 | -var ( | ||
2814 | 2734 | - ErrWriteAfterFlush = errors.New("Conn.Write called after Flush") | ||
2815 | 2735 | - ErrBodyNotAllowed = errors.New("http: request method or response status code does not allow body") | ||
2816 | 2736 | - ErrHijacked = errors.New("Conn has been hijacked") | ||
2817 | 2737 | - ErrContentLength = errors.New("Conn.Write wrote more than the declared Content-Length") | ||
2818 | 2738 | -) | ||
2819 | 2739 | - | ||
2820 | 2740 | -// Objects implementing the Handler interface can be | ||
2821 | 2741 | -// registered to serve a particular path or subtree | ||
2822 | 2742 | -// in the HTTP server. | ||
2823 | 2743 | -// | ||
2824 | 2744 | -// ServeHTTP should write reply headers and data to the ResponseWriter | ||
2825 | 2745 | -// and then return. Returning signals that the request is finished | ||
2826 | 2746 | -// and that the HTTP server can move on to the next request on | ||
2827 | 2747 | -// the connection. | ||
2828 | 2748 | -type Handler interface { | ||
2829 | 2749 | - ServeHTTP(ResponseWriter, *Request) | ||
2830 | 2750 | -} | ||
2831 | 2751 | - | ||
2832 | 2752 | -// A ResponseWriter interface is used by an HTTP handler to | ||
2833 | 2753 | -// construct an HTTP response. | ||
2834 | 2754 | -type ResponseWriter interface { | ||
2835 | 2755 | - // Header returns the header map that will be sent by WriteHeader. | ||
2836 | 2756 | - // Changing the header after a call to WriteHeader (or Write) has | ||
2837 | 2757 | - // no effect. | ||
2838 | 2758 | - Header() Header | ||
2839 | 2759 | - | ||
2840 | 2760 | - // Write writes the data to the connection as part of an HTTP reply. | ||
2841 | 2761 | - // If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) | ||
2842 | 2762 | - // before writing the data. If the Header does not contain a | ||
2843 | 2763 | - // Content-Type line, Write adds a Content-Type set to the result of passing | ||
2844 | 2764 | - // the initial 512 bytes of written data to DetectContentType. | ||
2845 | 2765 | - Write([]byte) (int, error) | ||
2846 | 2766 | - | ||
2847 | 2767 | - // WriteHeader sends an HTTP response header with status code. | ||
2848 | 2768 | - // If WriteHeader is not called explicitly, the first call to Write | ||
2849 | 2769 | - // will trigger an implicit WriteHeader(http.StatusOK). | ||
2850 | 2770 | - // Thus explicit calls to WriteHeader are mainly used to | ||
2851 | 2771 | - // send error codes. | ||
2852 | 2772 | - WriteHeader(int) | ||
2853 | 2773 | -} | ||
2854 | 2774 | - | ||
2855 | 2775 | -// The Flusher interface is implemented by ResponseWriters that allow | ||
2856 | 2776 | -// an HTTP handler to flush buffered data to the client. | ||
2857 | 2777 | -// | ||
2858 | 2778 | -// Note that even for ResponseWriters that support Flush, | ||
2859 | 2779 | -// if the client is connected through an HTTP proxy, | ||
2860 | 2780 | -// the buffered data may not reach the client until the response | ||
2861 | 2781 | -// completes. | ||
2862 | 2782 | -type Flusher interface { | ||
2863 | 2783 | - // Flush sends any buffered data to the client. | ||
2864 | 2784 | - Flush() | ||
2865 | 2785 | -} | ||
2866 | 2786 | - | ||
2867 | 2787 | -// The Hijacker interface is implemented by ResponseWriters that allow | ||
2868 | 2788 | -// an HTTP handler to take over the connection. | ||
2869 | 2789 | -type Hijacker interface { | ||
2870 | 2790 | - // Hijack lets the caller take over the connection. | ||
2871 | 2791 | - // After a call to Hijack(), the HTTP server library | ||
2872 | 2792 | - // will not do anything else with the connection. | ||
2873 | 2793 | - // It becomes the caller's responsibility to manage | ||
2874 | 2794 | - // and close the connection. | ||
2875 | 2795 | - Hijack() (net.Conn, *bufio.ReadWriter, error) | ||
2876 | 2796 | -} | ||
2877 | 2797 | - | ||
2878 | 2798 | -// The CloseNotifier interface is implemented by ResponseWriters which | ||
2879 | 2799 | -// allow detecting when the underlying connection has gone away. | ||
2880 | 2800 | -// | ||
2881 | 2801 | -// This mechanism can be used to cancel long operations on the server | ||
2882 | 2802 | -// if the client has disconnected before the response is ready. | ||
2883 | 2803 | -type CloseNotifier interface { | ||
2884 | 2804 | - // CloseNotify returns a channel that receives a single value | ||
2885 | 2805 | - // when the client connection has gone away. | ||
2886 | 2806 | - CloseNotify() <-chan bool | ||
2887 | 2807 | -} | ||
2888 | 2808 | - | ||
2889 | 2809 | -// A conn represents the server side of an HTTP connection. | ||
2890 | 2810 | -type conn struct { | ||
2891 | 2811 | - remoteAddr string // network address of remote side | ||
2892 | 2812 | - server *Server // the Server on which the connection arrived | ||
2893 | 2813 | - rwc net.Conn // i/o connection | ||
2894 | 2814 | - sr liveSwitchReader // where the LimitReader reads from; usually the rwc | ||
2895 | 2815 | - lr *io.LimitedReader // io.LimitReader(sr) | ||
2896 | 2816 | - buf *bufio.ReadWriter // buffered(lr,rwc), reading from bufio->limitReader->sr->rwc | ||
2897 | 2817 | - tlsState *tls.ConnectionState // or nil when not using TLS | ||
2898 | 2818 | - | ||
2899 | 2819 | - mu sync.Mutex // guards the following | ||
2900 | 2820 | - clientGone bool // if client has disconnected mid-request | ||
2901 | 2821 | - closeNotifyc chan bool // made lazily | ||
2902 | 2822 | - hijackedv bool // connection has been hijacked by handler | ||
2903 | 2823 | -} | ||
2904 | 2824 | - | ||
2905 | 2825 | -func (c *conn) hijacked() bool { | ||
2906 | 2826 | - c.mu.Lock() | ||
2907 | 2827 | - defer c.mu.Unlock() | ||
2908 | 2828 | - return c.hijackedv | ||
2909 | 2829 | -} | ||
2910 | 2830 | - | ||
2911 | 2831 | -func (c *conn) hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) { | ||
2912 | 2832 | - c.mu.Lock() | ||
2913 | 2833 | - defer c.mu.Unlock() | ||
2914 | 2834 | - if c.hijackedv { | ||
2915 | 2835 | - return nil, nil, ErrHijacked | ||
2916 | 2836 | - } | ||
2917 | 2837 | - if c.closeNotifyc != nil { | ||
2918 | 2838 | - return nil, nil, errors.New("http: Hijack is incompatible with use of CloseNotifier") | ||
2919 | 2839 | - } | ||
2920 | 2840 | - c.hijackedv = true | ||
2921 | 2841 | - rwc = c.rwc | ||
2922 | 2842 | - buf = c.buf | ||
2923 | 2843 | - c.rwc = nil | ||
2924 | 2844 | - c.buf = nil | ||
2925 | 2845 | - c.setState(rwc, StateHijacked) | ||
2926 | 2846 | - return | ||
2927 | 2847 | -} | ||
2928 | 2848 | - | ||
2929 | 2849 | -func (c *conn) closeNotify() <-chan bool { | ||
2930 | 2850 | - c.mu.Lock() | ||
2931 | 2851 | - defer c.mu.Unlock() | ||
2932 | 2852 | - if c.closeNotifyc == nil { | ||
2933 | 2853 | - c.closeNotifyc = make(chan bool, 1) | ||
2934 | 2854 | - if c.hijackedv { | ||
2935 | 2855 | - // to obey the function signature, even though | ||
2936 | 2856 | - // it'll never receive a value. | ||
2937 | 2857 | - return c.closeNotifyc | ||
2938 | 2858 | - } | ||
2939 | 2859 | - pr, pw := io.Pipe() | ||
2940 | 2860 | - | ||
2941 | 2861 | - readSource := c.sr.r | ||
2942 | 2862 | - c.sr.Lock() | ||
2943 | 2863 | - c.sr.r = pr | ||
2944 | 2864 | - c.sr.Unlock() | ||
2945 | 2865 | - go func() { | ||
2946 | 2866 | - _, err := io.Copy(pw, readSource) | ||
2947 | 2867 | - if err == nil { | ||
2948 | 2868 | - err = io.EOF | ||
2949 | 2869 | - } | ||
2950 | 2870 | - pw.CloseWithError(err) | ||
2951 | 2871 | - c.noteClientGone() | ||
2952 | 2872 | - }() | ||
2953 | 2873 | - } | ||
2954 | 2874 | - return c.closeNotifyc | ||
2955 | 2875 | -} | ||
2956 | 2876 | - | ||
2957 | 2877 | -func (c *conn) noteClientGone() { | ||
2958 | 2878 | - c.mu.Lock() | ||
2959 | 2879 | - defer c.mu.Unlock() | ||
2960 | 2880 | - if c.closeNotifyc != nil && !c.clientGone { | ||
2961 | 2881 | - c.closeNotifyc <- true | ||
2962 | 2882 | - } | ||
2963 | 2883 | - c.clientGone = true | ||
2964 | 2884 | -} | ||
2965 | 2885 | - | ||
2966 | 2886 | -// A switchReader can have its Reader changed at runtime. | ||
2967 | 2887 | -// It's not safe for concurrent Reads and switches. | ||
2968 | 2888 | -type switchReader struct { | ||
2969 | 2889 | - io.Reader | ||
2970 | 2890 | -} | ||
2971 | 2891 | - | ||
2972 | 2892 | -// A switchWriter can have its Writer changed at runtime. | ||
2973 | 2893 | -// It's not safe for concurrent Writes and switches. | ||
2974 | 2894 | -type switchWriter struct { | ||
2975 | 2895 | - io.Writer | ||
2976 | 2896 | -} | ||
2977 | 2897 | - | ||
2978 | 2898 | -// A liveSwitchReader is a switchReader that's safe for concurrent | ||
2979 | 2899 | -// reads and switches, if its mutex is held. | ||
2980 | 2900 | -type liveSwitchReader struct { | ||
2981 | 2901 | - sync.Mutex | ||
2982 | 2902 | - r io.Reader | ||
2983 | 2903 | -} | ||
2984 | 2904 | - | ||
2985 | 2905 | -func (sr *liveSwitchReader) Read(p []byte) (n int, err error) { | ||
2986 | 2906 | - sr.Lock() | ||
2987 | 2907 | - r := sr.r | ||
2988 | 2908 | - sr.Unlock() | ||
2989 | 2909 | - return r.Read(p) | ||
2990 | 2910 | -} | ||
2991 | 2911 | - | ||
2992 | 2912 | -// This should be >= 512 bytes for DetectContentType, | ||
2993 | 2913 | -// but otherwise it's somewhat arbitrary. | ||
2994 | 2914 | -const bufferBeforeChunkingSize = 2048 | ||
2995 | 2915 | - | ||
2996 | 2916 | -// chunkWriter writes to a response's conn buffer, and is the writer | ||
2997 | 2917 | -// wrapped by the response.bufw buffered writer. | ||
2998 | 2918 | -// | ||
2999 | 2919 | -// chunkWriter also is responsible for finalizing the Header, including | ||
3000 | 2920 | -// conditionally setting the Content-Type and setting a Content-Length | ||
3001 | 2921 | -// in cases where the handler's final output is smaller than the buffer | ||
3002 | 2922 | -// size. It also conditionally adds chunk headers, when in chunking mode. | ||
3003 | 2923 | -// | ||
3004 | 2924 | -// See the comment above (*response).Write for the entire write flow. | ||
3005 | 2925 | -type chunkWriter struct { | ||
3006 | 2926 | - res *response | ||
3007 | 2927 | - | ||
3008 | 2928 | - // header is either nil or a deep clone of res.handlerHeader | ||
3009 | 2929 | - // at the time of res.WriteHeader, if res.WriteHeader is | ||
3010 | 2930 | - // called and extra buffering is being done to calculate | ||
3011 | 2931 | - // Content-Type and/or Content-Length. | ||
3012 | 2932 | - header Header | ||
3013 | 2933 | - | ||
3014 | 2934 | - // wroteHeader tells whether the header's been written to "the | ||
3015 | 2935 | - // wire" (or rather: w.conn.buf). this is unlike | ||
3016 | 2936 | - // (*response).wroteHeader, which tells only whether it was | ||
3017 | 2937 | - // logically written. | ||
3018 | 2938 | - wroteHeader bool | ||
3019 | 2939 | - | ||
3020 | 2940 | - // set by the writeHeader method: | ||
3021 | 2941 | - chunking bool // using chunked transfer encoding for reply body | ||
3022 | 2942 | -} | ||
3023 | 2943 | - | ||
3024 | 2944 | -var ( | ||
3025 | 2945 | - crlf = []byte("\r\n") | ||
3026 | 2946 | - colonSpace = []byte(": ") | ||
3027 | 2947 | -) | ||
3028 | 2948 | - | ||
3029 | 2949 | -func (cw *chunkWriter) Write(p []byte) (n int, err error) { | ||
3030 | 2950 | - if !cw.wroteHeader { | ||
3031 | 2951 | - cw.writeHeader(p) | ||
3032 | 2952 | - } | ||
3033 | 2953 | - if cw.res.req.Method == "HEAD" { | ||
3034 | 2954 | - // Eat writes. | ||
3035 | 2955 | - return len(p), nil | ||
3036 | 2956 | - } | ||
3037 | 2957 | - if cw.chunking { | ||
3038 | 2958 | - _, err = fmt.Fprintf(cw.res.conn.buf, "%x\r\n", len(p)) | ||
3039 | 2959 | - if err != nil { | ||
3040 | 2960 | - cw.res.conn.rwc.Close() | ||
3041 | 2961 | - return | ||
3042 | 2962 | - } | ||
3043 | 2963 | - } | ||
3044 | 2964 | - n, err = cw.res.conn.buf.Write(p) | ||
3045 | 2965 | - if cw.chunking && err == nil { | ||
3046 | 2966 | - _, err = cw.res.conn.buf.Write(crlf) | ||
3047 | 2967 | - } | ||
3048 | 2968 | - if err != nil { | ||
3049 | 2969 | - cw.res.conn.rwc.Close() | ||
3050 | 2970 | - } | ||
3051 | 2971 | - return | ||
3052 | 2972 | -} | ||
3053 | 2973 | - | ||
3054 | 2974 | -func (cw *chunkWriter) flush() { | ||
3055 | 2975 | - if !cw.wroteHeader { | ||
3056 | 2976 | - cw.writeHeader(nil) | ||
3057 | 2977 | - } | ||
3058 | 2978 | - cw.res.conn.buf.Flush() | ||
3059 | 2979 | -} | ||
3060 | 2980 | - | ||
3061 | 2981 | -func (cw *chunkWriter) close() { | ||
3062 | 2982 | - if !cw.wroteHeader { | ||
3063 | 2983 | - cw.writeHeader(nil) | ||
3064 | 2984 | - } | ||
3065 | 2985 | - if cw.chunking { | ||
3066 | 2986 | - // zero EOF chunk, trailer key/value pairs (currently | ||
3067 | 2987 | - // unsupported in Go's server), followed by a blank | ||
3068 | 2988 | - // line. | ||
3069 | 2989 | - cw.res.conn.buf.WriteString("0\r\n\r\n") | ||
3070 | 2990 | - } | ||
3071 | 2991 | -} | ||
3072 | 2992 | - | ||
3073 | 2993 | -// A response represents the server side of an HTTP response. | ||
3074 | 2994 | -type response struct { | ||
3075 | 2995 | - conn *conn | ||
3076 | 2996 | - req *Request // request for this response | ||
3077 | 2997 | - wroteHeader bool // reply header has been (logically) written | ||
3078 | 2998 | - wroteContinue bool // 100 Continue response was written | ||
3079 | 2999 | - | ||
3080 | 3000 | - w *bufio.Writer // buffers output in chunks to chunkWriter | ||
3081 | 3001 | - cw chunkWriter | ||
3082 | 3002 | - sw *switchWriter // of the bufio.Writer, for return to putBufioWriter | ||
3083 | 3003 | - | ||
3084 | 3004 | - // handlerHeader is the Header that Handlers get access to, | ||
3085 | 3005 | - // which may be retained and mutated even after WriteHeader. | ||
3086 | 3006 | - // handlerHeader is copied into cw.header at WriteHeader | ||
3087 | 3007 | - // time, and privately mutated thereafter. | ||
3088 | 3008 | - handlerHeader Header | ||
3089 | 3009 | - calledHeader bool // handler accessed handlerHeader via Header | ||
3090 | 3010 | - | ||
3091 | 3011 | - written int64 // number of bytes written in body | ||
3092 | 3012 | - contentLength int64 // explicitly-declared Content-Length; or -1 | ||
3093 | 3013 | - status int // status code passed to WriteHeader | ||
3094 | 3014 | - | ||
3095 | 3015 | - // close connection after this reply. set on request and | ||
3096 | 3016 | - // updated after response from handler if there's a | ||
3097 | 3017 | - // "Connection: keep-alive" response header and a | ||
3098 | 3018 | - // Content-Length. | ||
3099 | 3019 | - closeAfterReply bool | ||
3100 | 3020 | - | ||
3101 | 3021 | - // requestBodyLimitHit is set by requestTooLarge when | ||
3102 | 3022 | - // maxBytesReader hits its max size. It is checked in | ||
3103 | 3023 | - // WriteHeader, to make sure we don't consume the | ||
3104 | 3024 | - // remaining request body to try to advance to the next HTTP | ||
3105 | 3025 | - // request. Instead, when this is set, we stop reading | ||
3106 | 3026 | - // subsequent requests on this connection and stop reading | ||
3107 | 3027 | - // input from it. | ||
3108 | 3028 | - requestBodyLimitHit bool | ||
3109 | 3029 | - | ||
3110 | 3030 | - handlerDone bool // set true when the handler exits | ||
3111 | 3031 | - | ||
3112 | 3032 | - // Buffers for Date and Content-Length | ||
3113 | 3033 | - dateBuf [len(TimeFormat)]byte | ||
3114 | 3034 | - clenBuf [10]byte | ||
3115 | 3035 | -} | ||
3116 | 3036 | - | ||
3117 | 3037 | -// requestTooLarge is called by maxBytesReader when too much input has | ||
3118 | 3038 | -// been read from the client. | ||
3119 | 3039 | -func (w *response) requestTooLarge() { | ||
3120 | 3040 | - w.closeAfterReply = true | ||
3121 | 3041 | - w.requestBodyLimitHit = true | ||
3122 | 3042 | - if !w.wroteHeader { | ||
3123 | 3043 | - w.Header().Set("Connection", "close") | ||
3124 | 3044 | - } | ||
3125 | 3045 | -} | ||
3126 | 3046 | - | ||
3127 | 3047 | -// needsSniff reports whether a Content-Type still needs to be sniffed. | ||
3128 | 3048 | -func (w *response) needsSniff() bool { | ||
3129 | 3049 | - _, haveType := w.handlerHeader["Content-Type"] | ||
3130 | 3050 | - return !w.cw.wroteHeader && !haveType && w.written < sniffLen | ||
3131 | 3051 | -} | ||
3132 | 3052 | - | ||
3133 | 3053 | -// writerOnly hides an io.Writer value's optional ReadFrom method | ||
3134 | 3054 | -// from io.Copy. | ||
3135 | 3055 | -type writerOnly struct { | ||
3136 | 3056 | - io.Writer | ||
3137 | 3057 | -} | ||
3138 | 3058 | - | ||
3139 | 3059 | -func srcIsRegularFile(src io.Reader) (isRegular bool, err error) { | ||
3140 | 3060 | - switch v := src.(type) { | ||
3141 | 3061 | - case *os.File: | ||
3142 | 3062 | - fi, err := v.Stat() | ||
3143 | 3063 | - if err != nil { | ||
3144 | 3064 | - return false, err | ||
3145 | 3065 | - } | ||
3146 | 3066 | - return fi.Mode().IsRegular(), nil | ||
3147 | 3067 | - case *io.LimitedReader: | ||
3148 | 3068 | - return srcIsRegularFile(v.R) | ||
3149 | 3069 | - default: | ||
3150 | 3070 | - return | ||
3151 | 3071 | - } | ||
3152 | 3072 | -} | ||
3153 | 3073 | - | ||
3154 | 3074 | -// ReadFrom is here to optimize copying from an *os.File regular file | ||
3155 | 3075 | -// to a *net.TCPConn with sendfile. | ||
3156 | 3076 | -func (w *response) ReadFrom(src io.Reader) (n int64, err error) { | ||
3157 | 3077 | - // Our underlying w.conn.rwc is usually a *TCPConn (with its | ||
3158 | 3078 | - // own ReadFrom method). If not, or if our src isn't a regular | ||
3159 | 3079 | - // file, just fall back to the normal copy method. | ||
3160 | 3080 | - rf, ok := w.conn.rwc.(io.ReaderFrom) | ||
3161 | 3081 | - regFile, err := srcIsRegularFile(src) | ||
3162 | 3082 | - if err != nil { | ||
3163 | 3083 | - return 0, err | ||
3164 | 3084 | - } | ||
3165 | 3085 | - if !ok || !regFile { | ||
3166 | 3086 | - return io.Copy(writerOnly{w}, src) | ||
3167 | 3087 | - } | ||
3168 | 3088 | - | ||
3169 | 3089 | - // sendfile path: | ||
3170 | 3090 | - | ||
3171 | 3091 | - if !w.wroteHeader { | ||
3172 | 3092 | - w.WriteHeader(StatusOK) | ||
3173 | 3093 | - } | ||
3174 | 3094 | - | ||
3175 | 3095 | - if w.needsSniff() { | ||
3176 | 3096 | - n0, err := io.Copy(writerOnly{w}, io.LimitReader(src, sniffLen)) | ||
3177 | 3097 | - n += n0 | ||
3178 | 3098 | - if err != nil { | ||
3179 | 3099 | - return n, err | ||
3180 | 3100 | - } | ||
3181 | 3101 | - } | ||
3182 | 3102 | - | ||
3183 | 3103 | - w.w.Flush() // get rid of any previous writes | ||
3184 | 3104 | - w.cw.flush() // make sure Header is written; flush data to rwc | ||
3185 | 3105 | - | ||
3186 | 3106 | - // Now that cw has been flushed, its chunking field is guaranteed initialized. | ||
3187 | 3107 | - if !w.cw.chunking && w.bodyAllowed() { | ||
3188 | 3108 | - n0, err := rf.ReadFrom(src) | ||
3189 | 3109 | - n += n0 | ||
3190 | 3110 | - w.written += n0 | ||
3191 | 3111 | - return n, err | ||
3192 | 3112 | - } | ||
3193 | 3113 | - | ||
3194 | 3114 | - n0, err := io.Copy(writerOnly{w}, src) | ||
3195 | 3115 | - n += n0 | ||
3196 | 3116 | - return n, err | ||
3197 | 3117 | -} | ||
3198 | 3118 | - | ||
3199 | 3119 | -// noLimit is an effective infinite upper bound for io.LimitedReader | ||
3200 | 3120 | -const noLimit int64 = (1 << 63) - 1 | ||
3201 | 3121 | - | ||
3202 | 3122 | -// debugServerConnections controls whether all server connections are wrapped | ||
3203 | 3123 | -// with a verbose logging wrapper. | ||
3204 | 3124 | -const debugServerConnections = false | ||
3205 | 3125 | - | ||
3206 | 3126 | -// Create new connection from rwc. | ||
3207 | 3127 | -func (srv *Server) newConn(rwc net.Conn) (c *conn, err error) { | ||
3208 | 3128 | - c = new(conn) | ||
3209 | 3129 | - c.remoteAddr = rwc.RemoteAddr().String() | ||
3210 | 3130 | - c.server = srv | ||
3211 | 3131 | - c.rwc = rwc | ||
3212 | 3132 | - if debugServerConnections { | ||
3213 | 3133 | - c.rwc = newLoggingConn("server", c.rwc) | ||
3214 | 3134 | - } | ||
3215 | 3135 | - c.sr = liveSwitchReader{r: c.rwc} | ||
3216 | 3136 | - c.lr = io.LimitReader(&c.sr, noLimit).(*io.LimitedReader) | ||
3217 | 3137 | - br := newBufioReader(c.lr) | ||
3218 | 3138 | - bw := newBufioWriterSize(c.rwc, 4<<10) | ||
3219 | 3139 | - c.buf = bufio.NewReadWriter(br, bw) | ||
3220 | 3140 | - return c, nil | ||
3221 | 3141 | -} | ||
3222 | 3142 | - | ||
3223 | 3143 | -// TODO: use a sync.Cache instead | ||
3224 | 3144 | -var ( | ||
3225 | 3145 | - bufioReaderCache = make(chan *bufio.Reader, 4) | ||
3226 | 3146 | - bufioWriterCache2k = make(chan *bufio.Writer, 4) | ||
3227 | 3147 | - bufioWriterCache4k = make(chan *bufio.Writer, 4) | ||
3228 | 3148 | -) | ||
3229 | 3149 | - | ||
3230 | 3150 | -func bufioWriterCache(size int) chan *bufio.Writer { | ||
3231 | 3151 | - switch size { | ||
3232 | 3152 | - case 2 << 10: | ||
3233 | 3153 | - return bufioWriterCache2k | ||
3234 | 3154 | - case 4 << 10: | ||
3235 | 3155 | - return bufioWriterCache4k | ||
3236 | 3156 | - } | ||
3237 | 3157 | - return nil | ||
3238 | 3158 | -} | ||
3239 | 3159 | - | ||
3240 | 3160 | -func newBufioReader(r io.Reader) *bufio.Reader { | ||
3241 | 3161 | - select { | ||
3242 | 3162 | - case p := <-bufioReaderCache: | ||
3243 | 3163 | - p.Reset(r) | ||
3244 | 3164 | - return p | ||
3245 | 3165 | - default: | ||
3246 | 3166 | - return bufio.NewReader(r) | ||
3247 | 3167 | - } | ||
3248 | 3168 | -} | ||
3249 | 3169 | - | ||
3250 | 3170 | -func putBufioReader(br *bufio.Reader) { | ||
3251 | 3171 | - br.Reset(nil) | ||
3252 | 3172 | - select { | ||
3253 | 3173 | - case bufioReaderCache <- br: | ||
3254 | 3174 | - default: | ||
3255 | 3175 | - } | ||
3256 | 3176 | -} | ||
3257 | 3177 | - | ||
3258 | 3178 | -func newBufioWriterSize(w io.Writer, size int) *bufio.Writer { | ||
3259 | 3179 | - select { | ||
3260 | 3180 | - case p := <-bufioWriterCache(size): | ||
3261 | 3181 | - p.Reset(w) | ||
3262 | 3182 | - return p | ||
3263 | 3183 | - default: | ||
3264 | 3184 | - return bufio.NewWriterSize(w, size) | ||
3265 | 3185 | - } | ||
3266 | 3186 | -} | ||
3267 | 3187 | - | ||
3268 | 3188 | -func putBufioWriter(bw *bufio.Writer) { | ||
3269 | 3189 | - bw.Reset(nil) | ||
3270 | 3190 | - select { | ||
3271 | 3191 | - case bufioWriterCache(bw.Available()) <- bw: | ||
3272 | 3192 | - default: | ||
3273 | 3193 | - } | ||
3274 | 3194 | -} | ||
3275 | 3195 | - | ||
3276 | 3196 | -// DefaultMaxHeaderBytes is the maximum permitted size of the headers | ||
3277 | 3197 | -// in an HTTP request. | ||
3278 | 3198 | -// This can be overridden by setting Server.MaxHeaderBytes. | ||
3279 | 3199 | -const DefaultMaxHeaderBytes = 1 << 20 // 1 MB | ||
3280 | 3200 | - | ||
3281 | 3201 | -func (srv *Server) maxHeaderBytes() int { | ||
3282 | 3202 | - if srv.MaxHeaderBytes > 0 { | ||
3283 | 3203 | - return srv.MaxHeaderBytes | ||
3284 | 3204 | - } | ||
3285 | 3205 | - return DefaultMaxHeaderBytes | ||
3286 | 3206 | -} | ||
3287 | 3207 | - | ||
3288 | 3208 | -func (srv *Server) initialLimitedReaderSize() int64 { | ||
3289 | 3209 | - return int64(srv.maxHeaderBytes()) + 4096 // bufio slop | ||
3290 | 3210 | -} | ||
3291 | 3211 | - | ||
3292 | 3212 | -// wrapper around io.ReaderCloser which on first read, sends an | ||
3293 | 3213 | -// HTTP/1.1 100 Continue header | ||
3294 | 3214 | -type expectContinueReader struct { | ||
3295 | 3215 | - resp *response | ||
3296 | 3216 | - readCloser io.ReadCloser | ||
3297 | 3217 | - closed bool | ||
3298 | 3218 | -} | ||
3299 | 3219 | - | ||
3300 | 3220 | -func (ecr *expectContinueReader) Read(p []byte) (n int, err error) { | ||
3301 | 3221 | - if ecr.closed { | ||
3302 | 3222 | - return 0, ErrBodyReadAfterClose | ||
3303 | 3223 | - } | ||
3304 | 3224 | - if !ecr.resp.wroteContinue && !ecr.resp.conn.hijacked() { | ||
3305 | 3225 | - ecr.resp.wroteContinue = true | ||
3306 | 3226 | - ecr.resp.conn.buf.WriteString("HTTP/1.1 100 Continue\r\n\r\n") | ||
3307 | 3227 | - ecr.resp.conn.buf.Flush() | ||
3308 | 3228 | - } | ||
3309 | 3229 | - return ecr.readCloser.Read(p) | ||
3310 | 3230 | -} | ||
3311 | 3231 | - | ||
3312 | 3232 | -func (ecr *expectContinueReader) Close() error { | ||
3313 | 3233 | - ecr.closed = true | ||
3314 | 3234 | - return ecr.readCloser.Close() | ||
3315 | 3235 | -} | ||
3316 | 3236 | - | ||
3317 | 3237 | -// TimeFormat is the time format to use with | ||
3318 | 3238 | -// time.Parse and time.Time.Format when parsing | ||
3319 | 3239 | -// or generating times in HTTP headers. | ||
3320 | 3240 | -// It is like time.RFC1123 but hard codes GMT as the time zone. | ||
3321 | 3241 | -const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT" | ||
3322 | 3242 | - | ||
3323 | 3243 | -// appendTime is a non-allocating version of []byte(t.UTC().Format(TimeFormat)) | ||
3324 | 3244 | -func appendTime(b []byte, t time.Time) []byte { | ||
3325 | 3245 | - const days = "SunMonTueWedThuFriSat" | ||
3326 | 3246 | - const months = "JanFebMarAprMayJunJulAugSepOctNovDec" | ||
3327 | 3247 | - | ||
3328 | 3248 | - t = t.UTC() | ||
3329 | 3249 | - yy, mm, dd := t.Date() | ||
3330 | 3250 | - hh, mn, ss := t.Clock() | ||
3331 | 3251 | - day := days[3*t.Weekday():] | ||
3332 | 3252 | - mon := months[3*(mm-1):] | ||
3333 | 3253 | - | ||
3334 | 3254 | - return append(b, | ||
3335 | 3255 | - day[0], day[1], day[2], ',', ' ', | ||
3336 | 3256 | - byte('0'+dd/10), byte('0'+dd%10), ' ', | ||
3337 | 3257 | - mon[0], mon[1], mon[2], ' ', | ||
3338 | 3258 | - byte('0'+yy/1000), byte('0'+(yy/100)%10), byte('0'+(yy/10)%10), byte('0'+yy%10), ' ', | ||
3339 | 3259 | - byte('0'+hh/10), byte('0'+hh%10), ':', | ||
3340 | 3260 | - byte('0'+mn/10), byte('0'+mn%10), ':', | ||
3341 | 3261 | - byte('0'+ss/10), byte('0'+ss%10), ' ', | ||
3342 | 3262 | - 'G', 'M', 'T') | ||
3343 | 3263 | -} | ||
3344 | 3264 | - | ||
3345 | 3265 | -var errTooLarge = errors.New("http: request too large") | ||
3346 | 3266 | - | ||
3347 | 3267 | -// Read next request from connection. | ||
3348 | 3268 | -func (c *conn) readRequest() (w *response, err error) { | ||
3349 | 3269 | - if c.hijacked() { | ||
3350 | 3270 | - return nil, ErrHijacked | ||
3351 | 3271 | - } | ||
3352 | 3272 | - | ||
3353 | 3273 | - if d := c.server.ReadTimeout; d != 0 { | ||
3354 | 3274 | - c.rwc.SetReadDeadline(time.Now().Add(d)) | ||
3355 | 3275 | - } | ||
3356 | 3276 | - if d := c.server.WriteTimeout; d != 0 { | ||
3357 | 3277 | - defer func() { | ||
3358 | 3278 | - c.rwc.SetWriteDeadline(time.Now().Add(d)) | ||
3359 | 3279 | - }() | ||
3360 | 3280 | - } | ||
3361 | 3281 | - | ||
3362 | 3282 | - c.lr.N = c.server.initialLimitedReaderSize() | ||
3363 | 3283 | - var req *Request | ||
3364 | 3284 | - if req, err = ReadRequest(c.buf.Reader); err != nil { | ||
3365 | 3285 | - if c.lr.N == 0 { | ||
3366 | 3286 | - return nil, errTooLarge | ||
3367 | 3287 | - } | ||
3368 | 3288 | - return nil, err | ||
3369 | 3289 | - } | ||
3370 | 3290 | - c.lr.N = noLimit | ||
3371 | 3291 | - | ||
3372 | 3292 | - req.RemoteAddr = c.remoteAddr | ||
3373 | 3293 | - req.TLS = c.tlsState | ||
3374 | 3294 | - | ||
3375 | 3295 | - w = &response{ | ||
3376 | 3296 | - conn: c, | ||
3377 | 3297 | - req: req, | ||
3378 | 3298 | - handlerHeader: make(Header), | ||
3379 | 3299 | - contentLength: -1, | ||
3380 | 3300 | - } | ||
3381 | 3301 | - w.cw.res = w | ||
3382 | 3302 | - w.w = newBufioWriterSize(&w.cw, bufferBeforeChunkingSize) | ||
3383 | 3303 | - return w, nil | ||
3384 | 3304 | -} | ||
3385 | 3305 | - | ||
3386 | 3306 | -func (w *response) Header() Header { | ||
3387 | 3307 | - if w.cw.header == nil && w.wroteHeader && !w.cw.wroteHeader { | ||
3388 | 3308 | - // Accessing the header between logically writing it | ||
3389 | 3309 | - // and physically writing it means we need to allocate | ||
3390 | 3310 | - // a clone to snapshot the logically written state. | ||
3391 | 3311 | - w.cw.header = w.handlerHeader.clone() | ||
3392 | 3312 | - } | ||
3393 | 3313 | - w.calledHeader = true | ||
3394 | 3314 | - return w.handlerHeader | ||
3395 | 3315 | -} | ||
3396 | 3316 | - | ||
3397 | 3317 | -// maxPostHandlerReadBytes is the max number of Request.Body bytes not | ||
3398 | 3318 | -// consumed by a handler that the server will read from the client | ||
3399 | 3319 | -// in order to keep a connection alive. If there are more bytes than | ||
3400 | 3320 | -// this then the server to be paranoid instead sends a "Connection: | ||
3401 | 3321 | -// close" response. | ||
3402 | 3322 | -// | ||
3403 | 3323 | -// This number is approximately what a typical machine's TCP buffer | ||
3404 | 3324 | -// size is anyway. (if we have the bytes on the machine, we might as | ||
3405 | 3325 | -// well read them) | ||
3406 | 3326 | -const maxPostHandlerReadBytes = 256 << 10 | ||
3407 | 3327 | - | ||
3408 | 3328 | -func (w *response) WriteHeader(code int) { | ||
3409 | 3329 | - if w.conn.hijacked() { | ||
3410 | 3330 | - w.conn.server.logf("http: response.WriteHeader on hijacked connection") | ||
3411 | 3331 | - return | ||
3412 | 3332 | - } | ||
3413 | 3333 | - if w.wroteHeader { | ||
3414 | 3334 | - w.conn.server.logf("http: multiple response.WriteHeader calls") | ||
3415 | 3335 | - return | ||
3416 | 3336 | - } | ||
3417 | 3337 | - w.wroteHeader = true | ||
3418 | 3338 | - w.status = code | ||
3419 | 3339 | - | ||
3420 | 3340 | - if w.calledHeader && w.cw.header == nil { | ||
3421 | 3341 | - w.cw.header = w.handlerHeader.clone() | ||
3422 | 3342 | - } | ||
3423 | 3343 | - | ||
3424 | 3344 | - if cl := w.handlerHeader.get("Content-Length"); cl != "" { | ||
3425 | 3345 | - v, err := strconv.ParseInt(cl, 10, 64) | ||
3426 | 3346 | - if err == nil && v >= 0 { | ||
3427 | 3347 | - w.contentLength = v | ||
3428 | 3348 | - } else { | ||
3429 | 3349 | - w.conn.server.logf("http: invalid Content-Length of %q", cl) | ||
3430 | 3350 | - w.handlerHeader.Del("Content-Length") | ||
3431 | 3351 | - } | ||
3432 | 3352 | - } | ||
3433 | 3353 | -} | ||
3434 | 3354 | - | ||
3435 | 3355 | -// extraHeader is the set of headers sometimes added by chunkWriter.writeHeader. | ||
3436 | 3356 | -// This type is used to avoid extra allocations from cloning and/or populating | ||
3437 | 3357 | -// the response Header map and all its 1-element slices. | ||
3438 | 3358 | -type extraHeader struct { | ||
3439 | 3359 | - contentType string | ||
3440 | 3360 | - connection string | ||
3441 | 3361 | - transferEncoding string | ||
3442 | 3362 | - date []byte // written if not nil | ||
3443 | 3363 | - contentLength []byte // written if not nil | ||
3444 | 3364 | -} | ||
3445 | 3365 | - | ||
3446 | 3366 | -// Sorted the same as extraHeader.Write's loop. | ||
3447 | 3367 | -var extraHeaderKeys = [][]byte{ | ||
3448 | 3368 | - []byte("Content-Type"), | ||
3449 | 3369 | - []byte("Connection"), | ||
3450 | 3370 | - []byte("Transfer-Encoding"), | ||
3451 | 3371 | -} | ||
3452 | 3372 | - | ||
3453 | 3373 | -var ( | ||
3454 | 3374 | - headerContentLength = []byte("Content-Length: ") | ||
3455 | 3375 | - headerDate = []byte("Date: ") | ||
3456 | 3376 | -) | ||
3457 | 3377 | - | ||
3458 | 3378 | -// Write writes the headers described in h to w. | ||
3459 | 3379 | -// | ||
3460 | 3380 | -// This method has a value receiver, despite the somewhat large size | ||
3461 | 3381 | -// of h, because it prevents an allocation. The escape analysis isn't | ||
3462 | 3382 | -// smart enough to realize this function doesn't mutate h. | ||
3463 | 3383 | -func (h extraHeader) Write(w *bufio.Writer) { | ||
3464 | 3384 | - if h.date != nil { | ||
3465 | 3385 | - w.Write(headerDate) | ||
3466 | 3386 | - w.Write(h.date) | ||
3467 | 3387 | - w.Write(crlf) | ||
3468 | 3388 | - } | ||
3469 | 3389 | - if h.contentLength != nil { | ||
3470 | 3390 | - w.Write(headerContentLength) | ||
3471 | 3391 | - w.Write(h.contentLength) | ||
3472 | 3392 | - w.Write(crlf) | ||
3473 | 3393 | - } | ||
3474 | 3394 | - for i, v := range []string{h.contentType, h.connection, h.transferEncoding} { | ||
3475 | 3395 | - if v != "" { | ||
3476 | 3396 | - w.Write(extraHeaderKeys[i]) | ||
3477 | 3397 | - w.Write(colonSpace) | ||
3478 | 3398 | - w.WriteString(v) | ||
3479 | 3399 | - w.Write(crlf) | ||
3480 | 3400 | - } | ||
3481 | 3401 | - } | ||
3482 | 3402 | -} | ||
3483 | 3403 | - | ||
3484 | 3404 | -// writeHeader finalizes the header sent to the client and writes it | ||
3485 | 3405 | -// to cw.res.conn.buf. | ||
3486 | 3406 | -// | ||
3487 | 3407 | -// p is not written by writeHeader, but is the first chunk of the body | ||
3488 | 3408 | -// that will be written. It is sniffed for a Content-Type if none is | ||
3489 | 3409 | -// set explicitly. It's also used to set the Content-Length, if the | ||
3490 | 3410 | -// total body size was small and the handler has already finished | ||
3491 | 3411 | -// running. | ||
3492 | 3412 | -func (cw *chunkWriter) writeHeader(p []byte) { | ||
3493 | 3413 | - if cw.wroteHeader { | ||
3494 | 3414 | - return | ||
3495 | 3415 | - } | ||
3496 | 3416 | - cw.wroteHeader = true | ||
3497 | 3417 | - | ||
3498 | 3418 | - w := cw.res | ||
3499 | 3419 | - keepAlivesEnabled := w.conn.server.doKeepAlives() | ||
3500 | 3420 | - isHEAD := w.req.Method == "HEAD" | ||
3501 | 3421 | - | ||
3502 | 3422 | - // header is written out to w.conn.buf below. Depending on the | ||
3503 | 3423 | - // state of the handler, we either own the map or not. If we | ||
3504 | 3424 | - // don't own it, the exclude map is created lazily for | ||
3505 | 3425 | - // WriteSubset to remove headers. The setHeader struct holds | ||
3506 | 3426 | - // headers we need to add. | ||
3507 | 3427 | - header := cw.header | ||
3508 | 3428 | - owned := header != nil | ||
3509 | 3429 | - if !owned { | ||
3510 | 3430 | - header = w.handlerHeader | ||
3511 | 3431 | - } | ||
3512 | 3432 | - var excludeHeader map[string]bool | ||
3513 | 3433 | - delHeader := func(key string) { | ||
3514 | 3434 | - if owned { | ||
3515 | 3435 | - header.Del(key) | ||
3516 | 3436 | - return | ||
3517 | 3437 | - } | ||
3518 | 3438 | - if _, ok := header[key]; !ok { | ||
3519 | 3439 | - return | ||
3520 | 3440 | - } | ||
3521 | 3441 | - if excludeHeader == nil { | ||
3522 | 3442 | - excludeHeader = make(map[string]bool) | ||
3523 | 3443 | - } | ||
3524 | 3444 | - excludeHeader[key] = true | ||
3525 | 3445 | - } | ||
3526 | 3446 | - var setHeader extraHeader | ||
3527 | 3447 | - | ||
3528 | 3448 | - // If the handler is done but never sent a Content-Length | ||
3529 | 3449 | - // response header and this is our first (and last) write, set | ||
3530 | 3450 | - // it, even to zero. This helps HTTP/1.0 clients keep their | ||
3531 | 3451 | - // "keep-alive" connections alive. | ||
3532 | 3452 | - // Exceptions: 304/204/1xx responses never get Content-Length, and if | ||
3533 | 3453 | - // it was a HEAD request, we don't know the difference between | ||
3534 | 3454 | - // 0 actual bytes and 0 bytes because the handler noticed it | ||
3535 | 3455 | - // was a HEAD request and chose not to write anything. So for | ||
3536 | 3456 | - // HEAD, the handler should either write the Content-Length or | ||
3537 | 3457 | - // write non-zero bytes. If it's actually 0 bytes and the | ||
3538 | 3458 | - // handler never looked at the Request.Method, we just don't | ||
3539 | 3459 | - // send a Content-Length header. | ||
3540 | 3460 | - if w.handlerDone && bodyAllowedForStatus(w.status) && header.get("Content-Length") == "" && (!isHEAD || len(p) > 0) { | ||
3541 | 3461 | - w.contentLength = int64(len(p)) | ||
3542 | 3462 | - setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10) | ||
3543 | 3463 | - } | ||
3544 | 3464 | - | ||
3545 | 3465 | - // If this was an HTTP/1.0 request with keep-alive and we sent a | ||
3546 | 3466 | - // Content-Length back, we can make this a keep-alive response ... | ||
3547 | 3467 | - if w.req.wantsHttp10KeepAlive() && keepAlivesEnabled { | ||
3548 | 3468 | - sentLength := header.get("Content-Length") != "" | ||
3549 | 3469 | - if sentLength && header.get("Connection") == "keep-alive" { | ||
3550 | 3470 | - w.closeAfterReply = false | ||
3551 | 3471 | - } | ||
3552 | 3472 | - } | ||
3553 | 3473 | - | ||
3554 | 3474 | - // Check for a explicit (and valid) Content-Length header. | ||
3555 | 3475 | - hasCL := w.contentLength != -1 | ||
3556 | 3476 | - | ||
3557 | 3477 | - if w.req.wantsHttp10KeepAlive() && (isHEAD || hasCL) { | ||
3558 | 3478 | - _, connectionHeaderSet := header["Connection"] | ||
3559 | 3479 | - if !connectionHeaderSet { | ||
3560 | 3480 | - setHeader.connection = "keep-alive" | ||
3561 | 3481 | - } | ||
3562 | 3482 | - } else if !w.req.ProtoAtLeast(1, 1) || w.req.wantsClose() { | ||
3563 | 3483 | - w.closeAfterReply = true | ||
3564 | 3484 | - } | ||
3565 | 3485 | - | ||
3566 | 3486 | - if header.get("Connection") == "close" || !keepAlivesEnabled { | ||
3567 | 3487 | - w.closeAfterReply = true | ||
3568 | 3488 | - } | ||
3569 | 3489 | - | ||
3570 | 3490 | - // Per RFC 2616, we should consume the request body before | ||
3571 | 3491 | - // replying, if the handler hasn't already done so. But we | ||
3572 | 3492 | - // don't want to do an unbounded amount of reading here for | ||
3573 | 3493 | - // DoS reasons, so we only try up to a threshold. | ||
3574 | 3494 | - if w.req.ContentLength != 0 && !w.closeAfterReply { | ||
3575 | 3495 | - ecr, isExpecter := w.req.Body.(*expectContinueReader) | ||
3576 | 3496 | - if !isExpecter || ecr.resp.wroteContinue { | ||
3577 | 3497 | - n, _ := io.CopyN(ioutil.Discard, w.req.Body, maxPostHandlerReadBytes+1) | ||
3578 | 3498 | - if n >= maxPostHandlerReadBytes { | ||
3579 | 3499 | - w.requestTooLarge() | ||
3580 | 3500 | - delHeader("Connection") | ||
3581 | 3501 | - setHeader.connection = "close" | ||
3582 | 3502 | - } else { | ||
3583 | 3503 | - w.req.Body.Close() | ||
3584 | 3504 | - } | ||
3585 | 3505 | - } | ||
3586 | 3506 | - } | ||
3587 | 3507 | - | ||
3588 | 3508 | - code := w.status | ||
3589 | 3509 | - if !bodyAllowedForStatus(code) { | ||
3590 | 3510 | - // Must not have body. | ||
3591 | 3511 | - // RFC 2616 section 10.3.5: "the response MUST NOT include other entity-headers" | ||
3592 | 3512 | - for _, k := range []string{"Content-Type", "Content-Length", "Transfer-Encoding"} { | ||
3593 | 3513 | - delHeader(k) | ||
3594 | 3514 | - } | ||
3595 | 3515 | - } else { | ||
3596 | 3516 | - // If no content type, apply sniffing algorithm to body. | ||
3597 | 3517 | - _, haveType := header["Content-Type"] | ||
3598 | 3518 | - if !haveType { | ||
3599 | 3519 | - setHeader.contentType = DetectContentType(p) | ||
3600 | 3520 | - } | ||
3601 | 3521 | - } | ||
3602 | 3522 | - | ||
3603 | 3523 | - if _, ok := header["Date"]; !ok { | ||
3604 | 3524 | - setHeader.date = appendTime(cw.res.dateBuf[:0], time.Now()) | ||
3605 | 3525 | - } | ||
3606 | 3526 | - | ||
3607 | 3527 | - te := header.get("Transfer-Encoding") | ||
3608 | 3528 | - hasTE := te != "" | ||
3609 | 3529 | - if hasCL && hasTE && te != "identity" { | ||
3610 | 3530 | - // TODO: return an error if WriteHeader gets a return parameter | ||
3611 | 3531 | - // For now just ignore the Content-Length. | ||
3612 | 3532 | - w.conn.server.logf("http: WriteHeader called with both Transfer-Encoding of %q and a Content-Length of %d", | ||
3613 | 3533 | - te, w.contentLength) | ||
3614 | 3534 | - delHeader("Content-Length") | ||
3615 | 3535 | - hasCL = false | ||
3616 | 3536 | - } | ||
3617 | 3537 | - | ||
3618 | 3538 | - if w.req.Method == "HEAD" || !bodyAllowedForStatus(code) { | ||
3619 | 3539 | - // do nothing | ||
3620 | 3540 | - } else if code == StatusNoContent { | ||
3621 | 3541 | - delHeader("Transfer-Encoding") | ||
3622 | 3542 | - } else if hasCL { | ||
3623 | 3543 | - delHeader("Transfer-Encoding") | ||
3624 | 3544 | - } else if w.req.ProtoAtLeast(1, 1) { | ||
3625 | 3545 | - // HTTP/1.1 or greater: use chunked transfer encoding | ||
3626 | 3546 | - // to avoid closing the connection at EOF. | ||
3627 | 3547 | - // TODO: this blows away any custom or stacked Transfer-Encoding they | ||
3628 | 3548 | - // might have set. Deal with that as need arises once we have a valid | ||
3629 | 3549 | - // use case. | ||
3630 | 3550 | - cw.chunking = true | ||
3631 | 3551 | - setHeader.transferEncoding = "chunked" | ||
3632 | 3552 | - } else { | ||
3633 | 3553 | - // HTTP version < 1.1: cannot do chunked transfer | ||
3634 | 3554 | - // encoding and we don't know the Content-Length so | ||
3635 | 3555 | - // signal EOF by closing connection. | ||
3636 | 3556 | - w.closeAfterReply = true | ||
3637 | 3557 | - delHeader("Transfer-Encoding") // in case already set | ||
3638 | 3558 | - } | ||
3639 | 3559 | - | ||
3640 | 3560 | - // Cannot use Content-Length with non-identity Transfer-Encoding. | ||
3641 | 3561 | - if cw.chunking { | ||
3642 | 3562 | - delHeader("Content-Length") | ||
3643 | 3563 | - } | ||
3644 | 3564 | - if !w.req.ProtoAtLeast(1, 0) { | ||
3645 | 3565 | - return | ||
3646 | 3566 | - } | ||
3647 | 3567 | - | ||
3648 | 3568 | - if w.closeAfterReply && (!keepAlivesEnabled || !hasToken(cw.header.get("Connection"), "close")) { | ||
3649 | 3569 | - delHeader("Connection") | ||
3650 | 3570 | - if w.req.ProtoAtLeast(1, 1) { | ||
3651 | 3571 | - setHeader.connection = "close" | ||
3652 | 3572 | - } | ||
3653 | 3573 | - } | ||
3654 | 3574 | - | ||
3655 | 3575 | - w.conn.buf.WriteString(statusLine(w.req, code)) | ||
3656 | 3576 | - cw.header.WriteSubset(w.conn.buf, excludeHeader) | ||
3657 | 3577 | - setHeader.Write(w.conn.buf.Writer) | ||
3658 | 3578 | - w.conn.buf.Write(crlf) | ||
3659 | 3579 | -} | ||
3660 | 3580 | - | ||
3661 | 3581 | -// statusLines is a cache of Status-Line strings, keyed by code (for | ||
3662 | 3582 | -// HTTP/1.1) or negative code (for HTTP/1.0). This is faster than a | ||
3663 | 3583 | -// map keyed by struct of two fields. This map's max size is bounded | ||
3664 | 3584 | -// by 2*len(statusText), two protocol types for each known official | ||
3665 | 3585 | -// status code in the statusText map. | ||
3666 | 3586 | -var ( | ||
3667 | 3587 | - statusMu sync.RWMutex | ||
3668 | 3588 | - statusLines = make(map[int]string) | ||
3669 | 3589 | -) | ||
3670 | 3590 | - | ||
3671 | 3591 | -// statusLine returns a response Status-Line (RFC 2616 Section 6.1) | ||
3672 | 3592 | -// for the given request and response status code. | ||
3673 | 3593 | -func statusLine(req *Request, code int) string { | ||
3674 | 3594 | - // Fast path: | ||
3675 | 3595 | - key := code | ||
3676 | 3596 | - proto11 := req.ProtoAtLeast(1, 1) | ||
3677 | 3597 | - if !proto11 { | ||
3678 | 3598 | - key = -key | ||
3679 | 3599 | - } | ||
3680 | 3600 | - statusMu.RLock() | ||
3681 | 3601 | - line, ok := statusLines[key] | ||
3682 | 3602 | - statusMu.RUnlock() | ||
3683 | 3603 | - if ok { | ||
3684 | 3604 | - return line | ||
3685 | 3605 | - } | ||
3686 | 3606 | - | ||
3687 | 3607 | - // Slow path: | ||
3688 | 3608 | - proto := "HTTP/1.0" | ||
3689 | 3609 | - if proto11 { | ||
3690 | 3610 | - proto = "HTTP/1.1" | ||
3691 | 3611 | - } | ||
3692 | 3612 | - codestring := strconv.Itoa(code) | ||
3693 | 3613 | - text, ok := statusText[code] | ||
3694 | 3614 | - if !ok { | ||
3695 | 3615 | - text = "status code " + codestring | ||
3696 | 3616 | - } | ||
3697 | 3617 | - line = proto + " " + codestring + " " + text + "\r\n" | ||
3698 | 3618 | - if ok { | ||
3699 | 3619 | - statusMu.Lock() | ||
3700 | 3620 | - defer statusMu.Unlock() | ||
3701 | 3621 | - statusLines[key] = line | ||
3702 | 3622 | - } | ||
3703 | 3623 | - return line | ||
3704 | 3624 | -} | ||
3705 | 3625 | - | ||
3706 | 3626 | -// bodyAllowed returns true if a Write is allowed for this response type. | ||
3707 | 3627 | -// It's illegal to call this before the header has been flushed. | ||
3708 | 3628 | -func (w *response) bodyAllowed() bool { | ||
3709 | 3629 | - if !w.wroteHeader { | ||
3710 | 3630 | - panic("") | ||
3711 | 3631 | - } | ||
3712 | 3632 | - return bodyAllowedForStatus(w.status) | ||
3713 | 3633 | -} | ||
3714 | 3634 | - | ||
3715 | 3635 | -// The Life Of A Write is like this: | ||
3716 | 3636 | -// | ||
3717 | 3637 | -// Handler starts. No header has been sent. The handler can either | ||
3718 | 3638 | -// write a header, or just start writing. Writing before sending a header | ||
3719 | 3639 | -// sends an implicitly empty 200 OK header. | ||
3720 | 3640 | -// | ||
3721 | 3641 | -// If the handler didn't declare a Content-Length up front, we either | ||
3722 | 3642 | -// go into chunking mode or, if the handler finishes running before | ||
3723 | 3643 | -// the chunking buffer size, we compute a Content-Length and send that | ||
3724 | 3644 | -// in the header instead. | ||
3725 | 3645 | -// | ||
3726 | 3646 | -// Likewise, if the handler didn't set a Content-Type, we sniff that | ||
3727 | 3647 | -// from the initial chunk of output. | ||
3728 | 3648 | -// | ||
3729 | 3649 | -// The Writers are wired together like: | ||
3730 | 3650 | -// | ||
3731 | 3651 | -// 1. *response (the ResponseWriter) -> | ||
3732 | 3652 | -// 2. (*response).w, a *bufio.Writer of bufferBeforeChunkingSize bytes | ||
3733 | 3653 | -// 3. chunkWriter.Writer (whose writeHeader finalizes Content-Length/Type) | ||
3734 | 3654 | -// and which writes the chunk headers, if needed. | ||
3735 | 3655 | -// 4. conn.buf, a bufio.Writer of default (4kB) bytes | ||
3736 | 3656 | -// 5. the rwc, the net.Conn. | ||
3737 | 3657 | -// | ||
3738 | 3658 | -// TODO(bradfitz): short-circuit some of the buffering when the | ||
3739 | 3659 | -// initial header contains both a Content-Type and Content-Length. | ||
3740 | 3660 | -// Also short-circuit in (1) when the header's been sent and not in | ||
3741 | 3661 | -// chunking mode, writing directly to (4) instead, if (2) has no | ||
3742 | 3662 | -// buffered data. More generally, we could short-circuit from (1) to | ||
3743 | 3663 | -// (3) even in chunking mode if the write size from (1) is over some | ||
3744 | 3664 | -// threshold and nothing is in (2). The answer might be mostly making | ||
3745 | 3665 | -// bufferBeforeChunkingSize smaller and having bufio's fast-paths deal | ||
3746 | 3666 | -// with this instead. | ||
3747 | 3667 | -func (w *response) Write(data []byte) (n int, err error) { | ||
3748 | 3668 | - return w.write(len(data), data, "") | ||
3749 | 3669 | -} | ||
3750 | 3670 | - | ||
3751 | 3671 | -func (w *response) WriteString(data string) (n int, err error) { | ||
3752 | 3672 | - return w.write(len(data), nil, data) | ||
3753 | 3673 | -} | ||
3754 | 3674 | - | ||
3755 | 3675 | -// either dataB or dataS is non-zero. | ||
3756 | 3676 | -func (w *response) write(lenData int, dataB []byte, dataS string) (n int, err error) { | ||
3757 | 3677 | - if w.conn.hijacked() { | ||
3758 | 3678 | - w.conn.server.logf("http: response.Write on hijacked connection") | ||
3759 | 3679 | - return 0, ErrHijacked | ||
3760 | 3680 | - } | ||
3761 | 3681 | - if !w.wroteHeader { | ||
3762 | 3682 | - w.WriteHeader(StatusOK) | ||
3763 | 3683 | - } | ||
3764 | 3684 | - if lenData == 0 { | ||
3765 | 3685 | - return 0, nil | ||
3766 | 3686 | - } | ||
3767 | 3687 | - if !w.bodyAllowed() { | ||
3768 | 3688 | - return 0, ErrBodyNotAllowed | ||
3769 | 3689 | - } | ||
3770 | 3690 | - | ||
3771 | 3691 | - w.written += int64(lenData) // ignoring errors, for errorKludge | ||
3772 | 3692 | - if w.contentLength != -1 && w.written > w.contentLength { | ||
3773 | 3693 | - return 0, ErrContentLength | ||
3774 | 3694 | - } | ||
3775 | 3695 | - if dataB != nil { | ||
3776 | 3696 | - return w.w.Write(dataB) | ||
3777 | 3697 | - } else { | ||
3778 | 3698 | - return w.w.WriteString(dataS) | ||
3779 | 3699 | - } | ||
3780 | 3700 | -} | ||
3781 | 3701 | - | ||
3782 | 3702 | -func (w *response) finishRequest() { | ||
3783 | 3703 | - w.handlerDone = true | ||
3784 | 3704 | - | ||
3785 | 3705 | - if !w.wroteHeader { | ||
3786 | 3706 | - w.WriteHeader(StatusOK) | ||
3787 | 3707 | - } | ||
3788 | 3708 | - | ||
3789 | 3709 | - w.w.Flush() | ||
3790 | 3710 | - putBufioWriter(w.w) | ||
3791 | 3711 | - w.cw.close() | ||
3792 | 3712 | - w.conn.buf.Flush() | ||
3793 | 3713 | - | ||
3794 | 3714 | - // Close the body (regardless of w.closeAfterReply) so we can | ||
3795 | 3715 | - // re-use its bufio.Reader later safely. | ||
3796 | 3716 | - w.req.Body.Close() | ||
3797 | 3717 | - | ||
3798 | 3718 | - if w.req.MultipartForm != nil { | ||
3799 | 3719 | - w.req.MultipartForm.RemoveAll() | ||
3800 | 3720 | - } | ||
3801 | 3721 | - | ||
3802 | 3722 | - if w.req.Method != "HEAD" && w.contentLength != -1 && w.bodyAllowed() && w.contentLength != w.written { | ||
3803 | 3723 | - // Did not write enough. Avoid getting out of sync. | ||
3804 | 3724 | - w.closeAfterReply = true | ||
3805 | 3725 | - } | ||
3806 | 3726 | -} | ||
3807 | 3727 | - | ||
3808 | 3728 | -func (w *response) Flush() { | ||
3809 | 3729 | - if !w.wroteHeader { | ||
3810 | 3730 | - w.WriteHeader(StatusOK) | ||
3811 | 3731 | - } | ||
3812 | 3732 | - w.w.Flush() | ||
3813 | 3733 | - w.cw.flush() | ||
3814 | 3734 | -} | ||
3815 | 3735 | - | ||
3816 | 3736 | -func (c *conn) finalFlush() { | ||
3817 | 3737 | - if c.buf != nil { | ||
3818 | 3738 | - c.buf.Flush() | ||
3819 | 3739 | - | ||
3820 | 3740 | - // Steal the bufio.Reader (~4KB worth of memory) and its associated | ||
3821 | 3741 | - // reader for a future connection. | ||
3822 | 3742 | - putBufioReader(c.buf.Reader) | ||
3823 | 3743 | - | ||
3824 | 3744 | - // Steal the bufio.Writer (~4KB worth of memory) and its associated | ||
3825 | 3745 | - // writer for a future connection. | ||
3826 | 3746 | - putBufioWriter(c.buf.Writer) | ||
3827 | 3747 | - | ||
3828 | 3748 | - c.buf = nil | ||
3829 | 3749 | - } | ||
3830 | 3750 | -} | ||
3831 | 3751 | - | ||
3832 | 3752 | -// Close the connection. | ||
3833 | 3753 | -func (c *conn) close() { | ||
3834 | 3754 | - c.finalFlush() | ||
3835 | 3755 | - if c.rwc != nil { | ||
3836 | 3756 | - c.rwc.Close() | ||
3837 | 3757 | - c.rwc = nil | ||
3838 | 3758 | - } | ||
3839 | 3759 | -} | ||
3840 | 3760 | - | ||
3841 | 3761 | -// rstAvoidanceDelay is the amount of time we sleep after closing the | ||
3842 | 3762 | -// write side of a TCP connection before closing the entire socket. | ||
3843 | 3763 | -// By sleeping, we increase the chances that the client sees our FIN | ||
3844 | 3764 | -// and processes its final data before they process the subsequent RST | ||
3845 | 3765 | -// from closing a connection with known unread data. | ||
3846 | 3766 | -// This RST seems to occur mostly on BSD systems. (And Windows?) | ||
3847 | 3767 | -// This timeout is somewhat arbitrary (~latency around the planet). | ||
3848 | 3768 | -const rstAvoidanceDelay = 500 * time.Millisecond | ||
3849 | 3769 | - | ||
3850 | 3770 | -// closeWrite flushes any outstanding data and sends a FIN packet (if | ||
3851 | 3771 | -// client is connected via TCP), signalling that we're done. We then | ||
3852 | 3772 | -// pause for a bit, hoping the client processes it before `any | ||
3853 | 3773 | -// subsequent RST. | ||
3854 | 3774 | -// | ||
3855 | 3775 | -// See http://golang.org/issue/3595 | ||
3856 | 3776 | -func (c *conn) closeWriteAndWait() { | ||
3857 | 3777 | - c.finalFlush() | ||
3858 | 3778 | - if tcp, ok := c.rwc.(*net.TCPConn); ok { | ||
3859 | 3779 | - tcp.CloseWrite() | ||
3860 | 3780 | - } | ||
3861 | 3781 | - time.Sleep(rstAvoidanceDelay) | ||
3862 | 3782 | -} | ||
3863 | 3783 | - | ||
3864 | 3784 | -// validNPN reports whether the proto is not a blacklisted Next | ||
3865 | 3785 | -// Protocol Negotiation protocol. Empty and built-in protocol types | ||
3866 | 3786 | -// are blacklisted and can't be overridden with alternate | ||
3867 | 3787 | -// implementations. | ||
3868 | 3788 | -func validNPN(proto string) bool { | ||
3869 | 3789 | - switch proto { | ||
3870 | 3790 | - case "", "http/1.1", "http/1.0": | ||
3871 | 3791 | - return false | ||
3872 | 3792 | - } | ||
3873 | 3793 | - return true | ||
3874 | 3794 | -} | ||
3875 | 3795 | - | ||
3876 | 3796 | -func (c *conn) setState(nc net.Conn, state ConnState) { | ||
3877 | 3797 | - if hook := c.server.ConnState; hook != nil { | ||
3878 | 3798 | - hook(nc, state) | ||
3879 | 3799 | - } | ||
3880 | 3800 | -} | ||
3881 | 3801 | - | ||
3882 | 3802 | -// Serve a new connection. | ||
3883 | 3803 | -func (c *conn) serve() { | ||
3884 | 3804 | - origConn := c.rwc // copy it before it's set nil on Close or Hijack | ||
3885 | 3805 | - defer func() { | ||
3886 | 3806 | - if err := recover(); err != nil { | ||
3887 | 3807 | - const size = 64 << 10 | ||
3888 | 3808 | - buf := make([]byte, size) | ||
3889 | 3809 | - buf = buf[:runtime.Stack(buf, false)] | ||
3890 | 3810 | - c.server.logf("http: panic serving %v: %v\n%s", c.remoteAddr, err, buf) | ||
3891 | 3811 | - } | ||
3892 | 3812 | - if !c.hijacked() { | ||
3893 | 3813 | - c.close() | ||
3894 | 3814 | - c.setState(origConn, StateClosed) | ||
3895 | 3815 | - } | ||
3896 | 3816 | - }() | ||
3897 | 3817 | - | ||
3898 | 3818 | - if tlsConn, ok := c.rwc.(*tls.Conn); ok { | ||
3899 | 3819 | - if d := c.server.ReadTimeout; d != 0 { | ||
3900 | 3820 | - c.rwc.SetReadDeadline(time.Now().Add(d)) | ||
3901 | 3821 | - } | ||
3902 | 3822 | - if d := c.server.WriteTimeout; d != 0 { | ||
3903 | 3823 | - c.rwc.SetWriteDeadline(time.Now().Add(d)) | ||
3904 | 3824 | - } | ||
3905 | 3825 | - if err := tlsConn.Handshake(); err != nil { | ||
3906 | 3826 | - c.server.logf("http: TLS handshake error from %s: %v", c.rwc.RemoteAddr(), err) | ||
3907 | 3827 | - return | ||
3908 | 3828 | - } | ||
3909 | 3829 | - c.tlsState = new(tls.ConnectionState) | ||
3910 | 3830 | - *c.tlsState = tlsConn.ConnectionState() | ||
3911 | 3831 | - if proto := c.tlsState.NegotiatedProtocol; validNPN(proto) { | ||
3912 | 3832 | - if fn := c.server.TLSNextProto[proto]; fn != nil { | ||
3913 | 3833 | - h := initNPNRequest{tlsConn, serverHandler{c.server}} | ||
3914 | 3834 | - fn(c.server, tlsConn, h) | ||
3915 | 3835 | - } | ||
3916 | 3836 | - return | ||
3917 | 3837 | - } | ||
3918 | 3838 | - } | ||
3919 | 3839 | - | ||
3920 | 3840 | - for { | ||
3921 | 3841 | - w, err := c.readRequest() | ||
3922 | 3842 | - if c.lr.N != c.server.initialLimitedReaderSize() { | ||
3923 | 3843 | - // If we read any bytes off the wire, we're active. | ||
3924 | 3844 | - c.setState(c.rwc, StateActive) | ||
3925 | 3845 | - } | ||
3926 | 3846 | - if err != nil { | ||
3927 | 3847 | - if err == errTooLarge { | ||
3928 | 3848 | - // Their HTTP client may or may not be | ||
3929 | 3849 | - // able to read this if we're | ||
3930 | 3850 | - // responding to them and hanging up | ||
3931 | 3851 | - // while they're still writing their | ||
3932 | 3852 | - // request. Undefined behavior. | ||
3933 | 3853 | - io.WriteString(c.rwc, "HTTP/1.1 413 Request Entity Too Large\r\n\r\n") | ||
3934 | 3854 | - c.closeWriteAndWait() | ||
3935 | 3855 | - break | ||
3936 | 3856 | - } else if err == io.EOF { | ||
3937 | 3857 | - break // Don't reply | ||
3938 | 3858 | - } else if neterr, ok := err.(net.Error); ok && neterr.Timeout() { | ||
3939 | 3859 | - break // Don't reply | ||
3940 | 3860 | - } | ||
3941 | 3861 | - io.WriteString(c.rwc, "HTTP/1.1 400 Bad Request\r\n\r\n") | ||
3942 | 3862 | - break | ||
3943 | 3863 | - } | ||
3944 | 3864 | - | ||
3945 | 3865 | - // Expect 100 Continue support | ||
3946 | 3866 | - req := w.req | ||
3947 | 3867 | - if req.expectsContinue() { | ||
3948 | 3868 | - if req.ProtoAtLeast(1, 1) { | ||
3949 | 3869 | - // Wrap the Body reader with one that replies on the connection | ||
3950 | 3870 | - req.Body = &expectContinueReader{readCloser: req.Body, resp: w} | ||
3951 | 3871 | - } | ||
3952 | 3872 | - if req.ContentLength == 0 { | ||
3953 | 3873 | - w.Header().Set("Connection", "close") | ||
3954 | 3874 | - w.WriteHeader(StatusBadRequest) | ||
3955 | 3875 | - w.finishRequest() | ||
3956 | 3876 | - break | ||
3957 | 3877 | - } | ||
3958 | 3878 | - req.Header.Del("Expect") | ||
3959 | 3879 | - } else if req.Header.get("Expect") != "" { | ||
3960 | 3880 | - w.sendExpectationFailed() | ||
3961 | 3881 | - break | ||
3962 | 3882 | - } | ||
3963 | 3883 | - | ||
3964 | 3884 | - // HTTP cannot have multiple simultaneous active requests.[*] | ||
3965 | 3885 | - // Until the server replies to this request, it can't read another, | ||
3966 | 3886 | - // so we might as well run the handler in this goroutine. | ||
3967 | 3887 | - // [*] Not strictly true: HTTP pipelining. We could let them all process | ||
3968 | 3888 | - // in parallel even if their responses need to be serialized. | ||
3969 | 3889 | - serverHandler{c.server}.ServeHTTP(w, w.req) | ||
3970 | 3890 | - if c.hijacked() { | ||
3971 | 3891 | - return | ||
3972 | 3892 | - } | ||
3973 | 3893 | - w.finishRequest() | ||
3974 | 3894 | - if w.closeAfterReply { | ||
3975 | 3895 | - if w.requestBodyLimitHit { | ||
3976 | 3896 | - c.closeWriteAndWait() | ||
3977 | 3897 | - } | ||
3978 | 3898 | - break | ||
3979 | 3899 | - } | ||
3980 | 3900 | - c.setState(c.rwc, StateIdle) | ||
3981 | 3901 | - } | ||
3982 | 3902 | -} | ||
3983 | 3903 | - | ||
3984 | 3904 | -func (w *response) sendExpectationFailed() { | ||
3985 | 3905 | - // TODO(bradfitz): let ServeHTTP handlers handle | ||
3986 | 3906 | - // requests with non-standard expectation[s]? Seems | ||
3987 | 3907 | - // theoretical at best, and doesn't fit into the | ||
3988 | 3908 | - // current ServeHTTP model anyway. We'd need to | ||
3989 | 3909 | - // make the ResponseWriter an optional | ||
3990 | 3910 | - // "ExpectReplier" interface or something. | ||
3991 | 3911 | - // | ||
3992 | 3912 | - // For now we'll just obey RFC 2616 14.20 which says | ||
3993 | 3913 | - // "If a server receives a request containing an | ||
3994 | 3914 | - // Expect field that includes an expectation- | ||
3995 | 3915 | - // extension that it does not support, it MUST | ||
3996 | 3916 | - // respond with a 417 (Expectation Failed) status." | ||
3997 | 3917 | - w.Header().Set("Connection", "close") | ||
3998 | 3918 | - w.WriteHeader(StatusExpectationFailed) | ||
3999 | 3919 | - w.finishRequest() | ||
4000 | 3920 | -} | ||
4001 | 3921 | - | ||
4002 | 3922 | -// Hijack implements the Hijacker.Hijack method. Our response is both a ResponseWriter | ||
4003 | 3923 | -// and a Hijacker. | ||
4004 | 3924 | -func (w *response) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) { | ||
4005 | 3925 | - if w.wroteHeader { | ||
4006 | 3926 | - w.cw.flush() | ||
4007 | 3927 | - } | ||
4008 | 3928 | - // Release the bufioWriter that writes to the chunk writer, it is not | ||
4009 | 3929 | - // used after a connection has been hijacked. | ||
4010 | 3930 | - rwc, buf, err = w.conn.hijack() | ||
4011 | 3931 | - if err == nil { | ||
4012 | 3932 | - putBufioWriter(w.w) | ||
4013 | 3933 | - w.w = nil | ||
4014 | 3934 | - } | ||
4015 | 3935 | - return rwc, buf, err | ||
4016 | 3936 | -} | ||
4017 | 3937 | - | ||
4018 | 3938 | -func (w *response) CloseNotify() <-chan bool { | ||
4019 | 3939 | - return w.conn.closeNotify() | ||
4020 | 3940 | -} | ||
4021 | 3941 | - | ||
4022 | 3942 | -// The HandlerFunc type is an adapter to allow the use of | ||
4023 | 3943 | -// ordinary functions as HTTP handlers. If f is a function | ||
4024 | 3944 | -// with the appropriate signature, HandlerFunc(f) is a | ||
4025 | 3945 | -// Handler object that calls f. | ||
4026 | 3946 | -type HandlerFunc func(ResponseWriter, *Request) | ||
4027 | 3947 | - | ||
4028 | 3948 | -// ServeHTTP calls f(w, r). | ||
4029 | 3949 | -func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { | ||
4030 | 3950 | - f(w, r) | ||
4031 | 3951 | -} | ||
4032 | 3952 | - | ||
4033 | 3953 | -// Helper handlers | ||
4034 | 3954 | - | ||
4035 | 3955 | -// Error replies to the request with the specified error message and HTTP code. | ||
4036 | 3956 | -// The error message should be plain text. | ||
4037 | 3957 | -func Error(w ResponseWriter, error string, code int) { | ||
4038 | 3958 | - w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
4039 | 3959 | - w.WriteHeader(code) | ||
4040 | 3960 | - fmt.Fprintln(w, error) | ||
4041 | 3961 | -} | ||
4042 | 3962 | - | ||
4043 | 3963 | -// NotFound replies to the request with an HTTP 404 not found error. | ||
4044 | 3964 | -func NotFound(w ResponseWriter, r *Request) { Error(w, "404 page not found", StatusNotFound) } | ||
4045 | 3965 | - | ||
4046 | 3966 | -// NotFoundHandler returns a simple request handler | ||
4047 | 3967 | -// that replies to each request with a ``404 page not found'' reply. | ||
4048 | 3968 | -func NotFoundHandler() Handler { return HandlerFunc(NotFound) } | ||
4049 | 3969 | - | ||
4050 | 3970 | -// StripPrefix returns a handler that serves HTTP requests | ||
4051 | 3971 | -// by removing the given prefix from the request URL's Path | ||
4052 | 3972 | -// and invoking the handler h. StripPrefix handles a | ||
4053 | 3973 | -// request for a path that doesn't begin with prefix by | ||
4054 | 3974 | -// replying with an HTTP 404 not found error. | ||
4055 | 3975 | -func StripPrefix(prefix string, h Handler) Handler { | ||
4056 | 3976 | - if prefix == "" { | ||
4057 | 3977 | - return h | ||
4058 | 3978 | - } | ||
4059 | 3979 | - return HandlerFunc(func(w ResponseWriter, r *Request) { | ||
4060 | 3980 | - if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) { | ||
4061 | 3981 | - r.URL.Path = p | ||
4062 | 3982 | - h.ServeHTTP(w, r) | ||
4063 | 3983 | - } else { | ||
4064 | 3984 | - NotFound(w, r) | ||
4065 | 3985 | - } | ||
4066 | 3986 | - }) | ||
4067 | 3987 | -} | ||
4068 | 3988 | - | ||
4069 | 3989 | -// Redirect replies to the request with a redirect to url, | ||
4070 | 3990 | -// which may be a path relative to the request path. | ||
4071 | 3991 | -func Redirect(w ResponseWriter, r *Request, urlStr string, code int) { | ||
4072 | 3992 | - if u, err := url.Parse(urlStr); err == nil { | ||
4073 | 3993 | - // If url was relative, make absolute by | ||
4074 | 3994 | - // combining with request path. | ||
4075 | 3995 | - // The browser would probably do this for us, | ||
4076 | 3996 | - // but doing it ourselves is more reliable. | ||
4077 | 3997 | - | ||
4078 | 3998 | - // NOTE(rsc): RFC 2616 says that the Location | ||
4079 | 3999 | - // line must be an absolute URI, like | ||
4080 | 4000 | - // "http://www.google.com/redirect/", | ||
4081 | 4001 | - // not a path like "/redirect/". | ||
4082 | 4002 | - // Unfortunately, we don't know what to | ||
4083 | 4003 | - // put in the host name section to get the | ||
4084 | 4004 | - // client to connect to us again, so we can't | ||
4085 | 4005 | - // know the right absolute URI to send back. | ||
4086 | 4006 | - // Because of this problem, no one pays attention | ||
4087 | 4007 | - // to the RFC; they all send back just a new path. | ||
4088 | 4008 | - // So do we. | ||
4089 | 4009 | - oldpath := r.URL.Path | ||
4090 | 4010 | - if oldpath == "" { // should not happen, but avoid a crash if it does | ||
4091 | 4011 | - oldpath = "/" | ||
4092 | 4012 | - } | ||
4093 | 4013 | - if u.Scheme == "" { | ||
4094 | 4014 | - // no leading http://server | ||
4095 | 4015 | - if urlStr == "" || urlStr[0] != '/' { | ||
4096 | 4016 | - // make relative path absolute | ||
4097 | 4017 | - olddir, _ := path.Split(oldpath) | ||
4098 | 4018 | - urlStr = olddir + urlStr | ||
4099 | 4019 | - } | ||
4100 | 4020 | - | ||
4101 | 4021 | - var query string | ||
4102 | 4022 | - if i := strings.Index(urlStr, "?"); i != -1 { | ||
4103 | 4023 | - urlStr, query = urlStr[:i], urlStr[i:] | ||
4104 | 4024 | - } | ||
4105 | 4025 | - | ||
4106 | 4026 | - // clean up but preserve trailing slash | ||
4107 | 4027 | - trailing := strings.HasSuffix(urlStr, "/") | ||
4108 | 4028 | - urlStr = path.Clean(urlStr) | ||
4109 | 4029 | - if trailing && !strings.HasSuffix(urlStr, "/") { | ||
4110 | 4030 | - urlStr += "/" | ||
4111 | 4031 | - } | ||
4112 | 4032 | - urlStr += query | ||
4113 | 4033 | - } | ||
4114 | 4034 | - } | ||
4115 | 4035 | - | ||
4116 | 4036 | - w.Header().Set("Location", urlStr) | ||
4117 | 4037 | - w.WriteHeader(code) | ||
4118 | 4038 | - | ||
4119 | 4039 | - // RFC2616 recommends that a short note "SHOULD" be included in the | ||
4120 | 4040 | - // response because older user agents may not understand 301/307. | ||
4121 | 4041 | - // Shouldn't send the response for POST or HEAD; that leaves GET. | ||
4122 | 4042 | - if r.Method == "GET" { | ||
4123 | 4043 | - note := "<a href=\"" + htmlEscape(urlStr) + "\">" + statusText[code] + "</a>.\n" | ||
4124 | 4044 | - fmt.Fprintln(w, note) | ||
4125 | 4045 | - } | ||
4126 | 4046 | -} | ||
4127 | 4047 | - | ||
4128 | 4048 | -var htmlReplacer = strings.NewReplacer( | ||
4129 | 4049 | - "&", "&", | ||
4130 | 4050 | - "<", "<", | ||
4131 | 4051 | - ">", ">", | ||
4132 | 4052 | - // """ is shorter than """. | ||
4133 | 4053 | - `"`, """, | ||
4134 | 4054 | - // "'" is shorter than "'" and apos was not in HTML until HTML5. | ||
4135 | 4055 | - "'", "'", | ||
4136 | 4056 | -) | ||
4137 | 4057 | - | ||
4138 | 4058 | -func htmlEscape(s string) string { | ||
4139 | 4059 | - return htmlReplacer.Replace(s) | ||
4140 | 4060 | -} | ||
4141 | 4061 | - | ||
4142 | 4062 | -// Redirect to a fixed URL | ||
4143 | 4063 | -type redirectHandler struct { | ||
4144 | 4064 | - url string | ||
4145 | 4065 | - code int | ||
4146 | 4066 | -} | ||
4147 | 4067 | - | ||
4148 | 4068 | -func (rh *redirectHandler) ServeHTTP(w ResponseWriter, r *Request) { | ||
4149 | 4069 | - Redirect(w, r, rh.url, rh.code) | ||
4150 | 4070 | -} | ||
4151 | 4071 | - | ||
4152 | 4072 | -// RedirectHandler returns a request handler that redirects | ||
4153 | 4073 | -// each request it receives to the given url using the given | ||
4154 | 4074 | -// status code. | ||
4155 | 4075 | -func RedirectHandler(url string, code int) Handler { | ||
4156 | 4076 | - return &redirectHandler{url, code} | ||
4157 | 4077 | -} | ||
4158 | 4078 | - | ||
4159 | 4079 | -// ServeMux is an HTTP request multiplexer. | ||
4160 | 4080 | -// It matches the URL of each incoming request against a list of registered | ||
4161 | 4081 | -// patterns and calls the handler for the pattern that | ||
4162 | 4082 | -// most closely matches the URL. | ||
4163 | 4083 | -// | ||
4164 | 4084 | -// Patterns name fixed, rooted paths, like "/favicon.ico", | ||
4165 | 4085 | -// or rooted subtrees, like "/images/" (note the trailing slash). | ||
4166 | 4086 | -// Longer patterns take precedence over shorter ones, so that | ||
4167 | 4087 | -// if there are handlers registered for both "/images/" | ||
4168 | 4088 | -// and "/images/thumbnails/", the latter handler will be | ||
4169 | 4089 | -// called for paths beginning "/images/thumbnails/" and the | ||
4170 | 4090 | -// former will receive requests for any other paths in the | ||
4171 | 4091 | -// "/images/" subtree. | ||
4172 | 4092 | -// | ||
4173 | 4093 | -// Note that since a pattern ending in a slash names a rooted subtree, | ||
4174 | 4094 | -// the pattern "/" matches all paths not matched by other registered | ||
4175 | 4095 | -// patterns, not just the URL with Path == "/". | ||
4176 | 4096 | -// | ||
4177 | 4097 | -// Patterns may optionally begin with a host name, restricting matches to | ||
4178 | 4098 | -// URLs on that host only. Host-specific patterns take precedence over | ||
4179 | 4099 | -// general patterns, so that a handler might register for the two patterns | ||
4180 | 4100 | -// "/codesearch" and "codesearch.google.com/" without also taking over | ||
4181 | 4101 | -// requests for "http://www.google.com/". | ||
4182 | 4102 | -// | ||
4183 | 4103 | -// ServeMux also takes care of sanitizing the URL request path, | ||
4184 | 4104 | -// redirecting any request containing . or .. elements to an | ||
4185 | 4105 | -// equivalent .- and ..-free URL. | ||
4186 | 4106 | -type ServeMux struct { | ||
4187 | 4107 | - mu sync.RWMutex | ||
4188 | 4108 | - m map[string]muxEntry | ||
4189 | 4109 | - hosts bool // whether any patterns contain hostnames | ||
4190 | 4110 | -} | ||
4191 | 4111 | - | ||
4192 | 4112 | -type muxEntry struct { | ||
4193 | 4113 | - explicit bool | ||
4194 | 4114 | - h Handler | ||
4195 | 4115 | - pattern string | ||
4196 | 4116 | -} | ||
4197 | 4117 | - | ||
4198 | 4118 | -// NewServeMux allocates and returns a new ServeMux. | ||
4199 | 4119 | -func NewServeMux() *ServeMux { return &ServeMux{m: make(map[string]muxEntry)} } | ||
4200 | 4120 | - | ||
4201 | 4121 | -// DefaultServeMux is the default ServeMux used by Serve. | ||
4202 | 4122 | -var DefaultServeMux = NewServeMux() | ||
4203 | 4123 | - | ||
4204 | 4124 | -// Does path match pattern? | ||
4205 | 4125 | -func pathMatch(pattern, path string) bool { | ||
4206 | 4126 | - if len(pattern) == 0 { | ||
4207 | 4127 | - // should not happen | ||
4208 | 4128 | - return false | ||
4209 | 4129 | - } | ||
4210 | 4130 | - n := len(pattern) | ||
4211 | 4131 | - if pattern[n-1] != '/' { | ||
4212 | 4132 | - return pattern == path | ||
4213 | 4133 | - } | ||
4214 | 4134 | - return len(path) >= n && path[0:n] == pattern | ||
4215 | 4135 | -} | ||
4216 | 4136 | - | ||
4217 | 4137 | -// Return the canonical path for p, eliminating . and .. elements. | ||
4218 | 4138 | -func cleanPath(p string) string { | ||
4219 | 4139 | - if p == "" { | ||
4220 | 4140 | - return "/" | ||
4221 | 4141 | - } | ||
4222 | 4142 | - if p[0] != '/' { | ||
4223 | 4143 | - p = "/" + p | ||
4224 | 4144 | - } | ||
4225 | 4145 | - np := path.Clean(p) | ||
4226 | 4146 | - // path.Clean removes trailing slash except for root; | ||
4227 | 4147 | - // put the trailing slash back if necessary. | ||
4228 | 4148 | - if p[len(p)-1] == '/' && np != "/" { | ||
4229 | 4149 | - np += "/" | ||
4230 | 4150 | - } | ||
4231 | 4151 | - return np | ||
4232 | 4152 | -} | ||
4233 | 4153 | - | ||
4234 | 4154 | -// Find a handler on a handler map given a path string | ||
4235 | 4155 | -// Most-specific (longest) pattern wins | ||
4236 | 4156 | -func (mux *ServeMux) match(path string) (h Handler, pattern string) { | ||
4237 | 4157 | - var n = 0 | ||
4238 | 4158 | - for k, v := range mux.m { | ||
4239 | 4159 | - if !pathMatch(k, path) { | ||
4240 | 4160 | - continue | ||
4241 | 4161 | - } | ||
4242 | 4162 | - if h == nil || len(k) > n { | ||
4243 | 4163 | - n = len(k) | ||
4244 | 4164 | - h = v.h | ||
4245 | 4165 | - pattern = v.pattern | ||
4246 | 4166 | - } | ||
4247 | 4167 | - } | ||
4248 | 4168 | - return | ||
4249 | 4169 | -} | ||
4250 | 4170 | - | ||
4251 | 4171 | -// Handler returns the handler to use for the given request, | ||
4252 | 4172 | -// consulting r.Method, r.Host, and r.URL.Path. It always returns | ||
4253 | 4173 | -// a non-nil handler. If the path is not in its canonical form, the | ||
4254 | 4174 | -// handler will be an internally-generated handler that redirects | ||
4255 | 4175 | -// to the canonical path. | ||
4256 | 4176 | -// | ||
4257 | 4177 | -// Handler also returns the registered pattern that matches the | ||
4258 | 4178 | -// request or, in the case of internally-generated redirects, | ||
4259 | 4179 | -// the pattern that will match after following the redirect. | ||
4260 | 4180 | -// | ||
4261 | 4181 | -// If there is no registered handler that applies to the request, | ||
4262 | 4182 | -// Handler returns a ``page not found'' handler and an empty pattern. | ||
4263 | 4183 | -func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) { | ||
4264 | 4184 | - if r.Method != "CONNECT" { | ||
4265 | 4185 | - if p := cleanPath(r.URL.Path); p != r.URL.Path { | ||
4266 | 4186 | - _, pattern = mux.handler(r.Host, p) | ||
4267 | 4187 | - url := *r.URL | ||
4268 | 4188 | - url.Path = p | ||
4269 | 4189 | - return RedirectHandler(url.String(), StatusMovedPermanently), pattern | ||
4270 | 4190 | - } | ||
4271 | 4191 | - } | ||
4272 | 4192 | - | ||
4273 | 4193 | - return mux.handler(r.Host, r.URL.Path) | ||
4274 | 4194 | -} | ||
4275 | 4195 | - | ||
4276 | 4196 | -// handler is the main implementation of Handler. | ||
4277 | 4197 | -// The path is known to be in canonical form, except for CONNECT methods. | ||
4278 | 4198 | -func (mux *ServeMux) handler(host, path string) (h Handler, pattern string) { | ||
4279 | 4199 | - mux.mu.RLock() | ||
4280 | 4200 | - defer mux.mu.RUnlock() | ||
4281 | 4201 | - | ||
4282 | 4202 | - // Host-specific pattern takes precedence over generic ones | ||
4283 | 4203 | - if mux.hosts { | ||
4284 | 4204 | - h, pattern = mux.match(host + path) | ||
4285 | 4205 | - } | ||
4286 | 4206 | - if h == nil { | ||
4287 | 4207 | - h, pattern = mux.match(path) | ||
4288 | 4208 | - } | ||
4289 | 4209 | - if h == nil { | ||
4290 | 4210 | - h, pattern = NotFoundHandler(), "" | ||
4291 | 4211 | - } | ||
4292 | 4212 | - return | ||
4293 | 4213 | -} | ||
4294 | 4214 | - | ||
4295 | 4215 | -// ServeHTTP dispatches the request to the handler whose | ||
4296 | 4216 | -// pattern most closely matches the request URL. | ||
4297 | 4217 | -func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) { | ||
4298 | 4218 | - if r.RequestURI == "*" { | ||
4299 | 4219 | - if r.ProtoAtLeast(1, 1) { | ||
4300 | 4220 | - w.Header().Set("Connection", "close") | ||
4301 | 4221 | - } | ||
4302 | 4222 | - w.WriteHeader(StatusBadRequest) | ||
4303 | 4223 | - return | ||
4304 | 4224 | - } | ||
4305 | 4225 | - h, _ := mux.Handler(r) | ||
4306 | 4226 | - h.ServeHTTP(w, r) | ||
4307 | 4227 | -} | ||
4308 | 4228 | - | ||
4309 | 4229 | -// Handle registers the handler for the given pattern. | ||
4310 | 4230 | -// If a handler already exists for pattern, Handle panics. | ||
4311 | 4231 | -func (mux *ServeMux) Handle(pattern string, handler Handler) { | ||
4312 | 4232 | - mux.mu.Lock() | ||
4313 | 4233 | - defer mux.mu.Unlock() | ||
4314 | 4234 | - | ||
4315 | 4235 | - if pattern == "" { | ||
4316 | 4236 | - panic("http: invalid pattern " + pattern) | ||
4317 | 4237 | - } | ||
4318 | 4238 | - if handler == nil { | ||
4319 | 4239 | - panic("http: nil handler") | ||
4320 | 4240 | - } | ||
4321 | 4241 | - if mux.m[pattern].explicit { | ||
4322 | 4242 | - panic("http: multiple registrations for " + pattern) | ||
4323 | 4243 | - } | ||
4324 | 4244 | - | ||
4325 | 4245 | - mux.m[pattern] = muxEntry{explicit: true, h: handler, pattern: pattern} | ||
4326 | 4246 | - | ||
4327 | 4247 | - if pattern[0] != '/' { | ||
4328 | 4248 | - mux.hosts = true | ||
4329 | 4249 | - } | ||
4330 | 4250 | - | ||
4331 | 4251 | - // Helpful behavior: | ||
4332 | 4252 | - // If pattern is /tree/, insert an implicit permanent redirect for /tree. | ||
4333 | 4253 | - // It can be overridden by an explicit registration. | ||
4334 | 4254 | - n := len(pattern) | ||
4335 | 4255 | - if n > 0 && pattern[n-1] == '/' && !mux.m[pattern[0:n-1]].explicit { | ||
4336 | 4256 | - // If pattern contains a host name, strip it and use remaining | ||
4337 | 4257 | - // path for redirect. | ||
4338 | 4258 | - path := pattern | ||
4339 | 4259 | - if pattern[0] != '/' { | ||
4340 | 4260 | - // In pattern, at least the last character is a '/', so | ||
4341 | 4261 | - // strings.Index can't be -1. | ||
4342 | 4262 | - path = pattern[strings.Index(pattern, "/"):] | ||
4343 | 4263 | - } | ||
4344 | 4264 | - mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(path, StatusMovedPermanently), pattern: pattern} | ||
4345 | 4265 | - } | ||
4346 | 4266 | -} | ||
4347 | 4267 | - | ||
4348 | 4268 | -// HandleFunc registers the handler function for the given pattern. | ||
4349 | 4269 | -func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { | ||
4350 | 4270 | - mux.Handle(pattern, HandlerFunc(handler)) | ||
4351 | 4271 | -} | ||
4352 | 4272 | - | ||
4353 | 4273 | -// Handle registers the handler for the given pattern | ||
4354 | 4274 | -// in the DefaultServeMux. | ||
4355 | 4275 | -// The documentation for ServeMux explains how patterns are matched. | ||
4356 | 4276 | -func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) } | ||
4357 | 4277 | - | ||
4358 | 4278 | -// HandleFunc registers the handler function for the given pattern | ||
4359 | 4279 | -// in the DefaultServeMux. | ||
4360 | 4280 | -// The documentation for ServeMux explains how patterns are matched. | ||
4361 | 4281 | -func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { | ||
4362 | 4282 | - DefaultServeMux.HandleFunc(pattern, handler) | ||
4363 | 4283 | -} | ||
4364 | 4284 | - | ||
4365 | 4285 | -// Serve accepts incoming HTTP connections on the listener l, | ||
4366 | 4286 | -// creating a new service goroutine for each. The service goroutines | ||
4367 | 4287 | -// read requests and then call handler to reply to them. | ||
4368 | 4288 | -// Handler is typically nil, in which case the DefaultServeMux is used. | ||
4369 | 4289 | -func Serve(l net.Listener, handler Handler) error { | ||
4370 | 4290 | - srv := &Server{Handler: handler} | ||
4371 | 4291 | - return srv.Serve(l) | ||
4372 | 4292 | -} | ||
4373 | 4293 | - | ||
4374 | 4294 | -// A Server defines parameters for running an HTTP server. | ||
4375 | 4295 | -// The zero value for Server is a valid configuration. | ||
4376 | 4296 | -type Server struct { | ||
4377 | 4297 | - Addr string // TCP address to listen on, ":http" if empty | ||
4378 | 4298 | - Handler Handler // handler to invoke, http.DefaultServeMux if nil | ||
4379 | 4299 | - ReadTimeout time.Duration // maximum duration before timing out read of the request | ||
4380 | 4300 | - WriteTimeout time.Duration // maximum duration before timing out write of the response | ||
4381 | 4301 | - MaxHeaderBytes int // maximum size of request headers, DefaultMaxHeaderBytes if 0 | ||
4382 | 4302 | - TLSConfig *tls.Config // optional TLS config, used by ListenAndServeTLS | ||
4383 | 4303 | - | ||
4384 | 4304 | - // TLSNextProto optionally specifies a function to take over | ||
4385 | 4305 | - // ownership of the provided TLS connection when an NPN | ||
4386 | 4306 | - // protocol upgrade has occurred. The map key is the protocol | ||
4387 | 4307 | - // name negotiated. The Handler argument should be used to | ||
4388 | 4308 | - // handle HTTP requests and will initialize the Request's TLS | ||
4389 | 4309 | - // and RemoteAddr if not already set. The connection is | ||
4390 | 4310 | - // automatically closed when the function returns. | ||
4391 | 4311 | - TLSNextProto map[string]func(*Server, *tls.Conn, Handler) | ||
4392 | 4312 | - | ||
4393 | 4313 | - // ConnState specifies an optional callback function that is | ||
4394 | 4314 | - // called when a client connection changes state. See the | ||
4395 | 4315 | - // ConnState type and associated constants for details. | ||
4396 | 4316 | - ConnState func(net.Conn, ConnState) | ||
4397 | 4317 | - | ||
4398 | 4318 | - // ErrorLog specifies an optional logger for errors accepting | ||
4399 | 4319 | - // connections and unexpected behavior from handlers. | ||
4400 | 4320 | - // If nil, logging goes to os.Stderr via the log package's | ||
4401 | 4321 | - // standard logger. | ||
4402 | 4322 | - ErrorLog *log.Logger | ||
4403 | 4323 | - | ||
4404 | 4324 | - disableKeepAlives int32 // accessed atomically. | ||
4405 | 4325 | -} | ||
4406 | 4326 | - | ||
4407 | 4327 | -// A ConnState represents the state of a client connection to a server. | ||
4408 | 4328 | -// It's used by the optional Server.ConnState hook. | ||
4409 | 4329 | -type ConnState int | ||
4410 | 4330 | - | ||
4411 | 4331 | -const ( | ||
4412 | 4332 | - // StateNew represents a new connection that is expected to | ||
4413 | 4333 | - // send a request immediately. Connections begin at this | ||
4414 | 4334 | - // state and then transition to either StateActive or | ||
4415 | 4335 | - // StateClosed. | ||
4416 | 4336 | - StateNew ConnState = iota | ||
4417 | 4337 | - | ||
4418 | 4338 | - // StateActive represents a connection that has read 1 or more | ||
4419 | 4339 | - // bytes of a request. The Server.ConnState hook for | ||
4420 | 4340 | - // StateActive fires before the request has entered a handler | ||
4421 | 4341 | - // and doesn't fire again until the request has been | ||
4422 | 4342 | - // handled. After the request is handled, the state | ||
4423 | 4343 | - // transitions to StateClosed, StateHijacked, or StateIdle. | ||
4424 | 4344 | - StateActive | ||
4425 | 4345 | - | ||
4426 | 4346 | - // StateIdle represents a connection that has finished | ||
4427 | 4347 | - // handling a request and is in the keep-alive state, waiting | ||
4428 | 4348 | - // for a new request. Connections transition from StateIdle | ||
4429 | 4349 | - // to either StateActive or StateClosed. | ||
4430 | 4350 | - StateIdle | ||
4431 | 4351 | - | ||
4432 | 4352 | - // StateHijacked represents a hijacked connection. | ||
4433 | 4353 | - // This is a terminal state. It does not transition to StateClosed. | ||
4434 | 4354 | - StateHijacked | ||
4435 | 4355 | - | ||
4436 | 4356 | - // StateClosed represents a closed connection. | ||
4437 | 4357 | - // This is a terminal state. Hijacked connections do not | ||
4438 | 4358 | - // transition to StateClosed. | ||
4439 | 4359 | - StateClosed | ||
4440 | 4360 | -) | ||
4441 | 4361 | - | ||
4442 | 4362 | -var stateName = map[ConnState]string{ | ||
4443 | 4363 | - StateNew: "new", | ||
4444 | 4364 | - StateActive: "active", | ||
4445 | 4365 | - StateIdle: "idle", | ||
4446 | 4366 | - StateHijacked: "hijacked", | ||
4447 | 4367 | - StateClosed: "closed", | ||
4448 | 4368 | -} | ||
4449 | 4369 | - | ||
4450 | 4370 | -func (c ConnState) String() string { | ||
4451 | 4371 | - return stateName[c] | ||
4452 | 4372 | -} | ||
4453 | 4373 | - | ||
4454 | 4374 | -// serverHandler delegates to either the server's Handler or | ||
4455 | 4375 | -// DefaultServeMux and also handles "OPTIONS *" requests. | ||
4456 | 4376 | -type serverHandler struct { | ||
4457 | 4377 | - srv *Server | ||
4458 | 4378 | -} | ||
4459 | 4379 | - | ||
4460 | 4380 | -func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) { | ||
4461 | 4381 | - handler := sh.srv.Handler | ||
4462 | 4382 | - if handler == nil { | ||
4463 | 4383 | - handler = DefaultServeMux | ||
4464 | 4384 | - } | ||
4465 | 4385 | - if req.RequestURI == "*" && req.Method == "OPTIONS" { | ||
4466 | 4386 | - handler = globalOptionsHandler{} | ||
4467 | 4387 | - } | ||
4468 | 4388 | - handler.ServeHTTP(rw, req) | ||
4469 | 4389 | -} | ||
4470 | 4390 | - | ||
4471 | 4391 | -// ListenAndServe listens on the TCP network address srv.Addr and then | ||
4472 | 4392 | -// calls Serve to handle requests on incoming connections. If | ||
4473 | 4393 | -// srv.Addr is blank, ":http" is used. | ||
4474 | 4394 | -func (srv *Server) ListenAndServe() error { | ||
4475 | 4395 | - addr := srv.Addr | ||
4476 | 4396 | - if addr == "" { | ||
4477 | 4397 | - addr = ":http" | ||
4478 | 4398 | - } | ||
4479 | 4399 | - ln, err := net.Listen("tcp", addr) | ||
4480 | 4400 | - if err != nil { | ||
4481 | 4401 | - return err | ||
4482 | 4402 | - } | ||
4483 | 4403 | - return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}) | ||
4484 | 4404 | -} | ||
4485 | 4405 | - | ||
4486 | 4406 | -// Serve accepts incoming connections on the Listener l, creating a | ||
4487 | 4407 | -// new service goroutine for each. The service goroutines read requests and | ||
4488 | 4408 | -// then call srv.Handler to reply to them. | ||
4489 | 4409 | -func (srv *Server) Serve(l net.Listener) error { | ||
4490 | 4410 | - defer l.Close() | ||
4491 | 4411 | - var tempDelay time.Duration // how long to sleep on accept failure | ||
4492 | 4412 | - for { | ||
4493 | 4413 | - rw, e := l.Accept() | ||
4494 | 4414 | - if e != nil { | ||
4495 | 4415 | - if ne, ok := e.(net.Error); ok && ne.Temporary() { | ||
4496 | 4416 | - if tempDelay == 0 { | ||
4497 | 4417 | - tempDelay = 5 * time.Millisecond | ||
4498 | 4418 | - } else { | ||
4499 | 4419 | - tempDelay *= 2 | ||
4500 | 4420 | - } | ||
4501 | 4421 | - if max := 1 * time.Second; tempDelay > max { | ||
4502 | 4422 | - tempDelay = max | ||
4503 | 4423 | - } | ||
4504 | 4424 | - srv.logf("http: Accept error: %v; retrying in %v", e, tempDelay) | ||
4505 | 4425 | - time.Sleep(tempDelay) | ||
4506 | 4426 | - continue | ||
4507 | 4427 | - } | ||
4508 | 4428 | - return e | ||
4509 | 4429 | - } | ||
4510 | 4430 | - tempDelay = 0 | ||
4511 | 4431 | - c, err := srv.newConn(rw) | ||
4512 | 4432 | - if err != nil { | ||
4513 | 4433 | - continue | ||
4514 | 4434 | - } | ||
4515 | 4435 | - c.setState(c.rwc, StateNew) // before Serve can return | ||
4516 | 4436 | - go c.serve() | ||
4517 | 4437 | - } | ||
4518 | 4438 | -} | ||
4519 | 4439 | - | ||
4520 | 4440 | -func (s *Server) doKeepAlives() bool { | ||
4521 | 4441 | - return atomic.LoadInt32(&s.disableKeepAlives) == 0 | ||
4522 | 4442 | -} | ||
4523 | 4443 | - | ||
4524 | 4444 | -// SetKeepAlivesEnabled controls whether HTTP keep-alives are enabled. | ||
4525 | 4445 | -// By default, keep-alives are always enabled. Only very | ||
4526 | 4446 | -// resource-constrained environments or servers in the process of | ||
4527 | 4447 | -// shutting down should disable them. | ||
4528 | 4448 | -func (s *Server) SetKeepAlivesEnabled(v bool) { | ||
4529 | 4449 | - if v { | ||
4530 | 4450 | - atomic.StoreInt32(&s.disableKeepAlives, 0) | ||
4531 | 4451 | - } else { | ||
4532 | 4452 | - atomic.StoreInt32(&s.disableKeepAlives, 1) | ||
4533 | 4453 | - } | ||
4534 | 4454 | -} | ||
4535 | 4455 | - | ||
4536 | 4456 | -func (s *Server) logf(format string, args ...interface{}) { | ||
4537 | 4457 | - if s.ErrorLog != nil { | ||
4538 | 4458 | - s.ErrorLog.Printf(format, args...) | ||
4539 | 4459 | - } else { | ||
4540 | 4460 | - log.Printf(format, args...) | ||
4541 | 4461 | - } | ||
4542 | 4462 | -} | ||
4543 | 4463 | - | ||
4544 | 4464 | -// ListenAndServe listens on the TCP network address addr | ||
4545 | 4465 | -// and then calls Serve with handler to handle requests | ||
4546 | 4466 | -// on incoming connections. Handler is typically nil, | ||
4547 | 4467 | -// in which case the DefaultServeMux is used. | ||
4548 | 4468 | -// | ||
4549 | 4469 | -// A trivial example server is: | ||
4550 | 4470 | -// | ||
4551 | 4471 | -// package main | ||
4552 | 4472 | -// | ||
4553 | 4473 | -// import ( | ||
4554 | 4474 | -// "io" | ||
4555 | 4475 | -// "launchpad.net/ubuntu-push/http13client" | ||
4556 | 4476 | -// "log" | ||
4557 | 4477 | -// ) | ||
4558 | 4478 | -// | ||
4559 | 4479 | -// // hello world, the web server | ||
4560 | 4480 | -// func HelloServer(w http.ResponseWriter, req *http.Request) { | ||
4561 | 4481 | -// io.WriteString(w, "hello, world!\n") | ||
4562 | 4482 | -// } | ||
4563 | 4483 | -// | ||
4564 | 4484 | -// func main() { | ||
4565 | 4485 | -// http.HandleFunc("/hello", HelloServer) | ||
4566 | 4486 | -// err := http.ListenAndServe(":12345", nil) | ||
4567 | 4487 | -// if err != nil { | ||
4568 | 4488 | -// log.Fatal("ListenAndServe: ", err) | ||
4569 | 4489 | -// } | ||
4570 | 4490 | -// } | ||
4571 | 4491 | -func ListenAndServe(addr string, handler Handler) error { | ||
4572 | 4492 | - server := &Server{Addr: addr, Handler: handler} | ||
4573 | 4493 | - return server.ListenAndServe() | ||
4574 | 4494 | -} | ||
4575 | 4495 | - | ||
4576 | 4496 | -// ListenAndServeTLS acts identically to ListenAndServe, except that it | ||
4577 | 4497 | -// expects HTTPS connections. Additionally, files containing a certificate and | ||
4578 | 4498 | -// matching private key for the server must be provided. If the certificate | ||
4579 | 4499 | -// is signed by a certificate authority, the certFile should be the concatenation | ||
4580 | 4500 | -// of the server's certificate followed by the CA's certificate. | ||
4581 | 4501 | -// | ||
4582 | 4502 | -// A trivial example server is: | ||
4583 | 4503 | -// | ||
4584 | 4504 | -// import ( | ||
4585 | 4505 | -// "log" | ||
4586 | 4506 | -// "launchpad.net/ubuntu-push/http13client" | ||
4587 | 4507 | -// ) | ||
4588 | 4508 | -// | ||
4589 | 4509 | -// func handler(w http.ResponseWriter, req *http.Request) { | ||
4590 | 4510 | -// w.Header().Set("Content-Type", "text/plain") | ||
4591 | 4511 | -// w.Write([]byte("This is an example server.\n")) | ||
4592 | 4512 | -// } | ||
4593 | 4513 | -// | ||
4594 | 4514 | -// func main() { | ||
4595 | 4515 | -// http.HandleFunc("/", handler) | ||
4596 | 4516 | -// log.Printf("About to listen on 10443. Go to https://127.0.0.1:10443/") | ||
4597 | 4517 | -// err := http.ListenAndServeTLS(":10443", "cert.pem", "key.pem", nil) | ||
4598 | 4518 | -// if err != nil { | ||
4599 | 4519 | -// log.Fatal(err) | ||
4600 | 4520 | -// } | ||
4601 | 4521 | -// } | ||
4602 | 4522 | -// | ||
4603 | 4523 | -// One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem. | ||
4604 | 4524 | -func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Handler) error { | ||
4605 | 4525 | - server := &Server{Addr: addr, Handler: handler} | ||
4606 | 4526 | - return server.ListenAndServeTLS(certFile, keyFile) | ||
4607 | 4527 | -} | ||
4608 | 4528 | - | ||
4609 | 4529 | -// ListenAndServeTLS listens on the TCP network address srv.Addr and | ||
4610 | 4530 | -// then calls Serve to handle requests on incoming TLS connections. | ||
4611 | 4531 | -// | ||
4612 | 4532 | -// Filenames containing a certificate and matching private key for | ||
4613 | 4533 | -// the server must be provided. If the certificate is signed by a | ||
4614 | 4534 | -// certificate authority, the certFile should be the concatenation | ||
4615 | 4535 | -// of the server's certificate followed by the CA's certificate. | ||
4616 | 4536 | -// | ||
4617 | 4537 | -// If srv.Addr is blank, ":https" is used. | ||
4618 | 4538 | -func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error { | ||
4619 | 4539 | - addr := srv.Addr | ||
4620 | 4540 | - if addr == "" { | ||
4621 | 4541 | - addr = ":https" | ||
4622 | 4542 | - } | ||
4623 | 4543 | - config := &tls.Config{} | ||
4624 | 4544 | - if srv.TLSConfig != nil { | ||
4625 | 4545 | - *config = *srv.TLSConfig | ||
4626 | 4546 | - } | ||
4627 | 4547 | - if config.NextProtos == nil { | ||
4628 | 4548 | - config.NextProtos = []string{"http/1.1"} | ||
4629 | 4549 | - } | ||
4630 | 4550 | - | ||
4631 | 4551 | - var err error | ||
4632 | 4552 | - config.Certificates = make([]tls.Certificate, 1) | ||
4633 | 4553 | - config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile) | ||
4634 | 4554 | - if err != nil { | ||
4635 | 4555 | - return err | ||
4636 | 4556 | - } | ||
4637 | 4557 | - | ||
4638 | 4558 | - ln, err := net.Listen("tcp", addr) | ||
4639 | 4559 | - if err != nil { | ||
4640 | 4560 | - return err | ||
4641 | 4561 | - } | ||
4642 | 4562 | - | ||
4643 | 4563 | - tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, config) | ||
4644 | 4564 | - return srv.Serve(tlsListener) | ||
4645 | 4565 | -} | ||
4646 | 4566 | - | ||
4647 | 4567 | -// TimeoutHandler returns a Handler that runs h with the given time limit. | ||
4648 | 4568 | -// | ||
4649 | 4569 | -// The new Handler calls h.ServeHTTP to handle each request, but if a | ||
4650 | 4570 | -// call runs for longer than its time limit, the handler responds with | ||
4651 | 4571 | -// a 503 Service Unavailable error and the given message in its body. | ||
4652 | 4572 | -// (If msg is empty, a suitable default message will be sent.) | ||
4653 | 4573 | -// After such a timeout, writes by h to its ResponseWriter will return | ||
4654 | 4574 | -// ErrHandlerTimeout. | ||
4655 | 4575 | -func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler { | ||
4656 | 4576 | - f := func() <-chan time.Time { | ||
4657 | 4577 | - return time.After(dt) | ||
4658 | 4578 | - } | ||
4659 | 4579 | - return &timeoutHandler{h, f, msg} | ||
4660 | 4580 | -} | ||
4661 | 4581 | - | ||
4662 | 4582 | -// ErrHandlerTimeout is returned on ResponseWriter Write calls | ||
4663 | 4583 | -// in handlers which have timed out. | ||
4664 | 4584 | -var ErrHandlerTimeout = errors.New("http: Handler timeout") | ||
4665 | 4585 | - | ||
4666 | 4586 | -type timeoutHandler struct { | ||
4667 | 4587 | - handler Handler | ||
4668 | 4588 | - timeout func() <-chan time.Time // returns channel producing a timeout | ||
4669 | 4589 | - body string | ||
4670 | 4590 | -} | ||
4671 | 4591 | - | ||
4672 | 4592 | -func (h *timeoutHandler) errorBody() string { | ||
4673 | 4593 | - if h.body != "" { | ||
4674 | 4594 | - return h.body | ||
4675 | 4595 | - } | ||
4676 | 4596 | - return "<html><head><title>Timeout</title></head><body><h1>Timeout</h1></body></html>" | ||
4677 | 4597 | -} | ||
4678 | 4598 | - | ||
4679 | 4599 | -func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) { | ||
4680 | 4600 | - done := make(chan bool, 1) | ||
4681 | 4601 | - tw := &timeoutWriter{w: w} | ||
4682 | 4602 | - go func() { | ||
4683 | 4603 | - h.handler.ServeHTTP(tw, r) | ||
4684 | 4604 | - done <- true | ||
4685 | 4605 | - }() | ||
4686 | 4606 | - select { | ||
4687 | 4607 | - case <-done: | ||
4688 | 4608 | - return | ||
4689 | 4609 | - case <-h.timeout(): | ||
4690 | 4610 | - tw.mu.Lock() | ||
4691 | 4611 | - defer tw.mu.Unlock() | ||
4692 | 4612 | - if !tw.wroteHeader { | ||
4693 | 4613 | - tw.w.WriteHeader(StatusServiceUnavailable) | ||
4694 | 4614 | - tw.w.Write([]byte(h.errorBody())) | ||
4695 | 4615 | - } | ||
4696 | 4616 | - tw.timedOut = true | ||
4697 | 4617 | - } | ||
4698 | 4618 | -} | ||
4699 | 4619 | - | ||
4700 | 4620 | -type timeoutWriter struct { | ||
4701 | 4621 | - w ResponseWriter | ||
4702 | 4622 | - | ||
4703 | 4623 | - mu sync.Mutex | ||
4704 | 4624 | - timedOut bool | ||
4705 | 4625 | - wroteHeader bool | ||
4706 | 4626 | -} | ||
4707 | 4627 | - | ||
4708 | 4628 | -func (tw *timeoutWriter) Header() Header { | ||
4709 | 4629 | - return tw.w.Header() | ||
4710 | 4630 | -} | ||
4711 | 4631 | - | ||
4712 | 4632 | -func (tw *timeoutWriter) Write(p []byte) (int, error) { | ||
4713 | 4633 | - tw.mu.Lock() | ||
4714 | 4634 | - timedOut := tw.timedOut | ||
4715 | 4635 | - tw.mu.Unlock() | ||
4716 | 4636 | - if timedOut { | ||
4717 | 4637 | - return 0, ErrHandlerTimeout | ||
4718 | 4638 | - } | ||
4719 | 4639 | - return tw.w.Write(p) | ||
4720 | 4640 | -} | ||
4721 | 4641 | - | ||
4722 | 4642 | -func (tw *timeoutWriter) WriteHeader(code int) { | ||
4723 | 4643 | - tw.mu.Lock() | ||
4724 | 4644 | - if tw.timedOut || tw.wroteHeader { | ||
4725 | 4645 | - tw.mu.Unlock() | ||
4726 | 4646 | - return | ||
4727 | 4647 | - } | ||
4728 | 4648 | - tw.wroteHeader = true | ||
4729 | 4649 | - tw.mu.Unlock() | ||
4730 | 4650 | - tw.w.WriteHeader(code) | ||
4731 | 4651 | -} | ||
4732 | 4652 | - | ||
4733 | 4653 | -// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted | ||
4734 | 4654 | -// connections. It's used by ListenAndServe and ListenAndServeTLS so | ||
4735 | 4655 | -// dead TCP connections (e.g. closing laptop mid-download) eventually | ||
4736 | 4656 | -// go away. | ||
4737 | 4657 | -type tcpKeepAliveListener struct { | ||
4738 | 4658 | - *net.TCPListener | ||
4739 | 4659 | -} | ||
4740 | 4660 | - | ||
4741 | 4661 | -func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) { | ||
4742 | 4662 | - tc, err := ln.AcceptTCP() | ||
4743 | 4663 | - if err != nil { | ||
4744 | 4664 | - return | ||
4745 | 4665 | - } | ||
4746 | 4666 | - tc.SetKeepAlive(true) | ||
4747 | 4667 | - tc.SetKeepAlivePeriod(3 * time.Minute) | ||
4748 | 4668 | - return tc, nil | ||
4749 | 4669 | -} | ||
4750 | 4670 | - | ||
4751 | 4671 | -// globalOptionsHandler responds to "OPTIONS *" requests. | ||
4752 | 4672 | -type globalOptionsHandler struct{} | ||
4753 | 4673 | - | ||
4754 | 4674 | -func (globalOptionsHandler) ServeHTTP(w ResponseWriter, r *Request) { | ||
4755 | 4675 | - w.Header().Set("Content-Length", "0") | ||
4756 | 4676 | - if r.ContentLength != 0 { | ||
4757 | 4677 | - // Read up to 4KB of OPTIONS body (as mentioned in the | ||
4758 | 4678 | - // spec as being reserved for future use), but anything | ||
4759 | 4679 | - // over that is considered a waste of server resources | ||
4760 | 4680 | - // (or an attack) and we abort and close the connection, | ||
4761 | 4681 | - // courtesy of MaxBytesReader's EOF behavior. | ||
4762 | 4682 | - mb := MaxBytesReader(w, r.Body, 4<<10) | ||
4763 | 4683 | - io.Copy(ioutil.Discard, mb) | ||
4764 | 4684 | - } | ||
4765 | 4685 | -} | ||
4766 | 4686 | +) | ||
4767 | 4687 | |||
4768 | 4688 | // eofReader is a non-nil io.ReadCloser that always returns EOF. | ||
4769 | 4689 | // It embeds a *strings.Reader so it still has a WriteTo method | ||
4770 | 4690 | @@ -1992,28 +25,6 @@ | ||
4771 | 4691 | ioutil.NopCloser(nil), | ||
4772 | 4692 | } | ||
4773 | 4693 | |||
4774 | 4694 | -// initNPNRequest is an HTTP handler that initializes certain | ||
4775 | 4695 | -// uninitialized fields in its *Request. Such partially-initialized | ||
4776 | 4696 | -// Requests come from NPN protocol handlers. | ||
4777 | 4697 | -type initNPNRequest struct { | ||
4778 | 4698 | - c *tls.Conn | ||
4779 | 4699 | - h serverHandler | ||
4780 | 4700 | -} | ||
4781 | 4701 | - | ||
4782 | 4702 | -func (h initNPNRequest) ServeHTTP(rw ResponseWriter, req *Request) { | ||
4783 | 4703 | - if req.TLS == nil { | ||
4784 | 4704 | - req.TLS = &tls.ConnectionState{} | ||
4785 | 4705 | - *req.TLS = h.c.ConnectionState() | ||
4786 | 4706 | - } | ||
4787 | 4707 | - if req.Body == nil { | ||
4788 | 4708 | - req.Body = eofReader | ||
4789 | 4709 | - } | ||
4790 | 4710 | - if req.RemoteAddr == "" { | ||
4791 | 4711 | - req.RemoteAddr = h.c.RemoteAddr().String() | ||
4792 | 4712 | - } | ||
4793 | 4713 | - h.h.ServeHTTP(rw, req) | ||
4794 | 4714 | -} | ||
4795 | 4715 | - | ||
4796 | 4716 | // loggingConn is used for debugging. | ||
4797 | 4717 | type loggingConn struct { | ||
4798 | 4718 | name string | ||
4799 | 4719 | |||
4800 | 0 | 4720 | ||
4801 | === added file 'http13client/_patches/fix_code.patch' | |||
4802 | --- http13client/_patches/fix_code.patch 1970-01-01 00:00:00 +0000 | |||
4803 | +++ http13client/_patches/fix_code.patch 2014-03-20 12:26:55 +0000 | |||
4804 | @@ -0,0 +1,739 @@ | |||
4805 | 1 | === modified file 'http13client/client.go' | ||
4806 | 2 | --- http13client/client.go 2014-03-19 20:20:19 +0000 | ||
4807 | 3 | +++ http13client/client.go 2014-03-19 22:27:37 +0000 | ||
4808 | 4 | @@ -17,6 +17,7 @@ | ||
4809 | 5 | "io/ioutil" | ||
4810 | 6 | "log" | ||
4811 | 7 | "net/url" | ||
4812 | 8 | + "net/http" | ||
4813 | 9 | "strings" | ||
4814 | 10 | "sync" | ||
4815 | 11 | "time" | ||
4816 | 12 | @@ -54,7 +55,7 @@ | ||
4817 | 13 | // Jar specifies the cookie jar. | ||
4818 | 14 | // If Jar is nil, cookies are not sent in requests and ignored | ||
4819 | 15 | // in responses. | ||
4820 | 16 | - Jar CookieJar | ||
4821 | 17 | + Jar http.CookieJar | ||
4822 | 18 | |||
4823 | 19 | // Timeout specifies a time limit for requests made by this | ||
4824 | 20 | // Client. The timeout includes connection time, any | ||
4825 | 21 | @@ -177,7 +178,7 @@ | ||
4826 | 22 | // Headers, leaving it uninitialized. We guarantee to the | ||
4827 | 23 | // Transport that this has been initialized, though. | ||
4828 | 24 | if req.Header == nil { | ||
4829 | 25 | - req.Header = make(Header) | ||
4830 | 26 | + req.Header = make(http.Header) | ||
4831 | 27 | } | ||
4832 | 28 | |||
4833 | 29 | if u := req.URL.User; u != nil { | ||
4834 | 30 | @@ -308,7 +309,7 @@ | ||
4835 | 31 | if ireq.Method == "POST" || ireq.Method == "PUT" { | ||
4836 | 32 | nreq.Method = "GET" | ||
4837 | 33 | } | ||
4838 | 34 | - nreq.Header = make(Header) | ||
4839 | 35 | + nreq.Header = make(http.Header) | ||
4840 | 36 | nreq.URL, err = base.Parse(urlStr) | ||
4841 | 37 | if err != nil { | ||
4842 | 38 | break | ||
4843 | 39 | |||
4844 | 40 | === modified file 'http13client/cookie.go' | ||
4845 | 41 | --- http13client/cookie.go 2014-03-19 20:20:19 +0000 | ||
4846 | 42 | +++ http13client/cookie.go 2014-03-19 22:27:37 +0000 | ||
4847 | 43 | @@ -5,10 +5,9 @@ | ||
4848 | 44 | package http | ||
4849 | 45 | |||
4850 | 46 | import ( | ||
4851 | 47 | - "bytes" | ||
4852 | 48 | - "fmt" | ||
4853 | 49 | "log" | ||
4854 | 50 | "net" | ||
4855 | 51 | + "net/http" | ||
4856 | 52 | "strconv" | ||
4857 | 53 | "strings" | ||
4858 | 54 | "time" | ||
4859 | 55 | @@ -18,30 +17,10 @@ | ||
4860 | 56 | // | ||
4861 | 57 | // http://tools.ietf.org/html/rfc6265 | ||
4862 | 58 | |||
4863 | 59 | -// A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an | ||
4864 | 60 | -// HTTP response or the Cookie header of an HTTP request. | ||
4865 | 61 | -type Cookie struct { | ||
4866 | 62 | - Name string | ||
4867 | 63 | - Value string | ||
4868 | 64 | - Path string | ||
4869 | 65 | - Domain string | ||
4870 | 66 | - Expires time.Time | ||
4871 | 67 | - RawExpires string | ||
4872 | 68 | - | ||
4873 | 69 | - // MaxAge=0 means no 'Max-Age' attribute specified. | ||
4874 | 70 | - // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0' | ||
4875 | 71 | - // MaxAge>0 means Max-Age attribute present and given in seconds | ||
4876 | 72 | - MaxAge int | ||
4877 | 73 | - Secure bool | ||
4878 | 74 | - HttpOnly bool | ||
4879 | 75 | - Raw string | ||
4880 | 76 | - Unparsed []string // Raw text of unparsed attribute-value pairs | ||
4881 | 77 | -} | ||
4882 | 78 | - | ||
4883 | 79 | // readSetCookies parses all "Set-Cookie" values from | ||
4884 | 80 | // the header h and returns the successfully parsed Cookies. | ||
4885 | 81 | -func readSetCookies(h Header) []*Cookie { | ||
4886 | 82 | - cookies := []*Cookie{} | ||
4887 | 83 | +func readSetCookies(h http.Header) []*http.Cookie { | ||
4888 | 84 | + cookies := []*http.Cookie{} | ||
4889 | 85 | for _, line := range h["Set-Cookie"] { | ||
4890 | 86 | parts := strings.Split(strings.TrimSpace(line), ";") | ||
4891 | 87 | if len(parts) == 1 && parts[0] == "" { | ||
4892 | 88 | @@ -60,7 +39,7 @@ | ||
4893 | 89 | if !success { | ||
4894 | 90 | continue | ||
4895 | 91 | } | ||
4896 | 92 | - c := &Cookie{ | ||
4897 | 93 | + c := &http.Cookie{ | ||
4898 | 94 | Name: name, | ||
4899 | 95 | Value: value, | ||
4900 | 96 | Raw: line, | ||
4901 | 97 | @@ -129,59 +108,12 @@ | ||
4902 | 98 | return cookies | ||
4903 | 99 | } | ||
4904 | 100 | |||
4905 | 101 | -// SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers. | ||
4906 | 102 | -func SetCookie(w ResponseWriter, cookie *Cookie) { | ||
4907 | 103 | - w.Header().Add("Set-Cookie", cookie.String()) | ||
4908 | 104 | -} | ||
4909 | 105 | - | ||
4910 | 106 | -// String returns the serialization of the cookie for use in a Cookie | ||
4911 | 107 | -// header (if only Name and Value are set) or a Set-Cookie response | ||
4912 | 108 | -// header (if other fields are set). | ||
4913 | 109 | -func (c *Cookie) String() string { | ||
4914 | 110 | - var b bytes.Buffer | ||
4915 | 111 | - fmt.Fprintf(&b, "%s=%s", sanitizeCookieName(c.Name), sanitizeCookieValue(c.Value)) | ||
4916 | 112 | - if len(c.Path) > 0 { | ||
4917 | 113 | - fmt.Fprintf(&b, "; Path=%s", sanitizeCookiePath(c.Path)) | ||
4918 | 114 | - } | ||
4919 | 115 | - if len(c.Domain) > 0 { | ||
4920 | 116 | - if validCookieDomain(c.Domain) { | ||
4921 | 117 | - // A c.Domain containing illegal characters is not | ||
4922 | 118 | - // sanitized but simply dropped which turns the cookie | ||
4923 | 119 | - // into a host-only cookie. A leading dot is okay | ||
4924 | 120 | - // but won't be sent. | ||
4925 | 121 | - d := c.Domain | ||
4926 | 122 | - if d[0] == '.' { | ||
4927 | 123 | - d = d[1:] | ||
4928 | 124 | - } | ||
4929 | 125 | - fmt.Fprintf(&b, "; Domain=%s", d) | ||
4930 | 126 | - } else { | ||
4931 | 127 | - log.Printf("net/http: invalid Cookie.Domain %q; dropping domain attribute", | ||
4932 | 128 | - c.Domain) | ||
4933 | 129 | - } | ||
4934 | 130 | - } | ||
4935 | 131 | - if c.Expires.Unix() > 0 { | ||
4936 | 132 | - fmt.Fprintf(&b, "; Expires=%s", c.Expires.UTC().Format(time.RFC1123)) | ||
4937 | 133 | - } | ||
4938 | 134 | - if c.MaxAge > 0 { | ||
4939 | 135 | - fmt.Fprintf(&b, "; Max-Age=%d", c.MaxAge) | ||
4940 | 136 | - } else if c.MaxAge < 0 { | ||
4941 | 137 | - fmt.Fprintf(&b, "; Max-Age=0") | ||
4942 | 138 | - } | ||
4943 | 139 | - if c.HttpOnly { | ||
4944 | 140 | - fmt.Fprintf(&b, "; HttpOnly") | ||
4945 | 141 | - } | ||
4946 | 142 | - if c.Secure { | ||
4947 | 143 | - fmt.Fprintf(&b, "; Secure") | ||
4948 | 144 | - } | ||
4949 | 145 | - return b.String() | ||
4950 | 146 | -} | ||
4951 | 147 | - | ||
4952 | 148 | // readCookies parses all "Cookie" values from the header h and | ||
4953 | 149 | // returns the successfully parsed Cookies. | ||
4954 | 150 | // | ||
4955 | 151 | // if filter isn't empty, only cookies of that name are returned | ||
4956 | 152 | -func readCookies(h Header, filter string) []*Cookie { | ||
4957 | 153 | - cookies := []*Cookie{} | ||
4958 | 154 | +func readCookies(h http.Header, filter string) []*http.Cookie { | ||
4959 | 155 | + cookies := []*http.Cookie{} | ||
4960 | 156 | lines, ok := h["Cookie"] | ||
4961 | 157 | if !ok { | ||
4962 | 158 | return cookies | ||
4963 | 159 | @@ -213,7 +145,7 @@ | ||
4964 | 160 | if !success { | ||
4965 | 161 | continue | ||
4966 | 162 | } | ||
4967 | 163 | - cookies = append(cookies, &Cookie{Name: name, Value: val}) | ||
4968 | 164 | + cookies = append(cookies, &http.Cookie{Name: name, Value: val}) | ||
4969 | 165 | parsedPairs++ | ||
4970 | 166 | } | ||
4971 | 167 | } | ||
4972 | 168 | |||
4973 | 169 | === modified file 'http13client/header.go' | ||
4974 | 170 | --- http13client/header.go 2014-03-19 20:20:19 +0000 | ||
4975 | 171 | +++ http13client/header.go 2014-03-19 22:27:37 +0000 | ||
4976 | 172 | @@ -5,176 +5,9 @@ | ||
4977 | 173 | package http | ||
4978 | 174 | |||
4979 | 175 | import ( | ||
4980 | 176 | - "io" | ||
4981 | 177 | - "net/textproto" | ||
4982 | 178 | - "sort" | ||
4983 | 179 | "strings" | ||
4984 | 180 | - "time" | ||
4985 | 181 | ) | ||
4986 | 182 | |||
4987 | 183 | -var raceEnabled = false // set by race.go | ||
4988 | 184 | - | ||
4989 | 185 | -// A Header represents the key-value pairs in an HTTP header. | ||
4990 | 186 | -type Header map[string][]string | ||
4991 | 187 | - | ||
4992 | 188 | -// Add adds the key, value pair to the header. | ||
4993 | 189 | -// It appends to any existing values associated with key. | ||
4994 | 190 | -func (h Header) Add(key, value string) { | ||
4995 | 191 | - textproto.MIMEHeader(h).Add(key, value) | ||
4996 | 192 | -} | ||
4997 | 193 | - | ||
4998 | 194 | -// Set sets the header entries associated with key to | ||
4999 | 195 | -// the single element value. It replaces any existing | ||
5000 | 196 | -// values associated with key. |
The diff has been truncated for viewing.
I think you just stole the "Biggest diff evar" trophy from me.