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