~chipaca/ubuntu-push/gsettings

« back to all changes in this revision

Viewing changes to http13client/server.go

  • Committer: Samuele Pedroni (Canonical Services Ltd.)
  • Date: 2014-03-19 20:20:19 UTC
  • mto: This revision was merged to the branch mainline in revision 82.
  • Revision ID: samuele.pedroni@canonical.com-20140319202019-p0w8krshj1098f82
grab go 1.3 dev net/http and massage it so that the test run on 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2009 The Go Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
// HTTP server.  See RFC 2616.
 
6
 
 
7
package http
 
8
 
 
9
import (
 
10
        "bufio"
 
11
        "crypto/tls"
 
12
        "errors"
 
13
        "fmt"
 
14
        "io"
 
15
        "io/ioutil"
 
16
        "log"
 
17
        "net"
 
18
        "net/url"
 
19
        "os"
 
20
        "path"
 
21
        "runtime"
 
22
        "strconv"
 
23
        "strings"
 
24
        "sync"
 
25
        "sync/atomic"
 
26
        "time"
 
27
)
 
28
 
 
29
// Errors introduced by the HTTP server.
 
30
var (
 
31
        ErrWriteAfterFlush = errors.New("Conn.Write called after Flush")
 
32
        ErrBodyNotAllowed  = errors.New("http: request method or response status code does not allow body")
 
33
        ErrHijacked        = errors.New("Conn has been hijacked")
 
34
        ErrContentLength   = errors.New("Conn.Write wrote more than the declared Content-Length")
 
35
)
 
36
 
 
37
// Objects implementing the Handler interface can be
 
38
// registered to serve a particular path or subtree
 
39
// in the HTTP server.
 
40
//
 
41
// ServeHTTP should write reply headers and data to the ResponseWriter
 
42
// and then return.  Returning signals that the request is finished
 
43
// and that the HTTP server can move on to the next request on
 
44
// the connection.
 
45
type Handler interface {
 
46
        ServeHTTP(ResponseWriter, *Request)
 
47
}
 
48
 
 
49
// A ResponseWriter interface is used by an HTTP handler to
 
50
// construct an HTTP response.
 
51
type ResponseWriter interface {
 
52
        // Header returns the header map that will be sent by WriteHeader.
 
53
        // Changing the header after a call to WriteHeader (or Write) has
 
54
        // no effect.
 
55
        Header() Header
 
56
 
 
57
        // Write writes the data to the connection as part of an HTTP reply.
 
58
        // If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)
 
59
        // before writing the data.  If the Header does not contain a
 
60
        // Content-Type line, Write adds a Content-Type set to the result of passing
 
61
        // the initial 512 bytes of written data to DetectContentType.
 
62
        Write([]byte) (int, error)
 
63
 
 
64
        // WriteHeader sends an HTTP response header with status code.
 
65
        // If WriteHeader is not called explicitly, the first call to Write
 
66
        // will trigger an implicit WriteHeader(http.StatusOK).
 
67
        // Thus explicit calls to WriteHeader are mainly used to
 
68
        // send error codes.
 
69
        WriteHeader(int)
 
70
}
 
71
 
 
72
// The Flusher interface is implemented by ResponseWriters that allow
 
73
// an HTTP handler to flush buffered data to the client.
 
74
//
 
75
// Note that even for ResponseWriters that support Flush,
 
76
// if the client is connected through an HTTP proxy,
 
77
// the buffered data may not reach the client until the response
 
78
// completes.
 
79
type Flusher interface {
 
80
        // Flush sends any buffered data to the client.
 
81
        Flush()
 
82
}
 
83
 
 
84
// The Hijacker interface is implemented by ResponseWriters that allow
 
85
// an HTTP handler to take over the connection.
 
86
type Hijacker interface {
 
87
        // Hijack lets the caller take over the connection.
 
88
        // After a call to Hijack(), the HTTP server library
 
89
        // will not do anything else with the connection.
 
90
        // It becomes the caller's responsibility to manage
 
91
        // and close the connection.
 
92
        Hijack() (net.Conn, *bufio.ReadWriter, error)
 
93
}
 
94
 
 
95
// The CloseNotifier interface is implemented by ResponseWriters which
 
96
// allow detecting when the underlying connection has gone away.
 
97
//
 
98
// This mechanism can be used to cancel long operations on the server
 
99
// if the client has disconnected before the response is ready.
 
100
type CloseNotifier interface {
 
101
        // CloseNotify returns a channel that receives a single value
 
102
        // when the client connection has gone away.
 
103
        CloseNotify() <-chan bool
 
104
}
 
105
 
 
106
// A conn represents the server side of an HTTP connection.
 
107
type conn struct {
 
108
        remoteAddr string               // network address of remote side
 
109
        server     *Server              // the Server on which the connection arrived
 
110
        rwc        net.Conn             // i/o connection
 
111
        sr         liveSwitchReader     // where the LimitReader reads from; usually the rwc
 
112
        lr         *io.LimitedReader    // io.LimitReader(sr)
 
113
        buf        *bufio.ReadWriter    // buffered(lr,rwc), reading from bufio->limitReader->sr->rwc
 
114
        tlsState   *tls.ConnectionState // or nil when not using TLS
 
115
 
 
116
        mu           sync.Mutex // guards the following
 
117
        clientGone   bool       // if client has disconnected mid-request
 
118
        closeNotifyc chan bool  // made lazily
 
119
        hijackedv    bool       // connection has been hijacked by handler
 
120
}
 
121
 
 
122
func (c *conn) hijacked() bool {
 
123
        c.mu.Lock()
 
124
        defer c.mu.Unlock()
 
125
        return c.hijackedv
 
126
}
 
127
 
 
128
func (c *conn) hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
 
129
        c.mu.Lock()
 
130
        defer c.mu.Unlock()
 
131
        if c.hijackedv {
 
132
                return nil, nil, ErrHijacked
 
133
        }
 
134
        if c.closeNotifyc != nil {
 
135
                return nil, nil, errors.New("http: Hijack is incompatible with use of CloseNotifier")
 
136
        }
 
137
        c.hijackedv = true
 
138
        rwc = c.rwc
 
139
        buf = c.buf
 
140
        c.rwc = nil
 
141
        c.buf = nil
 
142
        c.setState(rwc, StateHijacked)
 
143
        return
 
144
}
 
145
 
 
146
func (c *conn) closeNotify() <-chan bool {
 
147
        c.mu.Lock()
 
148
        defer c.mu.Unlock()
 
149
        if c.closeNotifyc == nil {
 
150
                c.closeNotifyc = make(chan bool, 1)
 
151
                if c.hijackedv {
 
152
                        // to obey the function signature, even though
 
153
                        // it'll never receive a value.
 
154
                        return c.closeNotifyc
 
155
                }
 
156
                pr, pw := io.Pipe()
 
157
 
 
158
                readSource := c.sr.r
 
159
                c.sr.Lock()
 
160
                c.sr.r = pr
 
161
                c.sr.Unlock()
 
162
                go func() {
 
163
                        _, err := io.Copy(pw, readSource)
 
164
                        if err == nil {
 
165
                                err = io.EOF
 
166
                        }
 
167
                        pw.CloseWithError(err)
 
168
                        c.noteClientGone()
 
169
                }()
 
170
        }
 
171
        return c.closeNotifyc
 
172
}
 
173
 
 
174
func (c *conn) noteClientGone() {
 
175
        c.mu.Lock()
 
176
        defer c.mu.Unlock()
 
177
        if c.closeNotifyc != nil && !c.clientGone {
 
178
                c.closeNotifyc <- true
 
179
        }
 
180
        c.clientGone = true
 
181
}
 
182
 
 
183
// A switchReader can have its Reader changed at runtime.
 
184
// It's not safe for concurrent Reads and switches.
 
185
type switchReader struct {
 
186
        io.Reader
 
187
}
 
188
 
 
189
// A switchWriter can have its Writer changed at runtime.
 
190
// It's not safe for concurrent Writes and switches.
 
191
type switchWriter struct {
 
192
        io.Writer
 
193
}
 
194
 
 
195
// A liveSwitchReader is a switchReader that's safe for concurrent
 
196
// reads and switches, if its mutex is held.
 
197
type liveSwitchReader struct {
 
198
        sync.Mutex
 
199
        r io.Reader
 
200
}
 
201
 
 
202
func (sr *liveSwitchReader) Read(p []byte) (n int, err error) {
 
203
        sr.Lock()
 
204
        r := sr.r
 
205
        sr.Unlock()
 
206
        return r.Read(p)
 
207
}
 
208
 
 
209
// This should be >= 512 bytes for DetectContentType,
 
210
// but otherwise it's somewhat arbitrary.
 
211
const bufferBeforeChunkingSize = 2048
 
212
 
 
213
// chunkWriter writes to a response's conn buffer, and is the writer
 
214
// wrapped by the response.bufw buffered writer.
 
215
//
 
216
// chunkWriter also is responsible for finalizing the Header, including
 
217
// conditionally setting the Content-Type and setting a Content-Length
 
218
// in cases where the handler's final output is smaller than the buffer
 
219
// size. It also conditionally adds chunk headers, when in chunking mode.
 
220
//
 
221
// See the comment above (*response).Write for the entire write flow.
 
222
type chunkWriter struct {
 
223
        res *response
 
224
 
 
225
        // header is either nil or a deep clone of res.handlerHeader
 
226
        // at the time of res.WriteHeader, if res.WriteHeader is
 
227
        // called and extra buffering is being done to calculate
 
228
        // Content-Type and/or Content-Length.
 
229
        header Header
 
230
 
 
231
        // wroteHeader tells whether the header's been written to "the
 
232
        // wire" (or rather: w.conn.buf). this is unlike
 
233
        // (*response).wroteHeader, which tells only whether it was
 
234
        // logically written.
 
235
        wroteHeader bool
 
236
 
 
237
        // set by the writeHeader method:
 
238
        chunking bool // using chunked transfer encoding for reply body
 
239
}
 
240
 
 
241
var (
 
242
        crlf       = []byte("\r\n")
 
243
        colonSpace = []byte(": ")
 
244
)
 
245
 
 
246
func (cw *chunkWriter) Write(p []byte) (n int, err error) {
 
247
        if !cw.wroteHeader {
 
248
                cw.writeHeader(p)
 
249
        }
 
250
        if cw.res.req.Method == "HEAD" {
 
251
                // Eat writes.
 
252
                return len(p), nil
 
253
        }
 
254
        if cw.chunking {
 
255
                _, err = fmt.Fprintf(cw.res.conn.buf, "%x\r\n", len(p))
 
256
                if err != nil {
 
257
                        cw.res.conn.rwc.Close()
 
258
                        return
 
259
                }
 
260
        }
 
261
        n, err = cw.res.conn.buf.Write(p)
 
262
        if cw.chunking && err == nil {
 
263
                _, err = cw.res.conn.buf.Write(crlf)
 
264
        }
 
265
        if err != nil {
 
266
                cw.res.conn.rwc.Close()
 
267
        }
 
268
        return
 
269
}
 
