@@ -635,39 +635,17 @@ func checkConnHeaders(req *http.Request) error {
635
635
return nil
636
636
}
637
637
638
- func bodyAndLength (req * http.Request ) (body io.Reader , contentLen int64 ) {
639
- body = req .Body
640
- if body == nil {
641
- return nil , 0
638
+ // actualContentLength returns a sanitized version of
639
+ // req.ContentLength, where 0 actually means zero (not unknown) and -1
640
+ // means unknown.
641
+ func actualContentLength (req * http.Request ) int64 {
642
+ if req .Body == nil {
643
+ return 0
642
644
}
643
645
if req .ContentLength != 0 {
644
- return req .Body , req .ContentLength
645
- }
646
- // Don't try to sniff the size if they're doing an expect
647
- // request (Issue 16002):
648
- if req .Header .Get ("Expect" ) == "100-continue" {
649
- return req .Body , - 1
650
- }
651
-
652
- // We have a body but a zero content length. Test to see if
653
- // it's actually zero or just unset.
654
- var buf [1 ]byte
655
- n , rerr := body .Read (buf [:])
656
- if rerr != nil && rerr != io .EOF {
657
- return errorReader {rerr }, - 1
658
- }
659
- if n == 1 {
660
- // Oh, guess there is data in this Body Reader after all.
661
- // The ContentLength field just wasn't set.
662
- // Stitch the Body back together again, re-attaching our
663
- // consumed byte.
664
- if rerr == io .EOF {
665
- return bytes .NewReader (buf [:]), 1
666
- }
667
- return io .MultiReader (bytes .NewReader (buf [:]), body ), - 1
646
+ return req .ContentLength
668
647
}
669
- // Body is actually zero bytes.
670
- return nil , 0
648
+ return - 1
671
649
}
672
650
673
651
func (cc * ClientConn ) RoundTrip (req * http.Request ) (* http.Response , error ) {
@@ -691,8 +669,9 @@ func (cc *ClientConn) RoundTrip(req *http.Request) (*http.Response, error) {
691
669
return nil , errClientConnUnusable
692
670
}
693
671
694
- body , contentLen := bodyAndLength ( req )
672
+ body := req . Body
695
673
hasBody := body != nil
674
+ contentLen := actualContentLength (req )
696
675
697
676
// TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere?
698
677
var requestedGzip bool
0 commit comments