PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/third_party/gofrontend/libgo/go/net/http/server.go

http://github.com/axw/llgo
Go | 2267 lines | 1413 code | 242 blank | 612 comment | 410 complexity | ea7a42909831ee0803b0064e15f65305 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT

Large files files are truncated, but you can click here to view the full file

  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. // HTTP server. See RFC 2616.
  5. package http
  6. import (
  7. "bufio"
  8. "crypto/tls"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "io/ioutil"
  13. "log"
  14. "net"
  15. "net/textproto"
  16. "net/url"
  17. "os"
  18. "path"
  19. "runtime"
  20. "strconv"
  21. "strings"
  22. "sync"
  23. "sync/atomic"
  24. "time"
  25. )
  26. // Errors introduced by the HTTP server.
  27. var (
  28. ErrWriteAfterFlush = errors.New("Conn.Write called after Flush")
  29. ErrBodyNotAllowed = errors.New("http: request method or response status code does not allow body")
  30. ErrHijacked = errors.New("Conn has been hijacked")
  31. ErrContentLength = errors.New("Conn.Write wrote more than the declared Content-Length")
  32. )
  33. // Objects implementing the Handler interface can be
  34. // registered to serve a particular path or subtree
  35. // in the HTTP server.
  36. //
  37. // ServeHTTP should write reply headers and data to the ResponseWriter
  38. // and then return. Returning signals that the request is finished
  39. // and that the HTTP server can move on to the next request on
  40. // the connection.
  41. //
  42. // If ServeHTTP panics, the server (the caller of ServeHTTP) assumes
  43. // that the effect of the panic was isolated to the active request.
  44. // It recovers the panic, logs a stack trace to the server error log,
  45. // and hangs up the connection.
  46. //
  47. type Handler interface {
  48. ServeHTTP(ResponseWriter, *Request)
  49. }
  50. // A ResponseWriter interface is used by an HTTP handler to
  51. // construct an HTTP response.
  52. type ResponseWriter interface {
  53. // Header returns the header map that will be sent by
  54. // WriteHeader. Changing the header after a call to
  55. // WriteHeader (or Write) has no effect unless the modified
  56. // headers were declared as trailers by setting the
  57. // "Trailer" header before the call to WriteHeader (see example).
  58. // To suppress implicit response headers, set their value to nil.
  59. Header() Header
  60. // Write writes the data to the connection as part of an HTTP reply.
  61. // If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)
  62. // before writing the data. If the Header does not contain a
  63. // Content-Type line, Write adds a Content-Type set to the result of passing
  64. // the initial 512 bytes of written data to DetectContentType.
  65. Write([]byte) (int, error)
  66. // WriteHeader sends an HTTP response header with status code.
  67. // If WriteHeader is not called explicitly, the first call to Write
  68. // will trigger an implicit WriteHeader(http.StatusOK).
  69. // Thus explicit calls to WriteHeader are mainly used to
  70. // send error codes.
  71. WriteHeader(int)
  72. }
  73. // The Flusher interface is implemented by ResponseWriters that allow
  74. // an HTTP handler to flush buffered data to the client.
  75. //
  76. // Note that even for ResponseWriters that support Flush,
  77. // if the client is connected through an HTTP proxy,
  78. // the buffered data may not reach the client until the response
  79. // completes.
  80. type Flusher interface {
  81. // Flush sends any buffered data to the client.
  82. Flush()
  83. }
  84. // The Hijacker interface is implemented by ResponseWriters that allow
  85. // an HTTP handler to take over the connection.
  86. type Hijacker interface {
  87. // Hijack lets the caller take over the connection.
  88. // After a call to Hijack(), the HTTP server library
  89. // will not do anything else with the connection.
  90. //
  91. // It becomes the caller's responsibility to manage
  92. // and close the connection.
  93. //
  94. // The returned net.Conn may have read or write deadlines
  95. // already set, depending on the configuration of the
  96. // Server. It is the caller's responsibility to set
  97. // or clear those deadlines as needed.
  98. Hijack() (net.Conn, *bufio.ReadWriter, error)
  99. }
  100. // The CloseNotifier interface is implemented by ResponseWriters which
  101. // allow detecting when the underlying connection has gone away.
  102. //
  103. // This mechanism can be used to cancel long operations on the server
  104. // if the client has disconnected before the response is ready.
  105. type CloseNotifier interface {
  106. // CloseNotify returns a channel that receives a single value
  107. // when the client connection has gone away.
  108. CloseNotify() <-chan bool
  109. }
  110. // A conn represents the server side of an HTTP connection.
  111. type conn struct {
  112. remoteAddr string // network address of remote side
  113. server *Server // the Server on which the connection arrived
  114. rwc net.Conn // i/o connection
  115. w io.Writer // checkConnErrorWriter's copy of wrc, not zeroed on Hijack
  116. werr error // any errors writing to w
  117. sr liveSwitchReader // where the LimitReader reads from; usually the rwc
  118. lr *io.LimitedReader // io.LimitReader(sr)
  119. buf *bufio.ReadWriter // buffered(lr,rwc), reading from bufio->limitReader->sr->rwc
  120. tlsState *tls.ConnectionState // or nil when not using TLS
  121. lastMethod string // method of previous request, or ""
  122. mu sync.Mutex // guards the following
  123. clientGone bool // if client has disconnected mid-request
  124. closeNotifyc chan bool // made lazily
  125. hijackedv bool // connection has been hijacked by handler
  126. }
  127. func (c *conn) hijacked() bool {
  128. c.mu.Lock()
  129. defer c.mu.Unlock()
  130. return c.hijackedv
  131. }
  132. func (c *conn) hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
  133. c.mu.Lock()
  134. defer c.mu.Unlock()
  135. if c.hijackedv {
  136. return nil, nil, ErrHijacked
  137. }
  138. if c.closeNotifyc != nil {
  139. return nil, nil, errors.New("http: Hijack is incompatible with use of CloseNotifier")
  140. }
  141. c.hijackedv = true
  142. rwc = c.rwc
  143. buf = c.buf
  144. c.rwc = nil
  145. c.buf = nil
  146. c.setState(rwc, StateHijacked)
  147. return
  148. }
  149. func (c *conn) closeNotify() <-chan bool {
  150. c.mu.Lock()
  151. defer c.mu.Unlock()
  152. if c.closeNotifyc == nil {
  153. c.closeNotifyc = make(chan bool, 1)
  154. if c.hijackedv {
  155. // to obey the function signature, even though
  156. // it'll never receive a value.
  157. return c.closeNotifyc
  158. }
  159. pr, pw := io.Pipe()
  160. readSource := c.sr.r
  161. c.sr.Lock()
  162. c.sr.r = pr
  163. c.sr.Unlock()
  164. go func() {
  165. _, err := io.Copy(pw, readSource)
  166. if err == nil {
  167. err = io.EOF
  168. }
  169. pw.CloseWithError(err)
  170. c.noteClientGone()
  171. }()
  172. }
  173. return c.closeNotifyc
  174. }
  175. func (c *conn) noteClientGone() {
  176. c.mu.Lock()
  177. defer c.mu.Unlock()
  178. if c.closeNotifyc != nil && !c.clientGone {
  179. c.closeNotifyc <- true
  180. }
  181. c.clientGone = true
  182. }
  183. // A switchWriter can have its Writer changed at runtime.
  184. // It's not safe for concurrent Writes and switches.
  185. type switchWriter struct {
  186. io.Writer
  187. }
  188. // A liveSwitchReader can have its Reader changed at runtime. It's
  189. // safe for concurrent reads and switches, if its mutex is held.
  190. type liveSwitchReader struct {
  191. sync.Mutex
  192. r io.Reader
  193. }
  194. func (sr *liveSwitchReader) Read(p []byte) (n int, err error) {
  195. sr.Lock()
  196. r := sr.r
  197. sr.Unlock()
  198. return r.Read(p)
  199. }
  200. // This should be >= 512 bytes for DetectContentType,
  201. // but otherwise it's somewhat arbitrary.
  202. const bufferBeforeChunkingSize = 2048
  203. // chunkWriter writes to a response's conn buffer, and is the writer
  204. // wrapped by the response.bufw buffered writer.
  205. //
  206. // chunkWriter also is responsible for finalizing the Header, including
  207. // conditionally setting the Content-Type and setting a Content-Length
  208. // in cases where the handler's final output is smaller than the buffer
  209. // size. It also conditionally adds chunk headers, when in chunking mode.
  210. //
  211. // See the comment above (*response).Write for the entire write flow.
  212. type chunkWriter struct {
  213. res *response
  214. // header is either nil or a deep clone of res.handlerHeader
  215. // at the time of res.WriteHeader, if res.WriteHeader is
  216. // called and extra buffering is being done to calculate
  217. // Content-Type and/or Content-Length.
  218. header Header
  219. // wroteHeader tells whether the header's been written to "the
  220. // wire" (or rather: w.conn.buf). this is unlike
  221. // (*response).wroteHeader, which tells only whether it was
  222. // logically written.
  223. wroteHeader bool
  224. // set by the writeHeader method:
  225. chunking bool // using chunked transfer encoding for reply body
  226. }
  227. var (
  228. crlf = []byte("\r\n")
  229. colonSpace = []byte(": ")
  230. )
  231. func (cw *chunkWriter) Write(p []byte) (n int, err error) {
  232. if !cw.wroteHeader {
  233. cw.writeHeader(p)
  234. }
  235. if cw.res.req.Method == "HEAD" {
  236. // Eat writes.
  237. return len(p), nil
  238. }
  239. if cw.chunking {
  240. _, err = fmt.Fprintf(cw.res.conn.buf, "%x\r\n", len(p))
  241. if err != nil {
  242. cw.res.conn.rwc.Close()
  243. return
  244. }
  245. }
  246. n, err = cw.res.conn.buf.Write(p)
  247. if cw.chunking && err == nil {
  248. _, err = cw.res.conn.buf.Write(crlf)
  249. }
  250. if err != nil {
  251. cw.res.conn.rwc.Close()
  252. }
  253. return
  254. }
  255. func (cw *chunkWriter) flush() {
  256. if !cw.wroteHeader {
  257. cw.writeHeader(nil)
  258. }
  259. cw.res.conn.buf.Flush()
  260. }
  261. func (cw *chunkWriter) close() {
  262. if !cw.wroteHeader {
  263. cw.writeHeader(nil)
  264. }
  265. if cw.chunking {
  266. bw := cw.res.conn.buf // conn's bufio writer
  267. // zero chunk to mark EOF
  268. bw.WriteString("0\r\n")
  269. if len(cw.res.trailers) > 0 {
  270. trailers := make(Header)
  271. for _, h := range cw.res.trailers {
  272. if vv := cw.res.handlerHeader[h]; len(vv) > 0 {
  273. trailers[h] = vv
  274. }
  275. }
  276. trailers.Write(bw) // the writer handles noting errors
  277. }
  278. // final blank line after the trailers (whether
  279. // present or not)
  280. bw.WriteString("\r\n")
  281. }
  282. }
  283. // A response represents the server side of an HTTP response.
  284. type response struct {
  285. conn *conn
  286. req *Request // request for this response
  287. wroteHeader bool // reply header has been (logically) written
  288. wroteContinue bool // 100 Continue response was written
  289. w *bufio.Writer // buffers output in chunks to chunkWriter
  290. cw chunkWriter
  291. sw *switchWriter // of the bufio.Writer, for return to putBufioWriter
  292. // handlerHeader is the Header that Handlers get access to,
  293. // which may be retained and mutated even after WriteHeader.
  294. // handlerHeader is copied into cw.header at WriteHeader
  295. // time, and privately mutated thereafter.
  296. handlerHeader Header
  297. calledHeader bool // handler accessed handlerHeader via Header
  298. written int64 // number of bytes written in body
  299. contentLength int64 // explicitly-declared Content-Length; or -1
  300. status int // status code passed to WriteHeader
  301. // close connection after this reply. set on request and
  302. // updated after response from handler if there's a
  303. // "Connection: keep-alive" response header and a
  304. // Content-Length.
  305. closeAfterReply bool
  306. // requestBodyLimitHit is set by requestTooLarge when
  307. // maxBytesReader hits its max size. It is checked in
  308. // WriteHeader, to make sure we don't consume the
  309. // remaining request body to try to advance to the next HTTP
  310. // request. Instead, when this is set, we stop reading
  311. // subsequent requests on this connection and stop reading
  312. // input from it.
  313. requestBodyLimitHit bool
  314. // trailers are the headers to be sent after the handler
  315. // finishes writing the body. This field is initialized from
  316. // the Trailer response header when the response header is
  317. // written.
  318. trailers []string
  319. handlerDone bool // set true when the handler exits
  320. // Buffers for Date and Content-Length
  321. dateBuf [len(TimeFormat)]byte
  322. clenBuf [10]byte
  323. }
  324. // declareTrailer is called for each Trailer header when the
  325. // response header is written. It notes that a header will need to be
  326. // written in the trailers at the end of the response.
  327. func (w *response) declareTrailer(k string) {
  328. k = CanonicalHeaderKey(k)
  329. switch k {
  330. case "Transfer-Encoding", "Content-Length", "Trailer":
  331. // Forbidden by RFC 2616 14.40.
  332. return
  333. }
  334. w.trailers = append(w.trailers, k)
  335. }
  336. // requestTooLarge is called by maxBytesReader when too much input has
  337. // been read from the client.
  338. func (w *response) requestTooLarge() {
  339. w.closeAfterReply = true
  340. w.requestBodyLimitHit = true
  341. if !w.wroteHeader {
  342. w.Header().Set("Connection", "close")
  343. }
  344. }
  345. // needsSniff reports whether a Content-Type still needs to be sniffed.
  346. func (w *response) needsSniff() bool {
  347. _, haveType := w.handlerHeader["Content-Type"]
  348. return !w.cw.wroteHeader && !haveType && w.written < sniffLen
  349. }
  350. // writerOnly hides an io.Writer value's optional ReadFrom method
  351. // from io.Copy.
  352. type writerOnly struct {
  353. io.Writer
  354. }
  355. func srcIsRegularFile(src io.Reader) (isRegular bool, err error) {
  356. switch v := src.(type) {
  357. case *os.File:
  358. fi, err := v.Stat()
  359. if err != nil {
  360. return false, err
  361. }
  362. return fi.Mode().IsRegular(), nil
  363. case *io.LimitedReader:
  364. return srcIsRegularFile(v.R)
  365. default:
  366. return
  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. // sendfile path:
  384. if !w.wroteHeader {
  385. w.WriteHeader(StatusOK)
  386. }
  387. if w.needsSniff() {
  388. n0, err := io.Copy(writerOnly{w}, io.LimitReader(src, sniffLen))
  389. n += n0
  390. if err != nil {
  391. return n, err
  392. }
  393. }
  394. w.w.Flush() // get rid of any previous writes
  395. w.cw.flush() // make sure Header is written; flush data to rwc
  396. // Now that cw has been flushed, its chunking field is guaranteed initialized.
  397. if !w.cw.chunking && w.bodyAllowed() {
  398. n0, err := rf.ReadFrom(src)
  399. n += n0
  400. w.written += n0
  401. return n, err
  402. }
  403. n0, err := io.Copy(writerOnly{w}, src)
  404. n += n0
  405. return n, err
  406. }
  407. // noLimit is an effective infinite upper bound for io.LimitedReader
  408. const noLimit int64 = (1 << 63) - 1
  409. // debugServerConnections controls whether all server connections are wrapped
  410. // with a verbose logging wrapper.
  411. const debugServerConnections = false
  412. // Create new connection from rwc.
  413. func (srv *Server) newConn(rwc net.Conn) (c *conn, err error) {
  414. c = new(conn)
  415. c.remoteAddr = rwc.RemoteAddr().String()
  416. c.server = srv
  417. c.rwc = rwc
  418. c.w = rwc
  419. if debugServerConnections {
  420. c.rwc = newLoggingConn("server", c.rwc)
  421. }
  422. c.sr.r = c.rwc
  423. c.lr = io.LimitReader(&c.sr, noLimit).(*io.LimitedReader)
  424. br := newBufioReader(c.lr)
  425. bw := newBufioWriterSize(checkConnErrorWriter{c}, 4<<10)
  426. c.buf = bufio.NewReadWriter(br, bw)
  427. return c, nil
  428. }
  429. var (
  430. bufioReaderPool sync.Pool
  431. bufioWriter2kPool sync.Pool
  432. bufioWriter4kPool sync.Pool
  433. )
  434. func bufioWriterPool(size int) *sync.Pool {
  435. switch size {
  436. case 2 << 10:
  437. return &bufioWriter2kPool
  438. case 4 << 10:
  439. return &bufioWriter4kPool
  440. }
  441. return nil
  442. }
  443. func newBufioReader(r io.Reader) *bufio.Reader {
  444. if v := bufioReaderPool.Get(); v != nil {
  445. br := v.(*bufio.Reader)
  446. br.Reset(r)
  447. return br
  448. }
  449. // Note: if this reader size is every changed, update
  450. // TestHandlerBodyClose's assumptions.
  451. return bufio.NewReader(r)
  452. }
  453. func putBufioReader(br *bufio.Reader) {
  454. br.Reset(nil)
  455. bufioReaderPool.Put(br)
  456. }
  457. func newBufioWriterSize(w io.Writer, size int) *bufio.Writer {
  458. pool := bufioWriterPool(size)
  459. if pool != nil {
  460. if v := pool.Get(); v != nil {
  461. bw := v.(*bufio.Writer)
  462. bw.Reset(w)
  463. return bw
  464. }
  465. }
  466. return bufio.NewWriterSize(w, size)
  467. }
  468. func putBufioWriter(bw *bufio.Writer) {
  469. bw.Reset(nil)
  470. if pool := bufioWriterPool(bw.Available()); pool != nil {
  471. pool.Put(bw)
  472. }
  473. }
  474. // DefaultMaxHeaderBytes is the maximum permitted size of the headers
  475. // in an HTTP request.
  476. // This can be overridden by setting Server.MaxHeaderBytes.
  477. const DefaultMaxHeaderBytes = 1 << 20 // 1 MB
  478. func (srv *Server) maxHeaderBytes() int {
  479. if srv.MaxHeaderBytes > 0 {
  480. return srv.MaxHeaderBytes
  481. }
  482. return DefaultMaxHeaderBytes
  483. }
  484. func (srv *Server) initialLimitedReaderSize() int64 {
  485. return int64(srv.maxHeaderBytes()) + 4096 // bufio slop
  486. }
  487. // wrapper around io.ReaderCloser which on first read, sends an
  488. // HTTP/1.1 100 Continue header
  489. type expectContinueReader struct {
  490. resp *response
  491. readCloser io.ReadCloser
  492. closed bool
  493. sawEOF bool
  494. }
  495. func (ecr *expectContinueReader) Read(p []byte) (n int, err error) {
  496. if ecr.closed {
  497. return 0, ErrBodyReadAfterClose
  498. }
  499. if !ecr.resp.wroteContinue && !ecr.resp.conn.hijacked() {
  500. ecr.resp.wroteContinue = true
  501. ecr.resp.conn.buf.WriteString("HTTP/1.1 100 Continue\r\n\r\n")
  502. ecr.resp.conn.buf.Flush()
  503. }
  504. n, err = ecr.readCloser.Read(p)
  505. if err == io.EOF {
  506. ecr.sawEOF = true
  507. }
  508. return
  509. }
  510. func (ecr *expectContinueReader) Close() error {
  511. ecr.closed = true
  512. return ecr.readCloser.Close()
  513. }
  514. // TimeFormat is the time format to use with
  515. // time.Parse and time.Time.Format when parsing
  516. // or generating times in HTTP headers.
  517. // It is like time.RFC1123 but hard codes GMT as the time zone.
  518. const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
  519. // appendTime is a non-allocating version of []byte(t.UTC().Format(TimeFormat))
  520. func appendTime(b []byte, t time.Time) []byte {
  521. const days = "SunMonTueWedThuFriSat"
  522. const months = "JanFebMarAprMayJunJulAugSepOctNovDec"
  523. t = t.UTC()
  524. yy, mm, dd := t.Date()
  525. hh, mn, ss := t.Clock()
  526. day := days[3*t.Weekday():]
  527. mon := months[3*(mm-1):]
  528. return append(b,
  529. day[0], day[1], day[2], ',', ' ',
  530. byte('0'+dd/10), byte('0'+dd%10), ' ',
  531. mon[0], mon[1], mon[2], ' ',
  532. byte('0'+yy/1000), byte('0'+(yy/100)%10), byte('0'+(yy/10)%10), byte('0'+yy%10), ' ',
  533. byte('0'+hh/10), byte('0'+hh%10), ':',
  534. byte('0'+mn/10), byte('0'+mn%10), ':',
  535. byte('0'+ss/10), byte('0'+ss%10), ' ',
  536. 'G', 'M', 'T')
  537. }
  538. var errTooLarge = errors.New("http: request too large")
  539. // Read next request from connection.
  540. func (c *conn) readRequest() (w *response, err error) {
  541. if c.hijacked() {
  542. return nil, ErrHijacked
  543. }
  544. if d := c.server.ReadTimeout; d != 0 {
  545. c.rwc.SetReadDeadline(time.Now().Add(d))
  546. }
  547. if d := c.server.WriteTimeout; d != 0 {
  548. defer func() {
  549. c.rwc.SetWriteDeadline(time.Now().Add(d))
  550. }()
  551. }
  552. c.lr.N = c.server.initialLimitedReaderSize()
  553. if c.lastMethod == "POST" {
  554. // RFC 2616 section 4.1 tolerance for old buggy clients.
  555. peek, _ := c.buf.Reader.Peek(4) // ReadRequest will get err below
  556. c.buf.Reader.Discard(numLeadingCRorLF(peek))
  557. }
  558. var req *Request
  559. if req, err = ReadRequest(c.buf.Reader); err != nil {
  560. if c.lr.N == 0 {
  561. return nil, errTooLarge
  562. }
  563. return nil, err
  564. }
  565. c.lr.N = noLimit
  566. c.lastMethod = req.Method
  567. req.RemoteAddr = c.remoteAddr
  568. req.TLS = c.tlsState
  569. if body, ok := req.Body.(*body); ok {
  570. body.doEarlyClose = true
  571. }
  572. w = &response{
  573. conn: c,
  574. req: req,
  575. handlerHeader: make(Header),
  576. contentLength: -1,
  577. }
  578. w.cw.res = w
  579. w.w = newBufioWriterSize(&w.cw, bufferBeforeChunkingSize)
  580. return w, nil
  581. }
  582. func (w *response) Header() Header {
  583. if w.cw.header == nil && w.wroteHeader && !w.cw.wroteHeader {
  584. // Accessing the header between logically writing it
  585. // and physically writing it means we need to allocate
  586. // a clone to snapshot the logically written state.
  587. w.cw.header = w.handlerHeader.clone()
  588. }
  589. w.calledHeader = true
  590. return w.handlerHeader
  591. }
  592. // maxPostHandlerReadBytes is the max number of Request.Body bytes not
  593. // consumed by a handler that the server will read from the client
  594. // in order to keep a connection alive. If there are more bytes than
  595. // this then the server to be paranoid instead sends a "Connection:
  596. // close" response.
  597. //
  598. // This number is approximately what a typical machine's TCP buffer
  599. // size is anyway. (if we have the bytes on the machine, we might as
  600. // well read them)
  601. const maxPostHandlerReadBytes = 256 << 10
  602. func (w *response) WriteHeader(code int) {
  603. if w.conn.hijacked() {
  604. w.conn.server.logf("http: response.WriteHeader on hijacked connection")
  605. return
  606. }
  607. if w.wroteHeader {
  608. w.conn.server.logf("http: multiple response.WriteHeader calls")
  609. return
  610. }
  611. w.wroteHeader = true
  612. w.status = code
  613. if w.calledHeader && w.cw.header == nil {
  614. w.cw.header = w.handlerHeader.clone()
  615. }
  616. if cl := w.handlerHeader.get("Content-Length"); cl != "" {
  617. v, err := strconv.ParseInt(cl, 10, 64)
  618. if err == nil && v >= 0 {
  619. w.contentLength = v
  620. } else {
  621. w.conn.server.logf("http: invalid Content-Length of %q", cl)
  622. w.handlerHeader.Del("Content-Length")
  623. }
  624. }
  625. }
  626. // extraHeader is the set of headers sometimes added by chunkWriter.writeHeader.
  627. // This type is used to avoid extra allocations from cloning and/or populating
  628. // the response Header map and all its 1-element slices.
  629. type extraHeader struct {
  630. contentType string
  631. connection string
  632. transferEncoding string
  633. date []byte // written if not nil
  634. contentLength []byte // written if not nil
  635. }
  636. // Sorted the same as extraHeader.Write's loop.
  637. var extraHeaderKeys = [][]byte{
  638. []byte("Content-Type"),
  639. []byte("Connection"),
  640. []byte("Transfer-Encoding"),
  641. }
  642. var (
  643. headerContentLength = []byte("Content-Length: ")
  644. headerDate = []byte("Date: ")
  645. )
  646. // Write writes the headers described in h to w.
  647. //
  648. // This method has a value receiver, despite the somewhat large size
  649. // of h, because it prevents an allocation. The escape analysis isn't
  650. // smart enough to realize this function doesn't mutate h.
  651. func (h extraHeader) Write(w *bufio.Writer) {
  652. if h.date != nil {
  653. w.Write(headerDate)
  654. w.Write(h.date)
  655. w.Write(crlf)
  656. }
  657. if h.contentLength != nil {
  658. w.Write(headerContentLength)
  659. w.Write(h.contentLength)
  660. w.Write(crlf)
  661. }
  662. for i, v := range []string{h.contentType, h.connection, h.transferEncoding} {
  663. if v != "" {
  664. w.Write(extraHeaderKeys[i])
  665. w.Write(colonSpace)
  666. w.WriteString(v)
  667. w.Write(crlf)
  668. }
  669. }
  670. }
  671. // writeHeader finalizes the header sent to the client and writes it
  672. // to cw.res.conn.buf.
  673. //
  674. // p is not written by writeHeader, but is the first chunk of the body
  675. // that will be written. It is sniffed for a Content-Type if none is
  676. // set explicitly. It's also used to set the Content-Length, if the
  677. // total body size was small and the handler has already finished
  678. // running.
  679. func (cw *chunkWriter) writeHeader(p []byte) {
  680. if cw.wroteHeader {
  681. return
  682. }
  683. cw.wroteHeader = true
  684. w := cw.res
  685. keepAlivesEnabled := w.conn.server.doKeepAlives()
  686. isHEAD := w.req.Method == "HEAD"
  687. // header is written out to w.conn.buf below. Depending on the
  688. // state of the handler, we either own the map or not. If we
  689. // don't own it, the exclude map is created lazily for
  690. // WriteSubset to remove headers. The setHeader struct holds
  691. // headers we need to add.
  692. header := cw.header
  693. owned := header != nil
  694. if !owned {
  695. header = w.handlerHeader
  696. }
  697. var excludeHeader map[string]bool
  698. delHeader := func(key string) {
  699. if owned {
  700. header.Del(key)
  701. return
  702. }
  703. if _, ok := header[key]; !ok {
  704. return
  705. }
  706. if excludeHeader == nil {
  707. excludeHeader = make(map[string]bool)
  708. }
  709. excludeHeader[key] = true
  710. }
  711. var setHeader extraHeader
  712. trailers := false
  713. for _, v := range cw.header["Trailer"] {
  714. trailers = true
  715. foreachHeaderElement(v, cw.res.declareTrailer)
  716. }
  717. te := header.get("Transfer-Encoding")
  718. hasTE := te != ""
  719. // If the handler is done but never sent a Content-Length
  720. // response header and this is our first (and last) write, set
  721. // it, even to zero. This helps HTTP/1.0 clients keep their
  722. // "keep-alive" connections alive.
  723. // Exceptions: 304/204/1xx responses never get Content-Length, and if
  724. // it was a HEAD request, we don't know the difference between
  725. // 0 actual bytes and 0 bytes because the handler noticed it
  726. // was a HEAD request and chose not to write anything. So for
  727. // HEAD, the handler should either write the Content-Length or
  728. // write non-zero bytes. If it's actually 0 bytes and the
  729. // handler never looked at the Request.Method, we just don't
  730. // send a Content-Length header.
  731. // Further, we don't send an automatic Content-Length if they
  732. // set a Transfer-Encoding, because they're generally incompatible.
  733. if w.handlerDone && !trailers && !hasTE && bodyAllowedForStatus(w.status) && header.get("Content-Length") == "" && (!isHEAD || len(p) > 0) {
  734. w.contentLength = int64(len(p))
  735. setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10)
  736. }
  737. // If this was an HTTP/1.0 request with keep-alive and we sent a
  738. // Content-Length back, we can make this a keep-alive response ...
  739. if w.req.wantsHttp10KeepAlive() && keepAlivesEnabled {
  740. sentLength := header.get("Content-Length") != ""
  741. if sentLength && header.get("Connection") == "keep-alive" {
  742. w.closeAfterReply = false
  743. }
  744. }
  745. // Check for a explicit (and valid) Content-Length header.
  746. hasCL := w.contentLength != -1
  747. if w.req.wantsHttp10KeepAlive() && (isHEAD || hasCL) {
  748. _, connectionHeaderSet := header["Connection"]
  749. if !connectionHeaderSet {
  750. setHeader.connection = "keep-alive"
  751. }
  752. } else if !w.req.ProtoAtLeast(1, 1) || w.req.wantsClose() {
  753. w.closeAfterReply = true
  754. }
  755. if header.get("Connection") == "close" || !keepAlivesEnabled {
  756. w.closeAfterReply = true
  757. }
  758. // If the client wanted a 100-continue but we never sent it to
  759. // them (or, more strictly: we never finished reading their
  760. // request body), don't reuse this connection because it's now
  761. // in an unknown state: we might be sending this response at
  762. // the same time the client is now sending its request body
  763. // after a timeout. (Some HTTP clients send Expect:
  764. // 100-continue but knowing that some servers don't support
  765. // it, the clients set a timer and send the body later anyway)
  766. // If we haven't seen EOF, we can't skip over the unread body
  767. // because we don't know if the next bytes on the wire will be
  768. // the body-following-the-timer or the subsequent request.
  769. // See Issue 11549.
  770. if ecr, ok := w.req.Body.(*expectContinueReader); ok && !ecr.sawEOF {
  771. w.closeAfterReply = true
  772. }
  773. // Per RFC 2616, we should consume the request body before
  774. // replying, if the handler hasn't already done so. But we
  775. // don't want to do an unbounded amount of reading here for
  776. // DoS reasons, so we only try up to a threshold.
  777. if w.req.ContentLength != 0 && !w.closeAfterReply {
  778. var discard, tooBig bool
  779. switch bdy := w.req.Body.(type) {
  780. case *expectContinueReader:
  781. if bdy.resp.wroteContinue {
  782. discard = true
  783. }
  784. case *body:
  785. bdy.mu.Lock()
  786. switch {
  787. case bdy.closed:
  788. if !bdy.sawEOF {
  789. // Body was closed in handler with non-EOF error.
  790. w.closeAfterReply = true
  791. }
  792. case bdy.unreadDataSizeLocked() >= maxPostHandlerReadBytes:
  793. tooBig = true
  794. default:
  795. discard = true
  796. }
  797. bdy.mu.Unlock()
  798. default:
  799. discard = true
  800. }
  801. if discard {
  802. _, err := io.CopyN(ioutil.Discard, w.req.Body, maxPostHandlerReadBytes+1)
  803. switch err {
  804. case nil:
  805. // There must be even more data left over.
  806. tooBig = true
  807. case ErrBodyReadAfterClose:
  808. // Body was already consumed and closed.
  809. case io.EOF:
  810. // The remaining body was just consumed, close it.
  811. err = w.req.Body.Close()
  812. if err != nil {
  813. w.closeAfterReply = true
  814. }
  815. default:
  816. // Some other kind of error occured, like a read timeout, or
  817. // corrupt chunked encoding. In any case, whatever remains
  818. // on the wire must not be parsed as another HTTP request.
  819. w.closeAfterReply = true
  820. }
  821. }
  822. if tooBig {
  823. w.requestTooLarge()
  824. delHeader("Connection")
  825. setHeader.connection = "close"
  826. }
  827. }
  828. code := w.status
  829. if bodyAllowedForStatus(code) {
  830. // If no content type, apply sniffing algorithm to body.
  831. _, haveType := header["Content-Type"]
  832. if !haveType && !hasTE {
  833. setHeader.contentType = DetectContentType(p)
  834. }
  835. } else {
  836. for _, k := range suppressedHeaders(code) {
  837. delHeader(k)
  838. }
  839. }
  840. if _, ok := header["Date"]; !ok {
  841. setHeader.date = appendTime(cw.res.dateBuf[:0], time.Now())
  842. }
  843. if hasCL && hasTE && te != "identity" {
  844. // TODO: return an error if WriteHeader gets a return parameter
  845. // For now just ignore the Content-Length.
  846. w.conn.server.logf("http: WriteHeader called with both Transfer-Encoding of %q and a Content-Length of %d",
  847. te, w.contentLength)
  848. delHeader("Content-Length")
  849. hasCL = false
  850. }
  851. if w.req.Method == "HEAD" || !bodyAllowedForStatus(code) {
  852. // do nothing
  853. } else if code == StatusNoContent {
  854. delHeader("Transfer-Encoding")
  855. } else if hasCL {
  856. delHeader("Transfer-Encoding")
  857. } else if w.req.ProtoAtLeast(1, 1) {
  858. // HTTP/1.1 or greater: Transfer-Encoding has been set to identity, and no
  859. // content-length has been provided. The connection must be closed after the
  860. // reply is written, and no chunking is to be done. This is the setup
  861. // recommended in the Server-Sent Events candidate recommendation 11,
  862. // section 8.
  863. if hasTE && te == "identity" {
  864. cw.chunking = false
  865. w.closeAfterReply = true
  866. } else {
  867. // HTTP/1.1 or greater: use chunked transfer encoding
  868. // to avoid closing the connection at EOF.
  869. cw.chunking = true
  870. setHeader.transferEncoding = "chunked"
  871. }
  872. } else {
  873. // HTTP version < 1.1: cannot do chunked transfer
  874. // encoding and we don't know the Content-Length so
  875. // signal EOF by closing connection.
  876. w.closeAfterReply = true
  877. delHeader("Transfer-Encoding") // in case already set
  878. }
  879. // Cannot use Content-Length with non-identity Transfer-Encoding.
  880. if cw.chunking {
  881. delHeader("Content-Length")
  882. }
  883. if !w.req.ProtoAtLeast(1, 0) {
  884. return
  885. }
  886. if w.closeAfterReply && (!keepAlivesEnabled || !hasToken(cw.header.get("Connection"), "close")) {
  887. delHeader("Connection")
  888. if w.req.ProtoAtLeast(1, 1) {
  889. setHeader.connection = "close"
  890. }
  891. }
  892. w.conn.buf.WriteString(statusLine(w.req, code))
  893. cw.header.WriteSubset(w.conn.buf, excludeHeader)
  894. setHeader.Write(w.conn.buf.Writer)
  895. w.conn.buf.Write(crlf)
  896. }
  897. // foreachHeaderElement splits v according to the "#rule" construction
  898. // in RFC 2616 section 2.1 and calls fn for each non-empty element.
  899. func foreachHeaderElement(v string, fn func(string)) {
  900. v = textproto.TrimString(v)
  901. if v == "" {
  902. return
  903. }
  904. if !strings.Contains(v, ",") {
  905. fn(v)
  906. return
  907. }
  908. for _, f := range strings.Split(v, ",") {
  909. if f = textproto.TrimString(f); f != "" {
  910. fn(f)
  911. }
  912. }
  913. }
  914. // statusLines is a cache of Status-Line strings, keyed by code (for
  915. // HTTP/1.1) or negative code (for HTTP/1.0). This is faster than a
  916. // map keyed by struct of two fields. This map's max size is bounded
  917. // by 2*len(statusText), two protocol types for each known official
  918. // status code in the statusText map.
  919. var (
  920. statusMu sync.RWMutex
  921. statusLines = make(map[int]string)
  922. )
  923. // statusLine returns a response Status-Line (RFC 2616 Section 6.1)
  924. // for the given request and response status code.
  925. func statusLine(req *Request, code int) string {
  926. // Fast path:
  927. key := code
  928. proto11 := req.ProtoAtLeast(1, 1)
  929. if !proto11 {
  930. key = -key
  931. }
  932. statusMu.RLock()
  933. line, ok := statusLines[key]
  934. statusMu.RUnlock()
  935. if ok {
  936. return line
  937. }
  938. // Slow path:
  939. proto := "HTTP/1.0"
  940. if proto11 {
  941. proto = "HTTP/1.1"
  942. }
  943. codestring := strconv.Itoa(code)
  944. text, ok := statusText[code]
  945. if !ok {
  946. text = "status code " + codestring
  947. }
  948. line = proto + " " + codestring + " " + text + "\r\n"
  949. if ok {
  950. statusMu.Lock()
  951. defer statusMu.Unlock()
  952. statusLines[key] = line
  953. }
  954. return line
  955. }
  956. // bodyAllowed reports whether a Write is allowed for this response type.
  957. // It's illegal to call this before the header has been flushed.
  958. func (w *response) bodyAllowed() bool {
  959. if !w.wroteHeader {
  960. panic("")
  961. }
  962. return bodyAllowedForStatus(w.status)
  963. }
  964. // The Life Of A Write is like this:
  965. //
  966. // Handler starts. No header has been sent. The handler can either
  967. // write a header, or just start writing. Writing before sending a header
  968. // sends an implicitly empty 200 OK header.
  969. //
  970. // If the handler didn't declare a Content-Length up front, we either
  971. // go into chunking mode or, if the handler finishes running before
  972. // the chunking buffer size, we compute a Content-Length and send that
  973. // in the header instead.
  974. //
  975. // Likewise, if the handler didn't set a Content-Type, we sniff that
  976. // from the initial chunk of output.
  977. //
  978. // The Writers are wired together like:
  979. //
  980. // 1. *response (the ResponseWriter) ->
  981. // 2. (*response).w, a *bufio.Writer of bufferBeforeChunkingSize bytes
  982. // 3. chunkWriter.Writer (whose writeHeader finalizes Content-Length/Type)
  983. // and which writes the chunk headers, if needed.
  984. // 4. conn.buf, a bufio.Writer of default (4kB) bytes, writing to ->
  985. // 5. checkConnErrorWriter{c}, which notes any non-nil error on Write
  986. // and populates c.werr with it if so. but otherwise writes to:
  987. // 6. the rwc, the net.Conn.
  988. //
  989. // TODO(bradfitz): short-circuit some of the buffering when the
  990. // initial header contains both a Content-Type and Content-Length.
  991. // Also short-circuit in (1) when the header's been sent and not in
  992. // chunking mode, writing directly to (4) instead, if (2) has no
  993. // buffered data. More generally, we could short-circuit from (1) to
  994. // (3) even in chunking mode if the write size from (1) is over some
  995. // threshold and nothing is in (2). The answer might be mostly making
  996. // bufferBeforeChunkingSize smaller and having bufio's fast-paths deal
  997. // with this instead.
  998. func (w *response) Write(data []byte) (n int, err error) {
  999. return w.write(len(data), data, "")
  1000. }
  1001. func (w *response) WriteString(data string) (n int, err error) {
  1002. return w.write(len(data), nil, data)
  1003. }
  1004. // either dataB or dataS is non-zero.
  1005. func (w *response) write(lenData int, dataB []byte, dataS string) (n int, err error) {
  1006. if w.conn.hijacked() {
  1007. w.conn.server.logf("http: response.Write on hijacked connection")
  1008. return 0, ErrHijacked
  1009. }
  1010. if !w.wroteHeader {
  1011. w.WriteHeader(StatusOK)
  1012. }
  1013. if lenData == 0 {
  1014. return 0, nil
  1015. }
  1016. if !w.bodyAllowed() {
  1017. return 0, ErrBodyNotAllowed
  1018. }
  1019. w.written += int64(lenData) // ignoring errors, for errorKludge
  1020. if w.contentLength != -1 && w.written > w.contentLength {
  1021. return 0, ErrContentLength
  1022. }
  1023. if dataB != nil {
  1024. return w.w.Write(dataB)
  1025. } else {
  1026. return w.w.WriteString(dataS)
  1027. }
  1028. }
  1029. func (w *response) finishRequest() {
  1030. w.handlerDone = true
  1031. if !w.wroteHeader {
  1032. w.WriteHeader(StatusOK)
  1033. }
  1034. w.w.Flush()
  1035. putBufioWriter(w.w)
  1036. w.cw.close()
  1037. w.conn.buf.Flush()
  1038. // Close the body (regardless of w.closeAfterReply) so we can
  1039. // re-use its bufio.Reader later safely.
  1040. w.req.Body.Close()
  1041. if w.req.MultipartForm != nil {
  1042. w.req.MultipartForm.RemoveAll()
  1043. }
  1044. }
  1045. // shouldReuseConnection reports whether the underlying TCP connection can be reused.
  1046. // It must only be called after the handler is done executing.
  1047. func (w *response) shouldReuseConnection() bool {
  1048. if w.closeAfterReply {
  1049. // The request or something set while executing the
  1050. // handler indicated we shouldn't reuse this
  1051. // connection.
  1052. return false
  1053. }
  1054. if w.req.Method != "HEAD" && w.contentLength != -1 && w.bodyAllowed() && w.contentLength != w.written {
  1055. // Did not write enough. Avoid getting out of sync.
  1056. return false
  1057. }
  1058. // There was some error writing to the underlying connection
  1059. // during the request, so don't re-use this conn.
  1060. if w.conn.werr != nil {
  1061. return false
  1062. }
  1063. if w.closedRequestBodyEarly() {
  1064. return false
  1065. }
  1066. return true
  1067. }
  1068. func (w *response) closedRequestBodyEarly() bool {
  1069. body, ok := w.req.Body.(*body)
  1070. return ok && body.didEarlyClose()
  1071. }
  1072. func (w *response) Flush() {
  1073. if !w.wroteHeader {
  1074. w.WriteHeader(StatusOK)
  1075. }
  1076. w.w.Flush()
  1077. w.cw.flush()
  1078. }
  1079. func (c *conn) finalFlush() {
  1080. if c.buf != nil {
  1081. c.buf.Flush()
  1082. // Steal the bufio.Reader (~4KB worth of memory) and its associated
  1083. // reader for a future connection.
  1084. putBufioReader(c.buf.Reader)
  1085. // Steal the bufio.Writer (~4KB worth of memory) and its associated
  1086. // writer for a future connection.
  1087. putBufioWriter(c.buf.Writer)
  1088. c.buf = nil
  1089. }
  1090. }
  1091. // Close the connection.
  1092. func (c *conn) close() {
  1093. c.finalFlush()
  1094. if c.rwc != nil {
  1095. c.rwc.Close()
  1096. c.rwc = nil
  1097. }
  1098. }
  1099. // rstAvoidanceDelay is the amount of time we sleep after closing the
  1100. // write side of a TCP connection before closing the entire socket.
  1101. // By sleeping, we increase the chances that the client sees our FIN
  1102. // and processes its final data before they process the subsequent RST
  1103. // from closing a connection with known unread data.
  1104. // This RST seems to occur mostly on BSD systems. (And Windows?)
  1105. // This timeout is somewhat arbitrary (~latency around the planet).
  1106. const rstAvoidanceDelay = 500 * time.Millisecond
  1107. type closeWriter interface {
  1108. CloseWrite() error
  1109. }
  1110. var _ closeWriter = (*net.TCPConn)(nil)
  1111. // closeWrite flushes any outstanding data and sends a FIN packet (if
  1112. // client is connected via TCP), signalling that we're done. We then
  1113. // pause for a bit, hoping the client processes it before any
  1114. // subsequent RST.
  1115. //
  1116. // See https://golang.org/issue/3595
  1117. func (c *conn) closeWriteAndWait() {
  1118. c.finalFlush()
  1119. if tcp, ok := c.rwc.(closeWriter); ok {
  1120. tcp.CloseWrite()
  1121. }
  1122. time.Sleep(rstAvoidanceDelay)
  1123. }
  1124. // validNPN reports whether the proto is not a blacklisted Next
  1125. // Protocol Negotiation protocol. Empty and built-in protocol types
  1126. // are blacklisted and can't be overridden with alternate
  1127. // implementations.
  1128. func validNPN(proto string) bool {
  1129. switch proto {
  1130. case "", "http/1.1", "http/1.0":
  1131. return false
  1132. }
  1133. return true
  1134. }
  1135. func (c *conn) setState(nc net.Conn, state ConnState) {
  1136. if hook := c.server.ConnState; hook != nil {
  1137. hook(nc, state)
  1138. }
  1139. }
  1140. // Serve a new connection.
  1141. func (c *conn) serve() {
  1142. origConn := c.rwc // copy it before it's set nil on Close or Hijack
  1143. defer func() {
  1144. if err := recover(); err != nil {
  1145. const size = 64 << 10
  1146. buf := make([]byte, size)
  1147. buf = buf[:runtime.Stack(buf, false)]
  1148. c.server.logf("http: panic serving %v: %v\n%s", c.remoteAddr, err, buf)
  1149. }
  1150. if !c.hijacked() {
  1151. c.close()
  1152. c.setState(origConn, StateClosed)
  1153. }
  1154. }()
  1155. if tlsConn, ok := c.rwc.(*tls.Conn); ok {
  1156. if d := c.server.ReadTimeout; d != 0 {
  1157. c.rwc.SetReadDeadline(time.Now().Add(d))
  1158. }
  1159. if d := c.server.WriteTimeout; d != 0 {
  1160. c.rwc.SetWriteDeadline(time.Now().Add(d))
  1161. }
  1162. if err := tlsConn.Handshake(); err != nil {
  1163. c.server.logf("http: TLS handshake error from %s: %v", c.rwc.RemoteAddr(), err)
  1164. return
  1165. }
  1166. c.tlsState = new(tls.ConnectionState)
  1167. *c.tlsState = tlsConn.ConnectionState()
  1168. if proto := c.tlsState.NegotiatedProtocol; validNPN(proto) {
  1169. if fn := c.server.TLSNextProto[proto]; fn != nil {
  1170. h := initNPNRequest{tlsConn, serverHandler{c.server}}
  1171. fn(c.server, tlsConn, h)
  1172. }
  1173. return
  1174. }
  1175. }
  1176. for {
  1177. w, err := c.readRequest()
  1178. if c.lr.N != c.server.initialLimitedReaderSize() {
  1179. // If we read any bytes off the wire, we're active.
  1180. c.setState(c.rwc, StateActive)
  1181. }
  1182. if err != nil {
  1183. if err == errTooLarge {
  1184. // Their HTTP client may or may not be
  1185. // able to read this if we're
  1186. // responding to them and hanging up
  1187. // while they're still writing their
  1188. // request. Undefined behavior.
  1189. io.WriteString(c.rwc, "HTTP/1.1 413 Request Entity Too Large\r\n\r\n")
  1190. c.closeWriteAndWait()
  1191. break
  1192. } else if err == io.EOF {
  1193. break // Don't reply
  1194. } else if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
  1195. break // Don't reply
  1196. }
  1197. io.WriteString(c.rwc, "HTTP/1.1 400 Bad Request\r\n\r\n")
  1198. break
  1199. }
  1200. // Expect 100 Continue support
  1201. req := w.req
  1202. if req.expectsContinue() {
  1203. if req.ProtoAtLeast(1, 1) && req.ContentLength != 0 {
  1204. // Wrap the Body reader with one that replies on the connection
  1205. req.Body = &expectContinueReader{readCloser: req.Body, resp: w}
  1206. }
  1207. req.Header.Del("Expect")
  1208. } else if req.Header.get("Expect") != "" {
  1209. w.sendExpectationFailed()
  1210. break
  1211. }
  1212. // HTTP cannot have multiple simultaneous active requests.[*]
  1213. // Until the server replies to this request, it can't read another,
  1214. // so we might as well run the handler in this goroutine.
  1215. // [*] Not strictly true: HTTP pipelining. We could let them all process
  1216. // in parallel even if their responses need to be serialized.
  1217. serverHandler{c.server}.ServeHTTP(w, w.req)
  1218. if c.hijacked() {
  1219. return
  1220. }
  1221. w.finishRequest()
  1222. if !w.shouldReuseConnection() {
  1223. if w.requestBodyLimitHit || w.closedRequestBodyEarly() {
  1224. c.closeWriteAndWait()
  1225. }
  1226. break
  1227. }
  1228. c.setState(c.rwc, StateIdle)
  1229. }
  1230. }
  1231. func (w *response) sendExpectationFailed() {
  1232. // TODO(bradfitz): let ServeHTTP handlers handle
  1233. // requests with non-standard expectation[s]? Seems
  1234. // theoretical at best, and doesn't fit into the
  1235. // current ServeHTTP model anyway. We'd need to
  1236. // make the ResponseWriter an optional
  1237. // "ExpectReplier" interface or something.
  1238. //
  1239. // For now we'll just obey RFC 2616 14.20 which says
  1240. // "If a server receives a request containing an
  1241. // Expect field that includes an expectation-
  1242. // extension that it does not support, it MUST
  1243. // respond with a 417 (Expectation Failed) status."
  1244. w.Header().Set("Connection", "close")
  1245. w.WriteHeader(StatusExpectationFailed)
  1246. w.finishRequest()
  1247. }
  1248. // Hijack implements the Hijacker.Hijack method. Our response is both a ResponseWriter
  1249. // and a Hijacker.
  1250. func (w *response) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
  1251. if w.wroteHeader {
  1252. w.cw.flush()
  1253. }
  1254. // Release the bufioWriter that writes to the chunk writer, it is not
  1255. // used after a connection has been hijacked.
  1256. rwc, buf, err = w.conn.hijack()
  1257. if err == nil {
  1258. putBufioWriter(w.w)
  1259. w.w = nil
  1260. }
  1261. return rwc, buf, err
  1262. }
  1263. func (w *response) CloseNotify() <-chan bool {
  1264. return w.conn.closeNotify()
  1265. }
  1266. // The HandlerFunc type is an adapter to allow the use of
  1267. // ordinary functions as HTTP handlers. If f is a function
  1268. // with the appropriate signature, HandlerFunc(f) is a
  1269. // Handler object that calls f.
  1270. type HandlerFunc func(ResponseWriter, *Request)
  1271. // ServeHTTP calls f(w, r).
  1272. func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
  1273. f(w, r)
  1274. }
  1275. // Helper handlers
  1276. // Error replies to the request with the specified error message and HTTP code.
  1277. // The error message should be plain text.
  1278. func Error(w ResponseWriter, error string, code int) {
  1279. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  1280. w.Header().Set("X-Content-Type-Options", "nosniff")
  1281. w.WriteHeader(code)
  1282. fmt.Fprintln(w, error)
  1283. }
  1284. // NotFound replies to the request with an HTTP 404 not found error.
  1285. func NotFound(w ResponseWriter, r *Request) { Error(w, "404 page not found", StatusNotFound) }
  1286. // NotFoundHandler returns a simple request handler
  1287. // that replies to each request with a ``404 page not found'' reply.
  1288. func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
  1289. // StripPrefix returns a handler that serves HTTP requests
  1290. // by removing the given prefix from the request URL's Path
  1291. // and invoking the handler h. StripPrefix handles a
  1292. // request for a path that doesn't begin with prefix by
  1293. // replying with an HTTP 404 not found error.
  1294. func StripPrefix(prefix string, h Handler) Handler {
  1295. if prefix == "" {
  1296. return h
  1297. }
  1298. return HandlerFunc(func(w ResponseWriter, r *Request) {
  1299. if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {
  1300. r.URL.Path = p
  1301. h.ServeHTTP(w, r)
  1302. } else {
  1303. NotFound(w, r)
  1304. }
  1305. })
  1306. }
  1307. // Redirect replies to the request with a redirect to url,
  1308. // which may be a path relative to the request path.
  1309. func Redirect(w ResponseWriter, r *Request, urlStr string, code int) {
  1310. if u, err := url.Parse(urlStr); err == nil {
  1311. // If url was relative, make absolute by
  1312. // combining with request path.
  1313. // The browser would probably do this for us,
  1314. // but doing it ourselves is more reliable.
  1315. // NOTE(rsc): RFC 2616 says that the Location
  1316. // line must be an absolute URI, like
  1317. // "http://www.google.com/redirect/",
  1318. // not a path like "/redirect/".
  1319. // Unfortunately, we don't know what to
  1320. // put in the host name section to get the
  1321. // client to connect to us again, so we can't
  1322. // know the right absolute URI to send back.
  1323. // Because of this problem, no one pays attention
  1324. // to the RFC; they all send back just a new path.
  1325. // So do we.
  1326. oldpath := r.URL.Path
  1327. if oldpath == "" { // should not happen, but avoid a crash if it does
  1328. oldpath = "/"
  1329. }
  1330. if u.Scheme == "" {
  1331. // no leading http://server
  1332. if urlStr == "" || urlStr[0] != '/' {
  1333. // make relative path absolute
  1334. olddir, _ := path.Split(oldpath)
  1335. urlStr = olddir + urlStr
  1336. }
  1337. var query string
  1338. if i := strings.Index(urlStr, "?"); i != -1 {
  1339. urlStr, query = urlStr[:i], urlStr[i:]
  1340. }
  1341. // clean up but preserve trailing slash
  1342. trailing := strings.HasSuffix(urlStr, "/")
  1343. urlStr = path.Clean(urlStr)
  1344. if trailing && !strings.HasSuffix(urlStr, "/") {
  1345. urlStr += "/"
  1346. }
  1347. urlStr += query
  1348. }
  1349. }
  1350. w.Header().Set("Location", urlStr)
  1351. w.WriteHeader(code)
  1352. // RFC2616 recommends that a short note "SHOULD" be included in the
  1353. // response because older user agents may not understand 301/307.
  1354. // Shouldn't send the response for POST or HEAD; that leaves GET.
  1355. if r.Method == "GET" {
  1356. note := "<a href=\"" + htmlEscape(urlStr) + "\">" + statusText[code] + "</a>.\n"
  1357. fmt.Fprintln(w, note)
  1358. }
  1359. }
  1360. var htmlReplacer = strings.NewReplacer(
  1361. "&", "&amp;",
  1362. "<", "&lt;",
  1363. ">", "&gt;",
  1364. // "&#34;" is shorter than "&quot;".
  1365. `"`, "&#34;",
  1366. // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
  1367. "'", "&#39;",
  1368. )
  1369. func htmlEscape(s string) string {
  1370. return htmlReplacer.Replace(s)
  1371. }
  1372. // Redirect to a fixed URL
  1373. type redirectHandler struct {
  1374. url string
  1375. code int
  1376. }
  1377. func (rh *redirectHandler) ServeHTTP(w ResponseWriter, r *Request) {
  1378. Redirect(w, r, rh.url, rh.code)
  1379. }
  1380. // RedirectHandler returns a request handler that redirects
  1381. // each request it receives to the given url using the given
  1382. // status code.
  1383. func RedirectHandler(url string, code int) Handler {
  1384. return &redirectHandler{url, code}
  1385. }
  1386. // ServeMux is an HTTP request multiplexer.
  1387. // It matches the URL of each incoming request against a list of registered
  1388. // patterns and calls the handler for the pattern that
  1389. // most closely matches the URL.
  1390. //
  1391. // Patterns name fixed, rooted paths, like "/favicon.ico",
  1392. // or rooted subtrees, like "/images/" (note the trailing slash).
  1393. // Longer patterns take precedence over shorter ones, so that
  1394. // if there are handlers registered for both "/images/"
  1395. // and "/images/thumbnails/", the latter handler will be
  1396. // called for paths beginning "/images/thumbnails/" and the
  1397. // former will receive requests for any other paths in the
  1398. // "/images/" subtree.
  1399. //
  1400. // Note that since a pattern ending in a slash names a rooted subtree,
  1401. // the pattern "/" matches all paths not matched by other registered
  1402. // patterns, not just the URL with Path == "/".
  1403. //
  1404. // Patterns may optionally begin with a host name, restricting matches to
  1405. // URLs on that host only. Host-specific patterns take precedence over
  1406. // general patterns, so that a handler might register for the two patterns
  1407. // "/codesearch" and "codesearch.google.com/" without also taking over
  1408. // requests for "http://www.google.com/".
  1409. //
  1410. // ServeMux also takes care of sanitizing the URL request path,
  1411. // redirecting any request containing . or .. elements to an
  1412. // equivalent .- and ..-free URL.
  1413. type ServeMux struct {
  1414. mu sync.RWMutex
  1415. m map[string]muxEntry
  1416. hosts bool // whether any patterns contain hostnames
  1417. }
  1418. type muxEntry struct {
  1419. explicit bool
  1420. h Handler
  1421. pattern string
  1422. }
  1423. // NewServeMux allocates and returns a new ServeMux.
  1424. func NewServeMux() *ServeMux { return &ServeMux{m: make(map[string]muxEntry)} }
  1425. // DefaultServeMux is the default ServeMux used by Serve.
  1426. var DefaultServeMux = NewServeMux()
  1427. // Does path match pattern?
  1428. func pathMatch(pattern, path string) bool {
  1429. if len(pattern) == 0 {
  1430. // should not happen
  1431. return false
  1432. }
  1433. n := len(pattern)
  1434. if pattern[n-1] != '/' {
  1435. return pattern == path
  1436. }
  1437. return len(path) >= n && path[0:n] == pattern
  1438. }
  1439. // Return the canonical path for p, eliminating . and .. elements.
  1440. func cleanPath(p string) string {
  1441. if p == "" {
  1442. return "/"
  1443. }
  1444. if p[0] != '/' {
  1445. p = "/" + p
  1446. }
  1447. np := path.Clean(p)
  1448. // path.Clean removes trailing slash except for root;
  1449. // put the trailing slash back if necessary.
  1450. if p[len(p)-1] == '/' && np != "/" {
  1451. np += "/"
  1452. }
  1453. return np
  1454. }
  1455. // Find a handler on a handler map given a path string
  1456. // Most-specific (longest) pattern wins
  1457. func (mux *ServeMux) match(path string) (h Handler, pattern string) {
  1458. var n = 0
  1459. for k, v := range mux.m {
  1460. if !pathMatch(k, path) {
  1461. continue
  1462. }
  1463. if h == nil || len(k) > n {
  1464. n = len(k)
  1465. h = v.h
  1466. pattern = v.pattern
  1467. }
  1468. }
  1469. return
  1470. }
  1471. // Handler returns the handler to use for the given request,
  1472. // consulting r.Method, r.Host, and r.URL.Path. It always returns
  1473. // a non-nil handler. If the path is not in its canonical form, the
  1474. // handler will be an internally-generated handler that redirects
  1475. // to the canonical path.
  1476. //
  1477. // Handler also returns the registered pattern that matches the
  1478. // request or, in the case of internally-generated redirects,
  1479. // the pattern that will match after following the redirect.
  1480. //
  1481. // If there is no registered handler that applies to the request,
  1482. // Handler returns a ``page not found'' handler and an empty pattern.
  1483. func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
  1484. if r.Method != "CONNECT" {
  1485. if p := cleanPath(r.URL.Path); p != r.URL.Path {
  1486. _, pattern = mux.handler(r.Host, p)
  1487. url := *r.URL
  1488. url.Path = p
  1489. return RedirectHandler(url.String(), StatusMovedPermanently), pattern
  1490. }
  1491. }
  1492. return mux.handler(r.Host, r.URL.Path)
  1493. }
  1494. // handler is the main implementation of Handler.
  1495. // The path is known to be in canonical form, except for CONNECT methods.
  1496. func (mux *ServeMux) handler(host, path string) (h Handler, pattern string) {
  1497. mux.mu.RLock()
  1498. defer mux.mu.RUnlock()
  1499. // Host-specific pattern takes precedence over generic ones
  1500. if mux.hosts {
  1501. h, pattern = mux.match(host + path)
  1502. }
  1503. if h == nil {
  1504. h, pattern = mux.match(path)
  1505. }
  1506. if h == nil {
  1507. h, pattern = NotFoundHandler(), ""
  1508. }
  1509. return
  1510. }
  1511. // ServeHTTP dispatches the request to the handler whose
  1512. // pattern most closely matches the request URL.
  1513. func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
  1514. if r.RequestURI == "*" {
  1515. if r.ProtoAtLeast(1, 1) {
  1516. w.Header().Set("Connection", "close")
  1517. }
  1518. w.WriteHeader(StatusBadRequest)
  1519. return
  1520. }
  1521. h, _ := mux.Handler(r)
  1522. h.ServeHTTP(w, r)
  1523. }
  1524. // Handle registers the handler for the given pattern.
  1525. // If a handler already exists for pattern, Handle panics.
  1526. func (mux *Serv

Large files files are truncated, but you can click here to view the full file