270
 
 
271
func (cw *chunkWriter) flush() {
 
272
        if !cw.wroteHeader {
 
273
                cw.writeHeader(nil)
 
274
        }
 
275
        cw.res.conn.buf.Flush()
 
276
}
 
277
 
 
278
func (cw *chunkWriter) close() {
 
279
        if !cw.wroteHeader {
 
280
                cw.writeHeader(nil)
 
281
        }
 
282
        if cw.chunking {
 
283
                // zero EOF chunk, trailer key/value pairs (currently
 
284
                // unsupported in Go's server), followed by a blank
 
285
                // line.
 
286
                cw.res.conn.buf.WriteString("0\r\n\r\n")
 
287
        }
 
288
}
 
289
 
 
290
// A response represents the server side of an HTTP response.
 
291
type response struct {
 
292
        conn          *conn
 
293
        req           *Request // request for this response
 
294
        wroteHeader   bool     // reply header has been (logically) written
 
295
        wroteContinue bool     // 100 Continue response was written
 
296
 
 
297
        w  *bufio.Writer // buffers output in chunks to chunkWriter
 
298
        cw chunkWriter
 
299
        sw *switchWriter // of the bufio.Writer, for return to putBufioWriter
 
300
 
 
301
        // handlerHeader is the Header that Handlers get access to,
 
302
        // which may be retained and mutated even after WriteHeader.
 
303
        // handlerHeader is copied into cw.header at WriteHeader
 
304
        // time, and privately mutated thereafter.
 
305
        handlerHeader Header
 
306
        calledHeader  bool // handler accessed handlerHeader via Header
 
307
 
 
308
        written       int64 // number of bytes written in body
 
309
        contentLength int64 // explicitly-declared Content-Length; or -1
 
310
        status        int   // status code passed to WriteHeader
 
311
 
 
312
        // close connection after this reply.  set on request and
 
313
        // updated after response from handler if there's a
 
314
        // "Connection: keep-alive" response header and a
 
315
        // Content-Length.
 
316
        closeAfterReply bool
 
317
 
 
318
        // requestBodyLimitHit is set by requestTooLarge when
 
319
        // maxBytesReader hits its max size. It is checked in
 
320
        // WriteHeader, to make sure we don't consume the
 
321
        // remaining request body to try to advance to the next HTTP
 
322
        // request. Instead, when this is set, we stop reading
 
323
        // subsequent requests on this connection and stop reading
 
324
        // input from it.
 
325
        requestBodyLimitHit bool
 
326
 
 
327
        handlerDone bool // set true when the handler exits
 
328
 
 
329
        // Buffers for Date and Content-Length
 
330
        dateBuf [len(TimeFormat)]byte
 
331
        clenBuf [10]byte
 
332
}
 
333
 
 
334
// requestTooLarge is called by maxBytesReader when too much input has
 
335
// been read from the client.
 
336
func (w *response) requestTooLarge() {
 
337
        w.closeAfterReply = true
 
338
        w.requestBodyLimitHit = true
 
339
        if !w.wroteHeader {
 
340
                w.Header().Set("Connection", "close")
 
341
        }
 
342
}
 
343
 
 
344
// needsSniff reports whether a Content-Type still needs to be sniffed.
 
345
func (w *response) needsSniff() bool {
 
346
        _, haveType := w.handlerHeader["Content-Type"]
 
347
        return !w.cw.wroteHeader && !haveType && w.written < sniffLen
 
348
}
 
349
 
 
350
// writerOnly hides an io.Writer value's optional ReadFrom method
 
351
// from io.Copy.
 
352
type writerOnly struct {
 
353
        io.Writer
 
354
}
 
355
 
 
356
func srcIsRegularFile(src io.Reader) (isRegular bool, err error) {
 
357
        switch v := src.(type) {
 
358
        case *os.File:
 
359
                fi, err := v.Stat()
 
360
                if err != nil {
 
361
                        return false, err
 
362
                }
 
363
                return fi.Mode().IsRegular(), nil
 
364
        case *io.LimitedReader:
 
365
                return srcIsRegularFile(v.R)
 
366
        default:
 
367
                return
 
368
        }
 
369
}
 
370
 
 
371
// ReadFrom is here to optimize copying from an *os.File regular file
 
372
// to a *net.TCPConn with sendfile.
 
373
func (w *response) ReadFrom(src io.Reader) (n int64, err error) {
 
374
        // Our underlying w.conn.rwc is usually a *TCPConn (with its
 
375
        // own ReadFrom method). If not, or if our src isn't a regular
 
376
        // file, just fall back to the normal copy method.
 
377
        rf, ok := w.conn.rwc.(io.ReaderFrom)
 
378
        regFile, err := srcIsRegularFile(src)
 
379
        if err != nil {
 
380
                return 0, err
 
381
        }
 
382
        if !ok || !regFile {
 
383
                return io.Copy(writerOnly{w}, src)
 
384
        }
 
385
 
 
386
        // sendfile path:
 
387
 
 
388
        if !w.wroteHeader {
 
389
                w.WriteHeader(StatusOK)
 
390
        }
 
391
 
 
392
        if w.needsSniff() {
 
393
                n0, err := io.Copy(writerOnly{w}, io.LimitReader(src, sniffLen))
 
394
                n += n0
 
395
                if err != nil {
 
396
                        return n, err
 
397
                }
 
398
        }
 
399
 
 
400
        w.w.Flush()  // get rid of any previous writes
 
401
        w.cw.flush() // make sure Header is written; flush data to rwc
 
402
 
 
403
        // Now that cw has been flushed, its chunking field is guaranteed initialized.
 
404
        if !w.cw.chunking && w.bodyAllowed() {
 
405
                n0, err := rf.ReadFrom(src)
 
406
                n += n0
 
407
                w.written += n0
 
408
                return n, err
 
409
        }
 
410
 
 
411
        n0, err := io.Copy(writerOnly{w}, src)
 
412
        n += n0
 
413
        return n, err
 
414
}
 
415
 
 
416
// noLimit is an effective infinite upper bound for io.LimitedReader
 
417
const noLimit int64 = (1 << 63) - 1
 
418
 
 
419
// debugServerConnections controls whether all server connections are wrapped
 
420
// with a verbose logging wrapper.
 
421
const debugServerConnections = false
 
422
 
 
423
// Create new connection from rwc.
 
424
func (srv *Server) newConn(rwc net.Conn) (c *conn, err error) {
 
425
        c = new(conn)
 
426
        c.remoteAddr = rwc.RemoteAddr().String()
 
427
        c.server = srv
 
428
        c.rwc = rwc
 
429
        if debugServerConnections {
 
430
                c.rwc = newLoggingConn("server", c.rwc)
 
431
        }
 
432
        c.sr = liveSwitchReader{r: c.rwc}
 
433
        c.lr = io.LimitReader(&c.sr, noLimit).(*io.LimitedReader)
 
434
        br := newBufioReader(c.lr)
 
435
        bw := newBufioWriterSize(c.rwc, 4<<10)
 
436
        c.buf = bufio.NewReadWriter(br, bw)
 
437
        return c, nil
 
438
}
 
439
 
 
440
// TODO: use a sync.Cache instead
 
441
var (
 
442
        bufioReaderCache   = make(chan *bufio.Reader, 4)
 
443
        bufioWriterCache2k = make(chan *bufio.Writer, 4)
 
444
        bufioWriterCache4k = make(chan *bufio.Writer, 4)
 
445
)
 
446
 
 
447
func bufioWriterCache(size int) chan *bufio.Writer {
 
448
        switch size {
 
449
        case 2 << 10:
 
450
                return bufioWriterCache2k
 
451
        case 4 << 10:
 
452
                return bufioWriterCache4k
 
453
        }
 
454
        return nil
 
455
}
 
456
 
 
457
func newBufioReader(r io.Reader) *bufio.Reader {
 
458
        select {
 
459
        case p := <-bufioReaderCache:
 
460
                p.Reset(r)
 
461
                return p
 
462
        default:
 
463
                return bufio.NewReader(r)
 
464
        }
 
465
}
 
466
 
 
467
func putBufioReader(br *bufio.Reader) {
 
468
        br.Reset(nil)
 
469
        select {
 
470
        case bufioReaderCache <- br:
 
471
        default:
 
472
        }
 
473
}
 
474
 
 
475
func newBufioWriterSize(w io.Writer, size int) *bufio.Writer {
 
476
        select {
 
477
        case p := <-bufioWriterCache(size):
 
478
                p.Reset(w)
 
479
                return p
 
480
        default:
 
481
                return bufio.NewWriterSize(w, size)
 
482
        }
 
483
}
 
484
 
 
485
func putBufioWriter(bw *bufio.Writer) {
 
486
        bw.Reset(nil)
 
487
        select {
 
488
        case bufioWriterCache(bw.Available()) <- bw:
 
489
        default:
 
490
        }
 
491
}
 
492
 
 
493
// DefaultMaxHeaderBytes is the maximum permitted size of the headers
 
494
// in an HTTP request.
 
495
// This can be overridden by setting Server.MaxHeaderBytes.
 
496
const DefaultMaxHeaderBytes = 1 << 20 // 1 MB
 
497
 
 
498
func (srv *Server) maxHeaderBytes() int {
 
499
        if srv.MaxHeaderBytes > 0 {
 
500
                return srv.MaxHeaderBytes
 
501
        }
 
502
        return DefaultMaxHeaderBytes
 
503
}
 
504
 
 
505
func (srv *Server) initialLimitedReaderSize() int64 {
 
506
        return int64(srv.maxHeaderBytes()) + 4096 // bufio slop
 
507
}
 
508
 
 
509
// wrapper around io.ReaderCloser which on first read, sends an
 
510
// HTTP/1.1 100 Continue header
 
511
type expectContinueReader struct {
 
512
        resp       *response
 
513
        readCloser io.ReadCloser
 
514
        closed     bool
 
515
}
 
516
 
 
517
func (ecr *expectContinueReader) Read(p []byte) (n int, err error) {
 
518
        if ecr.closed {
 
519
                return 0, ErrBodyReadAfterClose
 
520
        }
 
521
        if !ecr.resp.wroteContinue && !ecr.resp.conn.hijacked() {
 
522
                ecr.resp.wroteContinue = true
 
523
                ecr.resp.conn.buf.WriteString("HTTP/1.1 100 Continue\r\n\r\n")
 
524
                ecr.resp.conn.buf.Flush()
 
525
        }
 
526
        return ecr.readCloser.Read(p)
 
527
}
 
528
 
 
529
func (ecr *expectContinueReader) Close() error {
 
530
        ecr.closed = true
 
531
        return ecr.readCloser.Close()
 
532
}
 
533
 
 
534
// TimeFormat is the time format to use with
 
535
// time.Parse and time.Time.Format when parsing
 
536
// or generating times in HTTP headers.
 
537
// It is like time.RFC1123 but hard codes GMT as the time zone.
 
538
const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
 
