github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/net/http/transfer.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 package http 6 7 import ( 8 "bufio" 9 "bytes" 10 "errors" 11 "fmt" 12 "io" 13 "net/http/httptrace" 14 "net/http/internal" 15 "net/http/internal/ascii" 16 "net/textproto" 17 "reflect" 18 "sort" 19 "strconv" 20 "strings" 21 "sync" 22 "time" 23 24 "golang.org/x/net/http/httpguts" 25 ) 26 27 // ErrLineTooLong is returned when reading request or response bodies 28 // with malformed chunked encoding. 29 var ErrLineTooLong = internal.ErrLineTooLong 30 31 type errorReader struct { 32 err error 33 } 34 35 func (r errorReader) Read(p []byte) (n int, err error) { 36 return 0, r.err 37 } 38 39 type byteReader struct { 40 b byte 41 done bool 42 } 43 44 func (br *byteReader) Read(p []byte) (n int, err error) { 45 if br.done { 46 return 0, io.EOF 47 } 48 if len(p) == 0 { 49 return 0, nil 50 } 51 br.done = true 52 p[0] = br.b 53 return 1, io.EOF 54 } 55 56 // transferWriter inspects the fields of a user-supplied Request or Response, 57 // sanitizes them without changing the user object and provides methods for 58 // writing the respective header, body and trailer in wire format. 59 type transferWriter struct { 60 Method string 61 Body io.Reader 62 BodyCloser io.Closer 63 ResponseToHEAD bool 64 ContentLength int64 // -1 means unknown, 0 means exactly none 65 Close bool 66 TransferEncoding []string 67 Header Header 68 Trailer Header 69 IsResponse bool 70 bodyReadError error // any non-EOF error from reading Body 71 72 FlushHeaders bool // flush headers to network before body 73 ByteReadCh chan readResult // non-nil if probeRequestBody called 74 } 75 76 func newTransferWriter(r any) (t *transferWriter, err error) { 77 t = &transferWriter{} 78 79 // Extract relevant fields 80 atLeastHTTP11 := false 81 switch rr := r.(type) { 82 case *Request: 83 if rr.ContentLength != 0 && rr.Body == nil { 84 return nil, fmt.Errorf("http: Request.ContentLength=%d with nil Body", rr.ContentLength) 85 } 86 t.Method = valueOrDefault(rr.Method, "GET") 87 t.Close = rr.Close 88 t.TransferEncoding = rr.TransferEncoding 89 t.Header = rr.Header 90 t.Trailer = rr.Trailer 91 t.Body = rr.Body 92 t.BodyCloser = rr.Body 93 t.ContentLength = rr.outgoingLength() 94 if t.ContentLength < 0 && len(t.TransferEncoding) == 0 && t.shouldSendChunkedRequestBody() { 95 t.TransferEncoding = []string{"chunked"} 96 } 97 // If there's a body, conservatively flush the headers 98 // to any bufio.Writer we're writing to, just in case 99 // the server needs the headers early, before we copy 100 // the body and possibly block. We make an exception 101 // for the common standard library in-memory types, 102 // though, to avoid unnecessary TCP packets on the 103 // wire. (Issue 22088.) 104 if t.ContentLength != 0 && !isKnownInMemoryReader(t.Body) { 105 t.FlushHeaders = true 106 } 107 108 atLeastHTTP11 = true // Transport requests are always 1.1 or 2.0 109 case *Response: 110 t.IsResponse = true 111 if rr.Request != nil { 112 t.Method = rr.Request.Method 113 } 114 t.Body = rr.Body 115 t.BodyCloser = rr.Body 116 t.ContentLength = rr.ContentLength 117 t.Close = rr.Close 118 t.TransferEncoding = rr.TransferEncoding 119 t.Header = rr.Header 120 t.Trailer = rr.Trailer 121 atLeastHTTP11 = rr.ProtoAtLeast(1, 1) 122 t.ResponseToHEAD = noResponseBodyExpected(t.Method) 123 } 124 125 // Sanitize Body,ContentLength,TransferEncoding 126 if t.ResponseToHEAD { 127 t.Body = nil 128 if chunked(t.TransferEncoding) { 129 t.ContentLength = -1 130 } 131 } else { 132 if !atLeastHTTP11 || t.Body == nil { 133 t.TransferEncoding = nil 134 } 135 if chunked(t.TransferEncoding) { 136 t.ContentLength = -1 137 } else if t.Body == nil { // no chunking, no body 138 t.ContentLength = 0 139 } 140 } 141 142 // Sanitize Trailer 143 if !chunked(t.TransferEncoding) { 144 t.Trailer = nil 145 } 146 147 return t, nil 148 } 149 150 // shouldSendChunkedRequestBody reports whether we should try to send a 151 // chunked request body to the server. In particular, the case we really 152 // want to prevent is sending a GET or other typically-bodyless request to a 153 // server with a chunked body when the body has zero bytes, since GETs with 154 // bodies (while acceptable according to specs), even zero-byte chunked 155 // bodies, are approximately never seen in the wild and confuse most 156 // servers. See Issue 18257, as one example. 157 // 158 // The only reason we'd send such a request is if the user set the Body to a 159 // non-nil value (say, io.NopCloser(bytes.NewReader(nil))) and didn't 160 // set ContentLength, or NewRequest set it to -1 (unknown), so then we assume 161 // there's bytes to send. 162 // 163 // This code tries to read a byte from the Request.Body in such cases to see 164 // whether the body actually has content (super rare) or is actually just 165 // a non-nil content-less ReadCloser (the more common case). In that more 166 // common case, we act as if their Body were nil instead, and don't send 167 // a body. 168 func (t *transferWriter) shouldSendChunkedRequestBody() bool { 169 // Note that t.ContentLength is the corrected content length 170 // from rr.outgoingLength, so 0 actually means zero, not unknown. 171 if t.ContentLength >= 0 || t.Body == nil { // redundant checks; caller did them 172 return false 173 } 174 if t.Method == "CONNECT" { 175 return false 176 } 177 if requestMethodUsuallyLacksBody(t.Method) { 178 // Only probe the Request.Body for GET/HEAD/DELETE/etc 179 // requests, because it's only those types of requests 180 // that confuse servers. 181 t.probeRequestBody() // adjusts t.Body, t.ContentLength 182 return t.Body != nil 183 } 184 // For all other request types (PUT, POST, PATCH, or anything 185 // made-up we've never heard of), assume it's normal and the server 186 // can deal with a chunked request body. Maybe we'll adjust this 187 // later. 188 return true 189 } 190 191 // probeRequestBody reads a byte from t.Body to see whether it's empty 192 // (returns io.EOF right away). 193 // 194 // But because we've had problems with this blocking users in the past 195 // (issue 17480) when the body is a pipe (perhaps waiting on the response 196 // headers before the pipe is fed data), we need to be careful and bound how 197 // long we wait for it. This delay will only affect users if all the following 198 // are true: 199 // - the request body blocks 200 // - the content length is not set (or set to -1) 201 // - the method doesn't usually have a body (GET, HEAD, DELETE, ...) 202 // - there is no transfer-encoding=chunked already set. 203 // 204 // In other words, this delay will not normally affect anybody, and there 205 // are workarounds if it does. 206 func (t *transferWriter) probeRequestBody() { 207 t.ByteReadCh = make(chan readResult, 1) 208 go func(body io.Reader) { 209 var buf [1]byte 210 var rres readResult 211 rres.n, rres.err = body.Read(buf[:]) 212 if rres.n == 1 { 213 rres.b = buf[0] 214 } 215 t.ByteReadCh <- rres 216 close(t.ByteReadCh) 217 }(t.Body) 218 timer := time.NewTimer(200 * time.Millisecond) 219 select { 220 case rres := <-t.ByteReadCh: 221 timer.Stop() 222 if rres.n == 0 && rres.err == io.EOF { 223 // It was empty. 224 t.Body = nil 225 t.ContentLength = 0 226 } else if rres.n == 1 { 227 if rres.err != nil { 228 t.Body = io.MultiReader(&byteReader{b: rres.b}, errorReader{rres.err}) 229 } else { 230 t.Body = io.MultiReader(&byteReader{b: rres.b}, t.Body) 231 } 232 } else if rres.err != nil { 233 t.Body = errorReader{rres.err} 234 } 235 case <-timer.C: 236 // Too slow. Don't wait. Read it later, and keep 237 // assuming that this is ContentLength == -1 238 // (unknown), which means we'll send a 239 // "Transfer-Encoding: chunked" header. 240 t.Body = io.MultiReader(finishAsyncByteRead{t}, t.Body) 241 // Request that Request.Write flush the headers to the 242 // network before writing the body, since our body may not 243 // become readable until it's seen the response headers. 244 t.FlushHeaders = true 245 } 246 } 247 248 func noResponseBodyExpected(requestMethod string) bool { 249 return requestMethod == "HEAD" 250 } 251 252 func (t *transferWriter) shouldSendContentLength() bool { 253 if chunked(t.TransferEncoding) { 254 return false 255 } 256 if t.ContentLength > 0 { 257 return true 258 } 259 if t.ContentLength < 0 { 260 return false 261 } 262 // Many servers expect a Content-Length for these methods 263 if t.Method == "POST" || t.Method == "PUT" || t.Method == "PATCH" { 264 return true 265 } 266 if t.ContentLength == 0 && isIdentity(t.TransferEncoding) { 267 if t.Method == "GET" || t.Method == "HEAD" { 268 return false 269 } 270 return true 271 } 272 273 return false 274 } 275 276 func (t *transferWriter) writeHeader(w io.Writer, trace *httptrace.ClientTrace) error { 277 if t.Close && !hasToken(t.Header.get("Connection"), "close") { 278 if _, err := io.WriteString(w, "Connection: close\r\n"); err != nil { 279 return err 280 } 281 if trace != nil && trace.WroteHeaderField != nil { 282 trace.WroteHeaderField("Connection", []string{"close"}) 283 } 284 } 285 286 // Write Content-Length and/or Transfer-Encoding whose values are a 287 // function of the sanitized field triple (Body, ContentLength, 288 // TransferEncoding) 289 if t.shouldSendContentLength() { 290 if _, err := io.WriteString(w, "Content-Length: "); err != nil { 291 return err 292 } 293 if _, err := io.WriteString(w, strconv.FormatInt(t.ContentLength, 10)+"\r\n"); err != nil { 294 return err 295 } 296 if trace != nil && trace.WroteHeaderField != nil { 297 trace.WroteHeaderField("Content-Length", []string{strconv.FormatInt(t.ContentLength, 10)}) 298 } 299 } else if chunked(t.TransferEncoding) { 300 if _, err := io.WriteString(w, "Transfer-Encoding: chunked\r\n"); err != nil { 301 return err 302 } 303 if trace != nil && trace.WroteHeaderField != nil { 304 trace.WroteHeaderField("Transfer-Encoding", []string{"chunked"}) 305 } 306 } 307 308 // Write Trailer header 309 if t.Trailer != nil { 310 keys := make([]string, 0, len(t.Trailer)) 311 for k := range t.Trailer { 312 k = CanonicalHeaderKey(k) 313 switch k { 314 case "Transfer-Encoding", "Trailer", "Content-Length": 315 return badStringError("invalid Trailer key", k) 316 } 317 keys = append(keys, k) 318 } 319 if len(keys) > 0 { 320 sort.Strings(keys) 321 // TODO: could do better allocation-wise here, but trailers are rare, 322 // so being lazy for now. 323 if _, err := io.WriteString(w, "Trailer: "+strings.Join(keys, ",")+"\r\n"); err != nil { 324 return err 325 } 326 if trace != nil && trace.WroteHeaderField != nil { 327 trace.WroteHeaderField("Trailer", keys) 328 } 329 } 330 } 331 332 return nil 333 } 334 335 // always closes t.BodyCloser 336 func (t *transferWriter) writeBody(w io.Writer) (err error) { 337 var ncopy int64 338 closed := false 339 defer func() { 340 if closed || t.BodyCloser == nil { 341 return 342 } 343 if closeErr := t.BodyCloser.Close(); closeErr != nil && err == nil { 344 err = closeErr 345 } 346 }() 347 348 // Write body. We "unwrap" the body first if it was wrapped in a 349 // nopCloser or readTrackingBody. This is to ensure that we can take advantage of 350 // OS-level optimizations in the event that the body is an 351 // *os.File. 352 if t.Body != nil { 353 var body = t.unwrapBody() 354 if chunked(t.TransferEncoding) { 355 if bw, ok := w.(*bufio.Writer); ok && !t.IsResponse { 356 w = &internal.FlushAfterChunkWriter{Writer: bw} 357 } 358 cw := internal.NewChunkedWriter(w) 359 _, err = t.doBodyCopy(cw, body) 360 if err == nil { 361 err = cw.Close() 362 } 363 } else if t.ContentLength == -1 { 364 dst := w 365 if t.Method == "CONNECT" { 366 dst = bufioFlushWriter{dst} 367 } 368 ncopy, err = t.doBodyCopy(dst, body) 369 } else { 370 ncopy, err = t.doBodyCopy(w, io.LimitReader(body, t.ContentLength)) 371 if err != nil { 372 return err 373 } 374 var nextra int64 375 nextra, err = t.doBodyCopy(io.Discard, body) 376 ncopy += nextra 377 } 378 if err != nil { 379 return err 380 } 381 } 382 if t.BodyCloser != nil { 383 closed = true 384 if err := t.BodyCloser.Close(); err != nil { 385 return err 386 } 387 } 388 389 if !t.ResponseToHEAD && t.ContentLength != -1 && t.ContentLength != ncopy { 390 return fmt.Errorf("http: ContentLength=%d with Body length %d", 391 t.ContentLength, ncopy) 392 } 393 394 if chunked(t.TransferEncoding) { 395 // Write Trailer header 396 if t.Trailer != nil { 397 if err := t.Trailer.Write(w); err != nil { 398 return err 399 } 400 } 401 // Last chunk, empty trailer 402 _, err = io.WriteString(w, "\r\n") 403 } 404 return err 405 } 406 407 // doBodyCopy wraps a copy operation, with any resulting error also 408 // being saved in bodyReadError. 409 // 410 // This function is only intended for use in writeBody. 411 func (t *transferWriter) doBodyCopy(dst io.Writer, src io.Reader) (n int64, err error) { 412 n, err = io.Copy(dst, src) 413 if err != nil && err != io.EOF { 414 t.bodyReadError = err 415 } 416 return 417 } 418 419 // unwrapBody unwraps the body's inner reader if it's a 420 // nopCloser. This is to ensure that body writes sourced from local 421 // files (*os.File types) are properly optimized. 422 // 423 // This function is only intended for use in writeBody. 424 func (t *transferWriter) unwrapBody() io.Reader { 425 if r, ok := unwrapNopCloser(t.Body); ok { 426 return r 427 } 428 if r, ok := t.Body.(*readTrackingBody); ok { 429 r.didRead = true 430 return r.ReadCloser 431 } 432 return t.Body 433 } 434 435 type transferReader struct { 436 // Input 437 Header Header 438 StatusCode int 439 RequestMethod string 440 ProtoMajor int 441 ProtoMinor int 442 // Output 443 Body io.ReadCloser 444 ContentLength int64 445 Chunked bool 446 Close bool 447 Trailer Header 448 } 449 450 func (t *transferReader) protoAtLeast(m, n int) bool { 451 return t.ProtoMajor > m || (t.ProtoMajor == m && t.ProtoMinor >= n) 452 } 453 454 // bodyAllowedForStatus reports whether a given response status code 455 // permits a body. See RFC 7230, section 3.3. 456 func bodyAllowedForStatus(status int) bool { 457 switch { 458 case status >= 100 && status <= 199: 459 return false 460 case status == 204: 461 return false 462 case status == 304: 463 return false 464 } 465 return true 466 } 467 468 var ( 469 suppressedHeaders304 = []string{"Content-Type", "Content-Length", "Transfer-Encoding"} 470 suppressedHeadersNoBody = []string{"Content-Length", "Transfer-Encoding"} 471 excludedHeadersNoBody = map[string]bool{"Content-Length": true, "Transfer-Encoding": true} 472 ) 473 474 func suppressedHeaders(status int) []string { 475 switch { 476 case status == 304: 477 // RFC 7232 section 4.1 478 return suppressedHeaders304 479 case !bodyAllowedForStatus(status): 480 return suppressedHeadersNoBody 481 } 482 return nil 483 } 484 485 // msg is *Request or *Response. 486 func readTransfer(msg any, r *bufio.Reader) (err error) { 487 t := &transferReader{RequestMethod: "GET"} 488 489 // Unify input 490 isResponse := false 491 switch rr := msg.(type) { 492 case *Response: 493 t.Header = rr.Header 494 t.StatusCode = rr.StatusCode 495 t.ProtoMajor = rr.ProtoMajor 496 t.ProtoMinor = rr.ProtoMinor 497 t.Close = shouldClose(t.ProtoMajor, t.ProtoMinor, t.Header, true) 498 isResponse = true 499 if rr.Request != nil { 500 t.RequestMethod = rr.Request.Method 501 } 502 case *Request: 503 t.Header = rr.Header 504 t.RequestMethod = rr.Method 505 t.ProtoMajor = rr.ProtoMajor 506 t.ProtoMinor = rr.ProtoMinor 507 // Transfer semantics for Requests are exactly like those for 508 // Responses with status code 200, responding to a GET method 509 t.StatusCode = 200 510 t.Close = rr.Close 511 default: 512 panic("unexpected type") 513 } 514 515 // Default to HTTP/1.1 516 if t.ProtoMajor == 0 && t.ProtoMinor == 0 { 517 t.ProtoMajor, t.ProtoMinor = 1, 1 518 } 519 520 // Transfer-Encoding: chunked, and overriding Content-Length. 521 if err := t.parseTransferEncoding(); err != nil { 522 return err 523 } 524 525 realLength, err := fixLength(isResponse, t.StatusCode, t.RequestMethod, t.Header, t.Chunked) 526 if err != nil { 527 return err 528 } 529 if isResponse && t.RequestMethod == "HEAD" { 530 if n, err := parseContentLength(t.Header.get("Content-Length")); err != nil { 531 return err 532 } else { 533 t.ContentLength = n 534 } 535 } else { 536 t.ContentLength = realLength 537 } 538 539 // Trailer 540 t.Trailer, err = fixTrailer(t.Header, t.Chunked) 541 if err != nil { 542 return err 543 } 544 545 // If there is no Content-Length or chunked Transfer-Encoding on a *Response 546 // and the status is not 1xx, 204 or 304, then the body is unbounded. 547 // See RFC 7230, section 3.3. 548 switch msg.(type) { 549 case *Response: 550 if realLength == -1 && !t.Chunked && bodyAllowedForStatus(t.StatusCode) { 551 // Unbounded body. 552 t.Close = true 553 } 554 } 555 556 // Prepare body reader. ContentLength < 0 means chunked encoding 557 // or close connection when finished, since multipart is not supported yet 558 switch { 559 case t.Chunked: 560 if isResponse && (noResponseBodyExpected(t.RequestMethod) || !bodyAllowedForStatus(t.StatusCode)) { 561 t.Body = NoBody 562 } else { 563 t.Body = &body{src: internal.NewChunkedReader(r), hdr: msg, r: r, closing: t.Close} 564 } 565 case realLength == 0: 566 t.Body = NoBody 567 case realLength > 0: 568 t.Body = &body{src: io.LimitReader(r, realLength), closing: t.Close} 569 default: 570 // realLength < 0, i.e. "Content-Length" not mentioned in header 571 if t.Close { 572 // Close semantics (i.e. HTTP/1.0) 573 t.Body = &body{src: r, closing: t.Close} 574 } else { 575 // Persistent connection (i.e. HTTP/1.1) 576 t.Body = NoBody 577 } 578 } 579 580 // Unify output 581 switch rr := msg.(type) { 582 case *Request: 583 rr.Body = t.Body 584 rr.ContentLength = t.ContentLength 585 if t.Chunked { 586 rr.TransferEncoding = []string{"chunked"} 587 } 588 rr.Close = t.Close 589 rr.Trailer = t.Trailer 590 case *Response: 591 rr.Body = t.Body 592 rr.ContentLength = t.ContentLength 593 if t.Chunked { 594 rr.TransferEncoding = []string{"chunked"} 595 } 596 rr.Close = t.Close 597 rr.Trailer = t.Trailer 598 } 599 600 return nil 601 } 602 603 // Checks whether chunked is part of the encodings stack. 604 func chunked(te []string) bool { return len(te) > 0 && te[0] == "chunked" } 605 606 // Checks whether the encoding is explicitly "identity". 607 func isIdentity(te []string) bool { return len(te) == 1 && te[0] == "identity" } 608 609 // unsupportedTEError reports unsupported transfer-encodings. 610 type unsupportedTEError struct { 611 err string 612 } 613 614 func (uste *unsupportedTEError) Error() string { 615 return uste.err 616 } 617 618 // isUnsupportedTEError checks if the error is of type 619 // unsupportedTEError. It is usually invoked with a non-nil err. 620 func isUnsupportedTEError(err error) bool { 621 _, ok := err.(*unsupportedTEError) 622 return ok 623 } 624 625 // parseTransferEncoding sets t.Chunked based on the Transfer-Encoding header. 626 func (t *transferReader) parseTransferEncoding() error { 627 raw, present := t.Header["Transfer-Encoding"] 628 if !present { 629 return nil 630 } 631 delete(t.Header, "Transfer-Encoding") 632 633 // Issue 12785; ignore Transfer-Encoding on HTTP/1.0 requests. 634 if !t.protoAtLeast(1, 1) { 635 return nil 636 } 637 638 // Like nginx, we only support a single Transfer-Encoding header field, and 639 // only if set to "chunked". This is one of the most security sensitive 640 // surfaces in HTTP/1.1 due to the risk of request smuggling, so we keep it 641 // strict and simple. 642 if len(raw) != 1 { 643 return &unsupportedTEError{fmt.Sprintf("too many transfer encodings: %q", raw)} 644 } 645 if !ascii.EqualFold(raw[0], "chunked") { 646 return &unsupportedTEError{fmt.Sprintf("unsupported transfer encoding: %q", raw[0])} 647 } 648 649 // RFC 7230 3.3.2 says "A sender MUST NOT send a Content-Length header field 650 // in any message that contains a Transfer-Encoding header field." 651 // 652 // but also: "If a message is received with both a Transfer-Encoding and a 653 // Content-Length header field, the Transfer-Encoding overrides the 654 // Content-Length. Such a message might indicate an attempt to perform 655 // request smuggling (Section 9.5) or response splitting (Section 9.4) and 656 // ought to be handled as an error. A sender MUST remove the received 657 // Content-Length field prior to forwarding such a message downstream." 658 // 659 // Reportedly, these appear in the wild. 660 delete(t.Header, "Content-Length") 661 662 t.Chunked = true 663 return nil 664 } 665 666 // Determine the expected body length, using RFC 7230 Section 3.3. This 667 // function is not a method, because ultimately it should be shared by 668 // ReadResponse and ReadRequest. 669 func fixLength(isResponse bool, status int, requestMethod string, header Header, chunked bool) (int64, error) { 670 isRequest := !isResponse 671 contentLens := header["Content-Length"] 672 673 // Hardening against HTTP request smuggling 674 if len(contentLens) > 1 { 675 // Per RFC 7230 Section 3.3.2, prevent multiple 676 // Content-Length headers if they differ in value. 677 // If there are dups of the value, remove the dups. 678 // See Issue 16490. 679 first := textproto.TrimString(contentLens[0]) 680 for _, ct := range contentLens[1:] { 681 if first != textproto.TrimString(ct) { 682 return 0, fmt.Errorf("http: message cannot contain multiple Content-Length headers; got %q", contentLens) 683 } 684 } 685 686 // deduplicate Content-Length 687 header.Del("Content-Length") 688 header.Add("Content-Length", first) 689 690 contentLens = header["Content-Length"] 691 } 692 693 // Logic based on response type or status 694 if isResponse && noResponseBodyExpected(requestMethod) { 695 return 0, nil 696 } 697 if status/100 == 1 { 698 return 0, nil 699 } 700 switch status { 701 case 204, 304: 702 return 0, nil 703 } 704 705 // Logic based on Transfer-Encoding 706 if chunked { 707 return -1, nil 708 } 709 710 // Logic based on Content-Length 711 var cl string 712 if len(contentLens) == 1 { 713 cl = textproto.TrimString(contentLens[0]) 714 } 715 if cl != "" { 716 n, err := parseContentLength(cl) 717 if err != nil { 718 return -1, err 719 } 720 return n, nil 721 } 722 header.Del("Content-Length") 723 724 if isRequest { 725 // RFC 7230 neither explicitly permits nor forbids an 726 // entity-body on a GET request so we permit one if 727 // declared, but we default to 0 here (not -1 below) 728 // if there's no mention of a body. 729 // Likewise, all other request methods are assumed to have 730 // no body if neither Transfer-Encoding chunked nor a 731 // Content-Length are set. 732 return 0, nil 733 } 734 735 // Body-EOF logic based on other methods (like closing, or chunked coding) 736 return -1, nil 737 } 738 739 // Determine whether to hang up after sending a request and body, or 740 // receiving a response and body 741 // 'header' is the request headers. 742 func shouldClose(major, minor int, header Header, removeCloseHeader bool) bool { 743 if major < 1 { 744 return true 745 } 746 747 conv := header["Connection"] 748 hasClose := httpguts.HeaderValuesContainsToken(conv, "close") 749 if major == 1 && minor == 0 { 750 return hasClose || !httpguts.HeaderValuesContainsToken(conv, "keep-alive") 751 } 752 753 if hasClose && removeCloseHeader { 754 header.Del("Connection") 755 } 756 757 return hasClose 758 } 759 760 // Parse the trailer header. 761 func fixTrailer(header Header, chunked bool) (Header, error) { 762 vv, ok := header["Trailer"] 763 if !ok { 764 return nil, nil 765 } 766 if !chunked { 767 // Trailer and no chunking: 768 // this is an invalid use case for trailer header. 769 // Nevertheless, no error will be returned and we 770 // let users decide if this is a valid HTTP message. 771 // The Trailer header will be kept in Response.Header 772 // but not populate Response.Trailer. 773 // See issue #27197. 774 return nil, nil 775 } 776 header.Del("Trailer") 777 778 trailer := make(Header) 779 var err error 780 for _, v := range vv { 781 foreachHeaderElement(v, func(key string) { 782 key = CanonicalHeaderKey(key) 783 switch key { 784 case "Transfer-Encoding", "Trailer", "Content-Length": 785 if err == nil { 786 err = badStringError("bad trailer key", key) 787 return 788 } 789 } 790 trailer[key] = nil 791 }) 792 } 793 if err != nil { 794 return nil, err 795 } 796 if len(trailer) == 0 { 797 return nil, nil 798 } 799 return trailer, nil 800 } 801 802 // body turns a Reader into a ReadCloser. 803 // Close ensures that the body has been fully read 804 // and then reads the trailer if necessary. 805 type body struct { 806 src io.Reader 807 hdr any // non-nil (Response or Request) value means read trailer 808 r *bufio.Reader // underlying wire-format reader for the trailer 809 closing bool // is the connection to be closed after reading body? 810 doEarlyClose bool // whether Close should stop early 811 812 mu sync.Mutex // guards following, and calls to Read and Close 813 sawEOF bool 814 closed bool 815 earlyClose bool // Close called and we didn't read to the end of src 816 onHitEOF func() // if non-nil, func to call when EOF is Read 817 } 818 819 // ErrBodyReadAfterClose is returned when reading a Request or Response 820 // Body after the body has been closed. This typically happens when the body is 821 // read after an HTTP Handler calls WriteHeader or Write on its 822 // ResponseWriter. 823 var ErrBodyReadAfterClose = errors.New("http: invalid Read on closed Body") 824 825 func (b *body) Read(p []byte) (n int, err error) { 826 b.mu.Lock() 827 defer b.mu.Unlock() 828 if b.closed { 829 return 0, ErrBodyReadAfterClose 830 } 831 return b.readLocked(p) 832 } 833 834 // Must hold b.mu. 835 func (b *body) readLocked(p []byte) (n int, err error) { 836 if b.sawEOF { 837 return 0, io.EOF 838 } 839 n, err = b.src.Read(p) 840 841 if err == io.EOF { 842 b.sawEOF = true 843 // Chunked case. Read the trailer. 844 if b.hdr != nil { 845 if e := b.readTrailer(); e != nil { 846 err = e 847 // Something went wrong in the trailer, we must not allow any 848 // further reads of any kind to succeed from body, nor any 849 // subsequent requests on the server connection. See 850 // golang.org/issue/12027 851 b.sawEOF = false 852 b.closed = true 853 } 854 b.hdr = nil 855 } else { 856 // If the server declared the Content-Length, our body is a LimitedReader 857 // and we need to check whether this EOF arrived early. 858 if lr, ok := b.src.(*io.LimitedReader); ok && lr.N > 0 { 859 err = io.ErrUnexpectedEOF 860 } 861 } 862 } 863 864 // If we can return an EOF here along with the read data, do 865 // so. This is optional per the io.Reader contract, but doing 866 // so helps the HTTP transport code recycle its connection 867 // earlier (since it will see this EOF itself), even if the 868 // client doesn't do future reads or Close. 869 if err == nil && n > 0 { 870 if lr, ok := b.src.(*io.LimitedReader); ok && lr.N == 0 { 871 err = io.EOF 872 b.sawEOF = true 873 } 874 } 875 876 if b.sawEOF && b.onHitEOF != nil { 877 b.onHitEOF() 878 } 879 880 return n, err 881 } 882 883 var ( 884 singleCRLF = []byte("\r\n") 885 doubleCRLF = []byte("\r\n\r\n") 886 ) 887 888 func seeUpcomingDoubleCRLF(r *bufio.Reader) bool { 889 for peekSize := 4; ; peekSize++ { 890 // This loop stops when Peek returns an error, 891 // which it does when r's buffer has been filled. 892 buf, err := r.Peek(peekSize) 893 if bytes.HasSuffix(buf, doubleCRLF) { 894 return true 895 } 896 if err != nil { 897 break 898 } 899 } 900 return false 901 } 902 903 var errTrailerEOF = errors.New("http: unexpected EOF reading trailer") 904 905 func (b *body) readTrailer() error { 906 // The common case, since nobody uses trailers. 907 buf, err := b.r.Peek(2) 908 if bytes.Equal(buf, singleCRLF) { 909 b.r.Discard(2) 910 return nil 911 } 912 if len(buf) < 2 { 913 return errTrailerEOF 914 } 915 if err != nil { 916 return err 917 } 918 919 // Make sure there's a header terminator coming up, to prevent 920 // a DoS with an unbounded size Trailer. It's not easy to 921 // slip in a LimitReader here, as textproto.NewReader requires 922 // a concrete *bufio.Reader. Also, we can't get all the way 923 // back up to our conn's LimitedReader that *might* be backing 924 // this bufio.Reader. Instead, a hack: we iteratively Peek up 925 // to the bufio.Reader's max size, looking for a double CRLF. 926 // This limits the trailer to the underlying buffer size, typically 4kB. 927 if !seeUpcomingDoubleCRLF(b.r) { 928 return errors.New("http: suspiciously long trailer after chunked body") 929 } 930 931 hdr, err := textproto.NewReader(b.r).ReadMIMEHeader() 932 if err != nil { 933 if err == io.EOF { 934 return errTrailerEOF 935 } 936 return err 937 } 938 switch rr := b.hdr.(type) { 939 case *Request: 940 mergeSetHeader(&rr.Trailer, Header(hdr)) 941 case *Response: 942 mergeSetHeader(&rr.Trailer, Header(hdr)) 943 } 944 return nil 945 } 946 947 func mergeSetHeader(dst *Header, src Header) { 948 if *dst == nil { 949 *dst = src 950 return 951 } 952 for k, vv := range src { 953 (*dst)[k] = vv 954 } 955 } 956 957 // unreadDataSizeLocked returns the number of bytes of unread input. 958 // It returns -1 if unknown. 959 // b.mu must be held. 960 func (b *body) unreadDataSizeLocked() int64 { 961 if lr, ok := b.src.(*io.LimitedReader); ok { 962 return lr.N 963 } 964 return -1 965 } 966 967 func (b *body) Close() error { 968 b.mu.Lock() 969 defer b.mu.Unlock() 970 if b.closed { 971 return nil 972 } 973 var err error 974 switch { 975 case b.sawEOF: 976 // Already saw EOF, so no need going to look for it. 977 case b.hdr == nil && b.closing: 978 // no trailer and closing the connection next. 979 // no point in reading to EOF. 980 case b.doEarlyClose: 981 // Read up to maxPostHandlerReadBytes bytes of the body, looking 982 // for EOF (and trailers), so we can re-use this connection. 983 if lr, ok := b.src.(*io.LimitedReader); ok && lr.N > maxPostHandlerReadBytes { 984 // There was a declared Content-Length, and we have more bytes remaining 985 // than our maxPostHandlerReadBytes tolerance. So, give up. 986 b.earlyClose = true 987 } else { 988 var n int64 989 // Consume the body, or, which will also lead to us reading 990 // the trailer headers after the body, if present. 991 n, err = io.CopyN(io.Discard, bodyLocked{b}, maxPostHandlerReadBytes) 992 if err == io.EOF { 993 err = nil 994 } 995 if n == maxPostHandlerReadBytes { 996 b.earlyClose = true 997 } 998 } 999 default: 1000 // Fully consume the body, which will also lead to us reading 1001 // the trailer headers after the body, if present. 1002 _, err = io.Copy(io.Discard, bodyLocked{b}) 1003 } 1004 b.closed = true 1005 return err 1006 } 1007 1008 func (b *body) didEarlyClose() bool { 1009 b.mu.Lock() 1010 defer b.mu.Unlock() 1011 return b.earlyClose 1012 } 1013 1014 // bodyRemains reports whether future Read calls might 1015 // yield data. 1016 func (b *body) bodyRemains() bool { 1017 b.mu.Lock() 1018 defer b.mu.Unlock() 1019 return !b.sawEOF 1020 } 1021 1022 func (b *body) registerOnHitEOF(fn func()) { 1023 b.mu.Lock() 1024 defer b.mu.Unlock() 1025 b.onHitEOF = fn 1026 } 1027 1028 // bodyLocked is an io.Reader reading from a *body when its mutex is 1029 // already held. 1030 type bodyLocked struct { 1031 b *body 1032 } 1033 1034 func (bl bodyLocked) Read(p []byte) (n int, err error) { 1035 if bl.b.closed { 1036 return 0, ErrBodyReadAfterClose 1037 } 1038 return bl.b.readLocked(p) 1039 } 1040 1041 // parseContentLength trims whitespace from s and returns -1 if no value 1042 // is set, or the value if it's >= 0. 1043 func parseContentLength(cl string) (int64, error) { 1044 cl = textproto.TrimString(cl) 1045 if cl == "" { 1046 return -1, nil 1047 } 1048 n, err := strconv.ParseUint(cl, 10, 63) 1049 if err != nil { 1050 return 0, badStringError("bad Content-Length", cl) 1051 } 1052 return int64(n), nil 1053 1054 } 1055 1056 // finishAsyncByteRead finishes reading the 1-byte sniff 1057 // from the ContentLength==0, Body!=nil case. 1058 type finishAsyncByteRead struct { 1059 tw *transferWriter 1060 } 1061 1062 func (fr finishAsyncByteRead) Read(p []byte) (n int, err error) { 1063 if len(p) == 0 { 1064 return 1065 } 1066 rres := <-fr.tw.ByteReadCh 1067 n, err = rres.n, rres.err 1068 if n == 1 { 1069 p[0] = rres.b 1070 } 1071 if err == nil { 1072 err = io.EOF 1073 } 1074 return 1075 } 1076 1077 var nopCloserType = reflect.TypeOf(io.NopCloser(nil)) 1078 var nopCloserWriterToType = reflect.TypeOf(io.NopCloser(struct { 1079 io.Reader 1080 io.WriterTo 1081 }{})) 1082 1083 // unwrapNopCloser return the underlying reader and true if r is a NopCloser 1084 // else it return false. 1085 func unwrapNopCloser(r io.Reader) (underlyingReader io.Reader, isNopCloser bool) { 1086 switch reflect.TypeOf(r) { 1087 case nopCloserType, nopCloserWriterToType: 1088 return reflect.ValueOf(r).Field(0).Interface().(io.Reader), true 1089 default: 1090 return nil, false 1091 } 1092 } 1093 1094 // isKnownInMemoryReader reports whether r is a type known to not 1095 // block on Read. Its caller uses this as an optional optimization to 1096 // send fewer TCP packets. 1097 func isKnownInMemoryReader(r io.Reader) bool { 1098 switch r.(type) { 1099 case *bytes.Reader, *bytes.Buffer, *strings.Reader: 1100 return true 1101 } 1102 if r, ok := unwrapNopCloser(r); ok { 1103 return isKnownInMemoryReader(r) 1104 } 1105 if r, ok := r.(*readTrackingBody); ok { 1106 return isKnownInMemoryReader(r.ReadCloser) 1107 } 1108 return false 1109 } 1110 1111 // bufioFlushWriter is an io.Writer wrapper that flushes all writes 1112 // on its wrapped writer if it's a *bufio.Writer. 1113 type bufioFlushWriter struct{ w io.Writer } 1114 1115 func (fw bufioFlushWriter) Write(p []byte) (n int, err error) { 1116 n, err = fw.w.Write(p) 1117 if bw, ok := fw.w.(*bufio.Writer); n > 0 && ok { 1118 ferr := bw.Flush() 1119 if ferr != nil && err == nil { 1120 err = ferr 1121 } 1122 } 1123 return 1124 }