github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/net/http/server.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // HTTP server. See RFC 2616. 6 7 package http 8 9 import ( 10 "bufio" 11 "bytes" 12 "context" 13 "crypto/tls" 14 "errors" 15 "fmt" 16 "io" 17 "io/ioutil" 18 "log" 19 "net" 20 "net/textproto" 21 "net/url" 22 "os" 23 "path" 24 "runtime" 25 "strconv" 26 "strings" 27 "sync" 28 "sync/atomic" 29 "time" 30 31 "golang.org/x/net/lex/httplex" 32 ) 33 34 // Errors used by the HTTP server. 35 var ( 36 // ErrBodyNotAllowed is returned by ResponseWriter.Write calls 37 // when the HTTP method or response code does not permit a 38 // body. 39 ErrBodyNotAllowed = errors.New("http: request method or response status code does not allow body") 40 41 // ErrHijacked is returned by ResponseWriter.Write calls when 42 // the underlying connection has been hijacked using the 43 // Hijacker interfaced. 44 ErrHijacked = errors.New("http: connection has been hijacked") 45 46 // ErrContentLength is returned by ResponseWriter.Write calls 47 // when a Handler set a Content-Length response header with a 48 // declared size and then attempted to write more bytes than 49 // declared. 50 ErrContentLength = errors.New("http: wrote more than the declared Content-Length") 51 52 // Deprecated: ErrWriteAfterFlush is no longer used. 53 ErrWriteAfterFlush = errors.New("unused") 54 ) 55 56 // A Handler responds to an HTTP request. 57 // 58 // ServeHTTP should write reply headers and data to the ResponseWriter 59 // and then return. Returning signals that the request is finished; it 60 // is not valid to use the ResponseWriter or read from the 61 // Request.Body after or concurrently with the completion of the 62 // ServeHTTP call. 63 // 64 // Depending on the HTTP client software, HTTP protocol version, and 65 // any intermediaries between the client and the Go server, it may not 66 // be possible to read from the Request.Body after writing to the 67 // ResponseWriter. Cautious handlers should read the Request.Body 68 // first, and then reply. 69 // 70 // Except for reading the body, handlers should not modify the 71 // provided Request. 72 // 73 // If ServeHTTP panics, the server (the caller of ServeHTTP) assumes 74 // that the effect of the panic was isolated to the active request. 75 // It recovers the panic, logs a stack trace to the server error log, 76 // and hangs up the connection. 77 type Handler interface { 78 ServeHTTP(ResponseWriter, *Request) 79 } 80 81 // A ResponseWriter interface is used by an HTTP handler to 82 // construct an HTTP response. 83 // 84 // A ResponseWriter may not be used after the Handler.ServeHTTP method 85 // has returned. 86 type ResponseWriter interface { 87 // Header returns the header map that will be sent by 88 // WriteHeader. Changing the header after a call to 89 // WriteHeader (or Write) has no effect unless the modified 90 // headers were declared as trailers by setting the 91 // "Trailer" header before the call to WriteHeader (see example). 92 // To suppress implicit response headers, set their value to nil. 93 Header() Header 94 95 // Write writes the data to the connection as part of an HTTP reply. 96 // 97 // If WriteHeader has not yet been called, Write calls 98 // WriteHeader(http.StatusOK) before writing the data. If the Header 99 // does not contain a Content-Type line, Write adds a Content-Type set 100 // to the result of passing the initial 512 bytes of written data to 101 // DetectContentType. 102 // 103 // Depending on the HTTP protocol version and the client, calling 104 // Write or WriteHeader may prevent future reads on the 105 // Request.Body. For HTTP/1.x requests, handlers should read any 106 // needed request body data before writing the response. Once the 107 // headers have been flushed (due to either an explicit Flusher.Flush 108 // call or writing enough data to trigger a flush), the request body 109 // may be unavailable. For HTTP/2 requests, the Go HTTP server permits 110 // handlers to continue to read the request body while concurrently 111 // writing the response. However, such behavior may not be supported 112 // by all HTTP/2 clients. Handlers should read before writing if 113 // possible to maximize compatibility. 114 Write([]byte) (int, error) 115 116 // WriteHeader sends an HTTP response header with status code. 117 // If WriteHeader is not called explicitly, the first call to Write 118 // will trigger an implicit WriteHeader(http.StatusOK). 119 // Thus explicit calls to WriteHeader are mainly used to 120 // send error codes. 121 WriteHeader(int) 122 } 123 124 // The Flusher interface is implemented by ResponseWriters that allow 125 // an HTTP handler to flush buffered data to the client. 126 // 127 // The default HTTP/1.x and HTTP/2 ResponseWriter implementations 128 // support Flusher, but ResponseWriter wrappers may not. Handlers 129 // should always test for this ability at runtime. 130 // 131 // Note that even for ResponseWriters that support Flush, 132 // if the client is connected through an HTTP proxy, 133 // the buffered data may not reach the client until the response 134 // completes. 135 type Flusher interface { 136 // Flush sends any buffered data to the client. 137 Flush() 138 } 139 140 // The Hijacker interface is implemented by ResponseWriters that allow 141 // an HTTP handler to take over the connection. 142 // 143 // The default ResponseWriter for HTTP/1.x connections supports 144 // Hijacker, but HTTP/2 connections intentionally do not. 145 // ResponseWriter wrappers may also not support Hijacker. Handlers 146 // should always test for this ability at runtime. 147 type Hijacker interface { 148 // Hijack lets the caller take over the connection. 149 // After a call to Hijack(), the HTTP server library 150 // will not do anything else with the connection. 151 // 152 // It becomes the caller's responsibility to manage 153 // and close the connection. 154 // 155 // The returned net.Conn may have read or write deadlines 156 // already set, depending on the configuration of the 157 // Server. It is the caller's responsibility to set 158 // or clear those deadlines as needed. 159 Hijack() (net.Conn, *bufio.ReadWriter, error) 160 } 161 162 // The CloseNotifier interface is implemented by ResponseWriters which 163 // allow detecting when the underlying connection has gone away. 164 // 165 // This mechanism can be used to cancel long operations on the server 166 // if the client has disconnected before the response is ready. 167 type CloseNotifier interface { 168 // CloseNotify returns a channel that receives at most a 169 // single value (true) when the client connection has gone 170 // away. 171 // 172 // CloseNotify may wait to notify until Request.Body has been 173 // fully read. 174 // 175 // After the Handler has returned, there is no guarantee 176 // that the channel receives a value. 177 // 178 // If the protocol is HTTP/1.1 and CloseNotify is called while 179 // processing an idempotent request (such a GET) while 180 // HTTP/1.1 pipelining is in use, the arrival of a subsequent 181 // pipelined request may cause a value to be sent on the 182 // returned channel. In practice HTTP/1.1 pipelining is not 183 // enabled in browsers and not seen often in the wild. If this 184 // is a problem, use HTTP/2 or only use CloseNotify on methods 185 // such as POST. 186 CloseNotify() <-chan bool 187 } 188 189 var ( 190 // ServerContextKey is a context key. It can be used in HTTP 191 // handlers with context.WithValue to access the server that 192 // started the handler. The associated value will be of 193 // type *Server. 194 ServerContextKey = &contextKey{"http-server"} 195 196 // LocalAddrContextKey is a context key. It can be used in 197 // HTTP handlers with context.WithValue to access the address 198 // the local address the connection arrived on. 199 // The associated value will be of type net.Addr. 200 LocalAddrContextKey = &contextKey{"local-addr"} 201 ) 202 203 // A conn represents the server side of an HTTP connection. 204 type conn struct { 205 // server is the server on which the connection arrived. 206 // Immutable; never nil. 207 server *Server 208 209 // rwc is the underlying network connection. 210 // This is never wrapped by other types and is the value given out 211 // to CloseNotifier callers. It is usually of type *net.TCPConn or 212 // *tls.Conn. 213 rwc net.Conn 214 215 // remoteAddr is rwc.RemoteAddr().String(). It is not populated synchronously 216 // inside the Listener's Accept goroutine, as some implementations block. 217 // It is populated immediately inside the (*conn).serve goroutine. 218 // This is the value of a Handler's (*Request).RemoteAddr. 219 remoteAddr string 220 221 // tlsState is the TLS connection state when using TLS. 222 // nil means not TLS. 223 tlsState *tls.ConnectionState 224 225 // werr is set to the first write error to rwc. 226 // It is set via checkConnErrorWriter{w}, where bufw writes. 227 werr error 228 229 // r is bufr's read source. It's a wrapper around rwc that provides 230 // io.LimitedReader-style limiting (while reading request headers) 231 // and functionality to support CloseNotifier. See *connReader docs. 232 r *connReader 233 234 // bufr reads from r. 235 // Users of bufr must hold mu. 236 bufr *bufio.Reader 237 238 // bufw writes to checkConnErrorWriter{c}, which populates werr on error. 239 bufw *bufio.Writer 240 241 // lastMethod is the method of the most recent request 242 // on this connection, if any. 243 lastMethod string 244 245 // mu guards hijackedv, use of bufr, (*response).closeNotifyCh. 246 mu sync.Mutex 247 248 // hijackedv is whether this connection has been hijacked 249 // by a Handler with the Hijacker interface. 250 // It is guarded by mu. 251 hijackedv bool 252 } 253 254 func (c *conn) hijacked() bool { 255 c.mu.Lock() 256 defer c.mu.Unlock() 257 return c.hijackedv 258 } 259 260 // c.mu must be held. 261 func (c *conn) hijackLocked() (rwc net.Conn, buf *bufio.ReadWriter, err error) { 262 if c.hijackedv { 263 return nil, nil, ErrHijacked 264 } 265 c.hijackedv = true 266 rwc = c.rwc 267 buf = bufio.NewReadWriter(c.bufr, bufio.NewWriter(rwc)) 268 c.setState(rwc, StateHijacked) 269 return 270 } 271 272 // This should be >= 512 bytes for DetectContentType, 273 // but otherwise it's somewhat arbitrary. 274 const bufferBeforeChunkingSize = 2048 275 276 // chunkWriter writes to a response's conn buffer, and is the writer 277 // wrapped by the response.bufw buffered writer. 278 // 279 // chunkWriter also is responsible for finalizing the Header, including 280 // conditionally setting the Content-Type and setting a Content-Length 281 // in cases where the handler's final output is smaller than the buffer 282 // size. It also conditionally adds chunk headers, when in chunking mode. 283 // 284 // See the comment above (*response).Write for the entire write flow. 285 type chunkWriter struct { 286 res *response 287 288 // header is either nil or a deep clone of res.handlerHeader 289 // at the time of res.WriteHeader, if res.WriteHeader is 290 // called and extra buffering is being done to calculate 291 // Content-Type and/or Content-Length. 292 header Header 293 294 // wroteHeader tells whether the header's been written to "the 295 // wire" (or rather: w.conn.buf). this is unlike 296 // (*response).wroteHeader, which tells only whether it was 297 // logically written. 298 wroteHeader bool 299 300 // set by the writeHeader method: 301 chunking bool // using chunked transfer encoding for reply body 302 } 303 304 var ( 305 crlf = []byte("\r\n") 306 colonSpace = []byte(": ") 307 ) 308 309 func (cw *chunkWriter) Write(p []byte) (n int, err error) { 310 if !cw.wroteHeader { 311 cw.writeHeader(p) 312 } 313 if cw.res.req.Method == "HEAD" { 314 // Eat writes. 315 return len(p), nil 316 } 317 if cw.chunking { 318 _, err = fmt.Fprintf(cw.res.conn.bufw, "%x\r\n", len(p)) 319 if err != nil { 320 cw.res.conn.rwc.Close() 321 return 322 } 323 } 324 n, err = cw.res.conn.bufw.Write(p) 325 if cw.chunking && err == nil { 326 _, err = cw.res.conn.bufw.Write(crlf) 327 } 328 if err != nil { 329 cw.res.conn.rwc.Close() 330 } 331 return 332 } 333 334 func (cw *chunkWriter) flush() { 335 if !cw.wroteHeader { 336 cw.writeHeader(nil) 337 } 338 cw.res.conn.bufw.Flush() 339 } 340 341 func (cw *chunkWriter) close() { 342 if !cw.wroteHeader { 343 cw.writeHeader(nil) 344 } 345 if cw.chunking { 346 bw := cw.res.conn.bufw // conn's bufio writer 347 // zero chunk to mark EOF 348 bw.WriteString("0\r\n") 349 if len(cw.res.trailers) > 0 { 350 trailers := make(Header) 351 for _, h := range cw.res.trailers { 352 if vv := cw.res.handlerHeader[h]; len(vv) > 0 { 353 trailers[h] = vv 354 } 355 } 356 trailers.Write(bw) // the writer handles noting errors 357 } 358 // final blank line after the trailers (whether 359 // present or not) 360 bw.WriteString("\r\n") 361 } 362 } 363 364 // A response represents the server side of an HTTP response. 365 type response struct { 366 conn *conn 367 req *Request // request for this response 368 reqBody io.ReadCloser 369 cancelCtx context.CancelFunc // when ServeHTTP exits 370 wroteHeader bool // reply header has been (logically) written 371 wroteContinue bool // 100 Continue response was written 372 wants10KeepAlive bool // HTTP/1.0 w/ Connection "keep-alive" 373 wantsClose bool // HTTP request has Connection "close" 374 375 w *bufio.Writer // buffers output in chunks to chunkWriter 376 cw chunkWriter 377 378 // handlerHeader is the Header that Handlers get access to, 379 // which may be retained and mutated even after WriteHeader. 380 // handlerHeader is copied into cw.header at WriteHeader 381 // time, and privately mutated thereafter. 382 handlerHeader Header 383 calledHeader bool // handler accessed handlerHeader via Header 384 385 written int64 // number of bytes written in body 386 contentLength int64 // explicitly-declared Content-Length; or -1 387 status int // status code passed to WriteHeader 388 389 // close connection after this reply. set on request and 390 // updated after response from handler if there's a 391 // "Connection: keep-alive" response header and a 392 // Content-Length. 393 closeAfterReply bool 394 395 // requestBodyLimitHit is set by requestTooLarge when 396 // maxBytesReader hits its max size. It is checked in 397 // WriteHeader, to make sure we don't consume the 398 // remaining request body to try to advance to the next HTTP 399 // request. Instead, when this is set, we stop reading 400 // subsequent requests on this connection and stop reading 401 // input from it. 402 requestBodyLimitHit bool 403 404 // trailers are the headers to be sent after the handler 405 // finishes writing the body. This field is initialized from 406 // the Trailer response header when the response header is 407 // written. 408 trailers []string 409 410 handlerDone atomicBool // set true when the handler exits 411 412 // Buffers for Date and Content-Length 413 dateBuf [len(TimeFormat)]byte 414 clenBuf [10]byte 415 416 // closeNotifyCh is non-nil once CloseNotify is called. 417 // Guarded by conn.mu 418 closeNotifyCh <-chan bool 419 } 420 421 type atomicBool int32 422 423 func (b *atomicBool) isSet() bool { return atomic.LoadInt32((*int32)(b)) != 0 } 424 func (b *atomicBool) setTrue() { atomic.StoreInt32((*int32)(b), 1) } 425 426 // declareTrailer is called for each Trailer header when the 427 // response header is written. It notes that a header will need to be 428 // written in the trailers at the end of the response. 429 func (w *response) declareTrailer(k string) { 430 k = CanonicalHeaderKey(k) 431 switch k { 432 case "Transfer-Encoding", "Content-Length", "Trailer": 433 // Forbidden by RFC 2616 14.40. 434 return 435 } 436 w.trailers = append(w.trailers, k) 437 } 438 439 // requestTooLarge is called by maxBytesReader when too much input has 440 // been read from the client. 441 func (w *response) requestTooLarge() { 442 w.closeAfterReply = true 443 w.requestBodyLimitHit = true 444 if !w.wroteHeader { 445 w.Header().Set("Connection", "close") 446 } 447 } 448 449 // needsSniff reports whether a Content-Type still needs to be sniffed. 450 func (w *response) needsSniff() bool { 451 _, haveType := w.handlerHeader["Content-Type"] 452 return !w.cw.wroteHeader && !haveType && w.written < sniffLen 453 } 454 455 // writerOnly hides an io.Writer value's optional ReadFrom method 456 // from io.Copy. 457 type writerOnly struct { 458 io.Writer 459 } 460 461 func srcIsRegularFile(src io.Reader) (isRegular bool, err error) { 462 switch v := src.(type) { 463 case *os.File: 464 fi, err := v.Stat() 465 if err != nil { 466 return false, err 467 } 468 return fi.Mode().IsRegular(), nil 469 case *io.LimitedReader: 470 return srcIsRegularFile(v.R) 471 default: 472 return 473 } 474 } 475 476 // ReadFrom is here to optimize copying from an *os.File regular file 477 // to a *net.TCPConn with sendfile. 478 func (w *response) ReadFrom(src io.Reader) (n int64, err error) { 479 // Our underlying w.conn.rwc is usually a *TCPConn (with its 480 // own ReadFrom method). If not, or if our src isn't a regular 481 // file, just fall back to the normal copy method. 482 rf, ok := w.conn.rwc.(io.ReaderFrom) 483 regFile, err := srcIsRegularFile(src) 484 if err != nil { 485 return 0, err 486 } 487 if !ok || !regFile { 488 bufp := copyBufPool.Get().(*[]byte) 489 defer copyBufPool.Put(bufp) 490 return io.CopyBuffer(writerOnly{w}, src, *bufp) 491 } 492 493 // sendfile path: 494 495 if !w.wroteHeader { 496 w.WriteHeader(StatusOK) 497 } 498 499 if w.needsSniff() { 500 n0, err := io.Copy(writerOnly{w}, io.LimitReader(src, sniffLen)) 501 n += n0 502 if err != nil { 503 return n, err 504 } 505 } 506 507 w.w.Flush() // get rid of any previous writes 508 w.cw.flush() // make sure Header is written; flush data to rwc 509 510 // Now that cw has been flushed, its chunking field is guaranteed initialized. 511 if !w.cw.chunking && w.bodyAllowed() { 512 n0, err := rf.ReadFrom(src) 513 n += n0 514 w.written += n0 515 return n, err 516 } 517 518 n0, err := io.Copy(writerOnly{w}, src) 519 n += n0 520 return n, err 521 } 522 523 // debugServerConnections controls whether all server connections are wrapped 524 // with a verbose logging wrapper. 525 const debugServerConnections = false 526 527 // Create new connection from rwc. 528 func (srv *Server) newConn(rwc net.Conn) *conn { 529 c := &conn{ 530 server: srv, 531 rwc: rwc, 532 } 533 if debugServerConnections { 534 c.rwc = newLoggingConn("server", c.rwc) 535 } 536 return c 537 } 538 539 type readResult struct { 540 n int 541 err error 542 b byte // byte read, if n == 1 543 } 544 545 // connReader is the io.Reader wrapper used by *conn. It combines a 546 // selectively-activated io.LimitedReader (to bound request header 547 // read sizes) with support for selectively keeping an io.Reader.Read 548 // call blocked in a background goroutine to wait for activity and 549 // trigger a CloseNotifier channel. 550 type connReader struct { 551 r io.Reader 552 remain int64 // bytes remaining 553 554 // ch is non-nil if a background read is in progress. 555 // It is guarded by conn.mu. 556 ch chan readResult 557 } 558 559 func (cr *connReader) setReadLimit(remain int64) { cr.remain = remain } 560 func (cr *connReader) setInfiniteReadLimit() { cr.remain = maxInt64 } 561 func (cr *connReader) hitReadLimit() bool { return cr.remain <= 0 } 562 563 func (cr *connReader) Read(p []byte) (n int, err error) { 564 if cr.hitReadLimit() { 565 return 0, io.EOF 566 } 567 if len(p) == 0 { 568 return 569 } 570 if int64(len(p)) > cr.remain { 571 p = p[:cr.remain] 572 } 573 574 // Is a background read (started by CloseNotifier) already in 575 // flight? If so, wait for it and use its result. 576 ch := cr.ch 577 if ch != nil { 578 cr.ch = nil 579 res := <-ch 580 if res.n == 1 { 581 p[0] = res.b 582 cr.remain -= 1 583 } 584 return res.n, res.err 585 } 586 n, err = cr.r.Read(p) 587 cr.remain -= int64(n) 588 return 589 } 590 591 func (cr *connReader) startBackgroundRead(onReadComplete func()) { 592 if cr.ch != nil { 593 // Background read already started. 594 return 595 } 596 cr.ch = make(chan readResult, 1) 597 go cr.closeNotifyAwaitActivityRead(cr.ch, onReadComplete) 598 } 599 600 func (cr *connReader) closeNotifyAwaitActivityRead(ch chan<- readResult, onReadComplete func()) { 601 var buf [1]byte 602 n, err := cr.r.Read(buf[:1]) 603 onReadComplete() 604 ch <- readResult{n, err, buf[0]} 605 } 606 607 var ( 608 bufioReaderPool sync.Pool 609 bufioWriter2kPool sync.Pool 610 bufioWriter4kPool sync.Pool 611 ) 612 613 var copyBufPool = sync.Pool{ 614 New: func() interface{} { 615 b := make([]byte, 32*1024) 616 return &b 617 }, 618 } 619 620 func bufioWriterPool(size int) *sync.Pool { 621 switch size { 622 case 2 << 10: 623 return &bufioWriter2kPool 624 case 4 << 10: 625 return &bufioWriter4kPool 626 } 627 return nil 628 } 629 630 func newBufioReader(r io.Reader) *bufio.Reader { 631 if v := bufioReaderPool.Get(); v != nil { 632 br := v.(*bufio.Reader) 633 br.Reset(r) 634 return br 635 } 636 // Note: if this reader size is every changed, update 637 // TestHandlerBodyClose's assumptions. 638 return bufio.NewReader(r) 639 } 640 641 func putBufioReader(br *bufio.Reader) { 642 br.Reset(nil) 643 bufioReaderPool.Put(br) 644 } 645 646 func newBufioWriterSize(w io.Writer, size int) *bufio.Writer { 647 pool := bufioWriterPool(size) 648 if pool != nil { 649 if v := pool.Get(); v != nil { 650 bw := v.(*bufio.Writer) 651 bw.Reset(w) 652 return bw 653 } 654 } 655 return bufio.NewWriterSize(w, size) 656 } 657 658 func putBufioWriter(bw *bufio.Writer) { 659 bw.Reset(nil) 660 if pool := bufioWriterPool(bw.Available()); pool != nil { 661 pool.Put(bw) 662 } 663 } 664 665 // DefaultMaxHeaderBytes is the maximum permitted size of the headers 666 // in an HTTP request. 667 // This can be overridden by setting Server.MaxHeaderBytes. 668 const DefaultMaxHeaderBytes = 1 << 20 // 1 MB 669 670 func (srv *Server) maxHeaderBytes() int { 671 if srv.MaxHeaderBytes > 0 { 672 return srv.MaxHeaderBytes 673 } 674 return DefaultMaxHeaderBytes 675 } 676 677 func (srv *Server) initialReadLimitSize() int64 { 678 return int64(srv.maxHeaderBytes()) + 4096 // bufio slop 679 } 680 681 // wrapper around io.ReaderCloser which on first read, sends an 682 // HTTP/1.1 100 Continue header 683 type expectContinueReader struct { 684 resp *response 685 readCloser io.ReadCloser 686 closed bool 687 sawEOF bool 688 } 689 690 func (ecr *expectContinueReader) Read(p []byte) (n int, err error) { 691 if ecr.closed { 692 return 0, ErrBodyReadAfterClose 693 } 694 if !ecr.resp.wroteContinue && !ecr.resp.conn.hijacked() { 695 ecr.resp.wroteContinue = true 696 ecr.resp.conn.bufw.WriteString("HTTP/1.1 100 Continue\r\n\r\n") 697 ecr.resp.conn.bufw.Flush() 698 } 699 n, err = ecr.readCloser.Read(p) 700 if err == io.EOF { 701 ecr.sawEOF = true 702 } 703 return 704 } 705 706 func (ecr *expectContinueReader) Close() error { 707 ecr.closed = true 708 return ecr.readCloser.Close() 709 } 710 711 // TimeFormat is the time format to use when generating times in HTTP 712 // headers. It is like time.RFC1123 but hard-codes GMT as the time 713 // zone. The time being formatted must be in UTC for Format to 714 // generate the correct format. 715 // 716 // For parsing this time format, see ParseTime. 717 const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT" 718 719 // appendTime is a non-allocating version of []byte(t.UTC().Format(TimeFormat)) 720 func appendTime(b []byte, t time.Time) []byte { 721 const days = "SunMonTueWedThuFriSat" 722 const months = "JanFebMarAprMayJunJulAugSepOctNovDec" 723 724 t = t.UTC() 725 yy, mm, dd := t.Date() 726 hh, mn, ss := t.Clock() 727 day := days[3*t.Weekday():] 728 mon := months[3*(mm-1):] 729 730 return append(b, 731 day[0], day[1], day[2], ',', ' ', 732 byte('0'+dd/10), byte('0'+dd%10), ' ', 733 mon[0], mon[1], mon[2], ' ', 734 byte('0'+yy/1000), byte('0'+(yy/100)%10), byte('0'+(yy/10)%10), byte('0'+yy%10), ' ', 735 byte('0'+hh/10), byte('0'+hh%10), ':', 736 byte('0'+mn/10), byte('0'+mn%10), ':', 737 byte('0'+ss/10), byte('0'+ss%10), ' ', 738 'G', 'M', 'T') 739 } 740 741 var errTooLarge = errors.New("http: request too large") 742 743 // Read next request from connection. 744 func (c *conn) readRequest(ctx context.Context) (w *response, err error) { 745 if c.hijacked() { 746 return nil, ErrHijacked 747 } 748 749 if d := c.server.ReadTimeout; d != 0 { 750 c.rwc.SetReadDeadline(time.Now().Add(d)) 751 } 752 if d := c.server.WriteTimeout; d != 0 { 753 defer func() { 754 c.rwc.SetWriteDeadline(time.Now().Add(d)) 755 }() 756 } 757 758 c.r.setReadLimit(c.server.initialReadLimitSize()) 759 c.mu.Lock() // while using bufr 760 if c.lastMethod == "POST" { 761 // RFC 2616 section 4.1 tolerance for old buggy clients. 762 peek, _ := c.bufr.Peek(4) // ReadRequest will get err below 763 c.bufr.Discard(numLeadingCRorLF(peek)) 764 } 765 req, err := readRequest(c.bufr, keepHostHeader) 766 c.mu.Unlock() 767 if err != nil { 768 if c.r.hitReadLimit() { 769 return nil, errTooLarge 770 } 771 return nil, err 772 } 773 774 ctx, cancelCtx := context.WithCancel(ctx) 775 req.ctx = ctx 776 777 c.lastMethod = req.Method 778 c.r.setInfiniteReadLimit() 779 780 hosts, haveHost := req.Header["Host"] 781 isH2Upgrade := req.isH2Upgrade() 782 if req.ProtoAtLeast(1, 1) && (!haveHost || len(hosts) == 0) && !isH2Upgrade { 783 return nil, badRequestError("missing required Host header") 784 } 785 if len(hosts) > 1 { 786 return nil, badRequestError("too many Host headers") 787 } 788 if len(hosts) == 1 && !httplex.ValidHostHeader(hosts[0]) { 789 return nil, badRequestError("malformed Host header") 790 } 791 for k, vv := range req.Header { 792 if !httplex.ValidHeaderFieldName(k) { 793 return nil, badRequestError("invalid header name") 794 } 795 for _, v := range vv { 796 if !httplex.ValidHeaderFieldValue(v) { 797 return nil, badRequestError("invalid header value") 798 } 799 } 800 } 801 delete(req.Header, "Host") 802 803 req.RemoteAddr = c.remoteAddr 804 req.TLS = c.tlsState 805 if body, ok := req.Body.(*body); ok { 806 body.doEarlyClose = true 807 } 808 809 w = &response{ 810 conn: c, 811 cancelCtx: cancelCtx, 812 req: req, 813 reqBody: req.Body, 814 handlerHeader: make(Header), 815 contentLength: -1, 816 817 // We populate these ahead of time so we're not 818 // reading from req.Header after their Handler starts 819 // and maybe mutates it (Issue 14940) 820 wants10KeepAlive: req.wantsHttp10KeepAlive(), 821 wantsClose: req.wantsClose(), 822 } 823 if isH2Upgrade { 824 w.closeAfterReply = true 825 } 826 w.cw.res = w 827 w.w = newBufioWriterSize(&w.cw, bufferBeforeChunkingSize) 828 return w, nil 829 } 830 831 func (w *response) Header() Header { 832 if w.cw.header == nil && w.wroteHeader && !w.cw.wroteHeader { 833 // Accessing the header between logically writing it 834 // and physically writing it means we need to allocate 835 // a clone to snapshot the logically written state. 836 w.cw.header = w.handlerHeader.clone() 837 } 838 w.calledHeader = true 839 return w.handlerHeader 840 } 841 842 // maxPostHandlerReadBytes is the max number of Request.Body bytes not 843 // consumed by a handler that the server will read from the client 844 // in order to keep a connection alive. If there are more bytes than 845 // this then the server to be paranoid instead sends a "Connection: 846 // close" response. 847 // 848 // This number is approximately what a typical machine's TCP buffer 849 // size is anyway. (if we have the bytes on the machine, we might as 850 // well read them) 851 const maxPostHandlerReadBytes = 256 << 10 852 853 func (w *response) WriteHeader(code int) { 854 if w.conn.hijacked() { 855 w.conn.server.logf("http: response.WriteHeader on hijacked connection") 856 return 857 } 858 if w.wroteHeader { 859 w.conn.server.logf("http: multiple response.WriteHeader calls") 860 return 861 } 862 w.wroteHeader = true 863 w.status = code 864 865 if w.calledHeader && w.cw.header == nil { 866 w.cw.header = w.handlerHeader.clone() 867 } 868 869 if cl := w.handlerHeader.get("Content-Length"); cl != "" { 870 v, err := strconv.ParseInt(cl, 10, 64) 871 if err == nil && v >= 0 { 872 w.contentLength = v 873 } else { 874 w.conn.server.logf("http: invalid Content-Length of %q", cl) 875 w.handlerHeader.Del("Content-Length") 876 } 877 } 878 } 879 880 // extraHeader is the set of headers sometimes added by chunkWriter.writeHeader. 881 // This type is used to avoid extra allocations from cloning and/or populating 882 // the response Header map and all its 1-element slices. 883 type extraHeader struct { 884 contentType string 885 connection string 886 transferEncoding string 887 date []byte // written if not nil 888 contentLength []byte // written if not nil 889 } 890 891 // Sorted the same as extraHeader.Write's loop. 892 var extraHeaderKeys = [][]byte{ 893 []byte("Content-Type"), 894 []byte("Connection"), 895 []byte("Transfer-Encoding"), 896 } 897 898 var ( 899 headerContentLength = []byte("Content-Length: ") 900 headerDate = []byte("Date: ") 901 ) 902 903 // Write writes the headers described in h to w. 904 // 905 // This method has a value receiver, despite the somewhat large size 906 // of h, because it prevents an allocation. The escape analysis isn't 907 // smart enough to realize this function doesn't mutate h. 908 func (h extraHeader) Write(w *bufio.Writer) { 909 if h.date != nil { 910 w.Write(headerDate) 911 w.Write(h.date) 912 w.Write(crlf) 913 } 914 if h.contentLength != nil { 915 w.Write(headerContentLength) 916 w.Write(h.contentLength) 917 w.Write(crlf) 918 } 919 for i, v := range []string{h.contentType, h.connection, h.transferEncoding} { 920 if v != "" { 921 w.Write(extraHeaderKeys[i]) 922 w.Write(colonSpace) 923 w.WriteString(v) 924 w.Write(crlf) 925 } 926 } 927 } 928 929 // writeHeader finalizes the header sent to the client and writes it 930 // to cw.res.conn.bufw. 931 // 932 // p is not written by writeHeader, but is the first chunk of the body 933 // that will be written. It is sniffed for a Content-Type if none is 934 // set explicitly. It's also used to set the Content-Length, if the 935 // total body size was small and the handler has already finished 936 // running. 937 func (cw *chunkWriter) writeHeader(p []byte) { 938 if cw.wroteHeader { 939 return 940 } 941 cw.wroteHeader = true 942 943 w := cw.res 944 keepAlivesEnabled := w.conn.server.doKeepAlives() 945 isHEAD := w.req.Method == "HEAD" 946 947 // header is written out to w.conn.buf below. Depending on the 948 // state of the handler, we either own the map or not. If we 949 // don't own it, the exclude map is created lazily for 950 // WriteSubset to remove headers. The setHeader struct holds 951 // headers we need to add. 952 header := cw.header 953 owned := header != nil 954 if !owned { 955 header = w.handlerHeader 956 } 957 var excludeHeader map[string]bool 958 delHeader := func(key string) { 959 if owned { 960 header.Del(key) 961 return 962 } 963 if _, ok := header[key]; !ok { 964 return 965 } 966 if excludeHeader == nil { 967 excludeHeader = make(map[string]bool) 968 } 969 excludeHeader[key] = true 970 } 971 var setHeader extraHeader 972 973 trailers := false 974 for _, v := range cw.header["Trailer"] { 975 trailers = true 976 foreachHeaderElement(v, cw.res.declareTrailer) 977 } 978 979 te := header.get("Transfer-Encoding") 980 hasTE := te != "" 981 982 // If the handler is done but never sent a Content-Length 983 // response header and this is our first (and last) write, set 984 // it, even to zero. This helps HTTP/1.0 clients keep their 985 // "keep-alive" connections alive. 986 // Exceptions: 304/204/1xx responses never get Content-Length, and if 987 // it was a HEAD request, we don't know the difference between 988 // 0 actual bytes and 0 bytes because the handler noticed it 989 // was a HEAD request and chose not to write anything. So for 990 // HEAD, the handler should either write the Content-Length or 991 // write non-zero bytes. If it's actually 0 bytes and the 992 // handler never looked at the Request.Method, we just don't 993 // send a Content-Length header. 994 // Further, we don't send an automatic Content-Length if they 995 // set a Transfer-Encoding, because they're generally incompatible. 996 if w.handlerDone.isSet() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && header.get("Content-Length") == "" && (!isHEAD || len(p) > 0) { 997 w.contentLength = int64(len(p)) 998 setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10) 999 } 1000 1001 // If this was an HTTP/1.0 request with keep-alive and we sent a 1002 // Content-Length back, we can make this a keep-alive response ... 1003 if w.wants10KeepAlive && keepAlivesEnabled { 1004 sentLength := header.get("Content-Length") != "" 1005 if sentLength && header.get("Connection") == "keep-alive" { 1006 w.closeAfterReply = false 1007 } 1008 } 1009 1010 // Check for a explicit (and valid) Content-Length header. 1011 hasCL := w.contentLength != -1 1012 1013 if w.wants10KeepAlive && (isHEAD || hasCL || !bodyAllowedForStatus(w.status)) { 1014 _, connectionHeaderSet := header["Connection"] 1015 if !connectionHeaderSet { 1016 setHeader.connection = "keep-alive" 1017 } 1018 } else if !w.req.ProtoAtLeast(1, 1) || w.wantsClose { 1019 w.closeAfterReply = true 1020 } 1021 1022 if header.get("Connection") == "close" || !keepAlivesEnabled { 1023 w.closeAfterReply = true 1024 } 1025 1026 // If the client wanted a 100-continue but we never sent it to 1027 // them (or, more strictly: we never finished reading their 1028 // request body), don't reuse this connection because it's now 1029 // in an unknown state: we might be sending this response at 1030 // the same time the client is now sending its request body 1031 // after a timeout. (Some HTTP clients send Expect: 1032 // 100-continue but knowing that some servers don't support 1033 // it, the clients set a timer and send the body later anyway) 1034 // If we haven't seen EOF, we can't skip over the unread body 1035 // because we don't know if the next bytes on the wire will be 1036 // the body-following-the-timer or the subsequent request. 1037 // See Issue 11549. 1038 if ecr, ok := w.req.Body.(*expectContinueReader); ok && !ecr.sawEOF { 1039 w.closeAfterReply = true 1040 } 1041 1042 // Per RFC 2616, we should consume the request body before 1043 // replying, if the handler hasn't already done so. But we 1044 // don't want to do an unbounded amount of reading here for 1045 // DoS reasons, so we only try up to a threshold. 1046 // TODO(bradfitz): where does RFC 2616 say that? See Issue 15527 1047 // about HTTP/1.x Handlers concurrently reading and writing, like 1048 // HTTP/2 handlers can do. Maybe this code should be relaxed? 1049 if w.req.ContentLength != 0 && !w.closeAfterReply { 1050 var discard, tooBig bool 1051 1052 switch bdy := w.req.Body.(type) { 1053 case *expectContinueReader: 1054 if bdy.resp.wroteContinue { 1055 discard = true 1056 } 1057 case *body: 1058 bdy.mu.Lock() 1059 switch { 1060 case bdy.closed: 1061 if !bdy.sawEOF { 1062 // Body was closed in handler with non-EOF error. 1063 w.closeAfterReply = true 1064 } 1065 case bdy.unreadDataSizeLocked() >= maxPostHandlerReadBytes: 1066 tooBig = true 1067 default: 1068 discard = true 1069 } 1070 bdy.mu.Unlock() 1071 default: 1072 discard = true 1073 } 1074 1075 if discard { 1076 _, err := io.CopyN(ioutil.Discard, w.reqBody, maxPostHandlerReadBytes+1) 1077 switch err { 1078 case nil: 1079 // There must be even more data left over. 1080 tooBig = true 1081 case ErrBodyReadAfterClose: 1082 // Body was already consumed and closed. 1083 case io.EOF: 1084 // The remaining body was just consumed, close it. 1085 err = w.reqBody.Close() 1086 if err != nil { 1087 w.closeAfterReply = true 1088 } 1089 default: 1090 // Some other kind of error occurred, like a read timeout, or 1091 // corrupt chunked encoding. In any case, whatever remains 1092 // on the wire must not be parsed as another HTTP request. 1093 w.closeAfterReply = true 1094 } 1095 } 1096 1097 if tooBig { 1098 w.requestTooLarge() 1099 delHeader("Connection") 1100 setHeader.connection = "close" 1101 } 1102 } 1103 1104 code := w.status 1105 if bodyAllowedForStatus(code) { 1106 // If no content type, apply sniffing algorithm to body. 1107 _, haveType := header["Content-Type"] 1108 if !haveType && !hasTE { 1109 setHeader.contentType = DetectContentType(p) 1110 } 1111 } else { 1112 for _, k := range suppressedHeaders(code) { 1113 delHeader(k) 1114 } 1115 } 1116 1117 if _, ok := header["Date"]; !ok { 1118 setHeader.date = appendTime(cw.res.dateBuf[:0], time.Now()) 1119 } 1120 1121 if hasCL && hasTE && te != "identity" { 1122 // TODO: return an error if WriteHeader gets a return parameter 1123 // For now just ignore the Content-Length. 1124 w.conn.server.logf("http: WriteHeader called with both Transfer-Encoding of %q and a Content-Length of %d", 1125 te, w.contentLength) 1126 delHeader("Content-Length") 1127 hasCL = false 1128 } 1129 1130 if w.req.Method == "HEAD" || !bodyAllowedForStatus(code) { 1131 // do nothing 1132 } else if code == StatusNoContent { 1133 delHeader("Transfer-Encoding") 1134 } else if hasCL { 1135 delHeader("Transfer-Encoding") 1136 } else if w.req.ProtoAtLeast(1, 1) { 1137 // HTTP/1.1 or greater: Transfer-Encoding has been set to identity, and no 1138 // content-length has been provided. The connection must be closed after the 1139 // reply is written, and no chunking is to be done. This is the setup 1140 // recommended in the Server-Sent Events candidate recommendation 11, 1141 // section 8. 1142 if hasTE && te == "identity" { 1143 cw.chunking = false 1144 w.closeAfterReply = true 1145 } else { 1146 // HTTP/1.1 or greater: use chunked transfer encoding 1147 // to avoid closing the connection at EOF. 1148 cw.chunking = true 1149 setHeader.transferEncoding = "chunked" 1150 } 1151 } else { 1152 // HTTP version < 1.1: cannot do chunked transfer 1153 // encoding and we don't know the Content-Length so 1154 // signal EOF by closing connection. 1155 w.closeAfterReply = true 1156 delHeader("Transfer-Encoding") // in case already set 1157 } 1158 1159 // Cannot use Content-Length with non-identity Transfer-Encoding. 1160 if cw.chunking { 1161 delHeader("Content-Length") 1162 } 1163 if !w.req.ProtoAtLeast(1, 0) { 1164 return 1165 } 1166 1167 if w.closeAfterReply && (!keepAlivesEnabled || !hasToken(cw.header.get("Connection"), "close")) { 1168 delHeader("Connection") 1169 if w.req.ProtoAtLeast(1, 1) { 1170 setHeader.connection = "close" 1171 } 1172 } 1173 1174 w.conn.bufw.WriteString(statusLine(w.req, code)) 1175 cw.header.WriteSubset(w.conn.bufw, excludeHeader) 1176 setHeader.Write(w.conn.bufw) 1177 w.conn.bufw.Write(crlf) 1178 } 1179 1180 // foreachHeaderElement splits v according to the "#rule" construction 1181 // in RFC 2616 section 2.1 and calls fn for each non-empty element. 1182 func foreachHeaderElement(v string, fn func(string)) { 1183 v = textproto.TrimString(v) 1184 if v == "" { 1185 return 1186 } 1187 if !strings.Contains(v, ",") { 1188 fn(v) 1189 return 1190 } 1191 for _, f := range strings.Split(v, ",") { 1192 if f = textproto.TrimString(f); f != "" { 1193 fn(f) 1194 } 1195 } 1196 } 1197 1198 // statusLines is a cache of Status-Line strings, keyed by code (for 1199 // HTTP/1.1) or negative code (for HTTP/1.0). This is faster than a 1200 // map keyed by struct of two fields. This map's max size is bounded 1201 // by 2*len(statusText), two protocol types for each known official 1202 // status code in the statusText map. 1203 var ( 1204 statusMu sync.RWMutex 1205 statusLines = make(map[int]string) 1206 ) 1207 1208 // statusLine returns a response Status-Line (RFC 2616 Section 6.1) 1209 // for the given request and response status code. 1210 func statusLine(req *Request, code int) string { 1211 // Fast path: 1212 key := code 1213 proto11 := req.ProtoAtLeast(1, 1) 1214 if !proto11 { 1215 key = -key 1216 } 1217 statusMu.RLock() 1218 line, ok := statusLines[key] 1219 statusMu.RUnlock() 1220 if ok { 1221 return line 1222 } 1223 1224 // Slow path: 1225 proto := "HTTP/1.0" 1226 if proto11 { 1227 proto = "HTTP/1.1" 1228 } 1229 codestring := fmt.Sprintf("%03d", code) 1230 text, ok := statusText[code] 1231 if !ok { 1232 text = "status code " + codestring 1233 } 1234 line = proto + " " + codestring + " " + text + "\r\n" 1235 if ok { 1236 statusMu.Lock() 1237 defer statusMu.Unlock() 1238 statusLines[key] = line 1239 } 1240 return line 1241 } 1242 1243 // bodyAllowed reports whether a Write is allowed for this response type. 1244 // It's illegal to call this before the header has been flushed. 1245 func (w *response) bodyAllowed() bool { 1246 if !w.wroteHeader { 1247 panic("") 1248 } 1249 return bodyAllowedForStatus(w.status) 1250 } 1251 1252 // The Life Of A Write is like this: 1253 // 1254 // Handler starts. No header has been sent. The handler can either 1255 // write a header, or just start writing. Writing before sending a header 1256 // sends an implicitly empty 200 OK header. 1257 // 1258 // If the handler didn't declare a Content-Length up front, we either 1259 // go into chunking mode or, if the handler finishes running before 1260 // the chunking buffer size, we compute a Content-Length and send that 1261 // in the header instead. 1262 // 1263 // Likewise, if the handler didn't set a Content-Type, we sniff that 1264 // from the initial chunk of output. 1265 // 1266 // The Writers are wired together like: 1267 // 1268 // 1. *response (the ResponseWriter) -> 1269 // 2. (*response).w, a *bufio.Writer of bufferBeforeChunkingSize bytes 1270 // 3. chunkWriter.Writer (whose writeHeader finalizes Content-Length/Type) 1271 // and which writes the chunk headers, if needed. 1272 // 4. conn.buf, a bufio.Writer of default (4kB) bytes, writing to -> 1273 // 5. checkConnErrorWriter{c}, which notes any non-nil error on Write 1274 // and populates c.werr with it if so. but otherwise writes to: 1275 // 6. the rwc, the net.Conn. 1276 // 1277 // TODO(bradfitz): short-circuit some of the buffering when the 1278 // initial header contains both a Content-Type and Content-Length. 1279 // Also short-circuit in (1) when the header's been sent and not in 1280 // chunking mode, writing directly to (4) instead, if (2) has no 1281 // buffered data. More generally, we could short-circuit from (1) to 1282 // (3) even in chunking mode if the write size from (1) is over some 1283 // threshold and nothing is in (2). The answer might be mostly making 1284 // bufferBeforeChunkingSize smaller and having bufio's fast-paths deal 1285 // with this instead. 1286 func (w *response) Write(data []byte) (n int, err error) { 1287 return w.write(len(data), data, "") 1288 } 1289 1290 func (w *response) WriteString(data string) (n int, err error) { 1291 return w.write(len(data), nil, data) 1292 } 1293 1294 // either dataB or dataS is non-zero. 1295 func (w *response) write(lenData int, dataB []byte, dataS string) (n int, err error) { 1296 if w.conn.hijacked() { 1297 w.conn.server.logf("http: response.Write on hijacked connection") 1298 return 0, ErrHijacked 1299 } 1300 if !w.wroteHeader { 1301 w.WriteHeader(StatusOK) 1302 } 1303 if lenData == 0 { 1304 return 0, nil 1305 } 1306 if !w.bodyAllowed() { 1307 return 0, ErrBodyNotAllowed 1308 } 1309 1310 w.written += int64(lenData) // ignoring errors, for errorKludge 1311 if w.contentLength != -1 && w.written > w.contentLength { 1312 return 0, ErrContentLength 1313 } 1314 if dataB != nil { 1315 return w.w.Write(dataB) 1316 } else { 1317 return w.w.WriteString(dataS) 1318 } 1319 } 1320 1321 func (w *response) finishRequest() { 1322 w.handlerDone.setTrue() 1323 1324 if !w.wroteHeader { 1325 w.WriteHeader(StatusOK) 1326 } 1327 1328 w.w.Flush() 1329 putBufioWriter(w.w) 1330 w.cw.close() 1331 w.conn.bufw.Flush() 1332 1333 // Close the body (regardless of w.closeAfterReply) so we can 1334 // re-use its bufio.Reader later safely. 1335 w.reqBody.Close() 1336 1337 if w.req.MultipartForm != nil { 1338 w.req.MultipartForm.RemoveAll() 1339 } 1340 } 1341 1342 // shouldReuseConnection reports whether the underlying TCP connection can be reused. 1343 // It must only be called after the handler is done executing. 1344 func (w *response) shouldReuseConnection() bool { 1345 if w.closeAfterReply { 1346 // The request or something set while executing the 1347 // handler indicated we shouldn't reuse this 1348 // connection. 1349 return false 1350 } 1351 1352 if w.req.Method != "HEAD" && w.contentLength != -1 && w.bodyAllowed() && w.contentLength != w.written { 1353 // Did not write enough. Avoid getting out of sync. 1354 return false 1355 } 1356 1357 // There was some error writing to the underlying connection 1358 // during the request, so don't re-use this conn. 1359 if w.conn.werr != nil { 1360 return false 1361 } 1362 1363 if w.closedRequestBodyEarly() { 1364 return false 1365 } 1366 1367 return true 1368 } 1369 1370 func (w *response) closedRequestBodyEarly() bool { 1371 body, ok := w.req.Body.(*body) 1372 return ok && body.didEarlyClose() 1373 } 1374 1375 func (w *response) Flush() { 1376 if !w.wroteHeader { 1377 w.WriteHeader(StatusOK) 1378 } 1379 w.w.Flush() 1380 w.cw.flush() 1381 } 1382 1383 func (c *conn) finalFlush() { 1384 if c.bufr != nil { 1385 // Steal the bufio.Reader (~4KB worth of memory) and its associated 1386 // reader for a future connection. 1387 putBufioReader(c.bufr) 1388 c.bufr = nil 1389 } 1390 1391 if c.bufw != nil { 1392 c.bufw.Flush() 1393 // Steal the bufio.Writer (~4KB worth of memory) and its associated 1394 // writer for a future connection. 1395 putBufioWriter(c.bufw) 1396 c.bufw = nil 1397 } 1398 } 1399 1400 // Close the connection. 1401 func (c *conn) close() { 1402 c.finalFlush() 1403 c.rwc.Close() 1404 } 1405 1406 // rstAvoidanceDelay is the amount of time we sleep after closing the 1407 // write side of a TCP connection before closing the entire socket. 1408 // By sleeping, we increase the chances that the client sees our FIN 1409 // and processes its final data before they process the subsequent RST 1410 // from closing a connection with known unread data. 1411 // This RST seems to occur mostly on BSD systems. (And Windows?) 1412 // This timeout is somewhat arbitrary (~latency around the planet). 1413 const rstAvoidanceDelay = 500 * time.Millisecond 1414 1415 type closeWriter interface { 1416 CloseWrite() error 1417 } 1418 1419 var _ closeWriter = (*net.TCPConn)(nil) 1420 1421 // closeWrite flushes any outstanding data and sends a FIN packet (if 1422 // client is connected via TCP), signalling that we're done. We then 1423 // pause for a bit, hoping the client processes it before any 1424 // subsequent RST. 1425 // 1426 // See https://golang.org/issue/3595 1427 func (c *conn) closeWriteAndWait() { 1428 c.finalFlush() 1429 if tcp, ok := c.rwc.(closeWriter); ok { 1430 tcp.CloseWrite() 1431 } 1432 time.Sleep(rstAvoidanceDelay) 1433 } 1434 1435 // validNPN reports whether the proto is not a blacklisted Next 1436 // Protocol Negotiation protocol. Empty and built-in protocol types 1437 // are blacklisted and can't be overridden with alternate 1438 // implementations. 1439 func validNPN(proto string) bool { 1440 switch proto { 1441 case "", "http/1.1", "http/1.0": 1442 return false 1443 } 1444 return true 1445 } 1446 1447 func (c *conn) setState(nc net.Conn, state ConnState) { 1448 if hook := c.server.ConnState; hook != nil { 1449 hook(nc, state) 1450 } 1451 } 1452 1453 // badRequestError is a literal string (used by in the server in HTML, 1454 // unescaped) to tell the user why their request was bad. It should 1455 // be plain text without user info or other embedded errors. 1456 type badRequestError string 1457 1458 func (e badRequestError) Error() string { return "Bad Request: " + string(e) } 1459 1460 // Serve a new connection. 1461 func (c *conn) serve(ctx context.Context) { 1462 c.remoteAddr = c.rwc.RemoteAddr().String() 1463 defer func() { 1464 if err := recover(); err != nil { 1465 const size = 64 << 10 1466 buf := make([]byte, size) 1467 buf = buf[:runtime.Stack(buf, false)] 1468 c.server.logf("http: panic serving %v: %v\n%s", c.remoteAddr, err, buf) 1469 } 1470 if !c.hijacked() { 1471 c.close() 1472 c.setState(c.rwc, StateClosed) 1473 } 1474 }() 1475 1476 if tlsConn, ok := c.rwc.(*tls.Conn); ok { 1477 if d := c.server.ReadTimeout; d != 0 { 1478 c.rwc.SetReadDeadline(time.Now().Add(d)) 1479 } 1480 if d := c.server.WriteTimeout; d != 0 { 1481 c.rwc.SetWriteDeadline(time.Now().Add(d)) 1482 } 1483 if err := tlsConn.Handshake(); err != nil { 1484 c.server.logf("http: TLS handshake error from %s: %v", c.rwc.RemoteAddr(), err) 1485 return 1486 } 1487 c.tlsState = new(tls.ConnectionState) 1488 *c.tlsState = tlsConn.ConnectionState() 1489 if proto := c.tlsState.NegotiatedProtocol; validNPN(proto) { 1490 if fn := c.server.TLSNextProto[proto]; fn != nil { 1491 h := initNPNRequest{tlsConn, serverHandler{c.server}} 1492 fn(c.server, tlsConn, h) 1493 } 1494 return 1495 } 1496 } 1497 1498 // HTTP/1.x from here on. 1499 1500 c.r = &connReader{r: c.rwc} 1501 c.bufr = newBufioReader(c.r) 1502 c.bufw = newBufioWriterSize(checkConnErrorWriter{c}, 4<<10) 1503 1504 ctx, cancelCtx := context.WithCancel(ctx) 1505 defer cancelCtx() 1506 1507 for { 1508 w, err := c.readRequest(ctx) 1509 if c.r.remain != c.server.initialReadLimitSize() { 1510 // If we read any bytes off the wire, we're active. 1511 c.setState(c.rwc, StateActive) 1512 } 1513 if err != nil { 1514 if err == errTooLarge { 1515 // Their HTTP client may or may not be 1516 // able to read this if we're 1517 // responding to them and hanging up 1518 // while they're still writing their 1519 // request. Undefined behavior. 1520 io.WriteString(c.rwc, "HTTP/1.1 431 Request Header Fields Too Large\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n431 Request Header Fields Too Large") 1521 c.closeWriteAndWait() 1522 return 1523 } 1524 if err == io.EOF { 1525 return // don't reply 1526 } 1527 if neterr, ok := err.(net.Error); ok && neterr.Timeout() { 1528 return // don't reply 1529 } 1530 var publicErr string 1531 if v, ok := err.(badRequestError); ok { 1532 publicErr = ": " + string(v) 1533 } 1534 io.WriteString(c.rwc, "HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n400 Bad Request"+publicErr) 1535 return 1536 } 1537 1538 // Expect 100 Continue support 1539 req := w.req 1540 if req.expectsContinue() { 1541 if req.ProtoAtLeast(1, 1) && req.ContentLength != 0 { 1542 // Wrap the Body reader with one that replies on the connection 1543 req.Body = &expectContinueReader{readCloser: req.Body, resp: w} 1544 } 1545 } else if req.Header.get("Expect") != "" { 1546 w.sendExpectationFailed() 1547 return 1548 } 1549 1550 // HTTP cannot have multiple simultaneous active requests.[*] 1551 // Until the server replies to this request, it can't read another, 1552 // so we might as well run the handler in this goroutine. 1553 // [*] Not strictly true: HTTP pipelining. We could let them all process 1554 // in parallel even if their responses need to be serialized. 1555 serverHandler{c.server}.ServeHTTP(w, w.req) 1556 w.cancelCtx() 1557 if c.hijacked() { 1558 return 1559 } 1560 w.finishRequest() 1561 if !w.shouldReuseConnection() { 1562 if w.requestBodyLimitHit || w.closedRequestBodyEarly() { 1563 c.closeWriteAndWait() 1564 } 1565 return 1566 } 1567 c.setState(c.rwc, StateIdle) 1568 } 1569 } 1570 1571 func (w *response) sendExpectationFailed() { 1572 // TODO(bradfitz): let ServeHTTP handlers handle 1573 // requests with non-standard expectation[s]? Seems 1574 // theoretical at best, and doesn't fit into the 1575 // current ServeHTTP model anyway. We'd need to 1576 // make the ResponseWriter an optional 1577 // "ExpectReplier" interface or something. 1578 // 1579 // For now we'll just obey RFC 2616 14.20 which says 1580 // "If a server receives a request containing an 1581 // Expect field that includes an expectation- 1582 // extension that it does not support, it MUST 1583 // respond with a 417 (Expectation Failed) status." 1584 w.Header().Set("Connection", "close") 1585 w.WriteHeader(StatusExpectationFailed) 1586 w.finishRequest() 1587 } 1588 1589 // Hijack implements the Hijacker.Hijack method. Our response is both a ResponseWriter 1590 // and a Hijacker. 1591 func (w *response) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) { 1592 if w.handlerDone.isSet() { 1593 panic("net/http: Hijack called after ServeHTTP finished") 1594 } 1595 if w.wroteHeader { 1596 w.cw.flush() 1597 } 1598 1599 c := w.conn 1600 c.mu.Lock() 1601 defer c.mu.Unlock() 1602 1603 if w.closeNotifyCh != nil { 1604 return nil, nil, errors.New("http: Hijack is incompatible with use of CloseNotifier in same ServeHTTP call") 1605 } 1606 1607 // Release the bufioWriter that writes to the chunk writer, it is not 1608 // used after a connection has been hijacked. 1609 rwc, buf, err = c.hijackLocked() 1610 if err == nil { 1611 putBufioWriter(w.w) 1612 w.w = nil 1613 } 1614 return rwc, buf, err 1615 } 1616 1617 func (w *response) CloseNotify() <-chan bool { 1618 if w.handlerDone.isSet() { 1619 panic("net/http: CloseNotify called after ServeHTTP finished") 1620 } 1621 c := w.conn 1622 c.mu.Lock() 1623 defer c.mu.Unlock() 1624 1625 if w.closeNotifyCh != nil { 1626 return w.closeNotifyCh 1627 } 1628 ch := make(chan bool, 1) 1629 w.closeNotifyCh = ch 1630 1631 if w.conn.hijackedv { 1632 // CloseNotify is undefined after a hijack, but we have 1633 // no place to return an error, so just return a channel, 1634 // even though it'll never receive a value. 1635 return ch 1636 } 1637 1638 var once sync.Once 1639 notify := func() { once.Do(func() { ch <- true }) } 1640 1641 if requestBodyRemains(w.reqBody) { 1642 // They're still consuming the request body, so we 1643 // shouldn't notify yet. 1644 registerOnHitEOF(w.reqBody, func() { 1645 c.mu.Lock() 1646 defer c.mu.Unlock() 1647 startCloseNotifyBackgroundRead(c, notify) 1648 }) 1649 } else { 1650 startCloseNotifyBackgroundRead(c, notify) 1651 } 1652 return ch 1653 } 1654 1655 // c.mu must be held. 1656 func startCloseNotifyBackgroundRead(c *conn, notify func()) { 1657 if c.bufr.Buffered() > 0 { 1658 // They've consumed the request body, so anything 1659 // remaining is a pipelined request, which we 1660 // document as firing on. 1661 notify() 1662 } else { 1663 c.r.startBackgroundRead(notify) 1664 } 1665 } 1666 1667 func registerOnHitEOF(rc io.ReadCloser, fn func()) { 1668 switch v := rc.(type) { 1669 case *expectContinueReader: 1670 registerOnHitEOF(v.readCloser, fn) 1671 case *body: 1672 v.registerOnHitEOF(fn) 1673 default: 1674 panic("unexpected type " + fmt.Sprintf("%T", rc)) 1675 } 1676 } 1677 1678 // requestBodyRemains reports whether future calls to Read 1679 // on rc might yield more data. 1680 func requestBodyRemains(rc io.ReadCloser) bool { 1681 if rc == eofReader { 1682 return false 1683 } 1684 switch v := rc.(type) { 1685 case *expectContinueReader: 1686 return requestBodyRemains(v.readCloser) 1687 case *body: 1688 return v.bodyRemains() 1689 default: 1690 panic("unexpected type " + fmt.Sprintf("%T", rc)) 1691 } 1692 } 1693 1694 // The HandlerFunc type is an adapter to allow the use of 1695 // ordinary functions as HTTP handlers. If f is a function 1696 // with the appropriate signature, HandlerFunc(f) is a 1697 // Handler that calls f. 1698 type HandlerFunc func(ResponseWriter, *Request) 1699 1700 // ServeHTTP calls f(w, r). 1701 func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { 1702 f(w, r) 1703 } 1704 1705 // Helper handlers 1706 1707 // Error replies to the request with the specified error message and HTTP code. 1708 // It does not otherwise end the request; the caller should ensure no further 1709 // writes are done to w. 1710 // The error message should be plain text. 1711 func Error(w ResponseWriter, error string, code int) { 1712 w.Header().Set("Content-Type", "text/plain; charset=utf-8") 1713 w.Header().Set("X-Content-Type-Options", "nosniff") 1714 w.WriteHeader(code) 1715 fmt.Fprintln(w, error) 1716 } 1717 1718 // NotFound replies to the request with an HTTP 404 not found error. 1719 func NotFound(w ResponseWriter, r *Request) { Error(w, "404 page not found", StatusNotFound) } 1720 1721 // NotFoundHandler returns a simple request handler 1722 // that replies to each request with a ``404 page not found'' reply. 1723 func NotFoundHandler() Handler { return HandlerFunc(NotFound) } 1724 1725 // StripPrefix returns a handler that serves HTTP requests 1726 // by removing the given prefix from the request URL's Path 1727 // and invoking the handler h. StripPrefix handles a 1728 // request for a path that doesn't begin with prefix by 1729 // replying with an HTTP 404 not found error. 1730 func StripPrefix(prefix string, h Handler) Handler { 1731 if prefix == "" { 1732 return h 1733 } 1734 return HandlerFunc(func(w ResponseWriter, r *Request) { 1735 if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) { 1736 r.URL.Path = p 1737 h.ServeHTTP(w, r) 1738 } else { 1739 NotFound(w, r) 1740 } 1741 }) 1742 } 1743 1744 // Redirect replies to the request with a redirect to url, 1745 // which may be a path relative to the request path. 1746 // 1747 // The provided code should be in the 3xx range and is usually 1748 // StatusMovedPermanently, StatusFound or StatusSeeOther. 1749 func Redirect(w ResponseWriter, r *Request, urlStr string, code int) { 1750 if u, err := url.Parse(urlStr); err == nil { 1751 // If url was relative, make absolute by 1752 // combining with request path. 1753 // The browser would probably do this for us, 1754 // but doing it ourselves is more reliable. 1755 1756 // NOTE(rsc): RFC 2616 says that the Location 1757 // line must be an absolute URI, like 1758 // "http://www.google.com/redirect/", 1759 // not a path like "/redirect/". 1760 // Unfortunately, we don't know what to 1761 // put in the host name section to get the 1762 // client to connect to us again, so we can't 1763 // know the right absolute URI to send back. 1764 // Because of this problem, no one pays attention 1765 // to the RFC; they all send back just a new path. 1766 // So do we. 1767 if u.Scheme == "" && u.Host == "" { 1768 oldpath := r.URL.Path 1769 if oldpath == "" { // should not happen, but avoid a crash if it does 1770 oldpath = "/" 1771 } 1772 1773 // no leading http://server 1774 if urlStr == "" || urlStr[0] != '/' { 1775 // make relative path absolute 1776 olddir, _ := path.Split(oldpath) 1777 urlStr = olddir + urlStr 1778 } 1779 1780 var query string 1781 if i := strings.Index(urlStr, "?"); i != -1 { 1782 urlStr, query = urlStr[:i], urlStr[i:] 1783 } 1784 1785 // clean up but preserve trailing slash 1786 trailing := strings.HasSuffix(urlStr, "/") 1787 urlStr = path.Clean(urlStr) 1788 if trailing && !strings.HasSuffix(urlStr, "/") { 1789 urlStr += "/" 1790 } 1791 urlStr += query 1792 } 1793 } 1794 1795 w.Header().Set("Location", urlStr) 1796 w.WriteHeader(code) 1797 1798 // RFC 2616 recommends that a short note "SHOULD" be included in the 1799 // response because older user agents may not understand 301/307. 1800 // Shouldn't send the response for POST or HEAD; that leaves GET. 1801 if r.Method == "GET" { 1802 note := "<a href=\"" + htmlEscape(urlStr) + "\">" + statusText[code] + "</a>.\n" 1803 fmt.Fprintln(w, note) 1804 } 1805 } 1806 1807 var htmlReplacer = strings.NewReplacer( 1808 "&", "&", 1809 "<", "<", 1810 ">", ">", 1811 // """ is shorter than """. 1812 `"`, """, 1813 // "'" is shorter than "'" and apos was not in HTML until HTML5. 1814 "'", "'", 1815 ) 1816 1817 func htmlEscape(s string) string { 1818 return htmlReplacer.Replace(s) 1819 } 1820 1821 // Redirect to a fixed URL 1822 type redirectHandler struct { 1823 url string 1824 code int 1825 } 1826 1827 func (rh *redirectHandler) ServeHTTP(w ResponseWriter, r *Request) { 1828 Redirect(w, r, rh.url, rh.code) 1829 } 1830 1831 // RedirectHandler returns a request handler that redirects 1832 // each request it receives to the given url using the given 1833 // status code. 1834 // 1835 // The provided code should be in the 3xx range and is usually 1836 // StatusMovedPermanently, StatusFound or StatusSeeOther. 1837 func RedirectHandler(url string, code int) Handler { 1838 return &redirectHandler{url, code} 1839 } 1840 1841 // ServeMux is an HTTP request multiplexer. 1842 // It matches the URL of each incoming request against a list of registered 1843 // patterns and calls the handler for the pattern that 1844 // most closely matches the URL. 1845 // 1846 // Patterns name fixed, rooted paths, like "/favicon.ico", 1847 // or rooted subtrees, like "/images/" (note the trailing slash). 1848 // Longer patterns take precedence over shorter ones, so that 1849 // if there are handlers registered for both "/images/" 1850 // and "/images/thumbnails/", the latter handler will be 1851 // called for paths beginning "/images/thumbnails/" and the 1852 // former will receive requests for any other paths in the 1853 // "/images/" subtree. 1854 // 1855 // Note that since a pattern ending in a slash names a rooted subtree, 1856 // the pattern "/" matches all paths not matched by other registered 1857 // patterns, not just the URL with Path == "/". 1858 // 1859 // If a subtree has been registered and a request is received naming the 1860 // subtree root without its trailing slash, ServeMux redirects that 1861 // request to the subtree root (adding the trailing slash). This behavior can 1862 // be overridden with a separate registration for the path without 1863 // the trailing slash. For example, registering "/images/" causes ServeMux 1864 // to redirect a request for "/images" to "/images/", unless "/images" has 1865 // been registered separately. 1866 // 1867 // Patterns may optionally begin with a host name, restricting matches to 1868 // URLs on that host only. Host-specific patterns take precedence over 1869 // general patterns, so that a handler might register for the two patterns 1870 // "/codesearch" and "codesearch.google.com/" without also taking over 1871 // requests for "http://www.google.com/". 1872 // 1873 // ServeMux also takes care of sanitizing the URL request path, 1874 // redirecting any request containing . or .. elements or repeated slashes 1875 // to an equivalent, cleaner URL. 1876 type ServeMux struct { 1877 mu sync.RWMutex 1878 m map[string]muxEntry 1879 hosts bool // whether any patterns contain hostnames 1880 } 1881 1882 type muxEntry struct { 1883 explicit bool 1884 h Handler 1885 pattern string 1886 } 1887 1888 // NewServeMux allocates and returns a new ServeMux. 1889 func NewServeMux() *ServeMux { return new(ServeMux) } 1890 1891 // DefaultServeMux is the default ServeMux used by Serve. 1892 var DefaultServeMux = &defaultServeMux 1893 1894 var defaultServeMux ServeMux 1895 1896 // Does path match pattern? 1897 func pathMatch(pattern, path string) bool { 1898 if len(pattern) == 0 { 1899 // should not happen 1900 return false 1901 } 1902 n := len(pattern) 1903 if pattern[n-1] != '/' { 1904 return pattern == path 1905 } 1906 return len(path) >= n && path[0:n] == pattern 1907 } 1908 1909 // Return the canonical path for p, eliminating . and .. elements. 1910 func cleanPath(p string) string { 1911 if p == "" { 1912 return "/" 1913 } 1914 if p[0] != '/' { 1915 p = "/" + p 1916 } 1917 np := path.Clean(p) 1918 // path.Clean removes trailing slash except for root; 1919 // put the trailing slash back if necessary. 1920 if p[len(p)-1] == '/' && np != "/" { 1921 np += "/" 1922 } 1923 return np 1924 } 1925 1926 // Find a handler on a handler map given a path string 1927 // Most-specific (longest) pattern wins 1928 func (mux *ServeMux) match(path string) (h Handler, pattern string) { 1929 var n = 0 1930 for k, v := range mux.m { 1931 if !pathMatch(k, path) { 1932 continue 1933 } 1934 if h == nil || len(k) > n { 1935 n = len(k) 1936 h = v.h 1937 pattern = v.pattern 1938 } 1939 } 1940 return 1941 } 1942 1943 // Handler returns the handler to use for the given request, 1944 // consulting r.Method, r.Host, and r.URL.Path. It always returns 1945 // a non-nil handler. If the path is not in its canonical form, the 1946 // handler will be an internally-generated handler that redirects 1947 // to the canonical path. 1948 // 1949 // Handler also returns the registered pattern that matches the 1950 // request or, in the case of internally-generated redirects, 1951 // the pattern that will match after following the redirect. 1952 // 1953 // If there is no registered handler that applies to the request, 1954 // Handler returns a ``page not found'' handler and an empty pattern. 1955 func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) { 1956 if r.Method != "CONNECT" { 1957 if p := cleanPath(r.URL.Path); p != r.URL.Path { 1958 _, pattern = mux.handler(r.Host, p) 1959 url := *r.URL 1960 url.Path = p 1961 return RedirectHandler(url.String(), StatusMovedPermanently), pattern 1962 } 1963 } 1964 1965 return mux.handler(r.Host, r.URL.Path) 1966 } 1967 1968 // handler is the main implementation of Handler. 1969 // The path is known to be in canonical form, except for CONNECT methods. 1970 func (mux *ServeMux) handler(host, path string) (h Handler, pattern string) { 1971 mux.mu.RLock() 1972 defer mux.mu.RUnlock() 1973 1974 // Host-specific pattern takes precedence over generic ones 1975 if mux.hosts { 1976 h, pattern = mux.match(host + path) 1977 } 1978 if h == nil { 1979 h, pattern = mux.match(path) 1980 } 1981 if h == nil { 1982 h, pattern = NotFoundHandler(), "" 1983 } 1984 return 1985 } 1986 1987 // ServeHTTP dispatches the request to the handler whose 1988 // pattern most closely matches the request URL. 1989 func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) { 1990 if r.RequestURI == "*" { 1991 if r.ProtoAtLeast(1, 1) { 1992 w.Header().Set("Connection", "close") 1993 } 1994 w.WriteHeader(StatusBadRequest) 1995 return 1996 } 1997 h, _ := mux.Handler(r) 1998 h.ServeHTTP(w, r) 1999 } 2000 2001 // Handle registers the handler for the given pattern. 2002 // If a handler already exists for pattern, Handle panics. 2003 func (mux *ServeMux) Handle(pattern string, handler Handler) { 2004 mux.mu.Lock() 2005 defer mux.mu.Unlock() 2006 2007 if pattern == "" { 2008 panic("http: invalid pattern " + pattern) 2009 } 2010 if handler == nil { 2011 panic("http: nil handler") 2012 } 2013 if mux.m[pattern].explicit { 2014 panic("http: multiple registrations for " + pattern) 2015 } 2016 2017 if mux.m == nil { 2018 mux.m = make(map[string]muxEntry) 2019 } 2020 mux.m[pattern] = muxEntry{explicit: true, h: handler, pattern: pattern} 2021 2022 if pattern[0] != '/' { 2023 mux.hosts = true 2024 } 2025 2026 // Helpful behavior: 2027 // If pattern is /tree/, insert an implicit permanent redirect for /tree. 2028 // It can be overridden by an explicit registration. 2029 n := len(pattern) 2030 if n > 0 && pattern[n-1] == '/' && !mux.m[pattern[0:n-1]].explicit { 2031 // If pattern contains a host name, strip it and use remaining 2032 // path for redirect. 2033 path := pattern 2034 if pattern[0] != '/' { 2035 // In pattern, at least the last character is a '/', so 2036 // strings.Index can't be -1. 2037 path = pattern[strings.Index(pattern, "/"):] 2038 } 2039 url := &url.URL{Path: path} 2040 mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(url.String(), StatusMovedPermanently), pattern: pattern} 2041 } 2042 } 2043 2044 // HandleFunc registers the handler function for the given pattern. 2045 func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { 2046 mux.Handle(pattern, HandlerFunc(handler)) 2047 } 2048 2049 // Handle registers the handler for the given pattern 2050 // in the DefaultServeMux. 2051 // The documentation for ServeMux explains how patterns are matched. 2052 func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) } 2053 2054 // HandleFunc registers the handler function for the given pattern 2055 // in the DefaultServeMux. 2056 // The documentation for ServeMux explains how patterns are matched. 2057 func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { 2058 DefaultServeMux.HandleFunc(pattern, handler) 2059 } 2060 2061 // Serve accepts incoming HTTP connections on the listener l, 2062 // creating a new service goroutine for each. The service goroutines 2063 // read requests and then call handler to reply to them. 2064 // Handler is typically nil, in which case the DefaultServeMux is used. 2065 func Serve(l net.Listener, handler Handler) error { 2066 srv := &Server{Handler: handler} 2067 return srv.Serve(l) 2068 } 2069 2070 // A Server defines parameters for running an HTTP server. 2071 // The zero value for Server is a valid configuration. 2072 type Server struct { 2073 Addr string // TCP address to listen on, ":http" if empty 2074 Handler Handler // handler to invoke, http.DefaultServeMux if nil 2075 ReadTimeout time.Duration // maximum duration before timing out read of the request 2076 WriteTimeout time.Duration // maximum duration before timing out write of the response 2077 TLSConfig *tls.Config // optional TLS config, used by ListenAndServeTLS 2078 2079 // MaxHeaderBytes controls the maximum number of bytes the 2080 // server will read parsing the request header's keys and 2081 // values, including the request line. It does not limit the 2082 // size of the request body. 2083 // If zero, DefaultMaxHeaderBytes is used. 2084 MaxHeaderBytes int 2085 2086 // TLSNextProto optionally specifies a function to take over 2087 // ownership of the provided TLS connection when an NPN/ALPN 2088 // protocol upgrade has occurred. The map key is the protocol 2089 // name negotiated. The Handler argument should be used to 2090 // handle HTTP requests and will initialize the Request's TLS 2091 // and RemoteAddr if not already set. The connection is 2092 // automatically closed when the function returns. 2093 // If TLSNextProto is nil, HTTP/2 support is enabled automatically. 2094 TLSNextProto map[string]func(*Server, *tls.Conn, Handler) 2095 2096 // ConnState specifies an optional callback function that is 2097 // called when a client connection changes state. See the 2098 // ConnState type and associated constants for details. 2099 ConnState func(net.Conn, ConnState) 2100 2101 // ErrorLog specifies an optional logger for errors accepting 2102 // connections and unexpected behavior from handlers. 2103 // If nil, logging goes to os.Stderr via the log package's 2104 // standard logger. 2105 ErrorLog *log.Logger 2106 2107 disableKeepAlives int32 // accessed atomically. 2108 nextProtoOnce sync.Once // guards initialization of TLSNextProto in Serve 2109 nextProtoErr error 2110 } 2111 2112 // A ConnState represents the state of a client connection to a server. 2113 // It's used by the optional Server.ConnState hook. 2114 type ConnState int 2115 2116 const ( 2117 // StateNew represents a new connection that is expected to 2118 // send a request immediately. Connections begin at this 2119 // state and then transition to either StateActive or 2120 // StateClosed. 2121 StateNew ConnState = iota 2122 2123 // StateActive represents a connection that has read 1 or more 2124 // bytes of a request. The Server.ConnState hook for 2125 // StateActive fires before the request has entered a handler 2126 // and doesn't fire again until the request has been 2127 // handled. After the request is handled, the state 2128 // transitions to StateClosed, StateHijacked, or StateIdle. 2129 // For HTTP/2, StateActive fires on the transition from zero 2130 // to one active request, and only transitions away once all 2131 // active requests are complete. That means that ConnState 2132 // cannot be used to do per-request work; ConnState only notes 2133 // the overall state of the connection. 2134 StateActive 2135 2136 // StateIdle represents a connection that has finished 2137 // handling a request and is in the keep-alive state, waiting 2138 // for a new request. Connections transition from StateIdle 2139 // to either StateActive or StateClosed. 2140 StateIdle 2141 2142 // StateHijacked represents a hijacked connection. 2143 // This is a terminal state. It does not transition to StateClosed. 2144 StateHijacked 2145 2146 // StateClosed represents a closed connection. 2147 // This is a terminal state. Hijacked connections do not 2148 // transition to StateClosed. 2149 StateClosed 2150 ) 2151 2152 var stateName = map[ConnState]string{ 2153 StateNew: "new", 2154 StateActive: "active", 2155 StateIdle: "idle", 2156 StateHijacked: "hijacked", 2157 StateClosed: "closed", 2158 } 2159 2160 func (c ConnState) String() string { 2161 return stateName[c] 2162 } 2163 2164 // serverHandler delegates to either the server's Handler or 2165 // DefaultServeMux and also handles "OPTIONS *" requests. 2166 type serverHandler struct { 2167 srv *Server 2168 } 2169 2170 func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) { 2171 handler := sh.srv.Handler 2172 if handler == nil { 2173 handler = DefaultServeMux 2174 } 2175 if req.RequestURI == "*" && req.Method == "OPTIONS" { 2176 handler = globalOptionsHandler{} 2177 } 2178 handler.ServeHTTP(rw, req) 2179 } 2180 2181 // ListenAndServe listens on the TCP network address srv.Addr and then 2182 // calls Serve to handle requests on incoming connections. 2183 // Accepted connections are configured to enable TCP keep-alives. 2184 // If srv.Addr is blank, ":http" is used. 2185 // ListenAndServe always returns a non-nil error. 2186 func (srv *Server) ListenAndServe() error { 2187 addr := srv.Addr 2188 if addr == "" { 2189 addr = ":http" 2190 } 2191 ln, err := net.Listen("tcp", addr) 2192 if err != nil { 2193 return err 2194 } 2195 return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}) 2196 } 2197 2198 var testHookServerServe func(*Server, net.Listener) // used if non-nil 2199 2200 // Serve accepts incoming connections on the Listener l, creating a 2201 // new service goroutine for each. The service goroutines read requests and 2202 // then call srv.Handler to reply to them. 2203 // Serve always returns a non-nil error. 2204 func (srv *Server) Serve(l net.Listener) error { 2205 defer l.Close() 2206 if fn := testHookServerServe; fn != nil { 2207 fn(srv, l) 2208 } 2209 var tempDelay time.Duration // how long to sleep on accept failure 2210 if err := srv.setupHTTP2(); err != nil { 2211 return err 2212 } 2213 // TODO: allow changing base context? can't imagine concrete 2214 // use cases yet. 2215 baseCtx := context.Background() 2216 ctx := context.WithValue(baseCtx, ServerContextKey, srv) 2217 ctx = context.WithValue(ctx, LocalAddrContextKey, l.Addr()) 2218 for { 2219 rw, e := l.Accept() 2220 if e != nil { 2221 if ne, ok := e.(net.Error); ok && ne.Temporary() { 2222 if tempDelay == 0 { 2223 tempDelay = 5 * time.Millisecond 2224 } else { 2225 tempDelay *= 2 2226 } 2227 if max := 1 * time.Second; tempDelay > max { 2228 tempDelay = max 2229 } 2230 srv.logf("http: Accept error: %v; retrying in %v", e, tempDelay) 2231 time.Sleep(tempDelay) 2232 continue 2233 } 2234 return e 2235 } 2236 tempDelay = 0 2237 c := srv.newConn(rw) 2238 c.setState(c.rwc, StateNew) // before Serve can return 2239 go c.serve(ctx) 2240 } 2241 } 2242 2243 func (s *Server) doKeepAlives() bool { 2244 return atomic.LoadInt32(&s.disableKeepAlives) == 0 2245 } 2246 2247 // SetKeepAlivesEnabled controls whether HTTP keep-alives are enabled. 2248 // By default, keep-alives are always enabled. Only very 2249 // resource-constrained environments or servers in the process of 2250 // shutting down should disable them. 2251 func (srv *Server) SetKeepAlivesEnabled(v bool) { 2252 if v { 2253 atomic.StoreInt32(&srv.disableKeepAlives, 0) 2254 } else { 2255 atomic.StoreInt32(&srv.disableKeepAlives, 1) 2256 } 2257 } 2258 2259 func (s *Server) logf(format string, args ...interface{}) { 2260 if s.ErrorLog != nil { 2261 s.ErrorLog.Printf(format, args...) 2262 } else { 2263 log.Printf(format, args...) 2264 } 2265 } 2266 2267 // ListenAndServe listens on the TCP network address addr 2268 // and then calls Serve with handler to handle requests 2269 // on incoming connections. 2270 // Accepted connections are configured to enable TCP keep-alives. 2271 // Handler is typically nil, in which case the DefaultServeMux is 2272 // used. 2273 // 2274 // A trivial example server is: 2275 // 2276 // package main 2277 // 2278 // import ( 2279 // "io" 2280 // "net/http" 2281 // "log" 2282 // ) 2283 // 2284 // // hello world, the web server 2285 // func HelloServer(w http.ResponseWriter, req *http.Request) { 2286 // io.WriteString(w, "hello, world!\n") 2287 // } 2288 // 2289 // func main() { 2290 // http.HandleFunc("/hello", HelloServer) 2291 // log.Fatal(http.ListenAndServe(":12345", nil)) 2292 // } 2293 // 2294 // ListenAndServe always returns a non-nil error. 2295 func ListenAndServe(addr string, handler Handler) error { 2296 server := &Server{Addr: addr, Handler: handler} 2297 return server.ListenAndServe() 2298 } 2299 2300 // ListenAndServeTLS acts identically to ListenAndServe, except that it 2301 // expects HTTPS connections. Additionally, files containing a certificate and 2302 // matching private key for the server must be provided. If the certificate 2303 // is signed by a certificate authority, the certFile should be the concatenation 2304 // of the server's certificate, any intermediates, and the CA's certificate. 2305 // 2306 // A trivial example server is: 2307 // 2308 // import ( 2309 // "log" 2310 // "net/http" 2311 // ) 2312 // 2313 // func handler(w http.ResponseWriter, req *http.Request) { 2314 // w.Header().Set("Content-Type", "text/plain") 2315 // w.Write([]byte("This is an example server.\n")) 2316 // } 2317 // 2318 // func main() { 2319 // http.HandleFunc("/", handler) 2320 // log.Printf("About to listen on 10443. Go to https://127.0.0.1:10443/") 2321 // err := http.ListenAndServeTLS(":10443", "cert.pem", "key.pem", nil) 2322 // log.Fatal(err) 2323 // } 2324 // 2325 // One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem. 2326 // 2327 // ListenAndServeTLS always returns a non-nil error. 2328 func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error { 2329 server := &Server{Addr: addr, Handler: handler} 2330 return server.ListenAndServeTLS(certFile, keyFile) 2331 } 2332 2333 // ListenAndServeTLS listens on the TCP network address srv.Addr and 2334 // then calls Serve to handle requests on incoming TLS connections. 2335 // Accepted connections are configured to enable TCP keep-alives. 2336 // 2337 // Filenames containing a certificate and matching private key for the 2338 // server must be provided if neither the Server's TLSConfig.Certificates 2339 // nor TLSConfig.GetCertificate are populated. If the certificate is 2340 // signed by a certificate authority, the certFile should be the 2341 // concatenation of the server's certificate, any intermediates, and 2342 // the CA's certificate. 2343 // 2344 // If srv.Addr is blank, ":https" is used. 2345 // 2346 // ListenAndServeTLS always returns a non-nil error. 2347 func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error { 2348 addr := srv.Addr 2349 if addr == "" { 2350 addr = ":https" 2351 } 2352 2353 // Setup HTTP/2 before srv.Serve, to initialize srv.TLSConfig 2354 // before we clone it and create the TLS Listener. 2355 if err := srv.setupHTTP2(); err != nil { 2356 return err 2357 } 2358 2359 config := cloneTLSConfig(srv.TLSConfig) 2360 if !strSliceContains(config.NextProtos, "http/1.1") { 2361 config.NextProtos = append(config.NextProtos, "http/1.1") 2362 } 2363 2364 configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil 2365 if !configHasCert || certFile != "" || keyFile != "" { 2366 var err error 2367 config.Certificates = make([]tls.Certificate, 1) 2368 config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile) 2369 if err != nil { 2370 return err 2371 } 2372 } 2373 2374 ln, err := net.Listen("tcp", addr) 2375 if err != nil { 2376 return err 2377 } 2378 2379 tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, config) 2380 return srv.Serve(tlsListener) 2381 } 2382 2383 func (srv *Server) setupHTTP2() error { 2384 srv.nextProtoOnce.Do(srv.onceSetNextProtoDefaults) 2385 return srv.nextProtoErr 2386 } 2387 2388 // onceSetNextProtoDefaults configures HTTP/2, if the user hasn't 2389 // configured otherwise. (by setting srv.TLSNextProto non-nil) 2390 // It must only be called via srv.nextProtoOnce (use srv.setupHTTP2). 2391 func (srv *Server) onceSetNextProtoDefaults() { 2392 if strings.Contains(os.Getenv("GODEBUG"), "http2server=0") { 2393 return 2394 } 2395 // Enable HTTP/2 by default if the user hasn't otherwise 2396 // configured their TLSNextProto map. 2397 if srv.TLSNextProto == nil { 2398 srv.nextProtoErr = http2ConfigureServer(srv, nil) 2399 } 2400 } 2401 2402 // TimeoutHandler returns a Handler that runs h with the given time limit. 2403 // 2404 // The new Handler calls h.ServeHTTP to handle each request, but if a 2405 // call runs for longer than its time limit, the handler responds with 2406 // a 503 Service Unavailable error and the given message in its body. 2407 // (If msg is empty, a suitable default message will be sent.) 2408 // After such a timeout, writes by h to its ResponseWriter will return 2409 // ErrHandlerTimeout. 2410 // 2411 // TimeoutHandler buffers all Handler writes to memory and does not 2412 // support the Hijacker or Flusher interfaces. 2413 func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler { 2414 return &timeoutHandler{ 2415 handler: h, 2416 body: msg, 2417 dt: dt, 2418 } 2419 } 2420 2421 // ErrHandlerTimeout is returned on ResponseWriter Write calls 2422 // in handlers which have timed out. 2423 var ErrHandlerTimeout = errors.New("http: Handler timeout") 2424 2425 type timeoutHandler struct { 2426 handler Handler 2427 body string 2428 dt time.Duration 2429 2430 // When set, no timer will be created and this channel will 2431 // be used instead. 2432 testTimeout <-chan time.Time 2433 } 2434 2435 func (h *timeoutHandler) errorBody() string { 2436 if h.body != "" { 2437 return h.body 2438 } 2439 return "<html><head><title>Timeout</title></head><body><h1>Timeout</h1></body></html>" 2440 } 2441 2442 func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) { 2443 var t *time.Timer 2444 timeout := h.testTimeout 2445 if timeout == nil { 2446 t = time.NewTimer(h.dt) 2447 timeout = t.C 2448 } 2449 done := make(chan struct{}) 2450 tw := &timeoutWriter{ 2451 w: w, 2452 h: make(Header), 2453 } 2454 go func() { 2455 h.handler.ServeHTTP(tw, r) 2456 close(done) 2457 }() 2458 select { 2459 case <-done: 2460 tw.mu.Lock() 2461 defer tw.mu.Unlock() 2462 dst := w.Header() 2463 for k, vv := range tw.h { 2464 dst[k] = vv 2465 } 2466 w.WriteHeader(tw.code) 2467 w.Write(tw.wbuf.Bytes()) 2468 if t != nil { 2469 t.Stop() 2470 } 2471 case <-timeout: 2472 tw.mu.Lock() 2473 defer tw.mu.Unlock() 2474 w.WriteHeader(StatusServiceUnavailable) 2475 io.WriteString(w, h.errorBody()) 2476 tw.timedOut = true 2477 return 2478 } 2479 } 2480 2481 type timeoutWriter struct { 2482 w ResponseWriter 2483 h Header 2484 wbuf bytes.Buffer 2485 2486 mu sync.Mutex 2487 timedOut bool 2488 wroteHeader bool 2489 code int 2490 } 2491 2492 func (tw *timeoutWriter) Header() Header { return tw.h } 2493 2494 func (tw *timeoutWriter) Write(p []byte) (int, error) { 2495 tw.mu.Lock() 2496 defer tw.mu.Unlock() 2497 if tw.timedOut { 2498 return 0, ErrHandlerTimeout 2499 } 2500 if !tw.wroteHeader { 2501 tw.writeHeader(StatusOK) 2502 } 2503 return tw.wbuf.Write(p) 2504 } 2505 2506 func (tw *timeoutWriter) WriteHeader(code int) { 2507 tw.mu.Lock() 2508 defer tw.mu.Unlock() 2509 if tw.timedOut || tw.wroteHeader { 2510 return 2511 } 2512 tw.writeHeader(code) 2513 } 2514 2515 func (tw *timeoutWriter) writeHeader(code int) { 2516 tw.wroteHeader = true 2517 tw.code = code 2518 } 2519 2520 // tcpKeepAliveListener sets TCP keep-alive timeouts on accepted 2521 // connections. It's used by ListenAndServe and ListenAndServeTLS so 2522 // dead TCP connections (e.g. closing laptop mid-download) eventually 2523 // go away. 2524 type tcpKeepAliveListener struct { 2525 *net.TCPListener 2526 } 2527 2528 func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) { 2529 tc, err := ln.AcceptTCP() 2530 if err != nil { 2531 return 2532 } 2533 tc.SetKeepAlive(true) 2534 tc.SetKeepAlivePeriod(3 * time.Minute) 2535 return tc, nil 2536 } 2537 2538 // globalOptionsHandler responds to "OPTIONS *" requests. 2539 type globalOptionsHandler struct{} 2540 2541 func (globalOptionsHandler) ServeHTTP(w ResponseWriter, r *Request) { 2542 w.Header().Set("Content-Length", "0") 2543 if r.ContentLength != 0 { 2544 // Read up to 4KB of OPTIONS body (as mentioned in the 2545 // spec as being reserved for future use), but anything 2546 // over that is considered a waste of server resources 2547 // (or an attack) and we abort and close the connection, 2548 // courtesy of MaxBytesReader's EOF behavior. 2549 mb := MaxBytesReader(w, r.Body, 4<<10) 2550 io.Copy(ioutil.Discard, mb) 2551 } 2552 } 2553 2554 type eofReaderWithWriteTo struct{} 2555 2556 func (eofReaderWithWriteTo) WriteTo(io.Writer) (int64, error) { return 0, nil } 2557 func (eofReaderWithWriteTo) Read([]byte) (int, error) { return 0, io.EOF } 2558 2559 // eofReader is a non-nil io.ReadCloser that always returns EOF. 2560 // It has a WriteTo method so io.Copy won't need a buffer. 2561 var eofReader = &struct { 2562 eofReaderWithWriteTo 2563 io.Closer 2564 }{ 2565 eofReaderWithWriteTo{}, 2566 ioutil.NopCloser(nil), 2567 } 2568 2569 // Verify that an io.Copy from an eofReader won't require a buffer. 2570 var _ io.WriterTo = eofReader 2571 2572 // initNPNRequest is an HTTP handler that initializes certain 2573 // uninitialized fields in its *Request. Such partially-initialized 2574 // Requests come from NPN protocol handlers. 2575 type initNPNRequest struct { 2576 c *tls.Conn 2577 h serverHandler 2578 } 2579 2580 func (h initNPNRequest) ServeHTTP(rw ResponseWriter, req *Request) { 2581 if req.TLS == nil { 2582 req.TLS = &tls.ConnectionState{} 2583 *req.TLS = h.c.ConnectionState() 2584 } 2585 if req.Body == nil { 2586 req.Body = eofReader 2587 } 2588 if req.RemoteAddr == "" { 2589 req.RemoteAddr = h.c.RemoteAddr().String() 2590 } 2591 h.h.ServeHTTP(rw, req) 2592 } 2593 2594 // loggingConn is used for debugging. 2595 type loggingConn struct { 2596 name string 2597 net.Conn 2598 } 2599 2600 var ( 2601 uniqNameMu sync.Mutex 2602 uniqNameNext = make(map[string]int) 2603 ) 2604 2605 func newLoggingConn(baseName string, c net.Conn) net.Conn { 2606 uniqNameMu.Lock() 2607 defer uniqNameMu.Unlock() 2608 uniqNameNext[baseName]++ 2609 return &loggingConn{ 2610 name: fmt.Sprintf("%s-%d", baseName, uniqNameNext[baseName]), 2611 Conn: c, 2612 } 2613 } 2614 2615 func (c *loggingConn) Write(p []byte) (n int, err error) { 2616 log.Printf("%s.Write(%d) = ....", c.name, len(p)) 2617 n, err = c.Conn.Write(p) 2618 log.Printf("%s.Write(%d) = %d, %v", c.name, len(p), n, err) 2619 return 2620 } 2621 2622 func (c *loggingConn) Read(p []byte) (n int, err error) { 2623 log.Printf("%s.Read(%d) = ....", c.name, len(p)) 2624 n, err = c.Conn.Read(p) 2625 log.Printf("%s.Read(%d) = %d, %v", c.name, len(p), n, err) 2626 return 2627 } 2628 2629 func (c *loggingConn) Close() (err error) { 2630 log.Printf("%s.Close() = ...", c.name) 2631 err = c.Conn.Close() 2632 log.Printf("%s.Close() = %v", c.name, err) 2633 return 2634 } 2635 2636 // checkConnErrorWriter writes to c.rwc and records any write errors to c.werr. 2637 // It only contains one field (and a pointer field at that), so it 2638 // fits in an interface value without an extra allocation. 2639 type checkConnErrorWriter struct { 2640 c *conn 2641 } 2642 2643 func (w checkConnErrorWriter) Write(p []byte) (n int, err error) { 2644 n, err = w.c.rwc.Write(p) 2645 if err != nil && w.c.werr == nil { 2646 w.c.werr = err 2647 } 2648 return 2649 } 2650 2651 func numLeadingCRorLF(v []byte) (n int) { 2652 for _, b := range v { 2653 if b == '\r' || b == '\n' { 2654 n++ 2655 continue 2656 } 2657 break 2658 } 2659 return 2660 2661 } 2662 2663 func strSliceContains(ss []string, s string) bool { 2664 for _, v := range ss { 2665 if v == s { 2666 return true 2667 } 2668 } 2669 return false 2670 }