539
 
 
540
// appendTime is a non-allocating version of []byte(t.UTC().Format(TimeFormat))
 
541
func appendTime(b []byte, t time.Time) []byte {
 
542
        const days = "SunMonTueWedThuFriSat"
 
543
        const months = "JanFebMarAprMayJunJulAugSepOctNovDec"
 
544
 
 
545
        t = t.UTC()
 
546
        yy, mm, dd := t.Date()
 
547
        hh, mn, ss := t.Clock()
 
548
        day := days[3*t.Weekday():]
 
549
        mon := months[3*(mm-1):]
 
550
 
 
551
        return append(b,
 
552
                day[0], day[1], day[2], ',', ' ',
 
553
                byte('0'+dd/10), byte('0'+dd%10), ' ',
 
554
                mon[0], mon[1], mon[2], ' ',
 
555
                byte('0'+yy/1000), byte('0'+(yy/100)%10), byte('0'+(yy/10)%10), byte('0'+yy%10), ' ',
 
556
                byte('0'+hh/10), byte('0'+hh%10), ':',
 
557
                byte('0'+mn/10), byte('0'+mn%10), ':',
 
558
                byte('0'+ss/10), byte('0'+ss%10), ' ',
 
559
                'G', 'M', 'T')
 
560
}
 
561
 
 
562
var errTooLarge = errors.New("http: request too large")
 
563
 
 
564
// Read next request from connection.
 
565
func (c *conn) readRequest() (w *response, err error) {
 
566
        if c.hijacked() {
 
567
                return nil, ErrHijacked
 
568
        }
 
569
 
 
570
        if d := c.server.ReadTimeout; d != 0 {
 
571
                c.rwc.SetReadDeadline(time.Now().Add(d))
 
572
        }
 
573
        if d := c.server.WriteTimeout; d != 0 {
 
574
                defer func() {
 
575
                        c.rwc.SetWriteDeadline(time.Now().Add(d))
 
576
                }()
 
577
        }
 
578
 
 
579
        c.lr.N = c.server.initialLimitedReaderSize()
 
580
        var req *Request
 
581
        if req, err = ReadRequest(c.buf.Reader); err != nil {
 
582
                if c.lr.N == 0 {
 
583
                        return nil, errTooLarge
 
584
                }
 
585
                return nil, err
 
586
        }
 
587
        c.lr.N = noLimit
 
588
 
 
589
        req.RemoteAddr = c.remoteAddr
 
590
        req.TLS = c.tlsState
 
591
 
 
592
        w = &response{
 
593
                conn:          c,
 
594
                req:           req,
 
595
                handlerHeader: make(Header),
 
596
                contentLength: -1,
 
597
        }
 
598
        w.cw.res = w
 
599
        w.w = newBufioWriterSize(&w.cw, bufferBeforeChunkingSize)
 
600
        return w, nil
 
601
}
 
602
 
 
603
func (w *response) Header() Header {
 
604
        if w.cw.header == nil && w.wroteHeader && !w.cw.wroteHeader {
 
605
                // Accessing the header between logically writing it
 
606
                // and physically writing it means we need to allocate
 
607
                // a clone to snapshot the logically written state.
 
608
                w.cw.header = w.handlerHeader.clone()
 
609
        }
 
610
        w.calledHeader = true
 
611
        return w.handlerHeader
 
612
}
 
613
 
 
614
// maxPostHandlerReadBytes is the max number of Request.Body bytes not
 
615
// consumed by a handler that the server will read from the client
 
616
// in order to keep a connection alive.  If there are more bytes than
 
617
// this then the server to be paranoid instead sends a "Connection:
 
618
// close" response.
 
619
//
 
620
// This number is approximately what a typical machine's TCP buffer
 
621
// size is anyway.  (if we have the bytes on the machine, we might as
 
622
// well read them)
 
623
const maxPostHandlerReadBytes = 256 << 10
 
624
 
 
625
func (w *response) WriteHeader(code int) {
 
626
        if w.conn.hijacked() {
 
627
                w.conn.server.logf("http: response.WriteHeader on hijacked connection")
 
628
                return
 
629
        }
 
630
        if w.wroteHeader {
 
631
                w.conn.server.logf("http: multiple response.WriteHeader calls")
 
632
                return
 
633
        }
 
634
        w.wroteHeader = true
 
635
        w.status = code
 
636
 
 
637
        if w.calledHeader && w.cw.header == nil {
 
638
                w.cw.header = w.handlerHeader.clone()
 
639
        }
 
640
 
 
641
        if cl := w.handlerHeader.get("Content-Length"); cl != "" {
 
642
                v, err := strconv.ParseInt(cl, 10, 64)
 
643
                if err == nil && v >= 0 {
 
644
                        w.contentLength = v
 
645
                } else {
 
646
                        w.conn.server.logf("http: invalid Content-Length of %q", cl)
 
647
                        w.handlerHeader.Del("Content-Length")
 
648
                }
 
649
        }
 
650
}
 
651
 
 
652
// extraHeader is the set of headers sometimes added by chunkWriter.writeHeader.
 
653
// This type is used to avoid extra allocations from cloning and/or populating
 
654
// the response Header map and all its 1-element slices.
 
655
type extraHeader struct {
 
656
        contentType      string
 
657
        connection       string
 
658
        transferEncoding string
 
659
        date             []byte // written if not nil
 
660
        contentLength    []byte // written if not nil
 
661
}
 
662
 
 
663
// Sorted the same as extraHeader.Write's loop.
 
664
var extraHeaderKeys = [][]byte{
 
665
        []byte("Content-Type"),
 
666
        []byte("Connection"),
 
667
        []byte("Transfer-Encoding"),
 
668
}
 
669
 
 
670
var (
 
671
        headerContentLength = []byte("Content-Length: ")
 
672
        headerDate          = []byte("Date: ")
 
673
)
 
674
 
 
675
// Write writes the headers described in h to w.
 
676
//
 
677
// This method has a value receiver, despite the somewhat large size
 
678
// of h, because it prevents an allocation. The escape analysis isn't
 
679
// smart enough to realize this function doesn't mutate h.
 
680
func (h extraHeader) Write(w *bufio.Writer) {
 
681
        if h.date != nil {
 
682
                w.Write(headerDate)
 
683
                w.Write(h.date)
 
684
                w.Write(crlf)
 
685
        }
 
686
        if h.contentLength != nil {
 
687
                w.Write(headerContentLength)
 
688
                w.Write(h.contentLength)
 
689
                w.Write(crlf)
 
690
        }
 
691
        for i, v := range []string{h.contentType, h.connection, h.transferEncoding} {
 
692
                if v != "" {
 
693
                        w.Write(extraHeaderKeys[i])
 
694
                        w.Write(colonSpace)
 
695
                        w.WriteString(v)
 
696
                        w.Write(crlf)
 
697
                }
 
698
        }
 
699
}
 
700
 
 
701
// writeHeader finalizes the header sent to the client and writes it
 
702
// to cw.res.conn.buf.
 
703
//
 
704
// p is not written by writeHeader, but is the first chunk of the body
 
705
// that will be written.  It is sniffed for a Content-Type if none is
 
706
// set explicitly.  It's also used to set the Content-Length, if the
 
707
// total body size was small and the handler has already finished
 
708
// running.
 
709
func (cw *chunkWriter) writeHeader(p []byte) {
 
710
        if cw.wroteHeader {
 
711
                return
 
712
        }
 
713
        cw.wroteHeader = true
 
714
 
 
715
        w := cw.res
 
716
        keepAlivesEnabled := w.conn.server.doKeepAlives()
 
717
        isHEAD := w.req.Method == "HEAD"
 
718
 
 
719
        // header is written out to w.conn.buf below. Depending on the
 
720
        // state of the handler, we either own the map or not. If we
 
721
        // don't own it, the exclude map is created lazily for
 
722
        // WriteSubset to remove headers. The setHeader struct holds
 
723
        // headers we need to add.
 
724
        header := cw.header
 
725
        owned := header != nil
 
726
        if !owned {
 
727
                header = w.handlerHeader
 
728
        }
 
729
        var excludeHeader map[string]bool
 
730
        delHeader := func(key string) {
 
731
                if owned {
 
732
                        header.Del(key)
 
733
                        return
 
734
                }
 
735
                if _, ok := header[key]; !ok {
 
736
                        return
 
737
                }
 
738
                if excludeHeader == nil {
 
739
                        excludeHeader = make(map[string]bool)
 
740
                }
 
741
                excludeHeader[key] = true
 
742
        }
 
743
        var setHeader extraHeader
 
744
 
 
745
        // If the handler is done but never sent a Content-Length
 
746
        // response header and this is our first (and last) write, set
 
747
        // it, even to zero. This helps HTTP/1.0 clients keep their
 
748
        // "keep-alive" connections alive.
 
749
        // Exceptions: 304/204/1xx responses never get Content-Length, and if
 
750
        // it was a HEAD request, we don't know the difference between
 
751
        // 0 actual bytes and 0 bytes because the handler noticed it
 
752
        // was a HEAD request and chose not to write anything.  So for
 
753
        // HEAD, the handler should either write the Content-Length or
 
754
        // write non-zero bytes.  If it's actually 0 bytes and the
 
755
        // handler never looked at the Request.Method, we just don't
 
756
        // send a Content-Length header.
 
757
        if w.handlerDone && bodyAllowedForStatus(w.status) && header.get("Content-Length") == "" && (!isHEAD || len(p) > 0) {
 
758
                w.contentLength = int64(len(p))
 
759
                setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10)
 
760
        }
 
761
 
 
762
        // If this was an HTTP/1.0 request with keep-alive and we sent a
 
763
        // Content-Length back, we can make this a keep-alive response ...
 
764
        if w.req.wantsHttp10KeepAlive() && keepAlivesEnabled {
 
765
                sentLength := header.get("Content-Length") != ""
 
766
                if sentLength && header.get("Connection") == "keep-alive" {
 
767
                        w.closeAfterReply = false
 
768
                }
 
769
        }
 
770
 
 
771
        // Check for a explicit (and valid) Content-Length header.
 
772
        hasCL := w.contentLength != -1
 
773
 
 
774
        if w.req.wantsHttp10KeepAlive() && (isHEAD || hasCL) {
 
775
                _, connectionHeaderSet := header["Connection"]
 
776
                if !connectionHeaderSet {
 
777
                        setHeader.connection = "keep-alive"
 
778
                }
 
779
        } else if !w.req.ProtoAtLeast(1, 1) || w.req.wantsClose() {
 
780
                w.closeAfterReply = true
 
781
        }
 
782
 
 
783
        if header.get("Connection") == "close" || !keepAlivesEnabled {
 
784
                w.closeAfterReply = true
 
785
        }
 
786
 
 
787
        // Per RFC 2616, we should consume the request body before
 
788
        // replying, if the handler hasn't already done so.  But we
 
789
        // don't want to do an unbounded amount of reading here for
 
790
        // DoS reasons, so we only try up to a threshold.
 
