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.
5
// HTTP server. See RFC 2616.
29
// Errors introduced by the HTTP server.
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")
37
// Objects implementing the Handler interface can be
38
// registered to serve a particular path or subtree
39
// in the HTTP server.
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
45
type Handler interface {
46
ServeHTTP(ResponseWriter, *Request)
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
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)
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
72
// The Flusher interface is implemented by ResponseWriters that allow
73
// an HTTP handler to flush buffered data to the client.
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
79
type Flusher interface {
80
// Flush sends any buffered data to the client.
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)
95
// The CloseNotifier interface is implemented by ResponseWriters which
96
// allow detecting when the underlying connection has gone away.
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
106
// A conn represents the server side of an HTTP connection.
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
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
122
func (c *conn) hijacked() bool {
128
func (c *conn) hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
132
return nil, nil, ErrHijacked
134
if c.closeNotifyc != nil {
135
return nil, nil, errors.New("http: Hijack is incompatible with use of CloseNotifier")
142
c.setState(rwc, StateHijacked)
146
func (c *conn) closeNotify() <-chan bool {
149
if c.closeNotifyc == nil {
150
c.closeNotifyc = make(chan bool, 1)
152
// to obey the function signature, even though
153
// it'll never receive a value.
154
return c.closeNotifyc
163
_, err := io.Copy(pw, readSource)
167
pw.CloseWithError(err)
171
return c.closeNotifyc
174
func (c *conn) noteClientGone() {
177
if c.closeNotifyc != nil && !c.clientGone {
178
c.closeNotifyc <- true
183
// A switchReader can have its Reader changed at runtime.
184
// It's not safe for concurrent Reads and switches.
185
type switchReader struct {
189
// A switchWriter can have its Writer changed at runtime.
190
// It's not safe for concurrent Writes and switches.
191
type switchWriter struct {
195
// A liveSwitchReader is a switchReader that's safe for concurrent
196
// reads and switches, if its mutex is held.
197
type liveSwitchReader struct {
202
func (sr *liveSwitchReader) Read(p []byte) (n int, err error) {
209
// This should be >= 512 bytes for DetectContentType,
210
// but otherwise it's somewhat arbitrary.
211
const bufferBeforeChunkingSize = 2048
213
// chunkWriter writes to a response's conn buffer, and is the writer
214
// wrapped by the response.bufw buffered writer.
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.
221
// See the comment above (*response).Write for the entire write flow.
222
type chunkWriter struct {
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.
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.
237
// set by the writeHeader method:
238
chunking bool // using chunked transfer encoding for reply body
242
crlf = []byte("\r\n")
243
colonSpace = []byte(": ")
246
func (cw *chunkWriter) Write(p []byte) (n int, err error) {
250
if cw.res.req.Method == "HEAD" {
255
_, err = fmt.Fprintf(cw.res.conn.buf, "%x\r\n", len(p))
257
cw.res.conn.rwc.Close()
261
n, err = cw.res.conn.buf.Write(p)
262
if cw.chunking && err == nil {
263
_, err = cw.res.conn.buf.Write(crlf)
266
cw.res.conn.rwc.Close()
271
func (cw *chunkWriter) flush() {
275
cw.res.conn.buf.Flush()
278
func (cw *chunkWriter) close() {
283
// zero EOF chunk, trailer key/value pairs (currently
284
// unsupported in Go's server), followed by a blank
286
cw.res.conn.buf.WriteString("0\r\n\r\n")
290
// A response represents the server side of an HTTP response.
291
type response struct {
293
req *Request // request for this response
294
wroteHeader bool // reply header has been (logically) written
295
wroteContinue bool // 100 Continue response was written
297
w *bufio.Writer // buffers output in chunks to chunkWriter
299
sw *switchWriter // of the bufio.Writer, for return to putBufioWriter
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.
306
calledHeader bool // handler accessed handlerHeader via Header
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
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
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
325
requestBodyLimitHit bool
327
handlerDone bool // set true when the handler exits
329
// Buffers for Date and Content-Length
330
dateBuf [len(TimeFormat)]byte
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
340
w.Header().Set("Connection", "close")
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
350
// writerOnly hides an io.Writer value's optional ReadFrom method
352
type writerOnly struct {
356
func srcIsRegularFile(src io.Reader) (isRegular bool, err error) {
357
switch v := src.(type) {
363
return fi.Mode().IsRegular(), nil
364
case *io.LimitedReader:
365
return srcIsRegularFile(v.R)
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)
383
return io.Copy(writerOnly{w}, src)
389
w.WriteHeader(StatusOK)
393
n0, err := io.Copy(writerOnly{w}, io.LimitReader(src, sniffLen))
400
w.w.Flush() // get rid of any previous writes
401
w.cw.flush() // make sure Header is written; flush data to rwc
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)
411
n0, err := io.Copy(writerOnly{w}, src)
416
// noLimit is an effective infinite upper bound for io.LimitedReader
417
const noLimit int64 = (1 << 63) - 1
419
// debugServerConnections controls whether all server connections are wrapped
420
// with a verbose logging wrapper.
421
const debugServerConnections = false
423
// Create new connection from rwc.
424
func (srv *Server) newConn(rwc net.Conn) (c *conn, err error) {
426
c.remoteAddr = rwc.RemoteAddr().String()
429
if debugServerConnections {
430
c.rwc = newLoggingConn("server", c.rwc)
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)
440
// TODO: use a sync.Cache instead
442
bufioReaderCache = make(chan *bufio.Reader, 4)
443
bufioWriterCache2k = make(chan *bufio.Writer, 4)
444
bufioWriterCache4k = make(chan *bufio.Writer, 4)
447
func bufioWriterCache(size int) chan *bufio.Writer {
450
return bufioWriterCache2k
452
return bufioWriterCache4k
457
func newBufioReader(r io.Reader) *bufio.Reader {
459
case p := <-bufioReaderCache:
463
return bufio.NewReader(r)
467
func putBufioReader(br *bufio.Reader) {
470
case bufioReaderCache <- br:
475
func newBufioWriterSize(w io.Writer, size int) *bufio.Writer {
477
case p := <-bufioWriterCache(size):
481
return bufio.NewWriterSize(w, size)
485
func putBufioWriter(bw *bufio.Writer) {
488
case bufioWriterCache(bw.Available()) <- bw:
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
498
func (srv *Server) maxHeaderBytes() int {
499
if srv.MaxHeaderBytes > 0 {
500
return srv.MaxHeaderBytes
502
return DefaultMaxHeaderBytes
505
func (srv *Server) initialLimitedReaderSize() int64 {
506
return int64(srv.maxHeaderBytes()) + 4096 // bufio slop
509
// wrapper around io.ReaderCloser which on first read, sends an
510
// HTTP/1.1 100 Continue header
511
type expectContinueReader struct {
513
readCloser io.ReadCloser
517
func (ecr *expectContinueReader) Read(p []byte) (n int, err error) {
519
return 0, ErrBodyReadAfterClose
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()
526
return ecr.readCloser.Read(p)
529
func (ecr *expectContinueReader) Close() error {
531
return ecr.readCloser.Close()
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"
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"
546
yy, mm, dd := t.Date()
547
hh, mn, ss := t.Clock()
548
day := days[3*t.Weekday():]
549
mon := months[3*(mm-1):]
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), ' ',
562
var errTooLarge = errors.New("http: request too large")
564
// Read next request from connection.
565
func (c *conn) readRequest() (w *response, err error) {
567
return nil, ErrHijacked
570
if d := c.server.ReadTimeout; d != 0 {
571
c.rwc.SetReadDeadline(time.Now().Add(d))
573
if d := c.server.WriteTimeout; d != 0 {
575
c.rwc.SetWriteDeadline(time.Now().Add(d))
579
c.lr.N = c.server.initialLimitedReaderSize()
581
if req, err = ReadRequest(c.buf.Reader); err != nil {
583
return nil, errTooLarge
589
req.RemoteAddr = c.remoteAddr
595
handlerHeader: make(Header),
599
w.w = newBufioWriterSize(&w.cw, bufferBeforeChunkingSize)
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()
610
w.calledHeader = true
611
return w.handlerHeader
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:
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
623
const maxPostHandlerReadBytes = 256 << 10
625
func (w *response) WriteHeader(code int) {
626
if w.conn.hijacked() {
627
w.conn.server.logf("http: response.WriteHeader on hijacked connection")
631
w.conn.server.logf("http: multiple response.WriteHeader calls")
637
if w.calledHeader && w.cw.header == nil {
638
w.cw.header = w.handlerHeader.clone()
641
if cl := w.handlerHeader.get("Content-Length"); cl != "" {
642
v, err := strconv.ParseInt(cl, 10, 64)
643
if err == nil && v >= 0 {
646
w.conn.server.logf("http: invalid Content-Length of %q", cl)
647
w.handlerHeader.Del("Content-Length")
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 {
658
transferEncoding string
659
date []byte // written if not nil
660
contentLength []byte // written if not nil
663
// Sorted the same as extraHeader.Write's loop.
664
var extraHeaderKeys = [][]byte{
665
[]byte("Content-Type"),
666
[]byte("Connection"),
667
[]byte("Transfer-Encoding"),
671
headerContentLength = []byte("Content-Length: ")
672
headerDate = []byte("Date: ")
675
// Write writes the headers described in h to w.
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) {
686
if h.contentLength != nil {
687
w.Write(headerContentLength)
688
w.Write(h.contentLength)
691
for i, v := range []string{h.contentType, h.connection, h.transferEncoding} {
693
w.Write(extraHeaderKeys[i])
701
// writeHeader finalizes the header sent to the client and writes it
702
// to cw.res.conn.buf.
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
709
func (cw *chunkWriter) writeHeader(p []byte) {
713
cw.wroteHeader = true
716
keepAlivesEnabled := w.conn.server.doKeepAlives()
717
isHEAD := w.req.Method == "HEAD"
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.
725
owned := header != nil
727
header = w.handlerHeader
729
var excludeHeader map[string]bool
730
delHeader := func(key string) {
735
if _, ok := header[key]; !ok {
738
if excludeHeader == nil {
739
excludeHeader = make(map[string]bool)
741
excludeHeader[key] = true
743
var setHeader extraHeader
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)
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
771
// Check for a explicit (and valid) Content-Length header.
772
hasCL := w.contentLength != -1
774
if w.req.wantsHttp10KeepAlive() && (isHEAD || hasCL) {
775
_, connectionHeaderSet := header["Connection"]
776
if !connectionHeaderSet {
777
setHeader.connection = "keep-alive"
779
} else if !w.req.ProtoAtLeast(1, 1) || w.req.wantsClose() {
780
w.closeAfterReply = true
783
if header.get("Connection") == "close" || !keepAlivesEnabled {
784
w.closeAfterReply = true
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 {
797
delHeader("Connection")
798
setHeader.connection = "close"
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"} {
813
// If no content type, apply sniffing algorithm to body.
814
_, haveType := header["Content-Type"]
816
setHeader.contentType = DetectContentType(p)
820
if _, ok := header["Date"]; !ok {
821
setHeader.date = appendTime(cw.res.dateBuf[:0], time.Now())
824
te := header.get("Transfer-Encoding")
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",
831
delHeader("Content-Length")
835
if w.req.Method == "HEAD" || !bodyAllowedForStatus(code) {
837
} else if code == StatusNoContent {
838
delHeader("Transfer-Encoding")
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
848
setHeader.transferEncoding = "chunked"
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
857
// Cannot use Content-Length with non-identity Transfer-Encoding.
859
delHeader("Content-Length")
861
if !w.req.ProtoAtLeast(1, 0) {
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"
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)
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.
884
statusMu sync.RWMutex
885
statusLines = make(map[int]string)
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 {
893
proto11 := req.ProtoAtLeast(1, 1)
898
line, ok := statusLines[key]
909
codestring := strconv.Itoa(code)
910
text, ok := statusText[code]
912
text = "status code " + codestring
914
line = proto + " " + codestring + " " + text + "\r\n"
917
defer statusMu.Unlock()
918
statusLines[key] = line
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 {
929
return bodyAllowedForStatus(w.status)
932
// The Life Of A Write is like this:
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.
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.
943
// Likewise, if the handler didn't set a Content-Type, we sniff that
944
// from the initial chunk of output.
946
// The Writers are wired together like:
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.
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, "")
968
func (w *response) WriteString(data string) (n int, err error) {
969
return w.write(len(data), nil, data)
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
979
w.WriteHeader(StatusOK)
984
if !w.bodyAllowed() {
985
return 0, ErrBodyNotAllowed
988
w.written += int64(lenData) // ignoring errors, for errorKludge
989
if w.contentLength != -1 && w.written > w.contentLength {
990
return 0, ErrContentLength
993
return w.w.Write(dataB)
995
return w.w.WriteString(dataS)
999
func (w *response) finishRequest() {
1000
w.handlerDone = true
1003
w.WriteHeader(StatusOK)
1011
// Close the body (regardless of w.closeAfterReply) so we can
1012
// re-use its bufio.Reader later safely.
1015
if w.req.MultipartForm != nil {
1016
w.req.MultipartForm.RemoveAll()
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
1025
func (w *response) Flush() {
1027
w.WriteHeader(StatusOK)
1033
func (c *conn) finalFlush() {
1037
// Steal the bufio.Reader (~4KB worth of memory) and its associated
1038
// reader for a future connection.
1039
putBufioReader(c.buf.Reader)
1041
// Steal the bufio.Writer (~4KB worth of memory) and its associated
1042
// writer for a future connection.
1043
putBufioWriter(c.buf.Writer)
1049
// Close the connection.
1050
func (c *conn) close() {
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
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
1072
// See http://golang.org/issue/3595
1073
func (c *conn) closeWriteAndWait() {
1075
if tcp, ok := c.rwc.(*net.TCPConn); ok {
1078
time.Sleep(rstAvoidanceDelay)
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
1085
func validNPN(proto string) bool {
1087
case "", "http/1.1", "http/1.0":
1093
func (c *conn) setState(nc net.Conn, state ConnState) {
1094
if hook := c.server.ConnState; hook != nil {
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
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)
1111
c.setState(origConn, StateClosed)
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))
1119
if d := c.server.WriteTimeout; d != 0 {
1120
c.rwc.SetWriteDeadline(time.Now().Add(d))
1122
if err := tlsConn.Handshake(); err != nil {
1123
c.server.logf("http: TLS handshake error from %s: %v", c.rwc.RemoteAddr(), err)
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)
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)
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()
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
1158
io.WriteString(c.rwc, "HTTP/1.1 400 Bad Request\r\n\r\n")
1162
// Expect 100 Continue support
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}
1169
if req.ContentLength == 0 {
1170
w.Header().Set("Connection", "close")
1171
w.WriteHeader(StatusBadRequest)
1175
req.Header.Del("Expect")
1176
} else if req.Header.get("Expect") != "" {
1177
w.sendExpectationFailed()
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)
1191
if w.closeAfterReply {
1192
if w.requestBodyLimitHit {
1193
c.closeWriteAndWait()
1197
c.setState(c.rwc, StateIdle)
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.
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)
1219
// Hijack implements the Hijacker.Hijack method. Our response is both a ResponseWriter
1221
func (w *response) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
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()
1232
return rwc, buf, err
1235
func (w *response) CloseNotify() <-chan bool {
1236
return w.conn.closeNotify()
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)
1245
// ServeHTTP calls f(w, r).
1246
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
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")
1257
fmt.Fprintln(w, error)
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) }
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) }
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 {
1276
return HandlerFunc(func(w ResponseWriter, r *Request) {
1277
if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {
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.
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.
1306
oldpath := r.URL.Path
1307
if oldpath == "" { // should not happen, but avoid a crash if it does
1311
// no leading http://server
1312
if urlStr == "" || urlStr[0] != '/' {
1313
// make relative path absolute
1314
olddir, _ := path.Split(oldpath)
1315
urlStr = olddir + urlStr
1319
if i := strings.Index(urlStr, "?"); i != -1 {
1320
urlStr, query = urlStr[:i], urlStr[i:]
1323
// clean up but preserve trailing slash
1324
trailing := strings.HasSuffix(urlStr, "/")
1325
urlStr = path.Clean(urlStr)
1326
if trailing && !strings.HasSuffix(urlStr, "/") {
1333
w.Header().Set("Location", urlStr)
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)
1345
var htmlReplacer = strings.NewReplacer(
1349
// """ is shorter than """.
1351
// "'" is shorter than "'" and apos was not in HTML until HTML5.
1355
func htmlEscape(s string) string {
1356
return htmlReplacer.Replace(s)
1359
// Redirect to a fixed URL
1360
type redirectHandler struct {
1365
func (rh *redirectHandler) ServeHTTP(w ResponseWriter, r *Request) {
1366
Redirect(w, r, rh.url, rh.code)
1369
// RedirectHandler returns a request handler that redirects
1370
// each request it receives to the given url using the given
1372
func RedirectHandler(url string, code int) Handler {
1373
return &redirectHandler{url, code}
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.
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.
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 == "/".
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/".
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 {
1405
m map[string]muxEntry
1406
hosts bool // whether any patterns contain hostnames
1409
type muxEntry struct {
1415
// NewServeMux allocates and returns a new ServeMux.
1416
func NewServeMux() *ServeMux { return &ServeMux{m: make(map[string]muxEntry)} }
1418
// DefaultServeMux is the default ServeMux used by Serve.
1419
var DefaultServeMux = NewServeMux()
1421
// Does path match pattern?
1422
func pathMatch(pattern, path string) bool {
1423
if len(pattern) == 0 {
1424
// should not happen
1428
if pattern[n-1] != '/' {
1429
return pattern == path
1431
return len(path) >= n && path[0:n] == pattern
1434
// Return the canonical path for p, eliminating . and .. elements.
1435
func cleanPath(p string) string {
1443
// path.Clean removes trailing slash except for root;
1444
// put the trailing slash back if necessary.
1445
if p[len(p)-1] == '/' && np != "/" {
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) {
1455
for k, v := range mux.m {
1456
if !pathMatch(k, path) {
1459
if h == nil || len(k) > n {
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.
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.
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)
1486
return RedirectHandler(url.String(), StatusMovedPermanently), pattern
1490
return mux.handler(r.Host, r.URL.Path)
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) {
1497
defer mux.mu.RUnlock()
1499
// Host-specific pattern takes precedence over generic ones
1501
h, pattern = mux.match(host + path)
1504
h, pattern = mux.match(path)
1507
h, pattern = NotFoundHandler(), ""
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")
1519
w.WriteHeader(StatusBadRequest)
1522
h, _ := mux.Handler(r)
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) {
1530
defer mux.mu.Unlock()
1533
panic("http: invalid pattern " + pattern)
1536
panic("http: nil handler")
1538
if mux.m[pattern].explicit {
1539
panic("http: multiple registrations for " + pattern)
1542
mux.m[pattern] = muxEntry{explicit: true, h: handler, pattern: pattern}
1544
if pattern[0] != '/' {
1548
// Helpful behavior:
1549
// If pattern is /tree/, insert an implicit permanent redirect for /tree.
1550
// It can be overridden by an explicit registration.
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.
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, "/"):]
1561
mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(path, StatusMovedPermanently), pattern: pattern}
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))
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) }
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)
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}
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
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)
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)
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
1619
ErrorLog *log.Logger
1621
disableKeepAlives int32 // accessed atomically.
1624
// A ConnState represents the state of a client connection to a server.
1625
// It's used by the optional Server.ConnState hook.
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
1633
StateNew ConnState = iota
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.
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.
1649
// StateHijacked represents a hijacked connection.
1650
// This is a terminal state. It does not transition to StateClosed.
1653
// StateClosed represents a closed connection.
1654
// This is a terminal state. Hijacked connections do not
1655
// transition to StateClosed.
1659
var stateName = map[ConnState]string{
1661
StateActive: "active",
1663
StateHijacked: "hijacked",
1664
StateClosed: "closed",
1667
func (c ConnState) String() string {
1671
// serverHandler delegates to either the server's Handler or
1672
// DefaultServeMux and also handles "OPTIONS *" requests.
1673
type serverHandler struct {
1677
func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
1678
handler := sh.srv.Handler
1680
handler = DefaultServeMux
1682
if req.RequestURI == "*" && req.Method == "OPTIONS" {
1683
handler = globalOptionsHandler{}
1685
handler.ServeHTTP(rw, req)
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 {
1696
ln, err := net.Listen("tcp", addr)
1700
return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
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 {
1708
var tempDelay time.Duration // how long to sleep on accept failure
1712
if ne, ok := e.(net.Error); ok && ne.Temporary() {
1714
tempDelay = 5 * time.Millisecond
1718
if max := 1 * time.Second; tempDelay > max {
1721
srv.logf("http: Accept error: %v; retrying in %v", e, tempDelay)
1722
time.Sleep(tempDelay)
1728
c, err := srv.newConn(rw)
1732
c.setState(c.rwc, StateNew) // before Serve can return
1737
func (s *Server) doKeepAlives() bool {
1738
return atomic.LoadInt32(&s.disableKeepAlives) == 0
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) {
1747
atomic.StoreInt32(&s.disableKeepAlives, 0)
1749
atomic.StoreInt32(&s.disableKeepAlives, 1)
1753
func (s *Server) logf(format string, args ...interface{}) {
1754
if s.ErrorLog != nil {
1755
s.ErrorLog.Printf(format, args...)
1757
log.Printf(format, args...)
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.
1766
// A trivial example server is:
1772
// "launchpad.net/ubuntu-push/http13client"
1776
// // hello world, the web server
1777
// func HelloServer(w http.ResponseWriter, req *http.Request) {
1778
// io.WriteString(w, "hello, world!\n")
1782
// http.HandleFunc("/hello", HelloServer)
1783
// err := http.ListenAndServe(":12345", nil)
1785
// log.Fatal("ListenAndServe: ", err)
1788
func ListenAndServe(addr string, handler Handler) error {
1789
server := &Server{Addr: addr, Handler: handler}
1790
return server.ListenAndServe()
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.
1799
// A trivial example server is:
1803
// "launchpad.net/ubuntu-push/http13client"
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"))
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)
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)
1826
// ListenAndServeTLS listens on the TCP network address srv.Addr and
1827
// then calls Serve to handle requests on incoming TLS connections.
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.
1834
// If srv.Addr is blank, ":https" is used.
1835
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
1840
config := &tls.Config{}
1841
if srv.TLSConfig != nil {
1842
*config = *srv.TLSConfig
1844
if config.NextProtos == nil {
1845
config.NextProtos = []string{"http/1.1"}
1849
config.Certificates = make([]tls.Certificate, 1)
1850
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
1855
ln, err := net.Listen("tcp", addr)
1860
tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, config)
1861
return srv.Serve(tlsListener)
1864
// TimeoutHandler returns a Handler that runs h with the given time limit.
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)
1876
return &timeoutHandler{h, f, msg}
1879
// ErrHandlerTimeout is returned on ResponseWriter Write calls
1880
// in handlers which have timed out.
1881
var ErrHandlerTimeout = errors.New("http: Handler timeout")
1883
type timeoutHandler struct {
1885
timeout func() <-chan time.Time // returns channel producing a timeout
1889
func (h *timeoutHandler) errorBody() string {
1893
return "<html><head><title>Timeout</title></head><body><h1>Timeout</h1></body></html>"
1896
func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) {
1897
done := make(chan bool, 1)
1898
tw := &timeoutWriter{w: w}
1900
h.handler.ServeHTTP(tw, r)
1908
defer tw.mu.Unlock()
1909
if !tw.wroteHeader {
1910
tw.w.WriteHeader(StatusServiceUnavailable)
1911
tw.w.Write([]byte(h.errorBody()))
1917
type timeoutWriter struct {
1925
func (tw *timeoutWriter) Header() Header {
1926
return tw.w.Header()
1929
func (tw *timeoutWriter) Write(p []byte) (int, error) {
1931
timedOut := tw.timedOut
1934
return 0, ErrHandlerTimeout
1936
return tw.w.Write(p)
1939
func (tw *timeoutWriter) WriteHeader(code int) {
1941
if tw.timedOut || tw.wroteHeader {
1945
tw.wroteHeader = true
1947
tw.w.WriteHeader(code)
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
1954
type tcpKeepAliveListener struct {
1958
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
1959
tc, err := ln.AcceptTCP()
1963
tc.SetKeepAlive(true)
1964
tc.SetKeepAlivePeriod(3 * time.Minute)
1968
// globalOptionsHandler responds to "OPTIONS *" requests.
1969
type globalOptionsHandler struct{}
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)
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 {
1991
strings.NewReader(""),
1992
ioutil.NopCloser(nil),
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 {
2003
func (h initNPNRequest) ServeHTTP(rw ResponseWriter, req *Request) {
2005
req.TLS = &tls.ConnectionState{}
2006
*req.TLS = h.c.ConnectionState()
2008
if req.Body == nil {
2009
req.Body = eofReader
2011
if req.RemoteAddr == "" {
2012
req.RemoteAddr = h.c.RemoteAddr().String()
2014
h.h.ServeHTTP(rw, req)
2017
// loggingConn is used for debugging.
2018
type loggingConn struct {
2024
uniqNameMu sync.Mutex
2025
uniqNameNext = make(map[string]int)
2028
func newLoggingConn(baseName string, c net.Conn) net.Conn {
2030
defer uniqNameMu.Unlock()
2031
uniqNameNext[baseName]++
2032
return &loggingConn{
2033
name: fmt.Sprintf("%s-%d", baseName, uniqNameNext[baseName]),
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)
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)
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)