Add documentation for musttail attribute
[official-gcc.git] / libgo / go / net / http / server.go
blobf5cdc3a4d34df3008ecffb806f82312b874f9ea8
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 7230 through 7235.
7 package http
9 import (
10 "bufio"
11 "bytes"
12 "context"
13 "crypto/tls"
14 "errors"
15 "fmt"
16 "internal/godebug"
17 "io"
18 "log"
19 "math/rand"
20 "net"
21 "net/textproto"
22 "net/url"
23 urlpkg "net/url"
24 "path"
25 "runtime"
26 "sort"
27 "strconv"
28 "strings"
29 "sync"
30 "sync/atomic"
31 "time"
33 "golang.org/x/net/http/httpguts"
36 // Errors used by the HTTP server.
37 var (
38 // ErrBodyNotAllowed is returned by ResponseWriter.Write calls
39 // when the HTTP method or response code does not permit a
40 // body.
41 ErrBodyNotAllowed = errors.New("http: request method or response status code does not allow body")
43 // ErrHijacked is returned by ResponseWriter.Write calls when
44 // the underlying connection has been hijacked using the
45 // Hijacker interface. A zero-byte write on a hijacked
46 // connection will return ErrHijacked without any other side
47 // effects.
48 ErrHijacked = errors.New("http: connection has been hijacked")
50 // ErrContentLength is returned by ResponseWriter.Write calls
51 // when a Handler set a Content-Length response header with a
52 // declared size and then attempted to write more bytes than
53 // declared.
54 ErrContentLength = errors.New("http: wrote more than the declared Content-Length")
56 // Deprecated: ErrWriteAfterFlush is no longer returned by
57 // anything in the net/http package. Callers should not
58 // compare errors against this variable.
59 ErrWriteAfterFlush = errors.New("unused")
62 // A Handler responds to an HTTP request.
64 // ServeHTTP should write reply headers and data to the ResponseWriter
65 // and then return. Returning signals that the request is finished; it
66 // is not valid to use the ResponseWriter or read from the
67 // Request.Body after or concurrently with the completion of the
68 // ServeHTTP call.
70 // Depending on the HTTP client software, HTTP protocol version, and
71 // any intermediaries between the client and the Go server, it may not
72 // be possible to read from the Request.Body after writing to the
73 // ResponseWriter. Cautious handlers should read the Request.Body
74 // first, and then reply.
76 // Except for reading the body, handlers should not modify the
77 // provided Request.
79 // If ServeHTTP panics, the server (the caller of ServeHTTP) assumes
80 // that the effect of the panic was isolated to the active request.
81 // It recovers the panic, logs a stack trace to the server error log,
82 // and either closes the network connection or sends an HTTP/2
83 // RST_STREAM, depending on the HTTP protocol. To abort a handler so
84 // the client sees an interrupted response but the server doesn't log
85 // an error, panic with the value ErrAbortHandler.
86 type Handler interface {
87 ServeHTTP(ResponseWriter, *Request)
90 // A ResponseWriter interface is used by an HTTP handler to
91 // construct an HTTP response.
93 // A ResponseWriter may not be used after the Handler.ServeHTTP method
94 // has returned.
95 type ResponseWriter interface {
96 // Header returns the header map that will be sent by
97 // WriteHeader. The Header map also is the mechanism with which
98 // Handlers can set HTTP trailers.
100 // Changing the header map after a call to WriteHeader (or
101 // Write) has no effect unless the modified headers are
102 // trailers.
104 // There are two ways to set Trailers. The preferred way is to
105 // predeclare in the headers which trailers you will later
106 // send by setting the "Trailer" header to the names of the
107 // trailer keys which will come later. In this case, those
108 // keys of the Header map are treated as if they were
109 // trailers. See the example. The second way, for trailer
110 // keys not known to the Handler until after the first Write,
111 // is to prefix the Header map keys with the TrailerPrefix
112 // constant value. See TrailerPrefix.
114 // To suppress automatic response headers (such as "Date"), set
115 // their value to nil.
116 Header() Header
118 // Write writes the data to the connection as part of an HTTP reply.
120 // If WriteHeader has not yet been called, Write calls
121 // WriteHeader(http.StatusOK) before writing the data. If the Header
122 // does not contain a Content-Type line, Write adds a Content-Type set
123 // to the result of passing the initial 512 bytes of written data to
124 // DetectContentType. Additionally, if the total size of all written
125 // data is under a few KB and there are no Flush calls, the
126 // Content-Length header is added automatically.
128 // Depending on the HTTP protocol version and the client, calling
129 // Write or WriteHeader may prevent future reads on the
130 // Request.Body. For HTTP/1.x requests, handlers should read any
131 // needed request body data before writing the response. Once the
132 // headers have been flushed (due to either an explicit Flusher.Flush
133 // call or writing enough data to trigger a flush), the request body
134 // may be unavailable. For HTTP/2 requests, the Go HTTP server permits
135 // handlers to continue to read the request body while concurrently
136 // writing the response. However, such behavior may not be supported
137 // by all HTTP/2 clients. Handlers should read before writing if
138 // possible to maximize compatibility.
139 Write([]byte) (int, error)
141 // WriteHeader sends an HTTP response header with the provided
142 // status code.
144 // If WriteHeader is not called explicitly, the first call to Write
145 // will trigger an implicit WriteHeader(http.StatusOK).
146 // Thus explicit calls to WriteHeader are mainly used to
147 // send error codes.
149 // The provided code must be a valid HTTP 1xx-5xx status code.
150 // Only one header may be written. Go does not currently
151 // support sending user-defined 1xx informational headers,
152 // with the exception of 100-continue response header that the
153 // Server sends automatically when the Request.Body is read.
154 WriteHeader(statusCode int)
157 // The Flusher interface is implemented by ResponseWriters that allow
158 // an HTTP handler to flush buffered data to the client.
160 // The default HTTP/1.x and HTTP/2 ResponseWriter implementations
161 // support Flusher, but ResponseWriter wrappers may not. Handlers
162 // should always test for this ability at runtime.
164 // Note that even for ResponseWriters that support Flush,
165 // if the client is connected through an HTTP proxy,
166 // the buffered data may not reach the client until the response
167 // completes.
168 type Flusher interface {
169 // Flush sends any buffered data to the client.
170 Flush()
173 // The Hijacker interface is implemented by ResponseWriters that allow
174 // an HTTP handler to take over the connection.
176 // The default ResponseWriter for HTTP/1.x connections supports
177 // Hijacker, but HTTP/2 connections intentionally do not.
178 // ResponseWriter wrappers may also not support Hijacker. Handlers
179 // should always test for this ability at runtime.
180 type Hijacker interface {
181 // Hijack lets the caller take over the connection.
182 // After a call to Hijack the HTTP server library
183 // will not do anything else with the connection.
185 // It becomes the caller's responsibility to manage
186 // and close the connection.
188 // The returned net.Conn may have read or write deadlines
189 // already set, depending on the configuration of the
190 // Server. It is the caller's responsibility to set
191 // or clear those deadlines as needed.
193 // The returned bufio.Reader may contain unprocessed buffered
194 // data from the client.
196 // After a call to Hijack, the original Request.Body must not
197 // be used. The original Request's Context remains valid and
198 // is not canceled until the Request's ServeHTTP method
199 // returns.
200 Hijack() (net.Conn, *bufio.ReadWriter, error)
203 // The CloseNotifier interface is implemented by ResponseWriters which
204 // allow detecting when the underlying connection has gone away.
206 // This mechanism can be used to cancel long operations on the server
207 // if the client has disconnected before the response is ready.
209 // Deprecated: the CloseNotifier interface predates Go's context package.
210 // New code should use Request.Context instead.
211 type CloseNotifier interface {
212 // CloseNotify returns a channel that receives at most a
213 // single value (true) when the client connection has gone
214 // away.
216 // CloseNotify may wait to notify until Request.Body has been
217 // fully read.
219 // After the Handler has returned, there is no guarantee
220 // that the channel receives a value.
222 // If the protocol is HTTP/1.1 and CloseNotify is called while
223 // processing an idempotent request (such a GET) while
224 // HTTP/1.1 pipelining is in use, the arrival of a subsequent
225 // pipelined request may cause a value to be sent on the
226 // returned channel. In practice HTTP/1.1 pipelining is not
227 // enabled in browsers and not seen often in the wild. If this
228 // is a problem, use HTTP/2 or only use CloseNotify on methods
229 // such as POST.
230 CloseNotify() <-chan bool
233 var (
234 // ServerContextKey is a context key. It can be used in HTTP
235 // handlers with Context.Value to access the server that
236 // started the handler. The associated value will be of
237 // type *Server.
238 ServerContextKey = &contextKey{"http-server"}
240 // LocalAddrContextKey is a context key. It can be used in
241 // HTTP handlers with Context.Value to access the local
242 // address the connection arrived on.
243 // The associated value will be of type net.Addr.
244 LocalAddrContextKey = &contextKey{"local-addr"}
247 // A conn represents the server side of an HTTP connection.
248 type conn struct {
249 // server is the server on which the connection arrived.
250 // Immutable; never nil.
251 server *Server
253 // cancelCtx cancels the connection-level context.
254 cancelCtx context.CancelFunc
256 // rwc is the underlying network connection.
257 // This is never wrapped by other types and is the value given out
258 // to CloseNotifier callers. It is usually of type *net.TCPConn or
259 // *tls.Conn.
260 rwc net.Conn
262 // remoteAddr is rwc.RemoteAddr().String(). It is not populated synchronously
263 // inside the Listener's Accept goroutine, as some implementations block.
264 // It is populated immediately inside the (*conn).serve goroutine.
265 // This is the value of a Handler's (*Request).RemoteAddr.
266 remoteAddr string
268 // tlsState is the TLS connection state when using TLS.
269 // nil means not TLS.
270 tlsState *tls.ConnectionState
272 // werr is set to the first write error to rwc.
273 // It is set via checkConnErrorWriter{w}, where bufw writes.
274 werr error
276 // r is bufr's read source. It's a wrapper around rwc that provides
277 // io.LimitedReader-style limiting (while reading request headers)
278 // and functionality to support CloseNotifier. See *connReader docs.
279 r *connReader
281 // bufr reads from r.
282 bufr *bufio.Reader
284 // bufw writes to checkConnErrorWriter{c}, which populates werr on error.
285 bufw *bufio.Writer
287 // lastMethod is the method of the most recent request
288 // on this connection, if any.
289 lastMethod string
291 curReq atomic.Value // of *response (which has a Request in it)
293 curState struct{ atomic uint64 } // packed (unixtime<<8|uint8(ConnState))
295 // mu guards hijackedv
296 mu sync.Mutex
298 // hijackedv is whether this connection has been hijacked
299 // by a Handler with the Hijacker interface.
300 // It is guarded by mu.
301 hijackedv bool
304 func (c *conn) hijacked() bool {
305 c.mu.Lock()
306 defer c.mu.Unlock()
307 return c.hijackedv
310 // c.mu must be held.
311 func (c *conn) hijackLocked() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
312 if c.hijackedv {
313 return nil, nil, ErrHijacked
315 c.r.abortPendingRead()
317 c.hijackedv = true
318 rwc = c.rwc
319 rwc.SetDeadline(time.Time{})
321 buf = bufio.NewReadWriter(c.bufr, bufio.NewWriter(rwc))
322 if c.r.hasByte {
323 if _, err := c.bufr.Peek(c.bufr.Buffered() + 1); err != nil {
324 return nil, nil, fmt.Errorf("unexpected Peek failure reading buffered byte: %v", err)
327 c.setState(rwc, StateHijacked, runHooks)
328 return
331 // This should be >= 512 bytes for DetectContentType,
332 // but otherwise it's somewhat arbitrary.
333 const bufferBeforeChunkingSize = 2048
335 // chunkWriter writes to a response's conn buffer, and is the writer
336 // wrapped by the response.w buffered writer.
338 // chunkWriter also is responsible for finalizing the Header, including
339 // conditionally setting the Content-Type and setting a Content-Length
340 // in cases where the handler's final output is smaller than the buffer
341 // size. It also conditionally adds chunk headers, when in chunking mode.
343 // See the comment above (*response).Write for the entire write flow.
344 type chunkWriter struct {
345 res *response
347 // header is either nil or a deep clone of res.handlerHeader
348 // at the time of res.writeHeader, if res.writeHeader is
349 // called and extra buffering is being done to calculate
350 // Content-Type and/or Content-Length.
351 header Header
353 // wroteHeader tells whether the header's been written to "the
354 // wire" (or rather: w.conn.buf). this is unlike
355 // (*response).wroteHeader, which tells only whether it was
356 // logically written.
357 wroteHeader bool
359 // set by the writeHeader method:
360 chunking bool // using chunked transfer encoding for reply body
363 var (
364 crlf = []byte("\r\n")
365 colonSpace = []byte(": ")
368 func (cw *chunkWriter) Write(p []byte) (n int, err error) {
369 if !cw.wroteHeader {
370 cw.writeHeader(p)
372 if cw.res.req.Method == "HEAD" {
373 // Eat writes.
374 return len(p), nil
376 if cw.chunking {
377 _, err = fmt.Fprintf(cw.res.conn.bufw, "%x\r\n", len(p))
378 if err != nil {
379 cw.res.conn.rwc.Close()
380 return
383 n, err = cw.res.conn.bufw.Write(p)
384 if cw.chunking && err == nil {
385 _, err = cw.res.conn.bufw.Write(crlf)
387 if err != nil {
388 cw.res.conn.rwc.Close()
390 return
393 func (cw *chunkWriter) flush() {
394 if !cw.wroteHeader {
395 cw.writeHeader(nil)
397 cw.res.conn.bufw.Flush()
400 func (cw *chunkWriter) close() {
401 if !cw.wroteHeader {
402 cw.writeHeader(nil)
404 if cw.chunking {
405 bw := cw.res.conn.bufw // conn's bufio writer
406 // zero chunk to mark EOF
407 bw.WriteString("0\r\n")
408 if trailers := cw.res.finalTrailers(); trailers != nil {
409 trailers.Write(bw) // the writer handles noting errors
411 // final blank line after the trailers (whether
412 // present or not)
413 bw.WriteString("\r\n")
417 // A response represents the server side of an HTTP response.
418 type response struct {
419 conn *conn
420 req *Request // request for this response
421 reqBody io.ReadCloser
422 cancelCtx context.CancelFunc // when ServeHTTP exits
423 wroteHeader bool // reply header has been (logically) written
424 wroteContinue bool // 100 Continue response was written
425 wants10KeepAlive bool // HTTP/1.0 w/ Connection "keep-alive"
426 wantsClose bool // HTTP request has Connection "close"
428 // canWriteContinue is a boolean value accessed as an atomic int32
429 // that says whether or not a 100 Continue header can be written
430 // to the connection.
431 // writeContinueMu must be held while writing the header.
432 // These two fields together synchronize the body reader
433 // (the expectContinueReader, which wants to write 100 Continue)
434 // against the main writer.
435 canWriteContinue atomicBool
436 writeContinueMu sync.Mutex
438 w *bufio.Writer // buffers output in chunks to chunkWriter
439 cw chunkWriter
441 // handlerHeader is the Header that Handlers get access to,
442 // which may be retained and mutated even after WriteHeader.
443 // handlerHeader is copied into cw.header at WriteHeader
444 // time, and privately mutated thereafter.
445 handlerHeader Header
446 calledHeader bool // handler accessed handlerHeader via Header
448 written int64 // number of bytes written in body
449 contentLength int64 // explicitly-declared Content-Length; or -1
450 status int // status code passed to WriteHeader
452 // close connection after this reply. set on request and
453 // updated after response from handler if there's a
454 // "Connection: keep-alive" response header and a
455 // Content-Length.
456 closeAfterReply bool
458 // requestBodyLimitHit is set by requestTooLarge when
459 // maxBytesReader hits its max size. It is checked in
460 // WriteHeader, to make sure we don't consume the
461 // remaining request body to try to advance to the next HTTP
462 // request. Instead, when this is set, we stop reading
463 // subsequent requests on this connection and stop reading
464 // input from it.
465 requestBodyLimitHit bool
467 // trailers are the headers to be sent after the handler
468 // finishes writing the body. This field is initialized from
469 // the Trailer response header when the response header is
470 // written.
471 trailers []string
473 handlerDone atomicBool // set true when the handler exits
475 // Buffers for Date, Content-Length, and status code
476 dateBuf [len(TimeFormat)]byte
477 clenBuf [10]byte
478 statusBuf [3]byte
480 // closeNotifyCh is the channel returned by CloseNotify.
481 // TODO(bradfitz): this is currently (for Go 1.8) always
482 // non-nil. Make this lazily-created again as it used to be?
483 closeNotifyCh chan bool
484 didCloseNotify int32 // atomic (only 0->1 winner should send)
487 // TrailerPrefix is a magic prefix for ResponseWriter.Header map keys
488 // that, if present, signals that the map entry is actually for
489 // the response trailers, and not the response headers. The prefix
490 // is stripped after the ServeHTTP call finishes and the values are
491 // sent in the trailers.
493 // This mechanism is intended only for trailers that are not known
494 // prior to the headers being written. If the set of trailers is fixed
495 // or known before the header is written, the normal Go trailers mechanism
496 // is preferred:
497 // https://pkg.go.dev/net/http#ResponseWriter
498 // https://pkg.go.dev/net/http#example-ResponseWriter-Trailers
499 const TrailerPrefix = "Trailer:"
501 // finalTrailers is called after the Handler exits and returns a non-nil
502 // value if the Handler set any trailers.
503 func (w *response) finalTrailers() Header {
504 var t Header
505 for k, vv := range w.handlerHeader {
506 if strings.HasPrefix(k, TrailerPrefix) {
507 if t == nil {
508 t = make(Header)
510 t[strings.TrimPrefix(k, TrailerPrefix)] = vv
513 for _, k := range w.trailers {
514 if t == nil {
515 t = make(Header)
517 for _, v := range w.handlerHeader[k] {
518 t.Add(k, v)
521 return t
524 type atomicBool int32
526 func (b *atomicBool) isSet() bool { return atomic.LoadInt32((*int32)(b)) != 0 }
527 func (b *atomicBool) setTrue() { atomic.StoreInt32((*int32)(b), 1) }
528 func (b *atomicBool) setFalse() { atomic.StoreInt32((*int32)(b), 0) }
530 // declareTrailer is called for each Trailer header when the
531 // response header is written. It notes that a header will need to be
532 // written in the trailers at the end of the response.
533 func (w *response) declareTrailer(k string) {
534 k = CanonicalHeaderKey(k)
535 if !httpguts.ValidTrailerHeader(k) {
536 // Forbidden by RFC 7230, section 4.1.2
537 return
539 w.trailers = append(w.trailers, k)
542 // requestTooLarge is called by maxBytesReader when too much input has
543 // been read from the client.
544 func (w *response) requestTooLarge() {
545 w.closeAfterReply = true
546 w.requestBodyLimitHit = true
547 if !w.wroteHeader {
548 w.Header().Set("Connection", "close")
552 // needsSniff reports whether a Content-Type still needs to be sniffed.
553 func (w *response) needsSniff() bool {
554 _, haveType := w.handlerHeader["Content-Type"]
555 return !w.cw.wroteHeader && !haveType && w.written < sniffLen
558 // writerOnly hides an io.Writer value's optional ReadFrom method
559 // from io.Copy.
560 type writerOnly struct {
561 io.Writer
564 // ReadFrom is here to optimize copying from an *os.File regular file
565 // to a *net.TCPConn with sendfile, or from a supported src type such
566 // as a *net.TCPConn on Linux with splice.
567 func (w *response) ReadFrom(src io.Reader) (n int64, err error) {
568 bufp := copyBufPool.Get().(*[]byte)
569 buf := *bufp
570 defer copyBufPool.Put(bufp)
572 // Our underlying w.conn.rwc is usually a *TCPConn (with its
573 // own ReadFrom method). If not, just fall back to the normal
574 // copy method.
575 rf, ok := w.conn.rwc.(io.ReaderFrom)
576 if !ok {
577 return io.CopyBuffer(writerOnly{w}, src, buf)
580 // Copy the first sniffLen bytes before switching to ReadFrom.
581 // This ensures we don't start writing the response before the
582 // source is available (see golang.org/issue/5660) and provides
583 // enough bytes to perform Content-Type sniffing when required.
584 if !w.cw.wroteHeader {
585 n0, err := io.CopyBuffer(writerOnly{w}, io.LimitReader(src, sniffLen), buf)
586 n += n0
587 if err != nil || n0 < sniffLen {
588 return n, err
592 w.w.Flush() // get rid of any previous writes
593 w.cw.flush() // make sure Header is written; flush data to rwc
595 // Now that cw has been flushed, its chunking field is guaranteed initialized.
596 if !w.cw.chunking && w.bodyAllowed() {
597 n0, err := rf.ReadFrom(src)
598 n += n0
599 w.written += n0
600 return n, err
603 n0, err := io.CopyBuffer(writerOnly{w}, src, buf)
604 n += n0
605 return n, err
608 // debugServerConnections controls whether all server connections are wrapped
609 // with a verbose logging wrapper.
610 const debugServerConnections = false
612 // Create new connection from rwc.
613 func (srv *Server) newConn(rwc net.Conn) *conn {
614 c := &conn{
615 server: srv,
616 rwc: rwc,
618 if debugServerConnections {
619 c.rwc = newLoggingConn("server", c.rwc)
621 return c
624 type readResult struct {
625 _ incomparable
626 n int
627 err error
628 b byte // byte read, if n == 1
631 // connReader is the io.Reader wrapper used by *conn. It combines a
632 // selectively-activated io.LimitedReader (to bound request header
633 // read sizes) with support for selectively keeping an io.Reader.Read
634 // call blocked in a background goroutine to wait for activity and
635 // trigger a CloseNotifier channel.
636 type connReader struct {
637 conn *conn
639 mu sync.Mutex // guards following
640 hasByte bool
641 byteBuf [1]byte
642 cond *sync.Cond
643 inRead bool
644 aborted bool // set true before conn.rwc deadline is set to past
645 remain int64 // bytes remaining
648 func (cr *connReader) lock() {
649 cr.mu.Lock()
650 if cr.cond == nil {
651 cr.cond = sync.NewCond(&cr.mu)
655 func (cr *connReader) unlock() { cr.mu.Unlock() }
657 func (cr *connReader) startBackgroundRead() {
658 cr.lock()
659 defer cr.unlock()
660 if cr.inRead {
661 panic("invalid concurrent Body.Read call")
663 if cr.hasByte {
664 return
666 cr.inRead = true
667 cr.conn.rwc.SetReadDeadline(time.Time{})
668 go cr.backgroundRead()
671 func (cr *connReader) backgroundRead() {
672 n, err := cr.conn.rwc.Read(cr.byteBuf[:])
673 cr.lock()
674 if n == 1 {
675 cr.hasByte = true
676 // We were past the end of the previous request's body already
677 // (since we wouldn't be in a background read otherwise), so
678 // this is a pipelined HTTP request. Prior to Go 1.11 we used to
679 // send on the CloseNotify channel and cancel the context here,
680 // but the behavior was documented as only "may", and we only
681 // did that because that's how CloseNotify accidentally behaved
682 // in very early Go releases prior to context support. Once we
683 // added context support, people used a Handler's
684 // Request.Context() and passed it along. Having that context
685 // cancel on pipelined HTTP requests caused problems.
686 // Fortunately, almost nothing uses HTTP/1.x pipelining.
687 // Unfortunately, apt-get does, or sometimes does.
688 // New Go 1.11 behavior: don't fire CloseNotify or cancel
689 // contexts on pipelined requests. Shouldn't affect people, but
690 // fixes cases like Issue 23921. This does mean that a client
691 // closing their TCP connection after sending a pipelined
692 // request won't cancel the context, but we'll catch that on any
693 // write failure (in checkConnErrorWriter.Write).
694 // If the server never writes, yes, there are still contrived
695 // server & client behaviors where this fails to ever cancel the
696 // context, but that's kinda why HTTP/1.x pipelining died
697 // anyway.
699 if ne, ok := err.(net.Error); ok && cr.aborted && ne.Timeout() {
700 // Ignore this error. It's the expected error from
701 // another goroutine calling abortPendingRead.
702 } else if err != nil {
703 cr.handleReadError(err)
705 cr.aborted = false
706 cr.inRead = false
707 cr.unlock()
708 cr.cond.Broadcast()
711 func (cr *connReader) abortPendingRead() {
712 cr.lock()
713 defer cr.unlock()
714 if !cr.inRead {
715 return
717 cr.aborted = true
718 cr.conn.rwc.SetReadDeadline(aLongTimeAgo)
719 for cr.inRead {
720 cr.cond.Wait()
722 cr.conn.rwc.SetReadDeadline(time.Time{})
725 func (cr *connReader) setReadLimit(remain int64) { cr.remain = remain }
726 func (cr *connReader) setInfiniteReadLimit() { cr.remain = maxInt64 }
727 func (cr *connReader) hitReadLimit() bool { return cr.remain <= 0 }
729 // handleReadError is called whenever a Read from the client returns a
730 // non-nil error.
732 // The provided non-nil err is almost always io.EOF or a "use of
733 // closed network connection". In any case, the error is not
734 // particularly interesting, except perhaps for debugging during
735 // development. Any error means the connection is dead and we should
736 // down its context.
738 // It may be called from multiple goroutines.
739 func (cr *connReader) handleReadError(_ error) {
740 cr.conn.cancelCtx()
741 cr.closeNotify()
744 // may be called from multiple goroutines.
745 func (cr *connReader) closeNotify() {
746 res, _ := cr.conn.curReq.Load().(*response)
747 if res != nil && atomic.CompareAndSwapInt32(&res.didCloseNotify, 0, 1) {
748 res.closeNotifyCh <- true
752 func (cr *connReader) Read(p []byte) (n int, err error) {
753 cr.lock()
754 if cr.inRead {
755 cr.unlock()
756 if cr.conn.hijacked() {
757 panic("invalid Body.Read call. After hijacked, the original Request must not be used")
759 panic("invalid concurrent Body.Read call")
761 if cr.hitReadLimit() {
762 cr.unlock()
763 return 0, io.EOF
765 if len(p) == 0 {
766 cr.unlock()
767 return 0, nil
769 if int64(len(p)) > cr.remain {
770 p = p[:cr.remain]
772 if cr.hasByte {
773 p[0] = cr.byteBuf[0]
774 cr.hasByte = false
775 cr.unlock()
776 return 1, nil
778 cr.inRead = true
779 cr.unlock()
780 n, err = cr.conn.rwc.Read(p)
782 cr.lock()
783 cr.inRead = false
784 if err != nil {
785 cr.handleReadError(err)
787 cr.remain -= int64(n)
788 cr.unlock()
790 cr.cond.Broadcast()
791 return n, err
794 var (
795 bufioReaderPool sync.Pool
796 bufioWriter2kPool sync.Pool
797 bufioWriter4kPool sync.Pool
800 var copyBufPool = sync.Pool{
801 New: func() any {
802 b := make([]byte, 32*1024)
803 return &b
807 func bufioWriterPool(size int) *sync.Pool {
808 switch size {
809 case 2 << 10:
810 return &bufioWriter2kPool
811 case 4 << 10:
812 return &bufioWriter4kPool
814 return nil
817 func newBufioReader(r io.Reader) *bufio.Reader {
818 if v := bufioReaderPool.Get(); v != nil {
819 br := v.(*bufio.Reader)
820 br.Reset(r)
821 return br
823 // Note: if this reader size is ever changed, update
824 // TestHandlerBodyClose's assumptions.
825 return bufio.NewReader(r)
828 func putBufioReader(br *bufio.Reader) {
829 br.Reset(nil)
830 bufioReaderPool.Put(br)
833 func newBufioWriterSize(w io.Writer, size int) *bufio.Writer {
834 pool := bufioWriterPool(size)
835 if pool != nil {
836 if v := pool.Get(); v != nil {
837 bw := v.(*bufio.Writer)
838 bw.Reset(w)
839 return bw
842 return bufio.NewWriterSize(w, size)
845 func putBufioWriter(bw *bufio.Writer) {
846 bw.Reset(nil)
847 if pool := bufioWriterPool(bw.Available()); pool != nil {
848 pool.Put(bw)
852 // DefaultMaxHeaderBytes is the maximum permitted size of the headers
853 // in an HTTP request.
854 // This can be overridden by setting Server.MaxHeaderBytes.
855 const DefaultMaxHeaderBytes = 1 << 20 // 1 MB
857 func (srv *Server) maxHeaderBytes() int {
858 if srv.MaxHeaderBytes > 0 {
859 return srv.MaxHeaderBytes
861 return DefaultMaxHeaderBytes
864 func (srv *Server) initialReadLimitSize() int64 {
865 return int64(srv.maxHeaderBytes()) + 4096 // bufio slop
868 // tlsHandshakeTimeout returns the time limit permitted for the TLS
869 // handshake, or zero for unlimited.
871 // It returns the minimum of any positive ReadHeaderTimeout,
872 // ReadTimeout, or WriteTimeout.
873 func (srv *Server) tlsHandshakeTimeout() time.Duration {
874 var ret time.Duration
875 for _, v := range [...]time.Duration{
876 srv.ReadHeaderTimeout,
877 srv.ReadTimeout,
878 srv.WriteTimeout,
880 if v <= 0 {
881 continue
883 if ret == 0 || v < ret {
884 ret = v
887 return ret
890 // wrapper around io.ReadCloser which on first read, sends an
891 // HTTP/1.1 100 Continue header
892 type expectContinueReader struct {
893 resp *response
894 readCloser io.ReadCloser
895 closed atomicBool
896 sawEOF atomicBool
899 func (ecr *expectContinueReader) Read(p []byte) (n int, err error) {
900 if ecr.closed.isSet() {
901 return 0, ErrBodyReadAfterClose
903 w := ecr.resp
904 if !w.wroteContinue && w.canWriteContinue.isSet() && !w.conn.hijacked() {
905 w.wroteContinue = true
906 w.writeContinueMu.Lock()
907 if w.canWriteContinue.isSet() {
908 w.conn.bufw.WriteString("HTTP/1.1 100 Continue\r\n\r\n")
909 w.conn.bufw.Flush()
910 w.canWriteContinue.setFalse()
912 w.writeContinueMu.Unlock()
914 n, err = ecr.readCloser.Read(p)
915 if err == io.EOF {
916 ecr.sawEOF.setTrue()
918 return
921 func (ecr *expectContinueReader) Close() error {
922 ecr.closed.setTrue()
923 return ecr.readCloser.Close()
926 // TimeFormat is the time format to use when generating times in HTTP
927 // headers. It is like time.RFC1123 but hard-codes GMT as the time
928 // zone. The time being formatted must be in UTC for Format to
929 // generate the correct format.
931 // For parsing this time format, see ParseTime.
932 const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
934 // appendTime is a non-allocating version of []byte(t.UTC().Format(TimeFormat))
935 func appendTime(b []byte, t time.Time) []byte {
936 const days = "SunMonTueWedThuFriSat"
937 const months = "JanFebMarAprMayJunJulAugSepOctNovDec"
939 t = t.UTC()
940 yy, mm, dd := t.Date()
941 hh, mn, ss := t.Clock()
942 day := days[3*t.Weekday():]
943 mon := months[3*(mm-1):]
945 return append(b,
946 day[0], day[1], day[2], ',', ' ',
947 byte('0'+dd/10), byte('0'+dd%10), ' ',
948 mon[0], mon[1], mon[2], ' ',
949 byte('0'+yy/1000), byte('0'+(yy/100)%10), byte('0'+(yy/10)%10), byte('0'+yy%10), ' ',
950 byte('0'+hh/10), byte('0'+hh%10), ':',
951 byte('0'+mn/10), byte('0'+mn%10), ':',
952 byte('0'+ss/10), byte('0'+ss%10), ' ',
953 'G', 'M', 'T')
956 var errTooLarge = errors.New("http: request too large")
958 // Read next request from connection.
959 func (c *conn) readRequest(ctx context.Context) (w *response, err error) {
960 if c.hijacked() {
961 return nil, ErrHijacked
964 var (
965 wholeReqDeadline time.Time // or zero if none
966 hdrDeadline time.Time // or zero if none
968 t0 := time.Now()
969 if d := c.server.readHeaderTimeout(); d > 0 {
970 hdrDeadline = t0.Add(d)
972 if d := c.server.ReadTimeout; d > 0 {
973 wholeReqDeadline = t0.Add(d)
975 c.rwc.SetReadDeadline(hdrDeadline)
976 if d := c.server.WriteTimeout; d > 0 {
977 defer func() {
978 c.rwc.SetWriteDeadline(time.Now().Add(d))
982 c.r.setReadLimit(c.server.initialReadLimitSize())
983 if c.lastMethod == "POST" {
984 // RFC 7230 section 3 tolerance for old buggy clients.
985 peek, _ := c.bufr.Peek(4) // ReadRequest will get err below
986 c.bufr.Discard(numLeadingCRorLF(peek))
988 req, err := readRequest(c.bufr)
989 if err != nil {
990 if c.r.hitReadLimit() {
991 return nil, errTooLarge
993 return nil, err
996 if !http1ServerSupportsRequest(req) {
997 return nil, statusError{StatusHTTPVersionNotSupported, "unsupported protocol version"}
1000 c.lastMethod = req.Method
1001 c.r.setInfiniteReadLimit()
1003 hosts, haveHost := req.Header["Host"]
1004 isH2Upgrade := req.isH2Upgrade()
1005 if req.ProtoAtLeast(1, 1) && (!haveHost || len(hosts) == 0) && !isH2Upgrade && req.Method != "CONNECT" {
1006 return nil, badRequestError("missing required Host header")
1008 if len(hosts) == 1 && !httpguts.ValidHostHeader(hosts[0]) {
1009 return nil, badRequestError("malformed Host header")
1011 for k, vv := range req.Header {
1012 if !httpguts.ValidHeaderFieldName(k) {
1013 return nil, badRequestError("invalid header name")
1015 for _, v := range vv {
1016 if !httpguts.ValidHeaderFieldValue(v) {
1017 return nil, badRequestError("invalid header value")
1021 delete(req.Header, "Host")
1023 ctx, cancelCtx := context.WithCancel(ctx)
1024 req.ctx = ctx
1025 req.RemoteAddr = c.remoteAddr
1026 req.TLS = c.tlsState
1027 if body, ok := req.Body.(*body); ok {
1028 body.doEarlyClose = true
1031 // Adjust the read deadline if necessary.
1032 if !hdrDeadline.Equal(wholeReqDeadline) {
1033 c.rwc.SetReadDeadline(wholeReqDeadline)
1036 w = &response{
1037 conn: c,
1038 cancelCtx: cancelCtx,
1039 req: req,
1040 reqBody: req.Body,
1041 handlerHeader: make(Header),
1042 contentLength: -1,
1043 closeNotifyCh: make(chan bool, 1),
1045 // We populate these ahead of time so we're not
1046 // reading from req.Header after their Handler starts
1047 // and maybe mutates it (Issue 14940)
1048 wants10KeepAlive: req.wantsHttp10KeepAlive(),
1049 wantsClose: req.wantsClose(),
1051 if isH2Upgrade {
1052 w.closeAfterReply = true
1054 w.cw.res = w
1055 w.w = newBufioWriterSize(&w.cw, bufferBeforeChunkingSize)
1056 return w, nil
1059 // http1ServerSupportsRequest reports whether Go's HTTP/1.x server
1060 // supports the given request.
1061 func http1ServerSupportsRequest(req *Request) bool {
1062 if req.ProtoMajor == 1 {
1063 return true
1065 // Accept "PRI * HTTP/2.0" upgrade requests, so Handlers can
1066 // wire up their own HTTP/2 upgrades.
1067 if req.ProtoMajor == 2 && req.ProtoMinor == 0 &&
1068 req.Method == "PRI" && req.RequestURI == "*" {
1069 return true
1071 // Reject HTTP/0.x, and all other HTTP/2+ requests (which
1072 // aren't encoded in ASCII anyway).
1073 return false
1076 func (w *response) Header() Header {
1077 if w.cw.header == nil && w.wroteHeader && !w.cw.wroteHeader {
1078 // Accessing the header between logically writing it
1079 // and physically writing it means we need to allocate
1080 // a clone to snapshot the logically written state.
1081 w.cw.header = w.handlerHeader.Clone()
1083 w.calledHeader = true
1084 return w.handlerHeader
1087 // maxPostHandlerReadBytes is the max number of Request.Body bytes not
1088 // consumed by a handler that the server will read from the client
1089 // in order to keep a connection alive. If there are more bytes than
1090 // this then the server to be paranoid instead sends a "Connection:
1091 // close" response.
1093 // This number is approximately what a typical machine's TCP buffer
1094 // size is anyway. (if we have the bytes on the machine, we might as
1095 // well read them)
1096 const maxPostHandlerReadBytes = 256 << 10
1098 func checkWriteHeaderCode(code int) {
1099 // Issue 22880: require valid WriteHeader status codes.
1100 // For now we only enforce that it's three digits.
1101 // In the future we might block things over 599 (600 and above aren't defined
1102 // at https://httpwg.org/specs/rfc7231.html#status.codes)
1103 // and we might block under 200 (once we have more mature 1xx support).
1104 // But for now any three digits.
1106 // We used to send "HTTP/1.1 000 0" on the wire in responses but there's
1107 // no equivalent bogus thing we can realistically send in HTTP/2,
1108 // so we'll consistently panic instead and help people find their bugs
1109 // early. (We can't return an error from WriteHeader even if we wanted to.)
1110 if code < 100 || code > 999 {
1111 panic(fmt.Sprintf("invalid WriteHeader code %v", code))
1115 // relevantCaller searches the call stack for the first function outside of net/http.
1116 // The purpose of this function is to provide more helpful error messages.
1117 func relevantCaller() runtime.Frame {
1118 pc := make([]uintptr, 16)
1119 n := runtime.Callers(1, pc)
1120 frames := runtime.CallersFrames(pc[:n])
1121 prefix1 := "net/http."
1122 prefix2 := "net/http."
1123 if runtime.Compiler == "gccgo" {
1124 prefix2 = "http."
1126 var frame runtime.Frame
1127 for {
1128 frame, more := frames.Next()
1129 if !strings.HasPrefix(frame.Function, prefix1) && !strings.HasPrefix(frame.Function, prefix2) {
1130 return frame
1132 if !more {
1133 break
1136 return frame
1139 func (w *response) WriteHeader(code int) {
1140 if w.conn.hijacked() {
1141 caller := relevantCaller()
1142 w.conn.server.logf("http: response.WriteHeader on hijacked connection from %s (%s:%d)", caller.Function, path.Base(caller.File), caller.Line)
1143 return
1145 if w.wroteHeader {
1146 caller := relevantCaller()
1147 w.conn.server.logf("http: superfluous response.WriteHeader call from %s (%s:%d)", caller.Function, path.Base(caller.File), caller.Line)
1148 return
1150 checkWriteHeaderCode(code)
1151 w.wroteHeader = true
1152 w.status = code
1154 if w.calledHeader && w.cw.header == nil {
1155 w.cw.header = w.handlerHeader.Clone()
1158 if cl := w.handlerHeader.get("Content-Length"); cl != "" {
1159 v, err := strconv.ParseInt(cl, 10, 64)
1160 if err == nil && v >= 0 {
1161 w.contentLength = v
1162 } else {
1163 w.conn.server.logf("http: invalid Content-Length of %q", cl)
1164 w.handlerHeader.Del("Content-Length")
1169 // extraHeader is the set of headers sometimes added by chunkWriter.writeHeader.
1170 // This type is used to avoid extra allocations from cloning and/or populating
1171 // the response Header map and all its 1-element slices.
1172 type extraHeader struct {
1173 contentType string
1174 connection string
1175 transferEncoding string
1176 date []byte // written if not nil
1177 contentLength []byte // written if not nil
1180 // Sorted the same as extraHeader.Write's loop.
1181 var extraHeaderKeys = [][]byte{
1182 []byte("Content-Type"),
1183 []byte("Connection"),
1184 []byte("Transfer-Encoding"),
1187 var (
1188 headerContentLength = []byte("Content-Length: ")
1189 headerDate = []byte("Date: ")
1192 // Write writes the headers described in h to w.
1194 // This method has a value receiver, despite the somewhat large size
1195 // of h, because it prevents an allocation. The escape analysis isn't
1196 // smart enough to realize this function doesn't mutate h.
1197 func (h extraHeader) Write(w *bufio.Writer) {
1198 if h.date != nil {
1199 w.Write(headerDate)
1200 w.Write(h.date)
1201 w.Write(crlf)
1203 if h.contentLength != nil {
1204 w.Write(headerContentLength)
1205 w.Write(h.contentLength)
1206 w.Write(crlf)
1208 for i, v := range []string{h.contentType, h.connection, h.transferEncoding} {
1209 if v != "" {
1210 w.Write(extraHeaderKeys[i])
1211 w.Write(colonSpace)
1212 w.WriteString(v)
1213 w.Write(crlf)
1218 // writeHeader finalizes the header sent to the client and writes it
1219 // to cw.res.conn.bufw.
1221 // p is not written by writeHeader, but is the first chunk of the body
1222 // that will be written. It is sniffed for a Content-Type if none is
1223 // set explicitly. It's also used to set the Content-Length, if the
1224 // total body size was small and the handler has already finished
1225 // running.
1226 func (cw *chunkWriter) writeHeader(p []byte) {
1227 if cw.wroteHeader {
1228 return
1230 cw.wroteHeader = true
1232 w := cw.res
1233 keepAlivesEnabled := w.conn.server.doKeepAlives()
1234 isHEAD := w.req.Method == "HEAD"
1236 // header is written out to w.conn.buf below. Depending on the
1237 // state of the handler, we either own the map or not. If we
1238 // don't own it, the exclude map is created lazily for
1239 // WriteSubset to remove headers. The setHeader struct holds
1240 // headers we need to add.
1241 header := cw.header
1242 owned := header != nil
1243 if !owned {
1244 header = w.handlerHeader
1246 var excludeHeader map[string]bool
1247 delHeader := func(key string) {
1248 if owned {
1249 header.Del(key)
1250 return
1252 if _, ok := header[key]; !ok {
1253 return
1255 if excludeHeader == nil {
1256 excludeHeader = make(map[string]bool)
1258 excludeHeader[key] = true
1260 var setHeader extraHeader
1262 // Don't write out the fake "Trailer:foo" keys. See TrailerPrefix.
1263 trailers := false
1264 for k := range cw.header {
1265 if strings.HasPrefix(k, TrailerPrefix) {
1266 if excludeHeader == nil {
1267 excludeHeader = make(map[string]bool)
1269 excludeHeader[k] = true
1270 trailers = true
1273 for _, v := range cw.header["Trailer"] {
1274 trailers = true
1275 foreachHeaderElement(v, cw.res.declareTrailer)
1278 te := header.get("Transfer-Encoding")
1279 hasTE := te != ""
1281 // If the handler is done but never sent a Content-Length
1282 // response header and this is our first (and last) write, set
1283 // it, even to zero. This helps HTTP/1.0 clients keep their
1284 // "keep-alive" connections alive.
1285 // Exceptions: 304/204/1xx responses never get Content-Length, and if
1286 // it was a HEAD request, we don't know the difference between
1287 // 0 actual bytes and 0 bytes because the handler noticed it
1288 // was a HEAD request and chose not to write anything. So for
1289 // HEAD, the handler should either write the Content-Length or
1290 // write non-zero bytes. If it's actually 0 bytes and the
1291 // handler never looked at the Request.Method, we just don't
1292 // send a Content-Length header.
1293 // Further, we don't send an automatic Content-Length if they
1294 // set a Transfer-Encoding, because they're generally incompatible.
1295 if w.handlerDone.isSet() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && header.get("Content-Length") == "" && (!isHEAD || len(p) > 0) {
1296 w.contentLength = int64(len(p))
1297 setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10)
1300 // If this was an HTTP/1.0 request with keep-alive and we sent a
1301 // Content-Length back, we can make this a keep-alive response ...
1302 if w.wants10KeepAlive && keepAlivesEnabled {
1303 sentLength := header.get("Content-Length") != ""
1304 if sentLength && header.get("Connection") == "keep-alive" {
1305 w.closeAfterReply = false
1309 // Check for an explicit (and valid) Content-Length header.
1310 hasCL := w.contentLength != -1
1312 if w.wants10KeepAlive && (isHEAD || hasCL || !bodyAllowedForStatus(w.status)) {
1313 _, connectionHeaderSet := header["Connection"]
1314 if !connectionHeaderSet {
1315 setHeader.connection = "keep-alive"
1317 } else if !w.req.ProtoAtLeast(1, 1) || w.wantsClose {
1318 w.closeAfterReply = true
1321 if header.get("Connection") == "close" || !keepAlivesEnabled {
1322 w.closeAfterReply = true
1325 // If the client wanted a 100-continue but we never sent it to
1326 // them (or, more strictly: we never finished reading their
1327 // request body), don't reuse this connection because it's now
1328 // in an unknown state: we might be sending this response at
1329 // the same time the client is now sending its request body
1330 // after a timeout. (Some HTTP clients send Expect:
1331 // 100-continue but knowing that some servers don't support
1332 // it, the clients set a timer and send the body later anyway)
1333 // If we haven't seen EOF, we can't skip over the unread body
1334 // because we don't know if the next bytes on the wire will be
1335 // the body-following-the-timer or the subsequent request.
1336 // See Issue 11549.
1337 if ecr, ok := w.req.Body.(*expectContinueReader); ok && !ecr.sawEOF.isSet() {
1338 w.closeAfterReply = true
1341 // Per RFC 2616, we should consume the request body before
1342 // replying, if the handler hasn't already done so. But we
1343 // don't want to do an unbounded amount of reading here for
1344 // DoS reasons, so we only try up to a threshold.
1345 // TODO(bradfitz): where does RFC 2616 say that? See Issue 15527
1346 // about HTTP/1.x Handlers concurrently reading and writing, like
1347 // HTTP/2 handlers can do. Maybe this code should be relaxed?
1348 if w.req.ContentLength != 0 && !w.closeAfterReply {
1349 var discard, tooBig bool
1351 switch bdy := w.req.Body.(type) {
1352 case *expectContinueReader:
1353 if bdy.resp.wroteContinue {
1354 discard = true
1356 case *body:
1357 bdy.mu.Lock()
1358 switch {
1359 case bdy.closed:
1360 if !bdy.sawEOF {
1361 // Body was closed in handler with non-EOF error.
1362 w.closeAfterReply = true
1364 case bdy.unreadDataSizeLocked() >= maxPostHandlerReadBytes:
1365 tooBig = true
1366 default:
1367 discard = true
1369 bdy.mu.Unlock()
1370 default:
1371 discard = true
1374 if discard {
1375 _, err := io.CopyN(io.Discard, w.reqBody, maxPostHandlerReadBytes+1)
1376 switch err {
1377 case nil:
1378 // There must be even more data left over.
1379 tooBig = true
1380 case ErrBodyReadAfterClose:
1381 // Body was already consumed and closed.
1382 case io.EOF:
1383 // The remaining body was just consumed, close it.
1384 err = w.reqBody.Close()
1385 if err != nil {
1386 w.closeAfterReply = true
1388 default:
1389 // Some other kind of error occurred, like a read timeout, or
1390 // corrupt chunked encoding. In any case, whatever remains
1391 // on the wire must not be parsed as another HTTP request.
1392 w.closeAfterReply = true
1396 if tooBig {
1397 w.requestTooLarge()
1398 delHeader("Connection")
1399 setHeader.connection = "close"
1403 code := w.status
1404 if bodyAllowedForStatus(code) {
1405 // If no content type, apply sniffing algorithm to body.
1406 _, haveType := header["Content-Type"]
1408 // If the Content-Encoding was set and is non-blank,
1409 // we shouldn't sniff the body. See Issue 31753.
1410 ce := header.Get("Content-Encoding")
1411 hasCE := len(ce) > 0
1412 if !hasCE && !haveType && !hasTE && len(p) > 0 {
1413 setHeader.contentType = DetectContentType(p)
1415 } else {
1416 for _, k := range suppressedHeaders(code) {
1417 delHeader(k)
1421 if !header.has("Date") {
1422 setHeader.date = appendTime(cw.res.dateBuf[:0], time.Now())
1425 if hasCL && hasTE && te != "identity" {
1426 // TODO: return an error if WriteHeader gets a return parameter
1427 // For now just ignore the Content-Length.
1428 w.conn.server.logf("http: WriteHeader called with both Transfer-Encoding of %q and a Content-Length of %d",
1429 te, w.contentLength)
1430 delHeader("Content-Length")
1431 hasCL = false
1434 if w.req.Method == "HEAD" || !bodyAllowedForStatus(code) || code == StatusNoContent {
1435 // Response has no body.
1436 delHeader("Transfer-Encoding")
1437 } else if hasCL {
1438 // Content-Length has been provided, so no chunking is to be done.
1439 delHeader("Transfer-Encoding")
1440 } else if w.req.ProtoAtLeast(1, 1) {
1441 // HTTP/1.1 or greater: Transfer-Encoding has been set to identity, and no
1442 // content-length has been provided. The connection must be closed after the
1443 // reply is written, and no chunking is to be done. This is the setup
1444 // recommended in the Server-Sent Events candidate recommendation 11,
1445 // section 8.
1446 if hasTE && te == "identity" {
1447 cw.chunking = false
1448 w.closeAfterReply = true
1449 delHeader("Transfer-Encoding")
1450 } else {
1451 // HTTP/1.1 or greater: use chunked transfer encoding
1452 // to avoid closing the connection at EOF.
1453 cw.chunking = true
1454 setHeader.transferEncoding = "chunked"
1455 if hasTE && te == "chunked" {
1456 // We will send the chunked Transfer-Encoding header later.
1457 delHeader("Transfer-Encoding")
1460 } else {
1461 // HTTP version < 1.1: cannot do chunked transfer
1462 // encoding and we don't know the Content-Length so
1463 // signal EOF by closing connection.
1464 w.closeAfterReply = true
1465 delHeader("Transfer-Encoding") // in case already set
1468 // Cannot use Content-Length with non-identity Transfer-Encoding.
1469 if cw.chunking {
1470 delHeader("Content-Length")
1472 if !w.req.ProtoAtLeast(1, 0) {
1473 return
1476 // Only override the Connection header if it is not a successful
1477 // protocol switch response and if KeepAlives are not enabled.
1478 // See https://golang.org/issue/36381.
1479 delConnectionHeader := w.closeAfterReply &&
1480 (!keepAlivesEnabled || !hasToken(cw.header.get("Connection"), "close")) &&
1481 !isProtocolSwitchResponse(w.status, header)
1482 if delConnectionHeader {
1483 delHeader("Connection")
1484 if w.req.ProtoAtLeast(1, 1) {
1485 setHeader.connection = "close"
1489 writeStatusLine(w.conn.bufw, w.req.ProtoAtLeast(1, 1), code, w.statusBuf[:])
1490 cw.header.WriteSubset(w.conn.bufw, excludeHeader)
1491 setHeader.Write(w.conn.bufw)
1492 w.conn.bufw.Write(crlf)
1495 // foreachHeaderElement splits v according to the "#rule" construction
1496 // in RFC 7230 section 7 and calls fn for each non-empty element.
1497 func foreachHeaderElement(v string, fn func(string)) {
1498 v = textproto.TrimString(v)
1499 if v == "" {
1500 return
1502 if !strings.Contains(v, ",") {
1503 fn(v)
1504 return
1506 for _, f := range strings.Split(v, ",") {
1507 if f = textproto.TrimString(f); f != "" {
1508 fn(f)
1513 // writeStatusLine writes an HTTP/1.x Status-Line (RFC 7230 Section 3.1.2)
1514 // to bw. is11 is whether the HTTP request is HTTP/1.1. false means HTTP/1.0.
1515 // code is the response status code.
1516 // scratch is an optional scratch buffer. If it has at least capacity 3, it's used.
1517 func writeStatusLine(bw *bufio.Writer, is11 bool, code int, scratch []byte) {
1518 if is11 {
1519 bw.WriteString("HTTP/1.1 ")
1520 } else {
1521 bw.WriteString("HTTP/1.0 ")
1523 if text, ok := statusText[code]; ok {
1524 bw.Write(strconv.AppendInt(scratch[:0], int64(code), 10))
1525 bw.WriteByte(' ')
1526 bw.WriteString(text)
1527 bw.WriteString("\r\n")
1528 } else {
1529 // don't worry about performance
1530 fmt.Fprintf(bw, "%03d status code %d\r\n", code, code)
1534 // bodyAllowed reports whether a Write is allowed for this response type.
1535 // It's illegal to call this before the header has been flushed.
1536 func (w *response) bodyAllowed() bool {
1537 if !w.wroteHeader {
1538 panic("")
1540 return bodyAllowedForStatus(w.status)
1543 // The Life Of A Write is like this:
1545 // Handler starts. No header has been sent. The handler can either
1546 // write a header, or just start writing. Writing before sending a header
1547 // sends an implicitly empty 200 OK header.
1549 // If the handler didn't declare a Content-Length up front, we either
1550 // go into chunking mode or, if the handler finishes running before
1551 // the chunking buffer size, we compute a Content-Length and send that
1552 // in the header instead.
1554 // Likewise, if the handler didn't set a Content-Type, we sniff that
1555 // from the initial chunk of output.
1557 // The Writers are wired together like:
1559 // 1. *response (the ResponseWriter) ->
1560 // 2. (*response).w, a *bufio.Writer of bufferBeforeChunkingSize bytes ->
1561 // 3. chunkWriter.Writer (whose writeHeader finalizes Content-Length/Type)
1562 // and which writes the chunk headers, if needed ->
1563 // 4. conn.bufw, a *bufio.Writer of default (4kB) bytes, writing to ->
1564 // 5. checkConnErrorWriter{c}, which notes any non-nil error on Write
1565 // and populates c.werr with it if so, but otherwise writes to ->
1566 // 6. the rwc, the net.Conn.
1568 // TODO(bradfitz): short-circuit some of the buffering when the
1569 // initial header contains both a Content-Type and Content-Length.
1570 // Also short-circuit in (1) when the header's been sent and not in
1571 // chunking mode, writing directly to (4) instead, if (2) has no
1572 // buffered data. More generally, we could short-circuit from (1) to
1573 // (3) even in chunking mode if the write size from (1) is over some
1574 // threshold and nothing is in (2). The answer might be mostly making
1575 // bufferBeforeChunkingSize smaller and having bufio's fast-paths deal
1576 // with this instead.
1577 func (w *response) Write(data []byte) (n int, err error) {
1578 return w.write(len(data), data, "")
1581 func (w *response) WriteString(data string) (n int, err error) {
1582 return w.write(len(data), nil, data)
1585 // either dataB or dataS is non-zero.
1586 func (w *response) write(lenData int, dataB []byte, dataS string) (n int, err error) {
1587 if w.conn.hijacked() {
1588 if lenData > 0 {
1589 caller := relevantCaller()
1590 w.conn.server.logf("http: response.Write on hijacked connection from %s (%s:%d)", caller.Function, path.Base(caller.File), caller.Line)
1592 return 0, ErrHijacked
1595 if w.canWriteContinue.isSet() {
1596 // Body reader wants to write 100 Continue but hasn't yet.
1597 // Tell it not to. The store must be done while holding the lock
1598 // because the lock makes sure that there is not an active write
1599 // this very moment.
1600 w.writeContinueMu.Lock()
1601 w.canWriteContinue.setFalse()
1602 w.writeContinueMu.Unlock()
1605 if !w.wroteHeader {
1606 w.WriteHeader(StatusOK)
1608 if lenData == 0 {
1609 return 0, nil
1611 if !w.bodyAllowed() {
1612 return 0, ErrBodyNotAllowed
1615 w.written += int64(lenData) // ignoring errors, for errorKludge
1616 if w.contentLength != -1 && w.written > w.contentLength {
1617 return 0, ErrContentLength
1619 if dataB != nil {
1620 return w.w.Write(dataB)
1621 } else {
1622 return w.w.WriteString(dataS)
1626 func (w *response) finishRequest() {
1627 w.handlerDone.setTrue()
1629 if !w.wroteHeader {
1630 w.WriteHeader(StatusOK)
1633 w.w.Flush()
1634 putBufioWriter(w.w)
1635 w.cw.close()
1636 w.conn.bufw.Flush()
1638 w.conn.r.abortPendingRead()
1640 // Close the body (regardless of w.closeAfterReply) so we can
1641 // re-use its bufio.Reader later safely.
1642 w.reqBody.Close()
1644 if w.req.MultipartForm != nil {
1645 w.req.MultipartForm.RemoveAll()
1649 // shouldReuseConnection reports whether the underlying TCP connection can be reused.
1650 // It must only be called after the handler is done executing.
1651 func (w *response) shouldReuseConnection() bool {
1652 if w.closeAfterReply {
1653 // The request or something set while executing the
1654 // handler indicated we shouldn't reuse this
1655 // connection.
1656 return false
1659 if w.req.Method != "HEAD" && w.contentLength != -1 && w.bodyAllowed() && w.contentLength != w.written {
1660 // Did not write enough. Avoid getting out of sync.
1661 return false
1664 // There was some error writing to the underlying connection
1665 // during the request, so don't re-use this conn.
1666 if w.conn.werr != nil {
1667 return false
1670 if w.closedRequestBodyEarly() {
1671 return false
1674 return true
1677 func (w *response) closedRequestBodyEarly() bool {
1678 body, ok := w.req.Body.(*body)
1679 return ok && body.didEarlyClose()
1682 func (w *response) Flush() {
1683 if !w.wroteHeader {
1684 w.WriteHeader(StatusOK)
1686 w.w.Flush()
1687 w.cw.flush()
1690 func (c *conn) finalFlush() {
1691 if c.bufr != nil {
1692 // Steal the bufio.Reader (~4KB worth of memory) and its associated
1693 // reader for a future connection.
1694 putBufioReader(c.bufr)
1695 c.bufr = nil
1698 if c.bufw != nil {
1699 c.bufw.Flush()
1700 // Steal the bufio.Writer (~4KB worth of memory) and its associated
1701 // writer for a future connection.
1702 putBufioWriter(c.bufw)
1703 c.bufw = nil
1707 // Close the connection.
1708 func (c *conn) close() {
1709 c.finalFlush()
1710 c.rwc.Close()
1713 // rstAvoidanceDelay is the amount of time we sleep after closing the
1714 // write side of a TCP connection before closing the entire socket.
1715 // By sleeping, we increase the chances that the client sees our FIN
1716 // and processes its final data before they process the subsequent RST
1717 // from closing a connection with known unread data.
1718 // This RST seems to occur mostly on BSD systems. (And Windows?)
1719 // This timeout is somewhat arbitrary (~latency around the planet).
1720 const rstAvoidanceDelay = 500 * time.Millisecond
1722 type closeWriter interface {
1723 CloseWrite() error
1726 var _ closeWriter = (*net.TCPConn)(nil)
1728 // closeWrite flushes any outstanding data and sends a FIN packet (if
1729 // client is connected via TCP), signalling that we're done. We then
1730 // pause for a bit, hoping the client processes it before any
1731 // subsequent RST.
1733 // See https://golang.org/issue/3595
1734 func (c *conn) closeWriteAndWait() {
1735 c.finalFlush()
1736 if tcp, ok := c.rwc.(closeWriter); ok {
1737 tcp.CloseWrite()
1739 time.Sleep(rstAvoidanceDelay)
1742 // validNextProto reports whether the proto is a valid ALPN protocol name.
1743 // Everything is valid except the empty string and built-in protocol types,
1744 // so that those can't be overridden with alternate implementations.
1745 func validNextProto(proto string) bool {
1746 switch proto {
1747 case "", "http/1.1", "http/1.0":
1748 return false
1750 return true
1753 const (
1754 runHooks = true
1755 skipHooks = false
1758 func (c *conn) setState(nc net.Conn, state ConnState, runHook bool) {
1759 srv := c.server
1760 switch state {
1761 case StateNew:
1762 srv.trackConn(c, true)
1763 case StateHijacked, StateClosed:
1764 srv.trackConn(c, false)
1766 if state > 0xff || state < 0 {
1767 panic("internal error")
1769 packedState := uint64(time.Now().Unix()<<8) | uint64(state)
1770 atomic.StoreUint64(&c.curState.atomic, packedState)
1771 if !runHook {
1772 return
1774 if hook := srv.ConnState; hook != nil {
1775 hook(nc, state)
1779 func (c *conn) getState() (state ConnState, unixSec int64) {
1780 packedState := atomic.LoadUint64(&c.curState.atomic)
1781 return ConnState(packedState & 0xff), int64(packedState >> 8)
1784 // badRequestError is a literal string (used by in the server in HTML,
1785 // unescaped) to tell the user why their request was bad. It should
1786 // be plain text without user info or other embedded errors.
1787 func badRequestError(e string) error { return statusError{StatusBadRequest, e} }
1789 // statusError is an error used to respond to a request with an HTTP status.
1790 // The text should be plain text without user info or other embedded errors.
1791 type statusError struct {
1792 code int
1793 text string
1796 func (e statusError) Error() string { return StatusText(e.code) + ": " + e.text }
1798 // ErrAbortHandler is a sentinel panic value to abort a handler.
1799 // While any panic from ServeHTTP aborts the response to the client,
1800 // panicking with ErrAbortHandler also suppresses logging of a stack
1801 // trace to the server's error log.
1802 var ErrAbortHandler = errors.New("net/http: abort Handler")
1804 // isCommonNetReadError reports whether err is a common error
1805 // encountered during reading a request off the network when the
1806 // client has gone away or had its read fail somehow. This is used to
1807 // determine which logs are interesting enough to log about.
1808 func isCommonNetReadError(err error) bool {
1809 if err == io.EOF {
1810 return true
1812 if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
1813 return true
1815 if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
1816 return true
1818 return false
1821 // Serve a new connection.
1822 func (c *conn) serve(ctx context.Context) {
1823 c.remoteAddr = c.rwc.RemoteAddr().String()
1824 ctx = context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr())
1825 var inFlightResponse *response
1826 defer func() {
1827 if err := recover(); err != nil && err != ErrAbortHandler {
1828 const size = 64 << 10
1829 buf := make([]byte, size)
1830 buf = buf[:runtime.Stack(buf, false)]
1831 c.server.logf("http: panic serving %v: %v\n%s", c.remoteAddr, err, buf)
1833 if inFlightResponse != nil {
1834 inFlightResponse.cancelCtx()
1836 if !c.hijacked() {
1837 if inFlightResponse != nil {
1838 inFlightResponse.conn.r.abortPendingRead()
1839 inFlightResponse.reqBody.Close()
1841 c.close()
1842 c.setState(c.rwc, StateClosed, runHooks)
1846 if tlsConn, ok := c.rwc.(*tls.Conn); ok {
1847 tlsTO := c.server.tlsHandshakeTimeout()
1848 if tlsTO > 0 {
1849 dl := time.Now().Add(tlsTO)
1850 c.rwc.SetReadDeadline(dl)
1851 c.rwc.SetWriteDeadline(dl)
1853 if err := tlsConn.HandshakeContext(ctx); err != nil {
1854 // If the handshake failed due to the client not speaking
1855 // TLS, assume they're speaking plaintext HTTP and write a
1856 // 400 response on the TLS conn's underlying net.Conn.
1857 if re, ok := err.(tls.RecordHeaderError); ok && re.Conn != nil && tlsRecordHeaderLooksLikeHTTP(re.RecordHeader) {
1858 io.WriteString(re.Conn, "HTTP/1.0 400 Bad Request\r\n\r\nClient sent an HTTP request to an HTTPS server.\n")
1859 re.Conn.Close()
1860 return
1862 c.server.logf("http: TLS handshake error from %s: %v", c.rwc.RemoteAddr(), err)
1863 return
1865 // Restore Conn-level deadlines.
1866 if tlsTO > 0 {
1867 c.rwc.SetReadDeadline(time.Time{})
1868 c.rwc.SetWriteDeadline(time.Time{})
1870 c.tlsState = new(tls.ConnectionState)
1871 *c.tlsState = tlsConn.ConnectionState()
1872 if proto := c.tlsState.NegotiatedProtocol; validNextProto(proto) {
1873 if fn := c.server.TLSNextProto[proto]; fn != nil {
1874 h := initALPNRequest{ctx, tlsConn, serverHandler{c.server}}
1875 // Mark freshly created HTTP/2 as active and prevent any server state hooks
1876 // from being run on these connections. This prevents closeIdleConns from
1877 // closing such connections. See issue https://golang.org/issue/39776.
1878 c.setState(c.rwc, StateActive, skipHooks)
1879 fn(c.server, tlsConn, h)
1881 return
1885 // HTTP/1.x from here on.
1887 ctx, cancelCtx := context.WithCancel(ctx)
1888 c.cancelCtx = cancelCtx
1889 defer cancelCtx()
1891 c.r = &connReader{conn: c}
1892 c.bufr = newBufioReader(c.r)
1893 c.bufw = newBufioWriterSize(checkConnErrorWriter{c}, 4<<10)
1895 for {
1896 w, err := c.readRequest(ctx)
1897 if c.r.remain != c.server.initialReadLimitSize() {
1898 // If we read any bytes off the wire, we're active.
1899 c.setState(c.rwc, StateActive, runHooks)
1901 if err != nil {
1902 const errorHeaders = "\r\nContent-Type: text/plain; charset=utf-8\r\nConnection: close\r\n\r\n"
1904 switch {
1905 case err == errTooLarge:
1906 // Their HTTP client may or may not be
1907 // able to read this if we're
1908 // responding to them and hanging up
1909 // while they're still writing their
1910 // request. Undefined behavior.
1911 const publicErr = "431 Request Header Fields Too Large"
1912 fmt.Fprintf(c.rwc, "HTTP/1.1 "+publicErr+errorHeaders+publicErr)
1913 c.closeWriteAndWait()
1914 return
1916 case isUnsupportedTEError(err):
1917 // Respond as per RFC 7230 Section 3.3.1 which says,
1918 // A server that receives a request message with a
1919 // transfer coding it does not understand SHOULD
1920 // respond with 501 (Unimplemented).
1921 code := StatusNotImplemented
1923 // We purposefully aren't echoing back the transfer-encoding's value,
1924 // so as to mitigate the risk of cross side scripting by an attacker.
1925 fmt.Fprintf(c.rwc, "HTTP/1.1 %d %s%sUnsupported transfer encoding", code, StatusText(code), errorHeaders)
1926 return
1928 case isCommonNetReadError(err):
1929 return // don't reply
1931 default:
1932 if v, ok := err.(statusError); ok {
1933 fmt.Fprintf(c.rwc, "HTTP/1.1 %d %s: %s%s%d %s: %s", v.code, StatusText(v.code), v.text, errorHeaders, v.code, StatusText(v.code), v.text)
1934 return
1936 publicErr := "400 Bad Request"
1937 fmt.Fprintf(c.rwc, "HTTP/1.1 "+publicErr+errorHeaders+publicErr)
1938 return
1942 // Expect 100 Continue support
1943 req := w.req
1944 if req.expectsContinue() {
1945 if req.ProtoAtLeast(1, 1) && req.ContentLength != 0 {
1946 // Wrap the Body reader with one that replies on the connection
1947 req.Body = &expectContinueReader{readCloser: req.Body, resp: w}
1948 w.canWriteContinue.setTrue()
1950 } else if req.Header.get("Expect") != "" {
1951 w.sendExpectationFailed()
1952 return
1955 c.curReq.Store(w)
1957 if requestBodyRemains(req.Body) {
1958 registerOnHitEOF(req.Body, w.conn.r.startBackgroundRead)
1959 } else {
1960 w.conn.r.startBackgroundRead()
1963 // HTTP cannot have multiple simultaneous active requests.[*]
1964 // Until the server replies to this request, it can't read another,
1965 // so we might as well run the handler in this goroutine.
1966 // [*] Not strictly true: HTTP pipelining. We could let them all process
1967 // in parallel even if their responses need to be serialized.
1968 // But we're not going to implement HTTP pipelining because it
1969 // was never deployed in the wild and the answer is HTTP/2.
1970 inFlightResponse = w
1971 serverHandler{c.server}.ServeHTTP(w, w.req)
1972 inFlightResponse = nil
1973 w.cancelCtx()
1974 if c.hijacked() {
1975 return
1977 w.finishRequest()
1978 if !w.shouldReuseConnection() {
1979 if w.requestBodyLimitHit || w.closedRequestBodyEarly() {
1980 c.closeWriteAndWait()
1982 return
1984 c.setState(c.rwc, StateIdle, runHooks)
1985 c.curReq.Store((*response)(nil))
1987 if !w.conn.server.doKeepAlives() {
1988 // We're in shutdown mode. We might've replied
1989 // to the user without "Connection: close" and
1990 // they might think they can send another
1991 // request, but such is life with HTTP/1.1.
1992 return
1995 if d := c.server.idleTimeout(); d != 0 {
1996 c.rwc.SetReadDeadline(time.Now().Add(d))
1997 if _, err := c.bufr.Peek(4); err != nil {
1998 return
2001 c.rwc.SetReadDeadline(time.Time{})
2005 func (w *response) sendExpectationFailed() {
2006 // TODO(bradfitz): let ServeHTTP handlers handle
2007 // requests with non-standard expectation[s]? Seems
2008 // theoretical at best, and doesn't fit into the
2009 // current ServeHTTP model anyway. We'd need to
2010 // make the ResponseWriter an optional
2011 // "ExpectReplier" interface or something.
2013 // For now we'll just obey RFC 7231 5.1.1 which says
2014 // "A server that receives an Expect field-value other
2015 // than 100-continue MAY respond with a 417 (Expectation
2016 // Failed) status code to indicate that the unexpected
2017 // expectation cannot be met."
2018 w.Header().Set("Connection", "close")
2019 w.WriteHeader(StatusExpectationFailed)
2020 w.finishRequest()
2023 // Hijack implements the Hijacker.Hijack method. Our response is both a ResponseWriter
2024 // and a Hijacker.
2025 func (w *response) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
2026 if w.handlerDone.isSet() {
2027 panic("net/http: Hijack called after ServeHTTP finished")
2029 if w.wroteHeader {
2030 w.cw.flush()
2033 c := w.conn
2034 c.mu.Lock()
2035 defer c.mu.Unlock()
2037 // Release the bufioWriter that writes to the chunk writer, it is not
2038 // used after a connection has been hijacked.
2039 rwc, buf, err = c.hijackLocked()
2040 if err == nil {
2041 putBufioWriter(w.w)
2042 w.w = nil
2044 return rwc, buf, err
2047 func (w *response) CloseNotify() <-chan bool {
2048 if w.handlerDone.isSet() {
2049 panic("net/http: CloseNotify called after ServeHTTP finished")
2051 return w.closeNotifyCh
2054 func registerOnHitEOF(rc io.ReadCloser, fn func()) {
2055 switch v := rc.(type) {
2056 case *expectContinueReader:
2057 registerOnHitEOF(v.readCloser, fn)
2058 case *body:
2059 v.registerOnHitEOF(fn)
2060 default:
2061 panic("unexpected type " + fmt.Sprintf("%T", rc))
2065 // requestBodyRemains reports whether future calls to Read
2066 // on rc might yield more data.
2067 func requestBodyRemains(rc io.ReadCloser) bool {
2068 if rc == NoBody {
2069 return false
2071 switch v := rc.(type) {
2072 case *expectContinueReader:
2073 return requestBodyRemains(v.readCloser)
2074 case *body:
2075 return v.bodyRemains()
2076 default:
2077 panic("unexpected type " + fmt.Sprintf("%T", rc))
2081 // The HandlerFunc type is an adapter to allow the use of
2082 // ordinary functions as HTTP handlers. If f is a function
2083 // with the appropriate signature, HandlerFunc(f) is a
2084 // Handler that calls f.
2085 type HandlerFunc func(ResponseWriter, *Request)
2087 // ServeHTTP calls f(w, r).
2088 func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
2089 f(w, r)
2092 // Helper handlers
2094 // Error replies to the request with the specified error message and HTTP code.
2095 // It does not otherwise end the request; the caller should ensure no further
2096 // writes are done to w.
2097 // The error message should be plain text.
2098 func Error(w ResponseWriter, error string, code int) {
2099 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
2100 w.Header().Set("X-Content-Type-Options", "nosniff")
2101 w.WriteHeader(code)
2102 fmt.Fprintln(w, error)
2105 // NotFound replies to the request with an HTTP 404 not found error.
2106 func NotFound(w ResponseWriter, r *Request) { Error(w, "404 page not found", StatusNotFound) }
2108 // NotFoundHandler returns a simple request handler
2109 // that replies to each request with a ``404 page not found'' reply.
2110 func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
2112 // StripPrefix returns a handler that serves HTTP requests by removing the
2113 // given prefix from the request URL's Path (and RawPath if set) and invoking
2114 // the handler h. StripPrefix handles a request for a path that doesn't begin
2115 // with prefix by replying with an HTTP 404 not found error. The prefix must
2116 // match exactly: if the prefix in the request contains escaped characters
2117 // the reply is also an HTTP 404 not found error.
2118 func StripPrefix(prefix string, h Handler) Handler {
2119 if prefix == "" {
2120 return h
2122 return HandlerFunc(func(w ResponseWriter, r *Request) {
2123 p := strings.TrimPrefix(r.URL.Path, prefix)
2124 rp := strings.TrimPrefix(r.URL.RawPath, prefix)
2125 if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) {
2126 r2 := new(Request)
2127 *r2 = *r
2128 r2.URL = new(url.URL)
2129 *r2.URL = *r.URL
2130 r2.URL.Path = p
2131 r2.URL.RawPath = rp
2132 h.ServeHTTP(w, r2)
2133 } else {
2134 NotFound(w, r)
2139 // Redirect replies to the request with a redirect to url,
2140 // which may be a path relative to the request path.
2142 // The provided code should be in the 3xx range and is usually
2143 // StatusMovedPermanently, StatusFound or StatusSeeOther.
2145 // If the Content-Type header has not been set, Redirect sets it
2146 // to "text/html; charset=utf-8" and writes a small HTML body.
2147 // Setting the Content-Type header to any value, including nil,
2148 // disables that behavior.
2149 func Redirect(w ResponseWriter, r *Request, url string, code int) {
2150 if u, err := urlpkg.Parse(url); err == nil {
2151 // If url was relative, make its path absolute by
2152 // combining with request path.
2153 // The client would probably do this for us,
2154 // but doing it ourselves is more reliable.
2155 // See RFC 7231, section 7.1.2
2156 if u.Scheme == "" && u.Host == "" {
2157 oldpath := r.URL.Path
2158 if oldpath == "" { // should not happen, but avoid a crash if it does
2159 oldpath = "/"
2162 // no leading http://server
2163 if url == "" || url[0] != '/' {
2164 // make relative path absolute
2165 olddir, _ := path.Split(oldpath)
2166 url = olddir + url
2169 var query string
2170 if i := strings.Index(url, "?"); i != -1 {
2171 url, query = url[:i], url[i:]
2174 // clean up but preserve trailing slash
2175 trailing := strings.HasSuffix(url, "/")
2176 url = path.Clean(url)
2177 if trailing && !strings.HasSuffix(url, "/") {
2178 url += "/"
2180 url += query
2184 h := w.Header()
2186 // RFC 7231 notes that a short HTML body is usually included in
2187 // the response because older user agents may not understand 301/307.
2188 // Do it only if the request didn't already have a Content-Type header.
2189 _, hadCT := h["Content-Type"]
2191 h.Set("Location", hexEscapeNonASCII(url))
2192 if !hadCT && (r.Method == "GET" || r.Method == "HEAD") {
2193 h.Set("Content-Type", "text/html; charset=utf-8")
2195 w.WriteHeader(code)
2197 // Shouldn't send the body for POST or HEAD; that leaves GET.
2198 if !hadCT && r.Method == "GET" {
2199 body := "<a href=\"" + htmlEscape(url) + "\">" + statusText[code] + "</a>.\n"
2200 fmt.Fprintln(w, body)
2204 var htmlReplacer = strings.NewReplacer(
2205 "&", "&amp;",
2206 "<", "&lt;",
2207 ">", "&gt;",
2208 // "&#34;" is shorter than "&quot;".
2209 `"`, "&#34;",
2210 // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
2211 "'", "&#39;",
2214 func htmlEscape(s string) string {
2215 return htmlReplacer.Replace(s)
2218 // Redirect to a fixed URL
2219 type redirectHandler struct {
2220 url string
2221 code int
2224 func (rh *redirectHandler) ServeHTTP(w ResponseWriter, r *Request) {
2225 Redirect(w, r, rh.url, rh.code)
2228 // RedirectHandler returns a request handler that redirects
2229 // each request it receives to the given url using the given
2230 // status code.
2232 // The provided code should be in the 3xx range and is usually
2233 // StatusMovedPermanently, StatusFound or StatusSeeOther.
2234 func RedirectHandler(url string, code int) Handler {
2235 return &redirectHandler{url, code}
2238 // ServeMux is an HTTP request multiplexer.
2239 // It matches the URL of each incoming request against a list of registered
2240 // patterns and calls the handler for the pattern that
2241 // most closely matches the URL.
2243 // Patterns name fixed, rooted paths, like "/favicon.ico",
2244 // or rooted subtrees, like "/images/" (note the trailing slash).
2245 // Longer patterns take precedence over shorter ones, so that
2246 // if there are handlers registered for both "/images/"
2247 // and "/images/thumbnails/", the latter handler will be
2248 // called for paths beginning "/images/thumbnails/" and the
2249 // former will receive requests for any other paths in the
2250 // "/images/" subtree.
2252 // Note that since a pattern ending in a slash names a rooted subtree,
2253 // the pattern "/" matches all paths not matched by other registered
2254 // patterns, not just the URL with Path == "/".
2256 // If a subtree has been registered and a request is received naming the
2257 // subtree root without its trailing slash, ServeMux redirects that
2258 // request to the subtree root (adding the trailing slash). This behavior can
2259 // be overridden with a separate registration for the path without
2260 // the trailing slash. For example, registering "/images/" causes ServeMux
2261 // to redirect a request for "/images" to "/images/", unless "/images" has
2262 // been registered separately.
2264 // Patterns may optionally begin with a host name, restricting matches to
2265 // URLs on that host only. Host-specific patterns take precedence over
2266 // general patterns, so that a handler might register for the two patterns
2267 // "/codesearch" and "codesearch.google.com/" without also taking over
2268 // requests for "http://www.google.com/".
2270 // ServeMux also takes care of sanitizing the URL request path and the Host
2271 // header, stripping the port number and redirecting any request containing . or
2272 // .. elements or repeated slashes to an equivalent, cleaner URL.
2273 type ServeMux struct {
2274 mu sync.RWMutex
2275 m map[string]muxEntry
2276 es []muxEntry // slice of entries sorted from longest to shortest.
2277 hosts bool // whether any patterns contain hostnames
2280 type muxEntry struct {
2281 h Handler
2282 pattern string
2285 // NewServeMux allocates and returns a new ServeMux.
2286 func NewServeMux() *ServeMux { return new(ServeMux) }
2288 // DefaultServeMux is the default ServeMux used by Serve.
2289 var DefaultServeMux = &defaultServeMux
2291 var defaultServeMux ServeMux
2293 // cleanPath returns the canonical path for p, eliminating . and .. elements.
2294 func cleanPath(p string) string {
2295 if p == "" {
2296 return "/"
2298 if p[0] != '/' {
2299 p = "/" + p
2301 np := path.Clean(p)
2302 // path.Clean removes trailing slash except for root;
2303 // put the trailing slash back if necessary.
2304 if p[len(p)-1] == '/' && np != "/" {
2305 // Fast path for common case of p being the string we want:
2306 if len(p) == len(np)+1 && strings.HasPrefix(p, np) {
2307 np = p
2308 } else {
2309 np += "/"
2312 return np
2315 // stripHostPort returns h without any trailing ":<port>".
2316 func stripHostPort(h string) string {
2317 // If no port on host, return unchanged
2318 if !strings.Contains(h, ":") {
2319 return h
2321 host, _, err := net.SplitHostPort(h)
2322 if err != nil {
2323 return h // on error, return unchanged
2325 return host
2328 // Find a handler on a handler map given a path string.
2329 // Most-specific (longest) pattern wins.
2330 func (mux *ServeMux) match(path string) (h Handler, pattern string) {
2331 // Check for exact match first.
2332 v, ok := mux.m[path]
2333 if ok {
2334 return v.h, v.pattern
2337 // Check for longest valid match. mux.es contains all patterns
2338 // that end in / sorted from longest to shortest.
2339 for _, e := range mux.es {
2340 if strings.HasPrefix(path, e.pattern) {
2341 return e.h, e.pattern
2344 return nil, ""
2347 // redirectToPathSlash determines if the given path needs appending "/" to it.
2348 // This occurs when a handler for path + "/" was already registered, but
2349 // not for path itself. If the path needs appending to, it creates a new
2350 // URL, setting the path to u.Path + "/" and returning true to indicate so.
2351 func (mux *ServeMux) redirectToPathSlash(host, path string, u *url.URL) (*url.URL, bool) {
2352 mux.mu.RLock()
2353 shouldRedirect := mux.shouldRedirectRLocked(host, path)
2354 mux.mu.RUnlock()
2355 if !shouldRedirect {
2356 return u, false
2358 path = path + "/"
2359 u = &url.URL{Path: path, RawQuery: u.RawQuery}
2360 return u, true
2363 // shouldRedirectRLocked reports whether the given path and host should be redirected to
2364 // path+"/". This should happen if a handler is registered for path+"/" but
2365 // not path -- see comments at ServeMux.
2366 func (mux *ServeMux) shouldRedirectRLocked(host, path string) bool {
2367 p := []string{path, host + path}
2369 for _, c := range p {
2370 if _, exist := mux.m[c]; exist {
2371 return false
2375 n := len(path)
2376 if n == 0 {
2377 return false
2379 for _, c := range p {
2380 if _, exist := mux.m[c+"/"]; exist {
2381 return path[n-1] != '/'
2385 return false
2388 // Handler returns the handler to use for the given request,
2389 // consulting r.Method, r.Host, and r.URL.Path. It always returns
2390 // a non-nil handler. If the path is not in its canonical form, the
2391 // handler will be an internally-generated handler that redirects
2392 // to the canonical path. If the host contains a port, it is ignored
2393 // when matching handlers.
2395 // The path and host are used unchanged for CONNECT requests.
2397 // Handler also returns the registered pattern that matches the
2398 // request or, in the case of internally-generated redirects,
2399 // the pattern that will match after following the redirect.
2401 // If there is no registered handler that applies to the request,
2402 // Handler returns a ``page not found'' handler and an empty pattern.
2403 func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
2405 // CONNECT requests are not canonicalized.
2406 if r.Method == "CONNECT" {
2407 // If r.URL.Path is /tree and its handler is not registered,
2408 // the /tree -> /tree/ redirect applies to CONNECT requests
2409 // but the path canonicalization does not.
2410 if u, ok := mux.redirectToPathSlash(r.URL.Host, r.URL.Path, r.URL); ok {
2411 return RedirectHandler(u.String(), StatusMovedPermanently), u.Path
2414 return mux.handler(r.Host, r.URL.Path)
2417 // All other requests have any port stripped and path cleaned
2418 // before passing to mux.handler.
2419 host := stripHostPort(r.Host)
2420 path := cleanPath(r.URL.Path)
2422 // If the given path is /tree and its handler is not registered,
2423 // redirect for /tree/.
2424 if u, ok := mux.redirectToPathSlash(host, path, r.URL); ok {
2425 return RedirectHandler(u.String(), StatusMovedPermanently), u.Path
2428 if path != r.URL.Path {
2429 _, pattern = mux.handler(host, path)
2430 u := &url.URL{Path: path, RawQuery: r.URL.RawQuery}
2431 return RedirectHandler(u.String(), StatusMovedPermanently), pattern
2434 return mux.handler(host, r.URL.Path)
2437 // handler is the main implementation of Handler.
2438 // The path is known to be in canonical form, except for CONNECT methods.
2439 func (mux *ServeMux) handler(host, path string) (h Handler, pattern string) {
2440 mux.mu.RLock()
2441 defer mux.mu.RUnlock()
2443 // Host-specific pattern takes precedence over generic ones
2444 if mux.hosts {
2445 h, pattern = mux.match(host + path)
2447 if h == nil {
2448 h, pattern = mux.match(path)
2450 if h == nil {
2451 h, pattern = NotFoundHandler(), ""
2453 return
2456 // ServeHTTP dispatches the request to the handler whose
2457 // pattern most closely matches the request URL.
2458 func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
2459 if r.RequestURI == "*" {
2460 if r.ProtoAtLeast(1, 1) {
2461 w.Header().Set("Connection", "close")
2463 w.WriteHeader(StatusBadRequest)
2464 return
2466 h, _ := mux.Handler(r)
2467 h.ServeHTTP(w, r)
2470 // Handle registers the handler for the given pattern.
2471 // If a handler already exists for pattern, Handle panics.
2472 func (mux *ServeMux) Handle(pattern string, handler Handler) {
2473 mux.mu.Lock()
2474 defer mux.mu.Unlock()
2476 if pattern == "" {
2477 panic("http: invalid pattern")
2479 if handler == nil {
2480 panic("http: nil handler")
2482 if _, exist := mux.m[pattern]; exist {
2483 panic("http: multiple registrations for " + pattern)
2486 if mux.m == nil {
2487 mux.m = make(map[string]muxEntry)
2489 e := muxEntry{h: handler, pattern: pattern}
2490 mux.m[pattern] = e
2491 if pattern[len(pattern)-1] == '/' {
2492 mux.es = appendSorted(mux.es, e)
2495 if pattern[0] != '/' {
2496 mux.hosts = true
2500 func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
2501 n := len(es)
2502 i := sort.Search(n, func(i int) bool {
2503 return len(es[i].pattern) < len(e.pattern)
2505 if i == n {
2506 return append(es, e)
2508 // we now know that i points at where we want to insert
2509 es = append(es, muxEntry{}) // try to grow the slice in place, any entry works.
2510 copy(es[i+1:], es[i:]) // Move shorter entries down
2511 es[i] = e
2512 return es
2515 // HandleFunc registers the handler function for the given pattern.
2516 func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
2517 if handler == nil {
2518 panic("http: nil handler")
2520 mux.Handle(pattern, HandlerFunc(handler))
2523 // Handle registers the handler for the given pattern
2524 // in the DefaultServeMux.
2525 // The documentation for ServeMux explains how patterns are matched.
2526 func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }
2528 // HandleFunc registers the handler function for the given pattern
2529 // in the DefaultServeMux.
2530 // The documentation for ServeMux explains how patterns are matched.
2531 func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
2532 DefaultServeMux.HandleFunc(pattern, handler)
2535 // Serve accepts incoming HTTP connections on the listener l,
2536 // creating a new service goroutine for each. The service goroutines
2537 // read requests and then call handler to reply to them.
2539 // The handler is typically nil, in which case the DefaultServeMux is used.
2541 // HTTP/2 support is only enabled if the Listener returns *tls.Conn
2542 // connections and they were configured with "h2" in the TLS
2543 // Config.NextProtos.
2545 // Serve always returns a non-nil error.
2546 func Serve(l net.Listener, handler Handler) error {
2547 srv := &Server{Handler: handler}
2548 return srv.Serve(l)
2551 // ServeTLS accepts incoming HTTPS connections on the listener l,
2552 // creating a new service goroutine for each. The service goroutines
2553 // read requests and then call handler to reply to them.
2555 // The handler is typically nil, in which case the DefaultServeMux is used.
2557 // Additionally, files containing a certificate and matching private key
2558 // for the server must be provided. If the certificate is signed by a
2559 // certificate authority, the certFile should be the concatenation
2560 // of the server's certificate, any intermediates, and the CA's certificate.
2562 // ServeTLS always returns a non-nil error.
2563 func ServeTLS(l net.Listener, handler Handler, certFile, keyFile string) error {
2564 srv := &Server{Handler: handler}
2565 return srv.ServeTLS(l, certFile, keyFile)
2568 // A Server defines parameters for running an HTTP server.
2569 // The zero value for Server is a valid configuration.
2570 type Server struct {
2571 // Addr optionally specifies the TCP address for the server to listen on,
2572 // in the form "host:port". If empty, ":http" (port 80) is used.
2573 // The service names are defined in RFC 6335 and assigned by IANA.
2574 // See net.Dial for details of the address format.
2575 Addr string
2577 Handler Handler // handler to invoke, http.DefaultServeMux if nil
2579 // TLSConfig optionally provides a TLS configuration for use
2580 // by ServeTLS and ListenAndServeTLS. Note that this value is
2581 // cloned by ServeTLS and ListenAndServeTLS, so it's not
2582 // possible to modify the configuration with methods like
2583 // tls.Config.SetSessionTicketKeys. To use
2584 // SetSessionTicketKeys, use Server.Serve with a TLS Listener
2585 // instead.
2586 TLSConfig *tls.Config
2588 // ReadTimeout is the maximum duration for reading the entire
2589 // request, including the body. A zero or negative value means
2590 // there will be no timeout.
2592 // Because ReadTimeout does not let Handlers make per-request
2593 // decisions on each request body's acceptable deadline or
2594 // upload rate, most users will prefer to use
2595 // ReadHeaderTimeout. It is valid to use them both.
2596 ReadTimeout time.Duration
2598 // ReadHeaderTimeout is the amount of time allowed to read
2599 // request headers. The connection's read deadline is reset
2600 // after reading the headers and the Handler can decide what
2601 // is considered too slow for the body. If ReadHeaderTimeout
2602 // is zero, the value of ReadTimeout is used. If both are
2603 // zero, there is no timeout.
2604 ReadHeaderTimeout time.Duration
2606 // WriteTimeout is the maximum duration before timing out
2607 // writes of the response. It is reset whenever a new
2608 // request's header is read. Like ReadTimeout, it does not
2609 // let Handlers make decisions on a per-request basis.
2610 // A zero or negative value means there will be no timeout.
2611 WriteTimeout time.Duration
2613 // IdleTimeout is the maximum amount of time to wait for the
2614 // next request when keep-alives are enabled. If IdleTimeout
2615 // is zero, the value of ReadTimeout is used. If both are
2616 // zero, there is no timeout.
2617 IdleTimeout time.Duration
2619 // MaxHeaderBytes controls the maximum number of bytes the
2620 // server will read parsing the request header's keys and
2621 // values, including the request line. It does not limit the
2622 // size of the request body.
2623 // If zero, DefaultMaxHeaderBytes is used.
2624 MaxHeaderBytes int
2626 // TLSNextProto optionally specifies a function to take over
2627 // ownership of the provided TLS connection when an ALPN
2628 // protocol upgrade has occurred. The map key is the protocol
2629 // name negotiated. The Handler argument should be used to
2630 // handle HTTP requests and will initialize the Request's TLS
2631 // and RemoteAddr if not already set. The connection is
2632 // automatically closed when the function returns.
2633 // If TLSNextProto is not nil, HTTP/2 support is not enabled
2634 // automatically.
2635 TLSNextProto map[string]func(*Server, *tls.Conn, Handler)
2637 // ConnState specifies an optional callback function that is
2638 // called when a client connection changes state. See the
2639 // ConnState type and associated constants for details.
2640 ConnState func(net.Conn, ConnState)
2642 // ErrorLog specifies an optional logger for errors accepting
2643 // connections, unexpected behavior from handlers, and
2644 // underlying FileSystem errors.
2645 // If nil, logging is done via the log package's standard logger.
2646 ErrorLog *log.Logger
2648 // BaseContext optionally specifies a function that returns
2649 // the base context for incoming requests on this server.
2650 // The provided Listener is the specific Listener that's
2651 // about to start accepting requests.
2652 // If BaseContext is nil, the default is context.Background().
2653 // If non-nil, it must return a non-nil context.
2654 BaseContext func(net.Listener) context.Context
2656 // ConnContext optionally specifies a function that modifies
2657 // the context used for a new connection c. The provided ctx
2658 // is derived from the base context and has a ServerContextKey
2659 // value.
2660 ConnContext func(ctx context.Context, c net.Conn) context.Context
2662 inShutdown atomicBool // true when server is in shutdown
2664 disableKeepAlives int32 // accessed atomically.
2665 nextProtoOnce sync.Once // guards setupHTTP2_* init
2666 nextProtoErr error // result of http2.ConfigureServer if used
2668 mu sync.Mutex
2669 listeners map[*net.Listener]struct{}
2670 activeConn map[*conn]struct{}
2671 doneChan chan struct{}
2672 onShutdown []func()
2675 func (s *Server) getDoneChan() <-chan struct{} {
2676 s.mu.Lock()
2677 defer s.mu.Unlock()
2678 return s.getDoneChanLocked()
2681 func (s *Server) getDoneChanLocked() chan struct{} {
2682 if s.doneChan == nil {
2683 s.doneChan = make(chan struct{})
2685 return s.doneChan
2688 func (s *Server) closeDoneChanLocked() {
2689 ch := s.getDoneChanLocked()
2690 select {
2691 case <-ch:
2692 // Already closed. Don't close again.
2693 default:
2694 // Safe to close here. We're the only closer, guarded
2695 // by s.mu.
2696 close(ch)
2700 // Close immediately closes all active net.Listeners and any
2701 // connections in state StateNew, StateActive, or StateIdle. For a
2702 // graceful shutdown, use Shutdown.
2704 // Close does not attempt to close (and does not even know about)
2705 // any hijacked connections, such as WebSockets.
2707 // Close returns any error returned from closing the Server's
2708 // underlying Listener(s).
2709 func (srv *Server) Close() error {
2710 srv.inShutdown.setTrue()
2711 srv.mu.Lock()
2712 defer srv.mu.Unlock()
2713 srv.closeDoneChanLocked()
2714 err := srv.closeListenersLocked()
2715 for c := range srv.activeConn {
2716 c.rwc.Close()
2717 delete(srv.activeConn, c)
2719 return err
2722 // shutdownPollIntervalMax is the max polling interval when checking
2723 // quiescence during Server.Shutdown. Polling starts with a small
2724 // interval and backs off to the max.
2725 // Ideally we could find a solution that doesn't involve polling,
2726 // but which also doesn't have a high runtime cost (and doesn't
2727 // involve any contentious mutexes), but that is left as an
2728 // exercise for the reader.
2729 const shutdownPollIntervalMax = 500 * time.Millisecond
2731 // Shutdown gracefully shuts down the server without interrupting any
2732 // active connections. Shutdown works by first closing all open
2733 // listeners, then closing all idle connections, and then waiting
2734 // indefinitely for connections to return to idle and then shut down.
2735 // If the provided context expires before the shutdown is complete,
2736 // Shutdown returns the context's error, otherwise it returns any
2737 // error returned from closing the Server's underlying Listener(s).
2739 // When Shutdown is called, Serve, ListenAndServe, and
2740 // ListenAndServeTLS immediately return ErrServerClosed. Make sure the
2741 // program doesn't exit and waits instead for Shutdown to return.
2743 // Shutdown does not attempt to close nor wait for hijacked
2744 // connections such as WebSockets. The caller of Shutdown should
2745 // separately notify such long-lived connections of shutdown and wait
2746 // for them to close, if desired. See RegisterOnShutdown for a way to
2747 // register shutdown notification functions.
2749 // Once Shutdown has been called on a server, it may not be reused;
2750 // future calls to methods such as Serve will return ErrServerClosed.
2751 func (srv *Server) Shutdown(ctx context.Context) error {
2752 srv.inShutdown.setTrue()
2754 srv.mu.Lock()
2755 lnerr := srv.closeListenersLocked()
2756 srv.closeDoneChanLocked()
2757 for _, f := range srv.onShutdown {
2758 go f()
2760 srv.mu.Unlock()
2762 pollIntervalBase := time.Millisecond
2763 nextPollInterval := func() time.Duration {
2764 // Add 10% jitter.
2765 interval := pollIntervalBase + time.Duration(rand.Intn(int(pollIntervalBase/10)))
2766 // Double and clamp for next time.
2767 pollIntervalBase *= 2
2768 if pollIntervalBase > shutdownPollIntervalMax {
2769 pollIntervalBase = shutdownPollIntervalMax
2771 return interval
2774 timer := time.NewTimer(nextPollInterval())
2775 defer timer.Stop()
2776 for {
2777 if srv.closeIdleConns() && srv.numListeners() == 0 {
2778 return lnerr
2780 select {
2781 case <-ctx.Done():
2782 return ctx.Err()
2783 case <-timer.C:
2784 timer.Reset(nextPollInterval())
2789 // RegisterOnShutdown registers a function to call on Shutdown.
2790 // This can be used to gracefully shutdown connections that have
2791 // undergone ALPN protocol upgrade or that have been hijacked.
2792 // This function should start protocol-specific graceful shutdown,
2793 // but should not wait for shutdown to complete.
2794 func (srv *Server) RegisterOnShutdown(f func()) {
2795 srv.mu.Lock()
2796 srv.onShutdown = append(srv.onShutdown, f)
2797 srv.mu.Unlock()
2800 func (s *Server) numListeners() int {
2801 s.mu.Lock()
2802 defer s.mu.Unlock()
2803 return len(s.listeners)
2806 // closeIdleConns closes all idle connections and reports whether the
2807 // server is quiescent.
2808 func (s *Server) closeIdleConns() bool {
2809 s.mu.Lock()
2810 defer s.mu.Unlock()
2811 quiescent := true
2812 for c := range s.activeConn {
2813 st, unixSec := c.getState()
2814 // Issue 22682: treat StateNew connections as if
2815 // they're idle if we haven't read the first request's
2816 // header in over 5 seconds.
2817 if st == StateNew && unixSec < time.Now().Unix()-5 {
2818 st = StateIdle
2820 if st != StateIdle || unixSec == 0 {
2821 // Assume unixSec == 0 means it's a very new
2822 // connection, without state set yet.
2823 quiescent = false
2824 continue
2826 c.rwc.Close()
2827 delete(s.activeConn, c)
2829 return quiescent
2832 func (s *Server) closeListenersLocked() error {
2833 var err error
2834 for ln := range s.listeners {
2835 if cerr := (*ln).Close(); cerr != nil && err == nil {
2836 err = cerr
2839 return err
2842 // A ConnState represents the state of a client connection to a server.
2843 // It's used by the optional Server.ConnState hook.
2844 type ConnState int
2846 const (
2847 // StateNew represents a new connection that is expected to
2848 // send a request immediately. Connections begin at this
2849 // state and then transition to either StateActive or
2850 // StateClosed.
2851 StateNew ConnState = iota
2853 // StateActive represents a connection that has read 1 or more
2854 // bytes of a request. The Server.ConnState hook for
2855 // StateActive fires before the request has entered a handler
2856 // and doesn't fire again until the request has been
2857 // handled. After the request is handled, the state
2858 // transitions to StateClosed, StateHijacked, or StateIdle.
2859 // For HTTP/2, StateActive fires on the transition from zero
2860 // to one active request, and only transitions away once all
2861 // active requests are complete. That means that ConnState
2862 // cannot be used to do per-request work; ConnState only notes
2863 // the overall state of the connection.
2864 StateActive
2866 // StateIdle represents a connection that has finished
2867 // handling a request and is in the keep-alive state, waiting
2868 // for a new request. Connections transition from StateIdle
2869 // to either StateActive or StateClosed.
2870 StateIdle
2872 // StateHijacked represents a hijacked connection.
2873 // This is a terminal state. It does not transition to StateClosed.
2874 StateHijacked
2876 // StateClosed represents a closed connection.
2877 // This is a terminal state. Hijacked connections do not
2878 // transition to StateClosed.
2879 StateClosed
2882 var stateName = map[ConnState]string{
2883 StateNew: "new",
2884 StateActive: "active",
2885 StateIdle: "idle",
2886 StateHijacked: "hijacked",
2887 StateClosed: "closed",
2890 func (c ConnState) String() string {
2891 return stateName[c]
2894 // serverHandler delegates to either the server's Handler or
2895 // DefaultServeMux and also handles "OPTIONS *" requests.
2896 type serverHandler struct {
2897 srv *Server
2900 func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
2901 handler := sh.srv.Handler
2902 if handler == nil {
2903 handler = DefaultServeMux
2905 if req.RequestURI == "*" && req.Method == "OPTIONS" {
2906 handler = globalOptionsHandler{}
2909 if req.URL != nil && strings.Contains(req.URL.RawQuery, ";") {
2910 var allowQuerySemicolonsInUse int32
2911 req = req.WithContext(context.WithValue(req.Context(), silenceSemWarnContextKey, func() {
2912 atomic.StoreInt32(&allowQuerySemicolonsInUse, 1)
2914 defer func() {
2915 if atomic.LoadInt32(&allowQuerySemicolonsInUse) == 0 {
2916 sh.srv.logf("http: URL query contains semicolon, which is no longer a supported separator; parts of the query may be stripped when parsed; see golang.org/issue/25192")
2921 handler.ServeHTTP(rw, req)
2924 var silenceSemWarnContextKey = &contextKey{"silence-semicolons"}
2926 // AllowQuerySemicolons returns a handler that serves requests by converting any
2927 // unescaped semicolons in the URL query to ampersands, and invoking the handler h.
2929 // This restores the pre-Go 1.17 behavior of splitting query parameters on both
2930 // semicolons and ampersands. (See golang.org/issue/25192). Note that this
2931 // behavior doesn't match that of many proxies, and the mismatch can lead to
2932 // security issues.
2934 // AllowQuerySemicolons should be invoked before Request.ParseForm is called.
2935 func AllowQuerySemicolons(h Handler) Handler {
2936 return HandlerFunc(func(w ResponseWriter, r *Request) {
2937 if silenceSemicolonsWarning, ok := r.Context().Value(silenceSemWarnContextKey).(func()); ok {
2938 silenceSemicolonsWarning()
2940 if strings.Contains(r.URL.RawQuery, ";") {
2941 r2 := new(Request)
2942 *r2 = *r
2943 r2.URL = new(url.URL)
2944 *r2.URL = *r.URL
2945 r2.URL.RawQuery = strings.ReplaceAll(r.URL.RawQuery, ";", "&")
2946 h.ServeHTTP(w, r2)
2947 } else {
2948 h.ServeHTTP(w, r)
2953 // ListenAndServe listens on the TCP network address srv.Addr and then
2954 // calls Serve to handle requests on incoming connections.
2955 // Accepted connections are configured to enable TCP keep-alives.
2957 // If srv.Addr is blank, ":http" is used.
2959 // ListenAndServe always returns a non-nil error. After Shutdown or Close,
2960 // the returned error is ErrServerClosed.
2961 func (srv *Server) ListenAndServe() error {
2962 if srv.shuttingDown() {
2963 return ErrServerClosed
2965 addr := srv.Addr
2966 if addr == "" {
2967 addr = ":http"
2969 ln, err := net.Listen("tcp", addr)
2970 if err != nil {
2971 return err
2973 return srv.Serve(ln)
2976 var testHookServerServe func(*Server, net.Listener) // used if non-nil
2978 // shouldDoServeHTTP2 reports whether Server.Serve should configure
2979 // automatic HTTP/2. (which sets up the srv.TLSNextProto map)
2980 func (srv *Server) shouldConfigureHTTP2ForServe() bool {
2981 if srv.TLSConfig == nil {
2982 // Compatibility with Go 1.6:
2983 // If there's no TLSConfig, it's possible that the user just
2984 // didn't set it on the http.Server, but did pass it to
2985 // tls.NewListener and passed that listener to Serve.
2986 // So we should configure HTTP/2 (to set up srv.TLSNextProto)
2987 // in case the listener returns an "h2" *tls.Conn.
2988 return true
2990 // The user specified a TLSConfig on their http.Server.
2991 // In this, case, only configure HTTP/2 if their tls.Config
2992 // explicitly mentions "h2". Otherwise http2.ConfigureServer
2993 // would modify the tls.Config to add it, but they probably already
2994 // passed this tls.Config to tls.NewListener. And if they did,
2995 // it's too late anyway to fix it. It would only be potentially racy.
2996 // See Issue 15908.
2997 return strSliceContains(srv.TLSConfig.NextProtos, http2NextProtoTLS)
3000 // ErrServerClosed is returned by the Server's Serve, ServeTLS, ListenAndServe,
3001 // and ListenAndServeTLS methods after a call to Shutdown or Close.
3002 var ErrServerClosed = errors.New("http: Server closed")
3004 // Serve accepts incoming connections on the Listener l, creating a
3005 // new service goroutine for each. The service goroutines read requests and
3006 // then call srv.Handler to reply to them.
3008 // HTTP/2 support is only enabled if the Listener returns *tls.Conn
3009 // connections and they were configured with "h2" in the TLS
3010 // Config.NextProtos.
3012 // Serve always returns a non-nil error and closes l.
3013 // After Shutdown or Close, the returned error is ErrServerClosed.
3014 func (srv *Server) Serve(l net.Listener) error {
3015 if fn := testHookServerServe; fn != nil {
3016 fn(srv, l) // call hook with unwrapped listener
3019 origListener := l
3020 l = &onceCloseListener{Listener: l}
3021 defer l.Close()
3023 if err := srv.setupHTTP2_Serve(); err != nil {
3024 return err
3027 if !srv.trackListener(&l, true) {
3028 return ErrServerClosed
3030 defer srv.trackListener(&l, false)
3032 baseCtx := context.Background()
3033 if srv.BaseContext != nil {
3034 baseCtx = srv.BaseContext(origListener)
3035 if baseCtx == nil {
3036 panic("BaseContext returned a nil context")
3040 var tempDelay time.Duration // how long to sleep on accept failure
3042 ctx := context.WithValue(baseCtx, ServerContextKey, srv)
3043 for {
3044 rw, err := l.Accept()
3045 if err != nil {
3046 select {
3047 case <-srv.getDoneChan():
3048 return ErrServerClosed
3049 default:
3051 if ne, ok := err.(net.Error); ok && ne.Temporary() {
3052 if tempDelay == 0 {
3053 tempDelay = 5 * time.Millisecond
3054 } else {
3055 tempDelay *= 2
3057 if max := 1 * time.Second; tempDelay > max {
3058 tempDelay = max
3060 srv.logf("http: Accept error: %v; retrying in %v", err, tempDelay)
3061 time.Sleep(tempDelay)
3062 continue
3064 return err
3066 connCtx := ctx
3067 if cc := srv.ConnContext; cc != nil {
3068 connCtx = cc(connCtx, rw)
3069 if connCtx == nil {
3070 panic("ConnContext returned nil")
3073 tempDelay = 0
3074 c := srv.newConn(rw)
3075 c.setState(c.rwc, StateNew, runHooks) // before Serve can return
3076 go c.serve(connCtx)
3080 // ServeTLS accepts incoming connections on the Listener l, creating a
3081 // new service goroutine for each. The service goroutines perform TLS
3082 // setup and then read requests, calling srv.Handler to reply to them.
3084 // Files containing a certificate and matching private key for the
3085 // server must be provided if neither the Server's
3086 // TLSConfig.Certificates nor TLSConfig.GetCertificate are populated.
3087 // If the certificate is signed by a certificate authority, the
3088 // certFile should be the concatenation of the server's certificate,
3089 // any intermediates, and the CA's certificate.
3091 // ServeTLS always returns a non-nil error. After Shutdown or Close, the
3092 // returned error is ErrServerClosed.
3093 func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error {
3094 // Setup HTTP/2 before srv.Serve, to initialize srv.TLSConfig
3095 // before we clone it and create the TLS Listener.
3096 if err := srv.setupHTTP2_ServeTLS(); err != nil {
3097 return err
3100 config := cloneTLSConfig(srv.TLSConfig)
3101 if !strSliceContains(config.NextProtos, "http/1.1") {
3102 config.NextProtos = append(config.NextProtos, "http/1.1")
3105 configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil
3106 if !configHasCert || certFile != "" || keyFile != "" {
3107 var err error
3108 config.Certificates = make([]tls.Certificate, 1)
3109 config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
3110 if err != nil {
3111 return err
3115 tlsListener := tls.NewListener(l, config)
3116 return srv.Serve(tlsListener)
3119 // trackListener adds or removes a net.Listener to the set of tracked
3120 // listeners.
3122 // We store a pointer to interface in the map set, in case the
3123 // net.Listener is not comparable. This is safe because we only call
3124 // trackListener via Serve and can track+defer untrack the same
3125 // pointer to local variable there. We never need to compare a
3126 // Listener from another caller.
3128 // It reports whether the server is still up (not Shutdown or Closed).
3129 func (s *Server) trackListener(ln *net.Listener, add bool) bool {
3130 s.mu.Lock()
3131 defer s.mu.Unlock()
3132 if s.listeners == nil {
3133 s.listeners = make(map[*net.Listener]struct{})
3135 if add {
3136 if s.shuttingDown() {
3137 return false
3139 s.listeners[ln] = struct{}{}
3140 } else {
3141 delete(s.listeners, ln)
3143 return true
3146 func (s *Server) trackConn(c *conn, add bool) {
3147 s.mu.Lock()
3148 defer s.mu.Unlock()
3149 if s.activeConn == nil {
3150 s.activeConn = make(map[*conn]struct{})
3152 if add {
3153 s.activeConn[c] = struct{}{}
3154 } else {
3155 delete(s.activeConn, c)
3159 func (s *Server) idleTimeout() time.Duration {
3160 if s.IdleTimeout != 0 {
3161 return s.IdleTimeout
3163 return s.ReadTimeout
3166 func (s *Server) readHeaderTimeout() time.Duration {
3167 if s.ReadHeaderTimeout != 0 {
3168 return s.ReadHeaderTimeout
3170 return s.ReadTimeout
3173 func (s *Server) doKeepAlives() bool {
3174 return atomic.LoadInt32(&s.disableKeepAlives) == 0 && !s.shuttingDown()
3177 func (s *Server) shuttingDown() bool {
3178 return s.inShutdown.isSet()
3181 // SetKeepAlivesEnabled controls whether HTTP keep-alives are enabled.
3182 // By default, keep-alives are always enabled. Only very
3183 // resource-constrained environments or servers in the process of
3184 // shutting down should disable them.
3185 func (srv *Server) SetKeepAlivesEnabled(v bool) {
3186 if v {
3187 atomic.StoreInt32(&srv.disableKeepAlives, 0)
3188 return
3190 atomic.StoreInt32(&srv.disableKeepAlives, 1)
3192 // Close idle HTTP/1 conns:
3193 srv.closeIdleConns()
3195 // TODO: Issue 26303: close HTTP/2 conns as soon as they become idle.
3198 func (s *Server) logf(format string, args ...any) {
3199 if s.ErrorLog != nil {
3200 s.ErrorLog.Printf(format, args...)
3201 } else {
3202 log.Printf(format, args...)
3206 // logf prints to the ErrorLog of the *Server associated with request r
3207 // via ServerContextKey. If there's no associated server, or if ErrorLog
3208 // is nil, logging is done via the log package's standard logger.
3209 func logf(r *Request, format string, args ...any) {
3210 s, _ := r.Context().Value(ServerContextKey).(*Server)
3211 if s != nil && s.ErrorLog != nil {
3212 s.ErrorLog.Printf(format, args...)
3213 } else {
3214 log.Printf(format, args...)
3218 // ListenAndServe listens on the TCP network address addr and then calls
3219 // Serve with handler to handle requests on incoming connections.
3220 // Accepted connections are configured to enable TCP keep-alives.
3222 // The handler is typically nil, in which case the DefaultServeMux is used.
3224 // ListenAndServe always returns a non-nil error.
3225 func ListenAndServe(addr string, handler Handler) error {
3226 server := &Server{Addr: addr, Handler: handler}
3227 return server.ListenAndServe()
3230 // ListenAndServeTLS acts identically to ListenAndServe, except that it
3231 // expects HTTPS connections. Additionally, files containing a certificate and
3232 // matching private key for the server must be provided. If the certificate
3233 // is signed by a certificate authority, the certFile should be the concatenation
3234 // of the server's certificate, any intermediates, and the CA's certificate.
3235 func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error {
3236 server := &Server{Addr: addr, Handler: handler}
3237 return server.ListenAndServeTLS(certFile, keyFile)
3240 // ListenAndServeTLS listens on the TCP network address srv.Addr and
3241 // then calls ServeTLS to handle requests on incoming TLS connections.
3242 // Accepted connections are configured to enable TCP keep-alives.
3244 // Filenames containing a certificate and matching private key for the
3245 // server must be provided if neither the Server's TLSConfig.Certificates
3246 // nor TLSConfig.GetCertificate are populated. If the certificate is
3247 // signed by a certificate authority, the certFile should be the
3248 // concatenation of the server's certificate, any intermediates, and
3249 // the CA's certificate.
3251 // If srv.Addr is blank, ":https" is used.
3253 // ListenAndServeTLS always returns a non-nil error. After Shutdown or
3254 // Close, the returned error is ErrServerClosed.
3255 func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
3256 if srv.shuttingDown() {
3257 return ErrServerClosed
3259 addr := srv.Addr
3260 if addr == "" {
3261 addr = ":https"
3264 ln, err := net.Listen("tcp", addr)
3265 if err != nil {
3266 return err
3269 defer ln.Close()
3271 return srv.ServeTLS(ln, certFile, keyFile)
3274 // setupHTTP2_ServeTLS conditionally configures HTTP/2 on
3275 // srv and reports whether there was an error setting it up. If it is
3276 // not configured for policy reasons, nil is returned.
3277 func (srv *Server) setupHTTP2_ServeTLS() error {
3278 srv.nextProtoOnce.Do(srv.onceSetNextProtoDefaults)
3279 return srv.nextProtoErr
3282 // setupHTTP2_Serve is called from (*Server).Serve and conditionally
3283 // configures HTTP/2 on srv using a more conservative policy than
3284 // setupHTTP2_ServeTLS because Serve is called after tls.Listen,
3285 // and may be called concurrently. See shouldConfigureHTTP2ForServe.
3287 // The tests named TestTransportAutomaticHTTP2* and
3288 // TestConcurrentServerServe in server_test.go demonstrate some
3289 // of the supported use cases and motivations.
3290 func (srv *Server) setupHTTP2_Serve() error {
3291 srv.nextProtoOnce.Do(srv.onceSetNextProtoDefaults_Serve)
3292 return srv.nextProtoErr
3295 func (srv *Server) onceSetNextProtoDefaults_Serve() {
3296 if srv.shouldConfigureHTTP2ForServe() {
3297 srv.onceSetNextProtoDefaults()
3301 // onceSetNextProtoDefaults configures HTTP/2, if the user hasn't
3302 // configured otherwise. (by setting srv.TLSNextProto non-nil)
3303 // It must only be called via srv.nextProtoOnce (use srv.setupHTTP2_*).
3304 func (srv *Server) onceSetNextProtoDefaults() {
3305 if omitBundledHTTP2 || godebug.Get("http2server") == "0" {
3306 return
3308 // Enable HTTP/2 by default if the user hasn't otherwise
3309 // configured their TLSNextProto map.
3310 if srv.TLSNextProto == nil {
3311 conf := &http2Server{
3312 NewWriteScheduler: func() http2WriteScheduler { return http2NewPriorityWriteScheduler(nil) },
3314 srv.nextProtoErr = http2ConfigureServer(srv, conf)
3318 // TimeoutHandler returns a Handler that runs h with the given time limit.
3320 // The new Handler calls h.ServeHTTP to handle each request, but if a
3321 // call runs for longer than its time limit, the handler responds with
3322 // a 503 Service Unavailable error and the given message in its body.
3323 // (If msg is empty, a suitable default message will be sent.)
3324 // After such a timeout, writes by h to its ResponseWriter will return
3325 // ErrHandlerTimeout.
3327 // TimeoutHandler supports the Pusher interface but does not support
3328 // the Hijacker or Flusher interfaces.
3329 func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler {
3330 return &timeoutHandler{
3331 handler: h,
3332 body: msg,
3333 dt: dt,
3337 // ErrHandlerTimeout is returned on ResponseWriter Write calls
3338 // in handlers which have timed out.
3339 var ErrHandlerTimeout = errors.New("http: Handler timeout")
3341 type timeoutHandler struct {
3342 handler Handler
3343 body string
3344 dt time.Duration
3346 // When set, no context will be created and this context will
3347 // be used instead.
3348 testContext context.Context
3351 func (h *timeoutHandler) errorBody() string {
3352 if h.body != "" {
3353 return h.body
3355 return "<html><head><title>Timeout</title></head><body><h1>Timeout</h1></body></html>"
3358 func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) {
3359 ctx := h.testContext
3360 if ctx == nil {
3361 var cancelCtx context.CancelFunc
3362 ctx, cancelCtx = context.WithTimeout(r.Context(), h.dt)
3363 defer cancelCtx()
3365 r = r.WithContext(ctx)
3366 done := make(chan struct{})
3367 tw := &timeoutWriter{
3368 w: w,
3369 h: make(Header),
3370 req: r,
3372 panicChan := make(chan any, 1)
3373 go func() {
3374 defer func() {
3375 if p := recover(); p != nil {
3376 panicChan <- p
3379 h.handler.ServeHTTP(tw, r)
3380 close(done)
3382 select {
3383 case p := <-panicChan:
3384 panic(p)
3385 case <-done:
3386 tw.mu.Lock()
3387 defer tw.mu.Unlock()
3388 dst := w.Header()
3389 for k, vv := range tw.h {
3390 dst[k] = vv
3392 if !tw.wroteHeader {
3393 tw.code = StatusOK
3395 w.WriteHeader(tw.code)
3396 w.Write(tw.wbuf.Bytes())
3397 case <-ctx.Done():
3398 tw.mu.Lock()
3399 defer tw.mu.Unlock()
3400 switch err := ctx.Err(); err {
3401 case context.DeadlineExceeded:
3402 w.WriteHeader(StatusServiceUnavailable)
3403 io.WriteString(w, h.errorBody())
3404 tw.err = ErrHandlerTimeout
3405 default:
3406 w.WriteHeader(StatusServiceUnavailable)
3407 tw.err = err
3412 type timeoutWriter struct {
3413 w ResponseWriter
3414 h Header
3415 wbuf bytes.Buffer
3416 req *Request
3418 mu sync.Mutex
3419 err error
3420 wroteHeader bool
3421 code int
3424 var _ Pusher = (*timeoutWriter)(nil)
3426 // Push implements the Pusher interface.
3427 func (tw *timeoutWriter) Push(target string, opts *PushOptions) error {
3428 if pusher, ok := tw.w.(Pusher); ok {
3429 return pusher.Push(target, opts)
3431 return ErrNotSupported
3434 func (tw *timeoutWriter) Header() Header { return tw.h }
3436 func (tw *timeoutWriter) Write(p []byte) (int, error) {
3437 tw.mu.Lock()
3438 defer tw.mu.Unlock()
3439 if tw.err != nil {
3440 return 0, tw.err
3442 if !tw.wroteHeader {
3443 tw.writeHeaderLocked(StatusOK)
3445 return tw.wbuf.Write(p)
3448 func (tw *timeoutWriter) writeHeaderLocked(code int) {
3449 checkWriteHeaderCode(code)
3451 switch {
3452 case tw.err != nil:
3453 return
3454 case tw.wroteHeader:
3455 if tw.req != nil {
3456 caller := relevantCaller()
3457 logf(tw.req, "http: superfluous response.WriteHeader call from %s (%s:%d)", caller.Function, path.Base(caller.File), caller.Line)
3459 default:
3460 tw.wroteHeader = true
3461 tw.code = code
3465 func (tw *timeoutWriter) WriteHeader(code int) {
3466 tw.mu.Lock()
3467 defer tw.mu.Unlock()
3468 tw.writeHeaderLocked(code)
3471 // onceCloseListener wraps a net.Listener, protecting it from
3472 // multiple Close calls.
3473 type onceCloseListener struct {
3474 net.Listener
3475 once sync.Once
3476 closeErr error
3479 func (oc *onceCloseListener) Close() error {
3480 oc.once.Do(oc.close)
3481 return oc.closeErr
3484 func (oc *onceCloseListener) close() { oc.closeErr = oc.Listener.Close() }
3486 // globalOptionsHandler responds to "OPTIONS *" requests.
3487 type globalOptionsHandler struct{}
3489 func (globalOptionsHandler) ServeHTTP(w ResponseWriter, r *Request) {
3490 w.Header().Set("Content-Length", "0")
3491 if r.ContentLength != 0 {
3492 // Read up to 4KB of OPTIONS body (as mentioned in the
3493 // spec as being reserved for future use), but anything
3494 // over that is considered a waste of server resources
3495 // (or an attack) and we abort and close the connection,
3496 // courtesy of MaxBytesReader's EOF behavior.
3497 mb := MaxBytesReader(w, r.Body, 4<<10)
3498 io.Copy(io.Discard, mb)
3502 // initALPNRequest is an HTTP handler that initializes certain
3503 // uninitialized fields in its *Request. Such partially-initialized
3504 // Requests come from ALPN protocol handlers.
3505 type initALPNRequest struct {
3506 ctx context.Context
3507 c *tls.Conn
3508 h serverHandler
3511 // BaseContext is an exported but unadvertised http.Handler method
3512 // recognized by x/net/http2 to pass down a context; the TLSNextProto
3513 // API predates context support so we shoehorn through the only
3514 // interface we have available.
3515 func (h initALPNRequest) BaseContext() context.Context { return h.ctx }
3517 func (h initALPNRequest) ServeHTTP(rw ResponseWriter, req *Request) {
3518 if req.TLS == nil {
3519 req.TLS = &tls.ConnectionState{}
3520 *req.TLS = h.c.ConnectionState()
3522 if req.Body == nil {
3523 req.Body = NoBody
3525 if req.RemoteAddr == "" {
3526 req.RemoteAddr = h.c.RemoteAddr().String()
3528 h.h.ServeHTTP(rw, req)
3531 // loggingConn is used for debugging.
3532 type loggingConn struct {
3533 name string
3534 net.Conn
3537 var (
3538 uniqNameMu sync.Mutex
3539 uniqNameNext = make(map[string]int)
3542 func newLoggingConn(baseName string, c net.Conn) net.Conn {
3543 uniqNameMu.Lock()
3544 defer uniqNameMu.Unlock()
3545 uniqNameNext[baseName]++
3546 return &loggingConn{
3547 name: fmt.Sprintf("%s-%d", baseName, uniqNameNext[baseName]),
3548 Conn: c,
3552 func (c *loggingConn) Write(p []byte) (n int, err error) {
3553 log.Printf("%s.Write(%d) = ....", c.name, len(p))
3554 n, err = c.Conn.Write(p)
3555 log.Printf("%s.Write(%d) = %d, %v", c.name, len(p), n, err)
3556 return
3559 func (c *loggingConn) Read(p []byte) (n int, err error) {
3560 log.Printf("%s.Read(%d) = ....", c.name, len(p))
3561 n, err = c.Conn.Read(p)
3562 log.Printf("%s.Read(%d) = %d, %v", c.name, len(p), n, err)
3563 return
3566 func (c *loggingConn) Close() (err error) {
3567 log.Printf("%s.Close() = ...", c.name)
3568 err = c.Conn.Close()
3569 log.Printf("%s.Close() = %v", c.name, err)
3570 return
3573 // checkConnErrorWriter writes to c.rwc and records any write errors to c.werr.
3574 // It only contains one field (and a pointer field at that), so it
3575 // fits in an interface value without an extra allocation.
3576 type checkConnErrorWriter struct {
3577 c *conn
3580 func (w checkConnErrorWriter) Write(p []byte) (n int, err error) {
3581 n, err = w.c.rwc.Write(p)
3582 if err != nil && w.c.werr == nil {
3583 w.c.werr = err
3584 w.c.cancelCtx()
3586 return
3589 func numLeadingCRorLF(v []byte) (n int) {
3590 for _, b := range v {
3591 if b == '\r' || b == '\n' {
3593 continue
3595 break
3597 return
3601 func strSliceContains(ss []string, s string) bool {
3602 for _, v := range ss {
3603 if v == s {
3604 return true
3607 return false
3610 // tlsRecordHeaderLooksLikeHTTP reports whether a TLS record header
3611 // looks like it might've been a misdirected plaintext HTTP request.
3612 func tlsRecordHeaderLooksLikeHTTP(hdr [5]byte) bool {
3613 switch string(hdr[:]) {
3614 case "GET /", "HEAD ", "POST ", "PUT /", "OPTIO":
3615 return true
3617 return false
3620 // MaxBytesHandler returns a Handler that runs h with its ResponseWriter and Request.Body wrapped by a MaxBytesReader.
3621 func MaxBytesHandler(h Handler, n int64) Handler {
3622 return HandlerFunc(func(w ResponseWriter, r *Request) {
3623 r2 := *r
3624 r2.Body = MaxBytesReader(w, r.Body, n)
3625 h.ServeHTTP(w, &r2)