791
        if w.req.ContentLength != 0 && !w.closeAfterReply {
 
792
                ecr, isExpecter := w.req.Body.(*expectContinueReader)
 
793
                if !isExpecter || ecr.resp.wroteContinue {
 
794
                        n, _ := io.CopyN(ioutil.Discard, w.req.Body, maxPostHandlerReadBytes+1)
 
795
                        if n >= maxPostHandlerReadBytes {
 
796
                                w.requestTooLarge()
 
797
                                delHeader("Connection")
 
798
                                setHeader.connection = "close"
 
799
                        } else {
 
800
                                w.req.Body.Close()
 
801
                        }
 
802
                }
 
803
        }
 
804
 
 
805
        code := w.status
 
806
        if !bodyAllowedForStatus(code) {
 
807
                // Must not have body.
 
808
                // RFC 2616 section 10.3.5: "the response MUST NOT include other entity-headers"
 
809
                for _, k := range []string{"Content-Type", "Content-Length", "Transfer-Encoding"} {
 
810
                        delHeader(k)
 
811
                }
 
812
        } else {
 
813
                // If no content type, apply sniffing algorithm to body.
 
814
                _, haveType := header["Content-Type"]
 
815
                if !haveType {
 
816
                        setHeader.contentType = DetectContentType(p)
 
817
                }
 
818
        }
 
819
 
 
820
        if _, ok := header["Date"]; !ok {
 
821
                setHeader.date = appendTime(cw.res.dateBuf[:0], time.Now())
 
822
        }
 
823
 
 
824
        te := header.get("Transfer-Encoding")
 
825
        hasTE := te != ""
 
826
        if hasCL && hasTE && te != "identity" {
 
827
                // TODO: return an error if WriteHeader gets a return parameter
 
828
                // For now just ignore the Content-Length.
 
829
                w.conn.server.logf("http: WriteHeader called with both Transfer-Encoding of %q and a Content-Length of %d",
 
830
                        te, w.contentLength)
 
831
                delHeader("Content-Length")
 
832
                hasCL = false
 
833
        }
 
834
 
 
835
        if w.req.Method == "HEAD" || !bodyAllowedForStatus(code) {
 
836
                // do nothing
 
837
        } else if code == StatusNoContent {
 
838
                delHeader("Transfer-Encoding")
 
839
        } else if hasCL {
 
840
                delHeader("Transfer-Encoding")
 
841
        } else if w.req.ProtoAtLeast(1, 1) {
 
842
                // HTTP/1.1 or greater: use chunked transfer encoding
 
843
                // to avoid closing the connection at EOF.
 
844
                // TODO: this blows away any custom or stacked Transfer-Encoding they
 
845
                // might have set.  Deal with that as need arises once we have a valid
 
846
                // use case.
 
847
                cw.chunking = true
 
848
                setHeader.transferEncoding = "chunked"
 
849
        } else {
 
850
                // HTTP version < 1.1: cannot do chunked transfer
 
851
                // encoding and we don't know the Content-Length so
 
852
                // signal EOF by closing connection.
 
853
                w.closeAfterReply = true
 
854
                delHeader("Transfer-Encoding") // in case already set
 
855
        }
 
856
 
 
857
        // Cannot use Content-Length with non-identity Transfer-Encoding.
 
858
        if cw.chunking {
 
859
                delHeader("Content-Length")
 
860
        }
 
861
        if !w.req.ProtoAtLeast(1, 0) {
 
862
                return
 
863
        }
 
864
 
 
865
        if w.closeAfterReply && (!keepAlivesEnabled || !hasToken(cw.header.get("Connection"), "close")) {
 
866
                delHeader("Connection")
 
867
                if w.req.ProtoAtLeast(1, 1) {
 
868
                        setHeader.connection = "close"
 
869
                }
 
870
        }
 
871
 
 
872
        w.conn.buf.WriteString(statusLine(w.req, code))
 
873
        cw.header.WriteSubset(w.conn.buf, excludeHeader)
 
874
        setHeader.Write(w.conn.buf.Writer)
 
875
        w.conn.buf.Write(crlf)
 
876
}
 
877
 
 
878
// statusLines is a cache of Status-Line strings, keyed by code (for
 
879
// HTTP/1.1) or negative code (for HTTP/1.0). This is faster than a
 
880
// map keyed by struct of two fields. This map's max size is bounded
 
881
// by 2*len(statusText), two protocol types for each known official
 
882
// status code in the statusText map.
 
883
var (
 
884
        statusMu    sync.RWMutex
 
885
        statusLines = make(map[int]string)
 
886
)
 
887
 
 
888
// statusLine returns a response Status-Line (RFC 2616 Section 6.1)
 
889
// for the given request and response status code.
 
890
func statusLine(req *Request, code int) string {
 
891
        // Fast path:
 
892
        key := code
 
893
        proto11 := req.ProtoAtLeast(1, 1)
 
894
        if !proto11 {
 
895
                key = -key
 
896
        }
 
897
        statusMu.RLock()
 
898
        line, ok := statusLines[key]
 
899
        statusMu.RUnlock()
 
900
        if ok {
 
901
                return line
 
902
        }
 
903
 
 
904
        // Slow path:
 
905
        proto := "HTTP/1.0"
 
906
        if proto11 {
 
907
                proto = "HTTP/1.1"
 
908
        }
 
909
        codestring := strconv.Itoa(code)
 
910
        text, ok := statusText[code]
 
911
        if !ok {
 
912
                text = "status code " + codestring
 
913
        }
 
914
        line = proto + " " + codestring + " " + text + "\r\n"
 
915
        if ok {
 
916
                statusMu.Lock()
 
917
                defer statusMu.Unlock()
 
918
                statusLines[key] = line
 
919
        }
 
920
        return line
 
921
}
 
922
 
 
923
// bodyAllowed returns true if a Write is allowed for this response type.
 
924
// It's illegal to call this before the header has been flushed.
 
925
func (w *response) bodyAllowed() bool {
 
926
        if !w.wroteHeader {
 
927
                panic("")
 
928
        }
 
929
        return bodyAllowedForStatus(w.status)
 
930
}
 
931
 
 
932
// The Life Of A Write is like this:
 
933
//
 
934
// Handler starts. No header has been sent. The handler can either
 
935
// write a header, or just start writing.  Writing before sending a header
 
936
// sends an implicitly empty 200 OK header.
 
937
//
 
938
// If the handler didn't declare a Content-Length up front, we either
 
939
// go into chunking mode or, if the handler finishes running before
 
940
// the chunking buffer size, we compute a Content-Length and send that
 
941
// in the header instead.
 
942
//
 
943
// Likewise, if the handler didn't set a Content-Type, we sniff that
 
944
// from the initial chunk of output.
 
945
//
 
946
// The Writers are wired together like:
 
947
//
 
948
// 1. *response (the ResponseWriter) ->
 
949
// 2. (*response).w, a *bufio.Writer of bufferBeforeChunkingSize bytes
 
950
// 3. chunkWriter.Writer (whose writeHeader finalizes Content-Length/Type)
 
951
//    and which writes the chunk headers, if needed.
 
952
// 4. conn.buf, a bufio.Writer of default (4kB) bytes
 
953
// 5. the rwc, the net.Conn.
 
954
//
 
955
// TODO(bradfitz): short-circuit some of the buffering when the
 
956
// initial header contains both a Content-Type and Content-Length.
 
957
// Also short-circuit in (1) when the header's been sent and not in
 
958
// chunking mode, writing directly to (4) instead, if (2) has no
 
959
// buffered data.  More generally, we could short-circuit from (1) to
 
960
// (3) even in chunking mode if the write size from (1) is over some
 
961
// threshold and nothing is in (2).  The answer might be mostly making
 
962
// bufferBeforeChunkingSize smaller and having bufio's fast-paths deal
 
963
// with this instead.
 
964
func (w *response) Write(data []byte) (n int, err error) {
 
965
        return w.write(len(data), data, "")
 
966
}
 
967
 
 
968
func (w *response) WriteString(data string) (n int, err error) {
 
969
        return w.write(len(data), nil, data)
 
970
}
 
971
 
 
972
// either dataB or dataS is non-zero.
 
973
func (w *response) write(lenData int, dataB []byte, dataS string) (n int, err error) {
 
974
        if w.conn.hijacked() {
 
975
                w.conn.server.logf("http: response.Write on hijacked connection")
 
976
                return 0, ErrHijacked
 
977
        }
 
978
        if !w.wroteHeader {
 
979
                w.WriteHeader(StatusOK)
 
980
        }
 
981
        if lenData == 0 {
 
982
                return 0, nil
 
983
        }
 
984
        if !w.bodyAllowed() {
 
985
                return 0, ErrBodyNotAllowed
 
986
        }
 
987
 
 
988
        w.written += int64(lenData) // ignoring errors, for errorKludge
 
989
        if w.contentLength != -1 && w.written > w.contentLength {
 
990
                return 0, ErrContentLength
 
991
        }
 
992
        if dataB != nil {
 
993
                return w.w.Write(dataB)
 
994
        } else {
 
995
                return w.w.WriteString(dataS)
 
996
        }
 
997
}
 
998
 
 
999
func (w *response) finishRequest() {
 
1000
        w.handlerDone = true
 
1001
 
 
1002
        if !w.wroteHeader {
 
1003
                w.WriteHeader(StatusOK)
 
1004
        }
 
1005
 
 
1006
        w.w.Flush()
 
1007
        putBufioWriter(w.w)
 
1008
        w.cw.close()
 
1009
        w.conn.buf.Flush()
 
1010
 
 
1011
        // Close the body (regardless of w.closeAfterReply) so we can
 
1012
        // re-use its bufio.Reader later safely.
 
1013
        w.req.Body.Close()
 
1014
 
 
1015
        if w.req.MultipartForm != nil {
 
1016
                w.req.MultipartForm.RemoveAll()
 
1017
        }
 
1018
 
 
1019
        if w.req.Method != "HEAD" && w.contentLength != -1 && w.bodyAllowed() && w.contentLength != w.written {
 
1020
                // Did not write enough. Avoid getting out of sync.
 
1021
                w.closeAfterReply = true
 
1022
        }
 
1023
}
 
1024
 
 
1025
func (w *response) Flush() {
 
1026
        if !w.wroteHeader {
 
1027
                w.WriteHeader(StatusOK)
 
1028
        }
 
1029
        w.w.Flush()
 
1030
        w.cw.flush()
 
1031
}
 
1032
 
 
1033
func (c *conn) finalFlush() {
 
1034
        if c.buf != nil {
 
1035
                c.buf.Flush()
 
1036
 
 
1037
                // Steal the bufio.Reader (~4KB worth of memory) and its associated
 
1038
                // reader for a future connection.
 
1039
                putBufioReader(c.buf.Reader)
 
1040
 
 
1041
                // Steal the bufio.Writer (~4KB worth of memory) and its associated
 
1042
                // writer for a future connection.
 
1043
                putBufioWriter(c.buf.Writer)
 
1044
 
 
1045
                c.buf = nil
 
1046
        }
 
1047
}
 
