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.
22 "golang_org/x/net/lex/httplex"
25 // ErrLineTooLong is returned when reading request or response bodies
26 // with malformed chunked encoding.
27 var ErrLineTooLong = internal.ErrLineTooLong
29 type errorReader struct {
33 func (r errorReader) Read(p []byte) (n int, err error) {
37 type byteReader struct {
42 func (br *byteReader) Read(p []byte) (n int, err error) {
54 // transferWriter inspects the fields of a user-supplied Request or Response,
55 // sanitizes them without changing the user object and provides methods for
56 // writing the respective header, body and trailer in wire format.
57 type transferWriter struct {
62 ContentLength int64 // -1 means unknown, 0 means exactly none
64 TransferEncoding []string
68 FlushHeaders bool // flush headers to network before body
69 ByteReadCh chan readResult // non-nil if probeRequestBody called
72 func newTransferWriter(r interface{}) (t *transferWriter, err error) {
75 // Extract relevant fields
76 atLeastHTTP11 := false
77 switch rr := r.(type) {
79 if rr.ContentLength != 0 && rr.Body == nil {
80 return nil, fmt.Errorf("http: Request.ContentLength=%d with nil Body", rr.ContentLength)
82 t.Method = valueOrDefault(rr.Method, "GET")
84 t.TransferEncoding = rr.TransferEncoding
85 t.Trailer = rr.Trailer
86 atLeastHTTP11 = rr.protoAtLeastOutgoing(1, 1)
88 t.BodyCloser = rr.Body
89 t.ContentLength = rr.outgoingLength()
90 if t.ContentLength < 0 && len(t.TransferEncoding) == 0 && atLeastHTTP11 && t.shouldSendChunkedRequestBody() {
91 t.TransferEncoding = []string{"chunked"}
95 if rr.Request != nil {
96 t.Method = rr.Request.Method
99 t.BodyCloser = rr.Body
100 t.ContentLength = rr.ContentLength
102 t.TransferEncoding = rr.TransferEncoding
103 t.Trailer = rr.Trailer
104 atLeastHTTP11 = rr.ProtoAtLeast(1, 1)
105 t.ResponseToHEAD = noResponseBodyExpected(t.Method)
108 // Sanitize Body,ContentLength,TransferEncoding
109 if t.ResponseToHEAD {
111 if chunked(t.TransferEncoding) {
115 if !atLeastHTTP11 || t.Body == nil {
116 t.TransferEncoding = nil
118 if chunked(t.TransferEncoding) {
120 } else if t.Body == nil { // no chunking, no body
126 if !chunked(t.TransferEncoding) {
133 // shouldSendChunkedRequestBody reports whether we should try to send a
134 // chunked request body to the server. In particular, the case we really
135 // want to prevent is sending a GET or other typically-bodyless request to a
136 // server with a chunked body when the body has zero bytes, since GETs with
137 // bodies (while acceptable according to specs), even zero-byte chunked
138 // bodies, are approximately never seen in the wild and confuse most
139 // servers. See Issue 18257, as one example.
141 // The only reason we'd send such a request is if the user set the Body to a
142 // non-nil value (say, ioutil.NopCloser(bytes.NewReader(nil))) and didn't
143 // set ContentLength, or NewRequest set it to -1 (unknown), so then we assume
144 // there's bytes to send.
146 // This code tries to read a byte from the Request.Body in such cases to see
147 // whether the body actually has content (super rare) or is actually just
148 // a non-nil content-less ReadCloser (the more common case). In that more
149 // common case, we act as if their Body were nil instead, and don't send
151 func (t *transferWriter) shouldSendChunkedRequestBody() bool {
152 // Note that t.ContentLength is the corrected content length
153 // from rr.outgoingLength, so 0 actually means zero, not unknown.
154 if t.ContentLength >= 0 || t.Body == nil { // redundant checks; caller did them
157 if requestMethodUsuallyLacksBody(t.Method) {
158 // Only probe the Request.Body for GET/HEAD/DELETE/etc
159 // requests, because it's only those types of requests
160 // that confuse servers.
161 t.probeRequestBody() // adjusts t.Body, t.ContentLength
164 // For all other request types (PUT, POST, PATCH, or anything
165 // made-up we've never heard of), assume it's normal and the server
166 // can deal with a chunked request body. Maybe we'll adjust this
171 // probeRequestBody reads a byte from t.Body to see whether it's empty
172 // (returns io.EOF right away).
174 // But because we've had problems with this blocking users in the past
175 // (issue 17480) when the body is a pipe (perhaps waiting on the response
176 // headers before the pipe is fed data), we need to be careful and bound how
177 // long we wait for it. This delay will only affect users if all the following
179 // * the request body blocks
180 // * the content length is not set (or set to -1)
181 // * the method doesn't usually have a body (GET, HEAD, DELETE, ...)
182 // * there is no transfer-encoding=chunked already set.
183 // In other words, this delay will not normally affect anybody, and there
184 // are workarounds if it does.
185 func (t *transferWriter) probeRequestBody() {
186 t.ByteReadCh = make(chan readResult, 1)
187 go func(body io.Reader) {
190 rres.n, rres.err = body.Read(buf[:])
196 timer := time.NewTimer(200 * time.Millisecond)
198 case rres := <-t.ByteReadCh:
200 if rres.n == 0 && rres.err == io.EOF {
204 } else if rres.n == 1 {
206 t.Body = io.MultiReader(&byteReader{b: rres.b}, errorReader{rres.err})
208 t.Body = io.MultiReader(&byteReader{b: rres.b}, t.Body)
210 } else if rres.err != nil {
211 t.Body = errorReader{rres.err}
214 // Too slow. Don't wait. Read it later, and keep
215 // assuming that this is ContentLength == -1
216 // (unknown), which means we'll send a
217 // "Transfer-Encoding: chunked" header.
218 t.Body = io.MultiReader(finishAsyncByteRead{t}, t.Body)
219 // Request that Request.Write flush the headers to the
220 // network before writing the body, since our body may not
221 // become readable until it's seen the response headers.
222 t.FlushHeaders = true
226 func noResponseBodyExpected(requestMethod string) bool {
227 return requestMethod == "HEAD"
230 func (t *transferWriter) shouldSendContentLength() bool {
231 if chunked(t.TransferEncoding) {
234 if t.ContentLength > 0 {
237 if t.ContentLength < 0 {
240 // Many servers expect a Content-Length for these methods
241 if t.Method == "POST" || t.Method == "PUT" {
244 if t.ContentLength == 0 && isIdentity(t.TransferEncoding) {
245 if t.Method == "GET" || t.Method == "HEAD" {
254 func (t *transferWriter) WriteHeader(w io.Writer) error {
256 if _, err := io.WriteString(w, "Connection: close\r\n"); err != nil {
261 // Write Content-Length and/or Transfer-Encoding whose values are a
262 // function of the sanitized field triple (Body, ContentLength,
264 if t.shouldSendContentLength() {
265 if _, err := io.WriteString(w, "Content-Length: "); err != nil {
268 if _, err := io.WriteString(w, strconv.FormatInt(t.ContentLength, 10)+"\r\n"); err != nil {
271 } else if chunked(t.TransferEncoding) {
272 if _, err := io.WriteString(w, "Transfer-Encoding: chunked\r\n"); err != nil {
277 // Write Trailer header
278 if t.Trailer != nil {
279 keys := make([]string, 0, len(t.Trailer))
280 for k := range t.Trailer {
281 k = CanonicalHeaderKey(k)
283 case "Transfer-Encoding", "Trailer", "Content-Length":
284 return &badStringError{"invalid Trailer key", k}
286 keys = append(keys, k)
290 // TODO: could do better allocation-wise here, but trailers are rare,
291 // so being lazy for now.
292 if _, err := io.WriteString(w, "Trailer: "+strings.Join(keys, ",")+"\r\n"); err != nil {
301 func (t *transferWriter) WriteBody(w io.Writer) error {
307 if chunked(t.TransferEncoding) {
308 if bw, ok := w.(*bufio.Writer); ok && !t.IsResponse {
309 w = &internal.FlushAfterChunkWriter{Writer: bw}
311 cw := internal.NewChunkedWriter(w)
312 _, err = io.Copy(cw, t.Body)
316 } else if t.ContentLength == -1 {
317 ncopy, err = io.Copy(w, t.Body)
319 ncopy, err = io.Copy(w, io.LimitReader(t.Body, t.ContentLength))
324 nextra, err = io.Copy(ioutil.Discard, t.Body)
331 if t.BodyCloser != nil {
332 if err := t.BodyCloser.Close(); err != nil {
337 if !t.ResponseToHEAD && t.ContentLength != -1 && t.ContentLength != ncopy {
338 return fmt.Errorf("http: ContentLength=%d with Body length %d",
339 t.ContentLength, ncopy)
342 if chunked(t.TransferEncoding) {
343 // Write Trailer header
344 if t.Trailer != nil {
345 if err := t.Trailer.Write(w); err != nil {
349 // Last chunk, empty trailer
350 _, err = io.WriteString(w, "\r\n")
355 type transferReader struct {
365 TransferEncoding []string
370 func (t *transferReader) protoAtLeast(m, n int) bool {
371 return t.ProtoMajor > m || (t.ProtoMajor == m && t.ProtoMinor >= n)
374 // bodyAllowedForStatus reports whether a given response status code
375 // permits a body. See RFC 2616, section 4.4.
376 func bodyAllowedForStatus(status int) bool {
378 case status >= 100 && status <= 199:
389 suppressedHeaders304 = []string{"Content-Type", "Content-Length", "Transfer-Encoding"}
390 suppressedHeadersNoBody = []string{"Content-Length", "Transfer-Encoding"}
393 func suppressedHeaders(status int) []string {
396 // RFC 2616 section 10.3.5: "the response MUST NOT include other entity-headers"
397 return suppressedHeaders304
398 case !bodyAllowedForStatus(status):
399 return suppressedHeadersNoBody
404 // msg is *Request or *Response.
405 func readTransfer(msg interface{}, r *bufio.Reader) (err error) {
406 t := &transferReader{RequestMethod: "GET"}
410 switch rr := msg.(type) {
413 t.StatusCode = rr.StatusCode
414 t.ProtoMajor = rr.ProtoMajor
415 t.ProtoMinor = rr.ProtoMinor
416 t.Close = shouldClose(t.ProtoMajor, t.ProtoMinor, t.Header, true)
418 if rr.Request != nil {
419 t.RequestMethod = rr.Request.Method
423 t.RequestMethod = rr.Method
424 t.ProtoMajor = rr.ProtoMajor
425 t.ProtoMinor = rr.ProtoMinor
426 // Transfer semantics for Requests are exactly like those for
427 // Responses with status code 200, responding to a GET method
431 panic("unexpected type")
434 // Default to HTTP/1.1
435 if t.ProtoMajor == 0 && t.ProtoMinor == 0 {
436 t.ProtoMajor, t.ProtoMinor = 1, 1
439 // Transfer encoding, content length
440 err = t.fixTransferEncoding()
445 realLength, err := fixLength(isResponse, t.StatusCode, t.RequestMethod, t.Header, t.TransferEncoding)
449 if isResponse && t.RequestMethod == "HEAD" {
450 if n, err := parseContentLength(t.Header.get("Content-Length")); err != nil {
456 t.ContentLength = realLength
460 t.Trailer, err = fixTrailer(t.Header, t.TransferEncoding)
465 // If there is no Content-Length or chunked Transfer-Encoding on a *Response
466 // and the status is not 1xx, 204 or 304, then the body is unbounded.
467 // See RFC 2616, section 4.4.
470 if realLength == -1 &&
471 !chunked(t.TransferEncoding) &&
472 bodyAllowedForStatus(t.StatusCode) {
478 // Prepare body reader. ContentLength < 0 means chunked encoding
479 // or close connection when finished, since multipart is not supported yet
481 case chunked(t.TransferEncoding):
482 if noResponseBodyExpected(t.RequestMethod) {
485 t.Body = &body{src: internal.NewChunkedReader(r), hdr: msg, r: r, closing: t.Close}
487 case realLength == 0:
490 t.Body = &body{src: io.LimitReader(r, realLength), closing: t.Close}
492 // realLength < 0, i.e. "Content-Length" not mentioned in header
494 // Close semantics (i.e. HTTP/1.0)
495 t.Body = &body{src: r, closing: t.Close}
497 // Persistent connection (i.e. HTTP/1.1)
503 switch rr := msg.(type) {
506 rr.ContentLength = t.ContentLength
507 rr.TransferEncoding = t.TransferEncoding
509 rr.Trailer = t.Trailer
512 rr.ContentLength = t.ContentLength
513 rr.TransferEncoding = t.TransferEncoding
515 rr.Trailer = t.Trailer
521 // Checks whether chunked is part of the encodings stack
522 func chunked(te []string) bool { return len(te) > 0 && te[0] == "chunked" }
524 // Checks whether the encoding is explicitly "identity".
525 func isIdentity(te []string) bool { return len(te) == 1 && te[0] == "identity" }
527 // fixTransferEncoding sanitizes t.TransferEncoding, if needed.
528 func (t *transferReader) fixTransferEncoding() error {
529 raw, present := t.Header["Transfer-Encoding"]
533 delete(t.Header, "Transfer-Encoding")
535 // Issue 12785; ignore Transfer-Encoding on HTTP/1.0 requests.
536 if !t.protoAtLeast(1, 1) {
540 encodings := strings.Split(raw[0], ",")
541 te := make([]string, 0, len(encodings))
542 // TODO: Even though we only support "identity" and "chunked"
543 // encodings, the loop below is designed with foresight. One
544 // invariant that must be maintained is that, if present,
545 // chunked encoding must always come first.
546 for _, encoding := range encodings {
547 encoding = strings.ToLower(strings.TrimSpace(encoding))
548 // "identity" encoding is not recorded
549 if encoding == "identity" {
552 if encoding != "chunked" {
553 return &badStringError{"unsupported transfer encoding", encoding}
555 te = te[0 : len(te)+1]
556 te[len(te)-1] = encoding
559 return &badStringError{"too many transfer encodings", strings.Join(te, ",")}
562 // RFC 7230 3.3.2 says "A sender MUST NOT send a
563 // Content-Length header field in any message that
564 // contains a Transfer-Encoding header field."
567 // "If a message is received with both a
568 // Transfer-Encoding and a Content-Length header
569 // field, the Transfer-Encoding overrides the
570 // Content-Length. Such a message might indicate an
571 // attempt to perform request smuggling (Section 9.5)
572 // or response splitting (Section 9.4) and ought to be
573 // handled as an error. A sender MUST remove the
574 // received Content-Length field prior to forwarding
575 // such a message downstream."
577 // Reportedly, these appear in the wild.
578 delete(t.Header, "Content-Length")
579 t.TransferEncoding = te
586 // Determine the expected body length, using RFC 2616 Section 4.4. This
587 // function is not a method, because ultimately it should be shared by
588 // ReadResponse and ReadRequest.
589 func fixLength(isResponse bool, status int, requestMethod string, header Header, te []string) (int64, error) {
590 isRequest := !isResponse
591 contentLens := header["Content-Length"]
593 // Hardening against HTTP request smuggling
594 if len(contentLens) > 1 {
595 // Per RFC 7230 Section 3.3.2, prevent multiple
596 // Content-Length headers if they differ in value.
597 // If there are dups of the value, remove the dups.
599 first := strings.TrimSpace(contentLens[0])
600 for _, ct := range contentLens[1:] {
601 if first != strings.TrimSpace(ct) {
602 return 0, fmt.Errorf("http: message cannot contain multiple Content-Length headers; got %q", contentLens)
606 // deduplicate Content-Length
607 header.Del("Content-Length")
608 header.Add("Content-Length", first)
610 contentLens = header["Content-Length"]
613 // Logic based on response type or status
614 if noResponseBodyExpected(requestMethod) {
615 // For HTTP requests, as part of hardening against request
616 // smuggling (RFC 7230), don't allow a Content-Length header for
617 // methods which don't permit bodies. As an exception, allow
618 // exactly one Content-Length header if its value is "0".
619 if isRequest && len(contentLens) > 0 && !(len(contentLens) == 1 && contentLens[0] == "0") {
620 return 0, fmt.Errorf("http: method cannot contain a Content-Length; got %q", contentLens)
632 // Logic based on Transfer-Encoding
637 // Logic based on Content-Length
639 if len(contentLens) == 1 {
640 cl = strings.TrimSpace(contentLens[0])
643 n, err := parseContentLength(cl)
649 header.Del("Content-Length")
653 // RFC 2616 neither explicitly permits nor forbids an
654 // entity-body on a GET request so we permit one if
655 // declared, but we default to 0 here (not -1 below)
656 // if there's no mention of a body.
657 // Likewise, all other request methods are assumed to have
658 // no body if neither Transfer-Encoding chunked nor a
659 // Content-Length are set.
663 // Body-EOF logic based on other methods (like closing, or chunked coding)
667 // Determine whether to hang up after sending a request and body, or
668 // receiving a response and body
669 // 'header' is the request headers
670 func shouldClose(major, minor int, header Header, removeCloseHeader bool) bool {
675 conv := header["Connection"]
676 hasClose := httplex.HeaderValuesContainsToken(conv, "close")
677 if major == 1 && minor == 0 {
678 return hasClose || !httplex.HeaderValuesContainsToken(conv, "keep-alive")
681 if hasClose && removeCloseHeader {
682 header.Del("Connection")
688 // Parse the trailer header
689 func fixTrailer(header Header, te []string) (Header, error) {
690 vv, ok := header["Trailer"]
694 header.Del("Trailer")
696 trailer := make(Header)
698 for _, v := range vv {
699 foreachHeaderElement(v, func(key string) {
700 key = CanonicalHeaderKey(key)
702 case "Transfer-Encoding", "Trailer", "Content-Length":
704 err = &badStringError{"bad trailer key", key}
714 if len(trailer) == 0 {
718 // Trailer and no chunking
719 return nil, ErrUnexpectedTrailer
724 // body turns a Reader into a ReadCloser.
725 // Close ensures that the body has been fully read
726 // and then reads the trailer if necessary.
729 hdr interface{} // non-nil (Response or Request) value means read trailer
730 r *bufio.Reader // underlying wire-format reader for the trailer
731 closing bool // is the connection to be closed after reading body?
732 doEarlyClose bool // whether Close should stop early
734 mu sync.Mutex // guards following, and calls to Read and Close
737 earlyClose bool // Close called and we didn't read to the end of src
738 onHitEOF func() // if non-nil, func to call when EOF is Read
741 // ErrBodyReadAfterClose is returned when reading a Request or Response
742 // Body after the body has been closed. This typically happens when the body is
743 // read after an HTTP Handler calls WriteHeader or Write on its
745 var ErrBodyReadAfterClose = errors.New("http: invalid Read on closed Body")
747 func (b *body) Read(p []byte) (n int, err error) {
751 return 0, ErrBodyReadAfterClose
753 return b.readLocked(p)
757 func (b *body) readLocked(p []byte) (n int, err error) {
761 n, err = b.src.Read(p)
765 // Chunked case. Read the trailer.
767 if e := b.readTrailer(); e != nil {
769 // Something went wrong in the trailer, we must not allow any
770 // further reads of any kind to succeed from body, nor any
771 // subsequent requests on the server connection. See
772 // golang.org/issue/12027
778 // If the server declared the Content-Length, our body is a LimitedReader
779 // and we need to check whether this EOF arrived early.
780 if lr, ok := b.src.(*io.LimitedReader); ok && lr.N > 0 {
781 err = io.ErrUnexpectedEOF
786 // If we can return an EOF here along with the read data, do
787 // so. This is optional per the io.Reader contract, but doing
788 // so helps the HTTP transport code recycle its connection
789 // earlier (since it will see this EOF itself), even if the
790 // client doesn't do future reads or Close.
791 if err == nil && n > 0 {
792 if lr, ok := b.src.(*io.LimitedReader); ok && lr.N == 0 {
798 if b.sawEOF && b.onHitEOF != nil {
806 singleCRLF = []byte("\r\n")
807 doubleCRLF = []byte("\r\n\r\n")
810 func seeUpcomingDoubleCRLF(r *bufio.Reader) bool {
811 for peekSize := 4; ; peekSize++ {
812 // This loop stops when Peek returns an error,
813 // which it does when r's buffer has been filled.
814 buf, err := r.Peek(peekSize)
815 if bytes.HasSuffix(buf, doubleCRLF) {
825 var errTrailerEOF = errors.New("http: unexpected EOF reading trailer")
827 func (b *body) readTrailer() error {
828 // The common case, since nobody uses trailers.
829 buf, err := b.r.Peek(2)
830 if bytes.Equal(buf, singleCRLF) {
841 // Make sure there's a header terminator coming up, to prevent
842 // a DoS with an unbounded size Trailer. It's not easy to
843 // slip in a LimitReader here, as textproto.NewReader requires
844 // a concrete *bufio.Reader. Also, we can't get all the way
845 // back up to our conn's LimitedReader that *might* be backing
846 // this bufio.Reader. Instead, a hack: we iteratively Peek up
847 // to the bufio.Reader's max size, looking for a double CRLF.
848 // This limits the trailer to the underlying buffer size, typically 4kB.
849 if !seeUpcomingDoubleCRLF(b.r) {
850 return errors.New("http: suspiciously long trailer after chunked body")
853 hdr, err := textproto.NewReader(b.r).ReadMIMEHeader()
860 switch rr := b.hdr.(type) {
862 mergeSetHeader(&rr.Trailer, Header(hdr))
864 mergeSetHeader(&rr.Trailer, Header(hdr))
869 func mergeSetHeader(dst *Header, src Header) {
874 for k, vv := range src {
879 // unreadDataSizeLocked returns the number of bytes of unread input.
880 // It returns -1 if unknown.
881 // b.mu must be held.
882 func (b *body) unreadDataSizeLocked() int64 {
883 if lr, ok := b.src.(*io.LimitedReader); ok {
889 func (b *body) Close() error {
898 // Already saw EOF, so no need going to look for it.
899 case b.hdr == nil && b.closing:
900 // no trailer and closing the connection next.
901 // no point in reading to EOF.
903 // Read up to maxPostHandlerReadBytes bytes of the body, looking for
904 // for EOF (and trailers), so we can re-use this connection.
905 if lr, ok := b.src.(*io.LimitedReader); ok && lr.N > maxPostHandlerReadBytes {
906 // There was a declared Content-Length, and we have more bytes remaining
907 // than our maxPostHandlerReadBytes tolerance. So, give up.
911 // Consume the body, or, which will also lead to us reading
912 // the trailer headers after the body, if present.
913 n, err = io.CopyN(ioutil.Discard, bodyLocked{b}, maxPostHandlerReadBytes)
917 if n == maxPostHandlerReadBytes {
922 // Fully consume the body, which will also lead to us reading
923 // the trailer headers after the body, if present.
924 _, err = io.Copy(ioutil.Discard, bodyLocked{b})
930 func (b *body) didEarlyClose() bool {
936 // bodyRemains reports whether future Read calls might
938 func (b *body) bodyRemains() bool {
944 func (b *body) registerOnHitEOF(fn func()) {
950 // bodyLocked is a io.Reader reading from a *body when its mutex is
952 type bodyLocked struct {
956 func (bl bodyLocked) Read(p []byte) (n int, err error) {
958 return 0, ErrBodyReadAfterClose
960 return bl.b.readLocked(p)
963 // parseContentLength trims whitespace from s and returns -1 if no value
964 // is set, or the value if it's >= 0.
965 func parseContentLength(cl string) (int64, error) {
966 cl = strings.TrimSpace(cl)
970 n, err := strconv.ParseInt(cl, 10, 64)
971 if err != nil || n < 0 {
972 return 0, &badStringError{"bad Content-Length", cl}
978 // finishAsyncByteRead finishes reading the 1-byte sniff
979 // from the ContentLength==0, Body!=nil case.
980 type finishAsyncByteRead struct {
984 func (fr finishAsyncByteRead) Read(p []byte) (n int, err error) {
988 rres := <-fr.tw.ByteReadCh
989 n, err = rres.n, rres.err