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