1048
 
 
1049
// Close the connection.
 
1050
func (c *conn) close() {
 
1051
        c.finalFlush()
 
1052
        if c.rwc != nil {
 
1053
                c.rwc.Close()
 
1054
                c.rwc = nil
 
1055
        }
 
1056
}
 
1057
 
 
1058
// rstAvoidanceDelay is the amount of time we sleep after closing the
 
1059
// write side of a TCP connection before closing the entire socket.
 
1060
// By sleeping, we increase the chances that the client sees our FIN
 
1061
// and processes its final data before they process the subsequent RST
 
1062
// from closing a connection with known unread data.
 
1063
// This RST seems to occur mostly on BSD systems. (And Windows?)
 
1064
// This timeout is somewhat arbitrary (~latency around the planet).
 
1065
const rstAvoidanceDelay = 500 * time.Millisecond
 
1066
 
 
1067
// closeWrite flushes any outstanding data and sends a FIN packet (if
 
1068
// client is connected via TCP), signalling that we're done.  We then
 
1069
// pause for a bit, hoping the client processes it before `any
 
1070
// subsequent RST.
 
1071
//
 
1072
// See http://golang.org/issue/3595
 
1073
func (c *conn) closeWriteAndWait() {
 
1074
        c.finalFlush()
 
1075
        if tcp, ok := c.rwc.(*net.TCPConn); ok {
 
1076
                tcp.CloseWrite()
 
1077
        }
 
1078
        time.Sleep(rstAvoidanceDelay)
 
1079
}
 
1080
 
 
1081
// validNPN reports whether the proto is not a blacklisted Next
 
1082
// Protocol Negotiation protocol.  Empty and built-in protocol types
 
1083
// are blacklisted and can't be overridden with alternate
 
1084
// implementations.
 
1085
func validNPN(proto string) bool {
 
1086
        switch proto {
 
1087
        case "", "http/1.1", "http/1.0":
 
1088
                return false
 
1089
        }
 
1090
        return true
 
1091
}
 
1092
 
 
1093
func (c *conn) setState(nc net.Conn, state ConnState) {
 
1094
        if hook := c.server.ConnState; hook != nil {
 
1095
                hook(nc, state)
 
1096
        }
 
1097
}
 
1098
 
 
1099
// Serve a new connection.
 
1100
func (c *conn) serve() {
 
1101
        origConn := c.rwc // copy it before it's set nil on Close or Hijack
 
1102
        defer func() {
 
1103
                if err := recover(); err != nil {
 
1104
                        const size = 64 << 10
 
1105
                        buf := make([]byte, size)
 
1106
                        buf = buf[:runtime.Stack(buf, false)]
 
1107
                        c.server.logf("http: panic serving %v: %v\n%s", c.remoteAddr, err, buf)
 
1108
                }
 
1109
                if !c.hijacked() {
 
1110
                        c.close()
 
1111
                        c.setState(origConn, StateClosed)
 
1112
                }
 
1113
        }()
 
1114
 
 
1115
        if tlsConn, ok := c.rwc.(*tls.Conn); ok {
 
1116
                if d := c.server.ReadTimeout; d != 0 {
 
1117
                        c.rwc.SetReadDeadline(time.Now().Add(d))
 
1118
                }
 
1119
                if d := c.server.WriteTimeout; d != 0 {
 
1120
                        c.rwc.SetWriteDeadline(time.Now().Add(d))
 
1121
                }
 
1122
                if err := tlsConn.Handshake(); err != nil {
 
1123
                        c.server.logf("http: TLS handshake error from %s: %v", c.rwc.RemoteAddr(), err)
 
1124
                        return
 
1125
                }
 
1126
                c.tlsState = new(tls.ConnectionState)
 
1127
                *c.tlsState = tlsConn.ConnectionState()
 
1128
                if proto := c.tlsState.NegotiatedProtocol; validNPN(proto) {
 
1129
                        if fn := c.server.TLSNextProto[proto]; fn != nil {
 
1130
                                h := initNPNRequest{tlsConn, serverHandler{c.server}}
 
1131
                                fn(c.server, tlsConn, h)
 
1132
                        }
 
1133
                        return
 
1134
                }
 
1135
        }
 
1136
 
 
1137
        for {
 
1138
                w, err := c.readRequest()
 
1139
                if c.lr.N != c.server.initialLimitedReaderSize() {
 
1140
                        // If we read any bytes off the wire, we're active.
 
1141
                        c.setState(c.rwc, StateActive)
 
1142
                }
 
1143
                if err != nil {
 
1144
                        if err == errTooLarge {
 
1145
                                // Their HTTP client may or may not be
 
1146
                                // able to read this if we're
 
1147
                                // responding to them and hanging up
 
1148
                                // while they're still writing their
 
1149
                                // request.  Undefined behavior.
 
1150
                                io.WriteString(c.rwc, "HTTP/1.1 413 Request Entity Too Large\r\n\r\n")
 
1151
                                c.closeWriteAndWait()
 
1152
                                break
 
1153
                        } else if err == io.EOF {
 
1154
                                break // Don't reply
 
1155
                        } else if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
 
1156
                                break // Don't reply
 
1157
                        }
 
1158
                        io.WriteString(c.rwc, "HTTP/1.1 400 Bad Request\r\n\r\n")
 
1159
                        break
 
1160
                }
 
1161
 
 
1162
                // Expect 100 Continue support
 
1163
                req := w.req
 
1164
                if req.expectsContinue() {
 
1165
                        if req.ProtoAtLeast(1, 1) {
 
1166
                                // Wrap the Body reader with one that replies on the connection
 
1167
                                req.Body = &expectContinueReader{readCloser: req.Body, resp: w}
 
1168
                        }
 
1169
                        if req.ContentLength == 0 {
 
1170
                                w.Header().Set("Connection", "close")
 
1171
                                w.WriteHeader(StatusBadRequest)
 
1172
                                w.finishRequest()
 
1173
                                break
 
1174
                        }
 
1175
                        req.Header.Del("Expect")
 
1176
                } else if req.Header.get("Expect") != "" {
 
1177
                        w.sendExpectationFailed()
 
1178
                        break
 
1179
                }
 
1180
 
 
1181
                // HTTP cannot have multiple simultaneous active requests.[*]
 
1182
                // Until the server replies to this request, it can't read another,
 
1183
                // so we might as well run the handler in this goroutine.
 
1184
                // [*] Not strictly true: HTTP pipelining.  We could let them all process
 
1185
                // in parallel even if their responses need to be serialized.
 
1186
                serverHandler{c.server}.ServeHTTP(w, w.req)
 
1187
                if c.hijacked() {
 
1188
                        return
 
1189
                }
 
1190
                w.finishRequest()
 
1191
                if w.closeAfterReply {
 
1192
                        if w.requestBodyLimitHit {
 
1193
                                c.closeWriteAndWait()
 
1194
                        }
 
1195
                        break
 
1196
                }
 
1197
                c.setState(c.rwc, StateIdle)
 
1198
        }
 
1199
}
 
1200
 
 
1201
func (w *response) sendExpectationFailed() {
 
1202
        // TODO(bradfitz): let ServeHTTP handlers handle
 
1203
        // requests with non-standard expectation[s]? Seems
 
1204
        // theoretical at best, and doesn't fit into the
 
1205
        // current ServeHTTP model anyway.  We'd need to
 
1206
        // make the ResponseWriter an optional
 
1207
        // "ExpectReplier" interface or something.
 
1208
        //
 
1209
        // For now we'll just obey RFC 2616 14.20 which says
 
1210
        // "If a server receives a request containing an
 
1211
        // Expect field that includes an expectation-
 
1212
        // extension that it does not support, it MUST
 
1213
        // respond with a 417 (Expectation Failed) status."
 
1214
        w.Header().Set("Connection", "close")
 
1215
        w.WriteHeader(StatusExpectationFailed)
 
1216
        w.finishRequest()
 
1217
}
 
1218
 
 
1219
// Hijack implements the Hijacker.Hijack method. Our response is both a ResponseWriter
 
1220
// and a Hijacker.
 
1221
func (w *response) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
 
1222
        if w.wroteHeader {
 
1223
                w.cw.flush()
 
1224
        }
 
1225
        // Release the bufioWriter that writes to the chunk writer, it is not
 
1226
        // used after a connection has been hijacked.
 
1227
        rwc, buf, err = w.conn.hijack()
 
1228
        if err == nil {
 
1229
                putBufioWriter(w.w)
 
1230
                w.w = nil
 
1231
        }
 
1232
        return rwc, buf, err
 
1233
}
 
1234
 
 
1235
func (w *response) CloseNotify() <-chan bool {
 
1236
        return w.conn.closeNotify()
 
1237
}
 
1238
 
 
1239
// The HandlerFunc type is an adapter to allow the use of
 
1240
// ordinary functions as HTTP handlers.  If f is a function
 
1241
// with the appropriate signature, HandlerFunc(f) is a
 
1242
// Handler object that calls f.
 
1243
type HandlerFunc func(ResponseWriter, *Request)
 
1244
 
 
1245
// ServeHTTP calls f(w, r).
 
1246
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
 
1247
        f(w, r)
 
1248
}
 
1249
 
 
1250
// Helper handlers
 
1251
 
 
1252
// Error replies to the request with the specified error message and HTTP code.
 
1253
// The error message should be plain text.
 
1254
func Error(w ResponseWriter, error string, code int) {
 
1255
        w.Header().Set("Content-Type", "text/plain; charset=utf-8")
 
1256
        w.WriteHeader(code)
 
1257
        fmt.Fprintln(w, error)
 
1258
}
 
1259
 
 
1260
// NotFound replies to the request with an HTTP 404 not found error.
 
1261
func NotFound(w ResponseWriter, r *Request) { Error(w, "404 page not found", StatusNotFound) }
 
1262
 
 
1263
// NotFoundHandler returns a simple request handler
 
1264
// that replies to each request with a ``404 page not found'' reply.
 
1265
func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
 
1266
 
 
1267
// StripPrefix returns a handler that serves HTTP requests
 
1268
// by removing the given prefix from the request URL's Path
 
1269
// and invoking the handler h. StripPrefix handles a
 
1270
// request for a path that doesn't begin with prefix by
 
1271
// replying with an HTTP 404 not found error.
 
1272
func StripPrefix(prefix string, h Handler) Handler {
 
1273
        if prefix == "" {
 
1274
                return h
 
1275
        }
 
1276
        return HandlerFunc(func(w ResponseWriter, r *Request) {
 
1277
                if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {
 
1278
                        r.URL.Path = p
 
1279
                        h.ServeHTTP(w, r)
 
1280
                } else {
 
1281
                        NotFound(w, r)
 
1282
                }
 
1283
        })
 
1284
}
 
1285
 
 
1286
// Redirect replies to the request with a redirect to url,
 
1287
// which may be a path relative to the request path.
 
