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