msg is *http.Request or *http.Response.
(msg any, r *bufio.Reader)
| 448 | |
| 449 | // msg is *http.Request or *http.Response. |
| 450 | func readTransfer(msg any, r *bufio.Reader) (err error) { |
| 451 | t := &transferReader{RequestMethod: "GET"} |
| 452 | |
| 453 | // Unify input |
| 454 | isResponse := false |
| 455 | switch rr := msg.(type) { |
| 456 | case *http.Response: |
| 457 | t.Header = rr.Header |
| 458 | t.StatusCode = rr.StatusCode |
| 459 | t.ProtoMajor = rr.ProtoMajor |
| 460 | t.ProtoMinor = rr.ProtoMinor |
| 461 | t.Close = shouldClose(t.ProtoMajor, t.ProtoMinor, t.Header, true) |
| 462 | isResponse = true |
| 463 | if rr.Request != nil { |
| 464 | t.RequestMethod = rr.Request.Method |
| 465 | } |
| 466 | default: |
| 467 | panic("unexpected type") |
| 468 | } |
| 469 | |
| 470 | // Default to HTTP/1.1 |
| 471 | if t.ProtoMajor == 0 && t.ProtoMinor == 0 { |
| 472 | t.ProtoMajor, t.ProtoMinor = 1, 1 |
| 473 | } |
| 474 | |
| 475 | // Transfer-Encoding: chunked, and overriding Content-Length. |
| 476 | if err := t.parseTransferEncoding(); err != nil { |
| 477 | return err |
| 478 | } |
| 479 | |
| 480 | realLength, err := fixLength(isResponse, t.StatusCode, t.RequestMethod, t.Header, t.Chunked) |
| 481 | if err != nil { |
| 482 | return err |
| 483 | } |
| 484 | if isResponse && t.RequestMethod == "HEAD" { |
| 485 | if n, err := parseContentLength(t.Header["Content-Length"]); err != nil { |
| 486 | return err |
| 487 | } else { |
| 488 | t.ContentLength = n |
| 489 | } |
| 490 | } else { |
| 491 | t.ContentLength = realLength |
| 492 | } |
| 493 | |
| 494 | // Trailer |
| 495 | t.Trailer, err = fixTrailer(t.Header, t.Chunked) |
| 496 | if err != nil { |
| 497 | return err |
| 498 | } |
| 499 | |
| 500 | // If there is no Content-Length or chunked Transfer-Encoding on a *Response |
| 501 | // and the status is not 1xx, 204 or 304, then the body is unbounded. |
| 502 | // See RFC 7230, section 3.3. |
| 503 | switch msg.(type) { |
| 504 | case *http.Response: |
| 505 | if realLength == -1 && !t.Chunked && bodyAllowedForStatus(t.StatusCode) { |
| 506 | // Unbounded body. |
| 507 | t.Close = true |
no test coverage detected
searching dependent graphs…