1288
func Redirect(w ResponseWriter, r *Request, urlStr string, code int) {
 
1289
        if u, err := url.Parse(urlStr); err == nil {
 
1290
                // If url was relative, make absolute by
 
1291
                // combining with request path.
 
1292
                // The browser would probably do this for us,
 
1293
                // but doing it ourselves is more reliable.
 
1294
 
 
1295
                // NOTE(rsc): RFC 2616 says that the Location
 
1296
                // line must be an absolute URI, like
 
1297
                // "http://www.google.com/redirect/",
 
1298
                // not a path like "/redirect/".
 
1299
                // Unfortunately, we don't know what to
 
1300
                // put in the host name section to get the
 
1301
                // client to connect to us again, so we can't
 
1302
                // know the right absolute URI to send back.
 
1303
                // Because of this problem, no one pays attention
 
1304
                // to the RFC; they all send back just a new path.
 
1305
                // So do we.
 
1306
                oldpath := r.URL.Path
 
1307
                if oldpath == "" { // should not happen, but avoid a crash if it does
 
1308
                        oldpath = "/"
 
1309
                }
 
1310
                if u.Scheme == "" {
 
1311
                        // no leading http://server
 
1312
                        if urlStr == "" || urlStr[0] != '/' {
 
1313
                                // make relative path absolute
 
1314
                                olddir, _ := path.Split(oldpath)
 
1315
                                urlStr = olddir + urlStr
 
1316
                        }
 
1317
 
 
1318
                        var query string
 
1319
                        if i := strings.Index(urlStr, "?"); i != -1 {
 
1320
                                urlStr, query = urlStr[:i], urlStr[i:]
 
1321
                        }
 
1322
 
 
1323
                        // clean up but preserve trailing slash
 
1324
                        trailing := strings.HasSuffix(urlStr, "/")
 
1325
                        urlStr = path.Clean(urlStr)
 
1326
                        if trailing && !strings.HasSuffix(urlStr, "/") {
 
1327
                                urlStr += "/"
 
1328
                        }
 
1329
                        urlStr += query
 
1330
                }
 
1331
        }
 
1332
 
 
1333
        w.Header().Set("Location", urlStr)
 
1334
        w.WriteHeader(code)
 
1335
 
 
1336
        // RFC2616 recommends that a short note "SHOULD" be included in the
 
1337
        // response because older user agents may not understand 301/307.
 
1338
        // Shouldn't send the response for POST or HEAD; that leaves GET.
 
1339
        if r.Method == "GET" {
 
1340
                note := "<a href=\"" + htmlEscape(urlStr) + "\">" + statusText[code] + "</a>.\n"
 
1341
                fmt.Fprintln(w, note)
 
1342
        }
 
1343
}
 
1344
 
 
1345
var htmlReplacer = strings.NewReplacer(
 
1346
        "&", "&amp;",
 
1347
        "<", "&lt;",
 
1348
        ">", "&gt;",
 
1349
        // "&#34;" is shorter than "&quot;".
 
1350
        `"`, "&#34;",
 
1351
        // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
 
1352
        "'", "&#39;",
 
1353
)
 
1354
 
 
1355
func htmlEscape(s string) string {
 
1356
        return htmlReplacer.Replace(s)
 
1357
}
 
1358
 
 
1359
// Redirect to a fixed URL
 
1360
type redirectHandler struct {
 
1361
        url  string
 
1362
        code int
 
1363
}
 
1364
 
 
1365
func (rh *redirectHandler) ServeHTTP(w ResponseWriter, r *Request) {
 
1366
        Redirect(w, r, rh.url, rh.code)
 
1367
}
 
1368
 
 
1369
// RedirectHandler returns a request handler that redirects
 
1370
// each request it receives to the given url using the given
 
1371
// status code.
 
1372
func RedirectHandler(url string, code int) Handler {
 
1373
        return &redirectHandler{url, code}
 
1374
}
 
1375
 
 
1376
// ServeMux is an HTTP request multiplexer.
 
1377
// It matches the URL of each incoming request against a list of registered
 
1378
// patterns and calls the handler for the pattern that
 
1379
// most closely matches the URL.
 
1380
//
 
1381
// Patterns name fixed, rooted paths, like "/favicon.ico",
 
1382
// or rooted subtrees, like "/images/" (note the trailing slash).
 
1383
// Longer patterns take precedence over shorter ones, so that
 
1384
// if there are handlers registered for both "/images/"
 
1385
// and "/images/thumbnails/", the latter handler will be
 
1386
// called for paths beginning "/images/thumbnails/" and the
 
1387
// former will receive requests for any other paths in the
 
1388
// "/images/" subtree.
 
1389
//
 
1390
// Note that since a pattern ending in a slash names a rooted subtree,
 
1391
// the pattern "/" matches all paths not matched by other registered
 
1392
// patterns, not just the URL with Path == "/".
 
1393
//
 
1394
// Patterns may optionally begin with a host name, restricting matches to
 
1395
// URLs on that host only.  Host-specific patterns take precedence over
 
1396
// general patterns, so that a handler might register for the two patterns
 
1397
// "/codesearch" and "codesearch.google.com/" without also taking over
 
1398
// requests for "http://www.google.com/".
 
1399
//
 
1400
// ServeMux also takes care of sanitizing the URL request path,
 
1401
// redirecting any request containing . or .. elements to an
 
1402
// equivalent .- and ..-free URL.
 
1403
type ServeMux struct {
 
1404
        mu    sync.RWMutex
 
1405
        m     map[string]muxEntry
 
1406
        hosts bool // whether any patterns contain hostnames
 
1407
}
 
1408
 
 
1409
type muxEntry struct {
 
1410
        explicit bool
 
1411
        h        Handler
 
1412
        pattern  string
 
1413
}
 
1414
 
 
1415
// NewServeMux allocates and returns a new ServeMux.
 
1416
func NewServeMux() *ServeMux { return &ServeMux{m: make(map[string]muxEntry)} }
 
1417
 
 
1418
// DefaultServeMux is the default ServeMux used by Serve.
 
1419
var DefaultServeMux = NewServeMux()
 
1420
 
 
1421
// Does path match pattern?
 
1422
func pathMatch(pattern, path string) bool {
 
1423
        if len(pattern) == 0 {
 
1424
                // should not happen
 
1425
                return false
 
1426
        }
 
1427
        n := len(pattern)
 
1428
        if pattern[n-1] != '/' {
 
1429
                return pattern == path
 
1430
        }
 
1431
        return len(path) >= n && path[0:n] == pattern
 
1432
}
 
1433
 
 
1434
// Return the canonical path for p, eliminating . and .. elements.
 
1435
func cleanPath(p string) string {
 
1436
        if p == "" {
 
1437
                return "/"
 
1438
        }
 
1439
        if p[0] != '/' {
 
1440
                p = "/" + p
 
1441
        }
 
1442
        np := path.Clean(p)
 
1443
        // path.Clean removes trailing slash except for root;
 
1444
        // put the trailing slash back if necessary.
 
1445
        if p[len(p)-1] == '/' && np != "/" {
 
1446
                np += "/"
 
1447
        }
 
1448
        return np
 
1449
}
 
1450
 
 
1451
// Find a handler on a handler map given a path string
 
1452
// Most-specific (longest) pattern wins
 
1453
func (mux *ServeMux) match(path string) (h Handler, pattern string) {
 
1454
        var n = 0
 
1455
        for k, v := range mux.m {
 
1456
                if !pathMatch(k, path) {
 
1457
                        continue
 
1458
                }
 
1459
                if h == nil || len(k) > n {
 
1460
                        n = len(k)
 
1461
                        h = v.h
 
1462
                        pattern = v.pattern
 
1463
                }
 
1464
        }
 
1465
        return
 
1466
}
 
1467
 
 
1468
// Handler returns the handler to use for the given request,
 
1469
// consulting r.Method, r.Host, and r.URL.Path. It always returns
 
1470
// a non-nil handler. If the path is not in its canonical form, the
 
1471
// handler will be an internally-generated handler that redirects
 
1472
// to the canonical path.
 
1473
//
 
1474
// Handler also returns the registered pattern that matches the
 
1475
// request or, in the case of internally-generated redirects,
 
1476
// the pattern that will match after following the redirect.
 
1477
//
 
1478
// If there is no registered handler that applies to the request,
 
1479
// Handler returns a ``page not found'' handler and an empty pattern.
 
1480
func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
 
1481
        if r.Method != "CONNECT" {
 
1482
                if p := cleanPath(r.URL.Path); p != r.URL.Path {
 
1483
                        _, pattern = mux.handler(r.Host, p)
 
1484
                        url := *r.URL
 
1485
                        url.Path = p
 
1486
                        return RedirectHandler(url.String(), StatusMovedPermanently), pattern
 
1487
                }
 
1488
        }
 
1489
 
 
1490
        return mux.handler(r.Host, r.URL.Path)
 
1491
}
 
1492
 
 
1493
// handler is the main implementation of Handler.
 
1494
// The path is known to be in canonical form, except for CONNECT methods.
 
1495
func (mux *ServeMux) handler(host, path string) (h Handler, pattern string) {
 
1496
        mux.mu.RLock()
 
1497
        defer mux.mu.RUnlock()
 
1498
 
 
1499
        // Host-specific pattern takes precedence over generic ones
 
1500
        if mux.hosts {
 
1501
                h, pattern = mux.match(host + path)
 
1502
        }
 
1503
        if h == nil {
 
1504
                h, pattern = mux.match(path)
 
1505
        }
 
1506
        if h == nil {
 
1507
                h, pattern = NotFoundHandler(), ""
 
1508
        }
 
1509
        return
 
1510
}
 
1511
 
 
1512
// ServeHTTP dispatches the request to the handler whose
 
1513
// pattern most closely matches the request URL.
 
1514
func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
 
1515
        if r.RequestURI == "*" {
 
1516
                if r.ProtoAtLeast(1, 1) {
 
1517
                        w.Header().Set("Connection", "close")
 
1518
                }
 
1519
                w.WriteHeader(StatusBadRequest)
 
1520
                return
 
1521
        }
 
1522
        h, _ := mux.Handler(r)
 
1523
        h.ServeHTTP(w, r)
 
1524
}
 
1525
 
 
1526
// Handle registers the handler for the given pattern.
 
1527
// If a handler already exists for pattern, Handle panics.
 
1528
func (mux *ServeMux) Handle(pattern string, handler Handler) {
 
1529
        mux.mu.Lock()
 
1530
        defer mux.mu.Unlock()
 
1531
 
 
1532
        if pattern == "" {
 
1533
                panic("http: invalid pattern " + pattern)
 
1534
        }
 
1535
        if handler == nil {
 
1536
                panic("http: nil handler")
 
1537
        }
 
1538
        if mux.m[pattern].explicit {
 
1539
                panic("http: multiple registrations for " + pattern)
 
1540
        }
 
1541
 
 
1542
        mux.m[pattern] = muxEntry{explicit: true, h: handler, pattern: pattern}
 
1543
 
 
1544
        if pattern[0] != '/' {
 
1545
                mux.hosts = true
 
1546
        }
 
1547
 
 
1548
        // Helpful behavior:
 
1549
        // If pattern is /tree/, insert an implicit permanent redirect for /tree.
 
1550
        // It can be overridden by an explicit registration.
 
1551
        n := len(pattern)
 
1552
        if n > 0 && pattern[n-1] == '/' && !mux.m[pattern[0:n-1]].explicit {
 
1553
                // If pattern contains a host name, strip it and use remaining
 
1554
                // path for redirect.
 
1555
                path := pattern
 
1556
                if pattern[0] != '/' {
 
1557
                        // In pattern, at least the last character is a '/', so
 
1558
                        // strings.Index can't be -1.
 
1559
                        path = pattern[strings.Index(pattern, "/"):]
 
1560
                }
 
1561
                mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(path, StatusMovedPermanently), pattern: pattern}
 
1562
        }
 
1563
}
 
1564
 
 
1565
// HandleFunc registers the handler function for the given pattern.
 
1566
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
 
1567
        mux.Handle(pattern, HandlerFunc(handler))
 
1568
}
 
1569
 
 
1570
// Handle registers the handler for the given pattern
 
1571
// in the DefaultServeMux.
 
1572
// The documentation for ServeMux explains how patterns are matched.
 
1573
func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }
 
1574
 
 
1575
// HandleFunc registers the handler function for the given pattern
 
1576
// in the DefaultServeMux.
 
1577
// The documentation for ServeMux explains how patterns are matched.
 
1578
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
 
1579
        DefaultServeMux.HandleFunc(pattern, handler)
 
1580
}
 
1581
 
 
1582
// Serve accepts incoming HTTP connections on the listener l,
 
1583
// creating a new service goroutine for each.  The service goroutines
 
1584
// read requests and then call handler to reply to them.
 
1585
// Handler is typically nil, in which case the DefaultServeMux is used.
 
1586
func Serve(l net.Listener, handler Handler) error {
 
1587
        srv := &Server{Handler: handler}
 
1588
        return srv.Serve(l)
 
1589
}
 
1590
 
 
1591
// A Server defines parameters for running an HTTP server.
 
1592
// The zero value for Server is a valid configuration.
 
1593
type Server struct {
 
1594
        Addr           string        // TCP address to listen on, ":http" if empty
 
1595
        Handler        Handler       // handler to invoke, http.DefaultServeMux if nil
 
1596
        ReadTimeout    time.Duration // maximum duration before timing out read of the request
 
1597
        WriteTimeout   time.Duration // maximum duration before timing out write of the response
 
1598
        MaxHeaderBytes int           // maximum size of request headers, DefaultMaxHeaderBytes if 0
 
1599
        TLSConfig      *tls.Config   // optional TLS config, used by ListenAndServeTLS
 
1600
 
 
1601
        // TLSNextProto optionally specifies a function to take over
 
1602
        // ownership of the provided TLS connection when an NPN
 
1603
        // protocol upgrade has occurred.  The map key is the protocol
 
1604
        // name negotiated. The Handler argument should be used to
 
1605
        // handle HTTP requests and will initialize the Request's TLS
 
1606
        // and RemoteAddr if not already set.  The connection is
 
1607
        // automatically closed when the function returns.
 
1608
        TLSNextProto map[string]func(*Server, *tls.Conn, Handler)
 
1609
 
 
1610
        // ConnState specifies an optional callback function that is
 
1611
        // called when a client connection changes state. See the
 
1612
        // ConnState type and associated constants for details.
 
1613
        ConnState func(net.Conn, ConnState)
 
1614
 
 
1615
        // ErrorLog specifies an optional logger for errors accepting
 
1616
        // connections and unexpected behavior from handlers.
 
1617
        // If nil, logging goes to os.Stderr via the log package's
 
1618
        // standard logger.
 
1619
        ErrorLog *log.Logger
 
1620
 
 
1621
        disableKeepAlives int32 // accessed atomically.
 
1622
}
 
1623
 
 
1624
// A ConnState represents the state of a client connection to a server.
 
1625
// It's used by the optional Server.ConnState hook.
 
1626
type ConnState int
 
1627
 
 
1628
const (
 
1629
        // StateNew represents a new connection that is expected to
 
1630
        // send a request immediately. Connections begin at this
 
1631
        // state and then transition to either StateActive or
 
1632
        // StateClosed.
 
1633
        StateNew ConnState = iota
 
1634
 
 
1635
        // StateActive represents a connection that has read 1 or more
 
1636
        // bytes of a request. The Server.ConnState hook for
 
1637
        // StateActive fires before the request has entered a handler
 
1638
        // and doesn't fire again until the request has been
 
1639
        // handled. After the request is handled, the state
 
1640
        // transitions to StateClosed, StateHijacked, or StateIdle.
 
1641
        StateActive
 
1642
 
 
1643
        // StateIdle represents a connection that has finished
 
1644
        // handling a request and is in the keep-alive state, waiting
 
1645
        // for a new request. Connections transition from StateIdle
 
1646
        // to either StateActive or StateClosed.
 
1647
        StateIdle
 
1648
 
 
1649
        // StateHijacked represents a hijacked connection.
 
1650
        // This is a terminal state. It does not transition to StateClosed.
 
1651
        StateHijacked
 
1652
 
 
1653
        // StateClosed represents a closed connection.
 
1654
        // This is a terminal state. Hijacked connections do not
 
1655
        // transition to StateClosed.
 
1656
        StateClosed
 
1657
)
 
1658
 
 
1659
var stateName = map[ConnState]string{
 
1660
        StateNew:      "new",
 
1661
        StateActive:   "active",
 
1662
        StateIdle:     "idle",
 
1663
        StateHijacked: "hijacked",
 
1664
        StateClosed:   "closed",
 
1665
}
 
1666
 
 
1667
func (c ConnState) String() string {
 
1668
        return stateName[c]
 
1669
}
 
1670
 
 
1671
// serverHandler delegates to either the server's Handler or
 
1672
// DefaultServeMux and also handles "OPTIONS *" requests.
 
1673
type serverHandler struct {
 
1674
        srv *Server
 
1675
}
 
1676
 
 
1677
func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
 
1678
        handler := sh.srv.Handler
 
1679
        if handler == nil {
 
1680
                handler = DefaultServeMux
 
1681
        }
 
1682
        if req.RequestURI == "*" && req.Method == "OPTIONS" {
 
1683
                handler = globalOptionsHandler{}
 
1684
        }
 
1685
        handler.ServeHTTP(rw, req)
 
1686
}
 
1687
 
 
1688
// ListenAndServe listens on the TCP network address srv.Addr and then
 
1689
// calls Serve to handle requests on incoming connections.  If
 
1690
// srv.Addr is blank, ":http" is used.
 
1691
func (srv *Server) ListenAndServe() error {
 
1692
        addr := srv.Addr
 
1693
        if addr == "" {
 
1694
                addr = ":http"
 
1695
        }
 
1696
        ln, err := net.Listen("tcp", addr)
 
1697
        if err != nil {
 
1698
                return err
 
1699
        }
 
1700
        return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
 
1701
}
 
1702
 
 
1703
// Serve accepts incoming connections on the Listener l, creating a
 
1704
// new service goroutine for each.  The service goroutines read requests and
 
1705
// then call srv.Handler to reply to them.
 
1706
func (srv *Server) Serve(l net.Listener) error {
 
1707
        defer l.Close()
 
1708
        var tempDelay time.Duration // how long to sleep on accept failure
 
1709
        for {
 
1710
                rw, e := l.Accept()
 
1711
                if e != nil {
 
1712
                        if ne, ok := e.(net.Error); ok && ne.Temporary() {
 
1713
                                if tempDelay == 0 {
 
1714
                                        tempDelay = 5 * time.Millisecond
 
1715
                                } else {
 
1716
                                        tempDelay *= 2
 
1717
                                }
 
1718
                                if max := 1 * time.Second; tempDelay > max {
 
1719
                                        tempDelay = max
 
1720
                                }
 
1721
                                srv.logf("http: Accept error: %v; retrying in %v", e, tempDelay)
 
1722
                                time.Sleep(tempDelay)
 
1723
                                continue
 
1724
                        }
 
1725
                        return e
 
1726
                }
 
1727
                tempDelay = 0
 
1728
                c, err := srv.newConn(rw)
 
1729
                if err != nil {
 
1730
                        continue
 
1731
                }
 
1732
                c.setState(c.rwc, StateNew) // before Serve can return
 
1733
                go c.serve()
 
1734
        }
 
1735
}
 
1736
 
 
1737
func (s *Server) doKeepAlives() bool {
 
1738
        return atomic.LoadInt32(&s.disableKeepAlives) == 0
 
1739
}
 
1740
 
 
1741
// SetKeepAlivesEnabled controls whether HTTP keep-alives are enabled.
 
1742
// By default, keep-alives are always enabled. Only very
 
1743
// resource-constrained environments or servers in the process of
 
1744
// shutting down should disable them.
 
1745
func (s *Server) SetKeepAlivesEnabled(v bool) {
 
1746
        if v {
 
1747
                atomic.StoreInt32(&s.disableKeepAlives, 0)
 
1748
        } else {
 
1749
                atomic.StoreInt32(&s.disableKeepAlives, 1)
 
1750
        }
 
1751
}
 
1752
 
 
1753
func (s *Server) logf(format string, args ...interface{}) {
 
1754
        if s.ErrorLog != nil {
 
1755
                s.ErrorLog.Printf(format, args...)
 
1756
        } else {
 
1757
                log.Printf(format, args...)
 
1758
        }
 
1759
}
 
1760
 
 
1761
// ListenAndServe listens on the TCP network address addr
 
1762
// and then calls Serve with handler to handle requests
 
1763
// on incoming connections.  Handler is typically nil,
 
1764
// in which case the DefaultServeMux is used.
 
1765
//
 
1766
// A trivial example server is:
 
1767
//
 
1768
//      package main
 
1769
//
 
1770
//      import (
 
1771
//              "io"
 
1772
//              "launchpad.net/ubuntu-push/http13client"
 
1773
//              "log"
 
1774
//      )
 
1775
//
 
1776
//      // hello world, the web server
 
1777
//      func HelloServer(w http.ResponseWriter, req *http.Request) {
 
1778
//              io.WriteString(w, "hello, world!\n")
 
1779
//      }
 
1780
//
 
1781
//      func main() {
 
1782
//              http.HandleFunc("/hello", HelloServer)
 
1783
//              err := http.ListenAndServe(":12345", nil)
 
1784
//              if err != nil {
 
1785
//                      log.Fatal("ListenAndServe: ", err)
 
1786
//              }
 
1787
//      }
 
1788
func ListenAndServe(addr string, handler Handler) error {
 
1789
        server := &Server{Addr: addr, Handler: handler}
 
1790
        return server.ListenAndServe()
 
1791
}
 
1792
 
 
1793
// ListenAndServeTLS acts identically to ListenAndServe, except that it
 
1794
// expects HTTPS connections. Additionally, files containing a certificate and
 
1795
// matching private key for the server must be provided. If the certificate
 
1796
// is signed by a certificate authority, the certFile should be the concatenation
 
1797
// of the server's certificate followed by the CA's certificate.
 
1798
//
 
1799
// A trivial example server is:
 
1800
//
 
1801
//      import (
 
1802
//              "log"
 
1803
//              "launchpad.net/ubuntu-push/http13client"
 
1804
//      )
 
1805
//
 
1806
//      func handler(w http.ResponseWriter, req *http.Request) {
 
1807
//              w.Header().Set("Content-Type", "text/plain")
 
1808
//              w.Write([]byte("This is an example server.\n"))
 
1809
//      }
 
1810
//
 
1811
//      func main() {
 
1812
//              http.HandleFunc("/", handler)
 
1813
//              log.Printf("About to listen on 10443. Go to https://127.0.0.1:10443/")
 
1814
//              err := http.ListenAndServeTLS(":10443", "cert.pem", "key.pem", nil)
 
1815
//              if err != nil {
 
1816
//                      log.Fatal(err)
 
1817
//              }
 
1818
//      }
 
1819
//
 
1820
// One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem.
 
1821
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Handler) error {
 
1822
        server := &Server{Addr: addr, Handler: handler}
 
1823
        return server.ListenAndServeTLS(certFile, keyFile)
 
1824
}
 
1825
 
 
1826
// ListenAndServeTLS listens on the TCP network address srv.Addr and
 
1827
// then calls Serve to handle requests on incoming TLS connections.
 
1828
//
 
1829
// Filenames containing a certificate and matching private key for
 
1830
// the server must be provided. If the certificate is signed by a
 
1831
// certificate authority, the certFile should be the concatenation
 
1832
// of the server's certificate followed by the CA's certificate.
 
1833
//
 
1834
// If srv.Addr is blank, ":https" is used.
 
1835
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
 
1836
        addr := srv.Addr
 
1837
        if addr == "" {
 
1838
                addr = ":https"
 
1839
        }
 
1840
        config := &tls.Config{}
 
1841
        if srv.TLSConfig != nil {
 
1842
                *config = *srv.TLSConfig
 
1843
        }
 
1844
        if config.NextProtos == nil {
 
1845
                config.NextProtos = []string{"http/1.1"}
 
1846
        }
 
1847
 
 
1848
        var err error
 
1849
        config.Certificates = make([]tls.Certificate, 1)
 
1850
        config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
 
1851
        if err != nil {
 
1852
                return err
 
1853
        }
 
1854
 
 
1855
        ln, err := net.Listen("tcp", addr)
 
1856
        if err != nil {
 
1857
                return err
 
1858
        }
 
1859
 
 
1860
        tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, config)
 
1861
        return srv.Serve(tlsListener)
 
1862
}
 
1863
 
 
1864
// TimeoutHandler returns a Handler that runs h with the given time limit.
 
1865
//
 
1866
// The new Handler calls h.ServeHTTP to handle each request, but if a
 
1867
// call runs for longer than its time limit, the handler responds with
 
1868
// a 503 Service Unavailable error and the given message in its body.
 
1869
// (If msg is empty, a suitable default message will be sent.)
 
1870
// After such a timeout, writes by h to its ResponseWriter will return
 
1871
// ErrHandlerTimeout.
 
1872
func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler {
 
1873
        f := func() <-chan time.Time {
 
1874
                return time.After(dt)
 
1875
        }
 
1876
        return &timeoutHandler{h, f, msg}
 
1877
}
 
1878
 
 
1879
// ErrHandlerTimeout is returned on ResponseWriter Write calls
 
1880
// in handlers which have timed out.
 
1881
var ErrHandlerTimeout = errors.New("http: Handler timeout")
 
1882
 
 
1883
type timeoutHandler struct {
 
1884
        handler Handler
 
1885
        timeout func() <-chan time.Time // returns channel producing a timeout
 
1886
        body    string
 
1887
}
 
1888
 
 
1889
func (h *timeoutHandler) errorBody() string {
 
1890
        if h.body != "" {
 
1891
                return h.body
 
1892
        }
 
1893
        return "<html><head><title>Timeout</title></head><body><h1>Timeout</h1></body></html>"
 
1894
}
 
1895
 
 
1896
func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) {
 
1897
        done := make(chan bool, 1)
 
1898
        tw := &timeoutWriter{w: w}
 
1899
        go func() {
 
1900
                h.handler.ServeHTTP(tw, r)
 
1901
                done <- true
 
1902
        }()
 
1903
        select {
 
1904
        case <-done:
 
1905
                return
 
1906
        case <-h.timeout():
 
1907
                tw.mu.Lock()
 
1908
                defer tw.mu.Unlock()
 
1909
                if !tw.wroteHeader {
 
1910
                        tw.w.WriteHeader(StatusServiceUnavailable)
 
1911
                        tw.w.Write([]byte(h.errorBody()))
 
1912
                }
 
1913
                tw.timedOut = true
 
1914
        }
 
1915
}
 
1916
 
 
1917
type timeoutWriter struct {
 
1918
        w ResponseWriter
 
1919
 
 
1920
        mu          sync.Mutex
 
1921
        timedOut    bool
 
1922
        wroteHeader bool
 
1923
}
 
1924
 
 
1925
func (tw *timeoutWriter) Header() Header {
 
1926
        return tw.w.Header()
 
1927
}
 
1928
 
 
1929
func (tw *timeoutWriter) Write(p []byte) (int, error) {
 
1930
        tw.mu.Lock()
 
1931
        timedOut := tw.timedOut
 
1932
        tw.mu.Unlock()
 
1933
        if timedOut {
 
1934
                return 0, ErrHandlerTimeout
 
1935
        }
 
1936
        return tw.w.Write(p)
 
1937
}
 
1938
 
 
1939
func (tw *timeoutWriter) WriteHeader(code int) {
 
1940
        tw.mu.Lock()
 
1941
        if tw.timedOut || tw.wroteHeader {
 
1942
                tw.mu.Unlock()
 
1943
                return
 
1944
        }
 
1945
        tw.wroteHeader = true
 
1946
        tw.mu.Unlock()
 
1947
        tw.w.WriteHeader(code)
 
1948
}
 
1949
 
 
1950
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
 
1951
// connections. It's used by ListenAndServe and ListenAndServeTLS so
 
1952
// dead TCP connections (e.g. closing laptop mid-download) eventually
 
1953
// go away.
 
1954
type tcpKeepAliveListener struct {
 
1955
        *net.TCPListener
 
1956
}
 
1957
 
 
1958
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
 
1959
        tc, err := ln.AcceptTCP()
 
1960
        if err != nil {
 
1961
                return
 
1962
        }
 
1963
        tc.SetKeepAlive(true)
 
1964
        tc.SetKeepAlivePeriod(3 * time.Minute)
 
1965
        return tc, nil
 
1966
}
 
1967
 
 
1968
// globalOptionsHandler responds to "OPTIONS *" requests.
 
1969
type globalOptionsHandler struct{}
 
1970
 
 
1971
func (globalOptionsHandler) ServeHTTP(w ResponseWriter, r *Request) {
 
1972
        w.Header().Set("Content-Length", "0")
 
1973
        if r.ContentLength != 0 {
 
1974
                // Read up to 4KB of OPTIONS body (as mentioned in the
 
1975
                // spec as being reserved for future use), but anything
 
1976
                // over that is considered a waste of server resources
 
1977
                // (or an attack) and we abort and close the connection,
 
1978
                // courtesy of MaxBytesReader's EOF behavior.
 
1979
                mb := MaxBytesReader(w, r.Body, 4<<10)
 
1980
                io.Copy(ioutil.Discard, mb)
 
1981
        }
 
1982
}
 
1983
 
 
1984
// eofReader is a non-nil io.ReadCloser that always returns EOF.
 
1985
// It embeds a *strings.Reader so it still has a WriteTo method
 
1986
// and io.Copy won't need a buffer.
 
1987
var eofReader = &struct {
 
1988
        *strings.Reader
 
1989
        io.Closer
 
1990
}{
 
1991
        strings.NewReader(""),
 
1992
        ioutil.NopCloser(nil),
 
1993
}
 
1994
 
 
1995
// initNPNRequest is an HTTP handler that initializes certain
 
1996
// uninitialized fields in its *Request. Such partially-initialized
 
1997
// Requests come from NPN protocol handlers.
 
1998
type initNPNRequest struct {
 
1999
        c *tls.Conn
 
2000
        h serverHandler
 
2001
}
 
2002
 
 
2003
func (h initNPNRequest) ServeHTTP(rw ResponseWriter, req *Request) {
 
2004
        if req.TLS == nil {
 
2005
                req.TLS = &tls.ConnectionState{}
 
2006
                *req.TLS = h.c.ConnectionState()
 
2007
        }
 
2008
        if req.Body == nil {
 
2009
                req.Body = eofReader
 
2010
        }
 
2011
        if req.RemoteAddr == "" {
 
2012
                req.RemoteAddr = h.c.RemoteAddr().String()
 
2013
        }
 
2014
        h.h.ServeHTTP(rw, req)
 
2015
}
 
2016
 
 
2017
// loggingConn is used for debugging.
 
2018
type loggingConn struct {
 
2019
        name string
 
2020
        net.Conn
 
2021
}
 
2022
 
 
2023
var (
 
2024
        uniqNameMu   sync.Mutex
 
2025
        uniqNameNext = make(map[string]int)
 
2026
)
 
2027
 
 
2028
func newLoggingConn(baseName string, c net.Conn) net.Conn {
 
2029
        uniqNameMu.Lock()
 
2030
        defer uniqNameMu.Unlock()
 
2031
        uniqNameNext[baseName]++
 
2032
        return &loggingConn{
 
2033
                name: fmt.Sprintf("%s-%d", baseName, uniqNameNext[baseName]),
 
2034
                Conn: c,
 
2035
        }
 
2036
}
 
2037
 
 
2038
func (c *loggingConn) Write(p []byte) (n int, err error) {
 
2039
        log.Printf("%s.Write(%d) = ....", c.name, len(p))
 
2040
        n, err = c.Conn.Write(p)
 
2041
        log.Printf("%s.Write(%d) = %d, %v", c.name, len(p), n, err)
 
2042
        return
 
2043
}
 
2044
 
 
2045
func (c *loggingConn) Read(p []byte) (n int, err error) {
 
2046
        log.Printf("%s.Read(%d) = ....", c.name, len(p))
 
2047
        n, err = c.Conn.Read(p)
 
2048
        log.Printf("%s.Read(%d) = %d, %v", c.name, len(p), n, err)
 
2049
        return
 
2050
}
 
2051
 
 
2052
func (c *loggingConn) Close() (err error) {
 
2053
        log.Printf("%s.Close() = ...", c.name)
 
2054
        err = c.Conn.Close()
 
2055
        log.Printf("%s.Close() = %v", c.name, err)
 
2056
        return
 
2057
}