Source file
src/net/http/transport.go
1
2
3
4
5
6
7
8
9
10 package http
11
12 import (
13 "bufio"
14 "context"
15 "crypto/tls"
16 "errors"
17 "fmt"
18 "internal/godebug"
19 "io"
20 "log"
21 "maps"
22 "math/rand/v2"
23 "net"
24 "net/http/httptrace"
25 "net/http/internal"
26 "net/http/internal/ascii"
27 "net/http/internal/httpcommon"
28 "net/textproto"
29 "net/url"
30 "reflect"
31 "strings"
32 "sync"
33 "sync/atomic"
34 "time"
35 _ "unsafe"
36
37 "golang.org/x/net/http/httpguts"
38 "golang.org/x/net/http/httpproxy"
39 )
40
41
42
43
44
45
46
47 var DefaultTransport RoundTripper = &Transport{
48 Proxy: ProxyFromEnvironment,
49 DialContext: defaultTransportDialContext(&net.Dialer{
50 Timeout: 30 * time.Second,
51 KeepAlive: 30 * time.Second,
52 }),
53 ForceAttemptHTTP2: true,
54 MaxIdleConns: 100,
55 IdleConnTimeout: 90 * time.Second,
56 TLSHandshakeTimeout: 10 * time.Second,
57 ExpectContinueTimeout: 1 * time.Second,
58 }
59
60
61
62 const DefaultMaxIdleConnsPerHost = 2
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 type Transport struct {
99 pool clientConnPool
100
101 altMu sync.Mutex
102 altProto atomic.Value
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 Proxy func(*Request) (*url.URL, error)
119
120
121
122
123 OnProxyConnectResponse func(ctx context.Context, proxyURL *url.URL, connectReq *Request, connectRes *Response) error
124
125
126
127
128
129
130
131
132
133 DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
134
135
136
137
138
139
140
141
142
143
144
145 Dial func(network, addr string) (net.Conn, error)
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 DialTLSContext func(ctx context.Context, network, addr string) (net.Conn, error)
161
162
163
164
165
166
167
168 DialTLS func(network, addr string) (net.Conn, error)
169
170
171
172
173
174 TLSClientConfig *tls.Config
175
176
177
178 TLSHandshakeTimeout time.Duration
179
180
181
182
183
184
185 DisableKeepAlives bool
186
187
188
189
190
191
192
193
194
195 DisableCompression bool
196
197
198
199 MaxIdleConns int
200
201
202
203
204 MaxIdleConnsPerHost int
205
206
207
208
209
210
211 MaxConnsPerHost int
212
213
214
215
216
217 IdleConnTimeout time.Duration
218
219
220
221
222
223 ResponseHeaderTimeout time.Duration
224
225
226
227
228
229
230
231
232 ExpectContinueTimeout time.Duration
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 TLSNextProto map[string]func(authority string, c *tls.Conn) RoundTripper
248
249
250
251
252 ProxyConnectHeader Header
253
254
255
256
257
258
259
260
261 GetProxyConnectHeader func(ctx context.Context, proxyURL *url.URL, target string) (Header, error)
262
263
264
265
266
267
268 MaxResponseHeaderBytes int64
269
270
271
272
273 WriteBufferSize int
274
275
276
277
278 ReadBufferSize int
279
280
281
282 nextProtoOnce sync.Once
283 closeIdleFunc closeIdleConnectionser
284 h2Transport *http2Transport
285 h2Config http2ExternalTransportConfig
286 h3Transport http3Transport
287 tlsNextProtoWasNil bool
288
289
290
291
292
293
294 ForceAttemptHTTP2 bool
295
296
297 HTTP2 *HTTP2Config
298
299
300
301
302
303
304
305
306
307 Protocols *Protocols
308 }
309
310 func (t *Transport) writeBufferSize() int {
311 if t.WriteBufferSize > 0 {
312 return t.WriteBufferSize
313 }
314 return 4 << 10
315 }
316
317 func (t *Transport) readBufferSize() int {
318 if t.ReadBufferSize > 0 {
319 return t.ReadBufferSize
320 }
321 return 4 << 10
322 }
323
324 func (t *Transport) maxHeaderResponseSize() int64 {
325 if t.MaxResponseHeaderBytes > 0 {
326 return t.MaxResponseHeaderBytes
327 }
328 return 10 << 20
329 }
330
331
332 func (t *Transport) Clone() *Transport {
333 t.nextProtoOnce.Do(t.onceSetNextProtoDefaults)
334 t2 := &Transport{
335 Proxy: t.Proxy,
336 OnProxyConnectResponse: t.OnProxyConnectResponse,
337 DialContext: t.DialContext,
338 Dial: t.Dial,
339 DialTLS: t.DialTLS,
340 DialTLSContext: t.DialTLSContext,
341 TLSHandshakeTimeout: t.TLSHandshakeTimeout,
342 DisableKeepAlives: t.DisableKeepAlives,
343 DisableCompression: t.DisableCompression,
344 MaxIdleConns: t.MaxIdleConns,
345 MaxIdleConnsPerHost: t.MaxIdleConnsPerHost,
346 MaxConnsPerHost: t.MaxConnsPerHost,
347 IdleConnTimeout: t.IdleConnTimeout,
348 ResponseHeaderTimeout: t.ResponseHeaderTimeout,
349 ExpectContinueTimeout: t.ExpectContinueTimeout,
350 ProxyConnectHeader: t.ProxyConnectHeader.Clone(),
351 GetProxyConnectHeader: t.GetProxyConnectHeader,
352 MaxResponseHeaderBytes: t.MaxResponseHeaderBytes,
353 ForceAttemptHTTP2: t.ForceAttemptHTTP2,
354 WriteBufferSize: t.WriteBufferSize,
355 ReadBufferSize: t.ReadBufferSize,
356 }
357 if t.TLSClientConfig != nil {
358 t2.TLSClientConfig = t.TLSClientConfig.Clone()
359 }
360 if t.HTTP2 != nil {
361 t2.HTTP2 = &HTTP2Config{}
362 *t2.HTTP2 = *t.HTTP2
363 }
364 if t.Protocols != nil {
365 t2.Protocols = &Protocols{}
366 *t2.Protocols = *t.Protocols
367 }
368 if !t.tlsNextProtoWasNil {
369 npm := maps.Clone(t.TLSNextProto)
370 if npm == nil {
371 npm = make(map[string]func(authority string, c *tls.Conn) RoundTripper)
372 }
373 t2.TLSNextProto = npm
374 }
375 return t2
376 }
377
378 type closeIdleConnectionser interface {
379
380
381
382
383
384
385
386
387
388 CloseIdleConnections()
389 }
390
391 func (t *Transport) hasCustomTLSDialer() bool {
392 return t.DialTLS != nil || t.DialTLSContext != nil
393 }
394
395 var http2client = godebug.New("http2client")
396
397
398
399 func (t *Transport) onceSetNextProtoDefaults() {
400 t.tlsNextProtoWasNil = (t.TLSNextProto == nil)
401 if http2client.Value() == "0" {
402 http2client.IncNonDefault()
403 return
404 }
405
406
407
408
409
410
411 altProto, _ := t.altProto.Load().(map[string]RoundTripper)
412 if rv := reflect.ValueOf(altProto["https"]); rv.IsValid() && rv.Type().Kind() == reflect.Struct && rv.Type().NumField() == 1 {
413 if v := rv.Field(0); v.CanInterface() {
414 if h2i, ok := v.Interface().(closeIdleConnectionser); ok {
415 t.closeIdleFunc = h2i
416 return
417 }
418 }
419 }
420
421 if _, ok := t.TLSNextProto["h2"]; ok {
422
423 return
424 }
425 protocols := t.protocols()
426 if !protocols.HTTP2() && !protocols.UnencryptedHTTP2() {
427 return
428 }
429 if omitHTTP2Client {
430 return
431 }
432
433 t.configureHTTP2(protocols)
434 }
435
436 func (t *Transport) protocols() Protocols {
437 if t.Protocols != nil {
438 return *t.Protocols
439 }
440 var p Protocols
441 p.SetHTTP1(true)
442 switch {
443 case t.TLSNextProto != nil:
444
445
446 if t.TLSNextProto["h2"] != nil {
447 p.SetHTTP2(true)
448 }
449 case !t.ForceAttemptHTTP2 && (t.TLSClientConfig != nil || t.Dial != nil || t.DialContext != nil || t.hasCustomTLSDialer()):
450
451
452
453
454
455
456 case http2client.Value() == "0":
457 default:
458 p.SetHTTP2(true)
459 }
460 return p
461 }
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480 func ProxyFromEnvironment(req *Request) (*url.URL, error) {
481 return envProxyFunc()(req.URL)
482 }
483
484
485
486 func ProxyURL(fixedURL *url.URL) func(*Request) (*url.URL, error) {
487 return func(*Request) (*url.URL, error) {
488 return fixedURL, nil
489 }
490 }
491
492
493
494
495 type transportRequest struct {
496 *Request
497 extra Header
498 trace *httptrace.ClientTrace
499
500 ctx context.Context
501 cancel context.CancelCauseFunc
502
503 mu sync.Mutex
504 err error
505 }
506
507 func (tr *transportRequest) extraHeaders() Header {
508 if tr.extra == nil {
509 tr.extra = make(Header)
510 }
511 return tr.extra
512 }
513
514 func (tr *transportRequest) setError(err error) {
515 tr.mu.Lock()
516 if tr.err == nil {
517 tr.err = err
518 }
519 tr.mu.Unlock()
520 }
521
522
523
524 func (t *Transport) useRegisteredProtocol(req *Request) bool {
525 if req.URL.Scheme == "https" && req.requiresHTTP1() {
526
527
528
529
530 return false
531 }
532 return true
533 }
534
535
536
537
538 func (t *Transport) alternateRoundTripper(req *Request) RoundTripper {
539 if !t.useRegisteredProtocol(req) {
540 return nil
541 }
542 if req.URL.Scheme == "https" && t.h2Config != nil && t.h2Config.ExternalRoundTrip() {
543
544
545
546
547
548
549 return t.h2Config
550 }
551 altProto, _ := t.altProto.Load().(map[string]RoundTripper)
552 return altProto[req.URL.Scheme]
553 }
554
555 func validateTrailers(hdrs Header) string {
556 return validateHeaderFields(hdrs, false)
557 }
558
559 func validateHeadersWithProtocol(hdrs Header) string {
560 return validateHeaderFields(hdrs, true)
561 }
562
563 func validateHeaderFields(hdrs Header, allowProtocol bool) string {
564 for k, vv := range hdrs {
565 if !httpguts.ValidHeaderFieldName(k) && !(allowProtocol && k == ":protocol") {
566 return fmt.Sprintf("field name %q", k)
567 }
568 for _, v := range vv {
569 if !httpguts.ValidHeaderFieldValue(v) {
570
571
572 return fmt.Sprintf("field value for %q", k)
573 }
574 }
575 }
576 return ""
577 }
578
579
580 func (t *Transport) roundTrip(req *Request) (_ *Response, err error) {
581 t.nextProtoOnce.Do(t.onceSetNextProtoDefaults)
582 ctx := req.Context()
583
584 if req.URL == nil {
585 req.closeBody()
586 return nil, errors.New("http: nil Request.URL")
587 }
588 if req.Header == nil {
589 req.closeBody()
590 return nil, errors.New("http: nil Request.Header")
591 }
592 scheme := req.URL.Scheme
593 isHTTP := scheme == "http" || scheme == "https"
594 if isHTTP {
595
596
597 if err := validateHeadersWithProtocol(req.Header); err != "" {
598 req.closeBody()
599 return nil, fmt.Errorf("net/http: invalid header %s", err)
600 }
601
602
603 if err := validateTrailers(req.Trailer); err != "" {
604 req.closeBody()
605 return nil, fmt.Errorf("net/http: invalid trailer %s", err)
606 }
607 }
608
609 origReq := req
610 req = setupRewindBody(req)
611
612 retry := 0
613 if altRT := t.alternateRoundTripper(req); altRT != nil {
614 if resp, err := altRT.RoundTrip(req); err != ErrSkipAltProtocol {
615 return resp, err
616 }
617 req, err = rewindBody(req)
618 if err != nil {
619 return nil, err
620 }
621 }
622 if !isHTTP {
623 req.closeBody()
624 return nil, badStringError("unsupported protocol scheme", scheme)
625 }
626 if req.Method != "" && !validMethod(req.Method) {
627 req.closeBody()
628 return nil, fmt.Errorf("net/http: invalid method %q", req.Method)
629 }
630 if req.URL.Host == "" {
631 req.closeBody()
632 return nil, errors.New("http: no Host in request URL")
633 }
634
635
636
637
638
639
640 ctx, cancel := context.WithCancelCause(req.Context())
641
642
643 if origReq.Cancel != nil {
644 go awaitLegacyCancel(ctx, cancel, origReq)
645 }
646
647 defer func() {
648 if err != nil {
649 cancel(err)
650 } else {
651 cancel(errRequestDone)
652 }
653 }()
654
655 for {
656 select {
657 case <-ctx.Done():
658 req.closeBody()
659 return nil, context.Cause(ctx)
660 default:
661 }
662
663 cm, err := t.connectMethodForRequest(req)
664 if err != nil {
665 req.closeBody()
666 return nil, err
667 }
668
669
670
671
672
673 pconn, canRetry, err := t.getConn(ctx, cm, req.wantsClose())
674 if err != nil {
675 req.closeBody()
676 return nil, err
677 }
678
679 resp, err := pconn.cc.RoundTrip(req)
680 if err == nil {
681 resp.Request = origReq
682 return resp, nil
683 }
684
685
686 var shouldRetry, backoff bool
687 if http2isNoCachedConnError(err) {
688
689 shouldRetry = true
690 backoff = false
691 t.poolRemoveConn(pconn)
692 } else {
693 shouldRetry, backoff = shouldRetryRequest(retry, canRetry, req, err)
694 }
695 if !shouldRetry {
696
697
698 if e, ok := err.(nothingWrittenError); ok {
699 err = e.error
700 }
701 if e, ok := err.(transportReadFromServerError); ok {
702 err = e.err
703 }
704 if b, ok := req.Body.(*readTrackingBody); ok && !b.didClose.Load() {
705
706
707
708 req.closeBody()
709 }
710 return nil, err
711 }
712 testHookRoundTripRetried()
713
714
715 req, err = rewindBody(req)
716 if err != nil {
717 return nil, err
718 }
719
720 if !backoff {
721 continue
722 }
723 if pconn.cc.Proto() == 2 && retry > 0 {
724
725 backoff := float64(uint(1) << (uint(retry) - 1))
726 backoff += backoff * (0.1 * rand.Float64())
727 d := time.Duration(float64(time.Second) * backoff)
728 select {
729 case <-time.After(d):
730 case <-ctx.Done():
731 req.closeBody()
732 return nil, context.Cause(ctx)
733 }
734 }
735 retry++
736 }
737 }
738
739 func http2isNoCachedConnError(err error) bool {
740 _, ok := err.(interface{ IsHTTP2NoCachedConnError() })
741 return ok
742 }
743
744 func awaitLegacyCancel(ctx context.Context, cancel context.CancelCauseFunc, req *Request) {
745 select {
746 case <-req.Cancel:
747 cancel(errRequestCanceled)
748 case <-ctx.Done():
749 }
750 }
751
752 var errCannotRewind = errors.New("net/http: cannot rewind body after connection loss")
753
754 type readTrackingBody struct {
755 io.ReadCloser
756 didRead bool
757 didClose atomic.Bool
758 }
759
760 func (r *readTrackingBody) Read(data []byte) (int, error) {
761 r.didRead = true
762 return r.ReadCloser.Read(data)
763 }
764
765 func (r *readTrackingBody) Close() error {
766 if !r.didClose.CompareAndSwap(false, true) {
767 return nil
768 }
769 return r.ReadCloser.Close()
770 }
771
772
773
774
775
776 func setupRewindBody(req *Request) *Request {
777 if req.Body == nil || req.Body == NoBody {
778 return req
779 }
780 newReq := *req
781 newReq.Body = &readTrackingBody{ReadCloser: req.Body}
782 return &newReq
783 }
784
785
786
787
788
789 func rewindBody(req *Request) (rewound *Request, err error) {
790 if req.Body == nil || req.Body == NoBody || (!req.Body.(*readTrackingBody).didRead && !req.Body.(*readTrackingBody).didClose.Load()) {
791 return req, nil
792 }
793 if !req.Body.(*readTrackingBody).didClose.Load() {
794 req.closeBody()
795 }
796 if req.GetBody == nil {
797 return nil, errCannotRewind
798 }
799 body, err := req.GetBody()
800 if err != nil {
801 return nil, err
802 }
803 newReq := *req
804 newReq.Body = &readTrackingBody{ReadCloser: body}
805 return &newReq, nil
806 }
807
808
809
810
811 func shouldRetryRequest(retry int, canRetry bool, req *Request, err error) (shouldRetry, backoff bool) {
812 if retry > 6 {
813 return false, false
814 }
815 if http2isNoCachedConnError(err) {
816
817
818
819
820
821
822 return true, false
823 }
824 if err == errMissingHost {
825
826 return false, false
827 }
828 if e, ok := errors.AsType[internal.RetryableError](err); ok {
829
830
831
832
833
834
835 return true, e.Backoff
836 }
837 if !canRetry {
838
839
840
841
842
843
844
845 return false, false
846 }
847 if _, ok := err.(nothingWrittenError); ok {
848
849
850 return req.outgoingLength() == 0 || req.GetBody != nil, false
851 }
852 if !req.isReplayable() {
853
854 return false, false
855 }
856 if _, ok := err.(transportReadFromServerError); ok {
857
858
859 return true, false
860 }
861 if err == errServerClosedIdle {
862
863
864
865 return true, false
866 }
867 return false, false
868 }
869
870
871 var ErrSkipAltProtocol = internal.ErrSkipAltProtocol
872
873
874
875
876
877
878
879
880
881
882
883 func (t *Transport) RegisterProtocol(scheme string, rt RoundTripper) {
884 if err := t.registerProtocol(scheme, rt); err != nil {
885 panic(err)
886 }
887 }
888
889 func (t *Transport) registerProtocol(scheme string, rt RoundTripper) error {
890 t.altMu.Lock()
891 defer t.altMu.Unlock()
892
893 if scheme == "http/2" {
894 if t.h2Config != nil {
895 panic("http: HTTP/2 Transport already registered")
896 }
897 var ok bool
898 if t.h2Config, ok = rt.(http2ExternalTransportConfig); !ok {
899 panic("http: HTTP/2 configuration does not implement ExternalTransportConfig")
900 }
901 t.registerHTTP2Config()
902 }
903
904 if scheme == "http/3" {
905 if t.h3Transport != nil {
906 panic("http: HTTP/3 Transport already registered")
907 }
908 var ok bool
909 if t.h3Transport, ok = rt.(http3Transport); !ok {
910 panic("http: HTTP/3 RoundTripper does not implement DialClientConn")
911 }
912
913
914 if r, ok := rt.(interface {
915 Registered(*Transport)
916 }); ok {
917 r.Registered(t)
918 }
919 return nil
920 }
921
922 oldMap, _ := t.altProto.Load().(map[string]RoundTripper)
923 if _, exists := oldMap[scheme]; exists {
924 return errors.New("protocol " + scheme + " already registered")
925 }
926 newMap := maps.Clone(oldMap)
927 if newMap == nil {
928 newMap = make(map[string]RoundTripper)
929 }
930 newMap[scheme] = rt
931 t.altProto.Store(newMap)
932 return nil
933 }
934
935
936
937
938
939 func (t *Transport) CloseIdleConnections() {
940 t.nextProtoOnce.Do(t.onceSetNextProtoDefaults)
941
942 t.poolCloseIdleConnections()
943
944
945
946
947
948
949
950 if t2 := t.closeIdleFunc; t2 != nil {
951 t2.CloseIdleConnections()
952 }
953
954 if cc, ok := t.h3Transport.(closeIdleConnectionser); ok {
955 cc.CloseIdleConnections()
956 }
957 }
958
959
960
961
962
963 func (t *Transport) CancelRequest(req *Request) {
964 }
965
966
967
968
969
970 var (
971 envProxyOnce sync.Once
972 envProxyFuncValue func(*url.URL) (*url.URL, error)
973 )
974
975
976
977 func envProxyFunc() func(*url.URL) (*url.URL, error) {
978 envProxyOnce.Do(func() {
979 envProxyFuncValue = httpproxy.FromEnvironment().ProxyFunc()
980 })
981 return envProxyFuncValue
982 }
983
984
985 func resetProxyConfig() {
986 envProxyOnce = sync.Once{}
987 envProxyFuncValue = nil
988 }
989
990 func (t *Transport) connectMethodForRequest(req *Request) (cm connectMethod, err error) {
991 cm.targetScheme = req.URL.Scheme
992 cm.targetAddr = canonicalAddr(req.URL)
993 if t.Proxy != nil {
994 cm.proxyURL, err = t.Proxy(req)
995 }
996 cm.onlyH1 = req.requiresHTTP1()
997 return cm, err
998 }
999
1000
1001
1002 func (cm *connectMethod) proxyAuth() string {
1003 if cm.proxyURL == nil {
1004 return ""
1005 }
1006 if u := cm.proxyURL.User; u != nil {
1007 username := u.Username()
1008 password, _ := u.Password()
1009 return "Basic " + basicAuth(username, password)
1010 }
1011 return ""
1012 }
1013
1014
1015 var (
1016 errKeepAlivesDisabled = errors.New("http: keep alives disabled")
1017 errConnBroken = errors.New("http: connection is in bad state")
1018 errCloseIdle = errors.New("http: CloseIdleConnections was called")
1019 errCloseOnIdle = errors.New("http: connection marked as close-on-idle")
1020 errTooManyIdle = errors.New("http: too many idle connections")
1021 errTooManyIdleHost = errors.New("http: too many idle connections for host")
1022 errReadLoopExiting = errors.New("http: persistConn.readLoop exiting")
1023 errIdleConnTimeout = errors.New("http: idle connection timeout")
1024
1025
1026
1027
1028
1029 errServerClosedIdle = errors.New("http: server closed idle connection")
1030 )
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040 type transportReadFromServerError struct {
1041 err error
1042 }
1043
1044 func (e transportReadFromServerError) Unwrap() error { return e.err }
1045
1046 func (e transportReadFromServerError) Error() string {
1047 return fmt.Sprintf("net/http: Transport failed to read from server: %v", e.err)
1048 }
1049
1050 func (t *Transport) maxIdleConnsPerHost() int {
1051 if v := t.MaxIdleConnsPerHost; v != 0 {
1052 return v
1053 }
1054 return DefaultMaxIdleConnsPerHost
1055 }
1056
1057
1058
1059 func (pc *persistConn) roundTripDone(trace *httptrace.ClientTrace) error {
1060 putIdle := func(err error) {
1061 if !pc.isClientConn && trace != nil && trace.PutIdleConn != nil && err != errKeepAlivesDisabled && err != errCloseIdle && !pc.t.closeConnsOnIdle() {
1062 trace.PutIdleConn(err)
1063 }
1064 }
1065 if err := func() error {
1066 pc.mu.Lock()
1067 defer pc.mu.Unlock()
1068 if pc.closed != nil {
1069 return errConnBroken
1070 }
1071 if !pc.inFlight {
1072 panic("pconn is not in flight")
1073 }
1074 pc.inFlight = false
1075 select {
1076 case pc.availch <- struct{}{}:
1077 default:
1078 panic("unable to make pconn available")
1079 }
1080 return nil
1081 }(); err != nil {
1082 putIdle(err)
1083 return err
1084 }
1085 var err error
1086 if pc.internalStateHook != nil {
1087 err = pc.internalStateHook()
1088 }
1089 putIdle(err)
1090 if err != nil {
1091 return err
1092 }
1093
1094
1095 pc.mu.Lock()
1096 defer pc.mu.Unlock()
1097 return pc.closed
1098 }
1099
1100 var zeroDialer net.Dialer
1101
1102 func (t *Transport) dial(ctx context.Context, network, addr string) (net.Conn, error) {
1103 if t.DialContext != nil {
1104 c, err := t.DialContext(ctx, network, addr)
1105 if c == nil && err == nil {
1106 err = errors.New("net/http: Transport.DialContext hook returned (nil, nil)")
1107 }
1108 return c, err
1109 }
1110 if t.Dial != nil {
1111 c, err := t.Dial(network, addr)
1112 if c == nil && err == nil {
1113 err = errors.New("net/http: Transport.Dial hook returned (nil, nil)")
1114 }
1115 return c, err
1116 }
1117 return zeroDialer.DialContext(ctx, network, addr)
1118 }
1119
1120 func (t *Transport) customDialTLS(ctx context.Context, network, addr string) (conn net.Conn, err error) {
1121 if t.DialTLSContext != nil {
1122 conn, err = t.DialTLSContext(ctx, network, addr)
1123 } else {
1124 conn, err = t.DialTLS(network, addr)
1125 }
1126 if conn == nil && err == nil {
1127 err = errors.New("net/http: Transport.DialTLS or DialTLSContext returned (nil, nil)")
1128 }
1129 return
1130 }
1131
1132
1133
1134
1135
1136 func (t *Transport) getConn(ctx context.Context, cm connectMethod, connClose bool) (_ *poolConn, canRetry bool, err error) {
1137 trace := httptrace.ContextClientTrace(ctx)
1138 if trace != nil && trace.GetConn != nil {
1139 trace.GetConn(cm.addr())
1140 }
1141
1142 pconn, info, err := t.poolGetConn(ctx, cm, connClose)
1143 if err != nil {
1144 return nil, false, err
1145 }
1146 info.Reused = pconn.reused.Swap(true)
1147 if !pconn.isAlt() && trace != nil && trace.GotConn != nil {
1148 trace.GotConn(info)
1149 }
1150 canRetry = info.Reused || info.WasIdle
1151 return pconn, canRetry, nil
1152 }
1153
1154 func (t *Transport) tlsConfigForDial(host string) (*tls.Config, error) {
1155 firstTLSHost, _, err := net.SplitHostPort(host)
1156 if err != nil {
1157 return nil, err
1158 }
1159 cfg := cloneTLSConfig(t.TLSClientConfig)
1160 if cfg.ServerName == "" {
1161 cfg.ServerName = firstTLSHost
1162 }
1163 return cfg, nil
1164 }
1165
1166
1167
1168
1169 func (pconn *persistConn) addTLS(ctx context.Context, addr string, trace *httptrace.ClientTrace) error {
1170 cfg, err := pconn.t.tlsConfigForDial(addr)
1171 if err != nil {
1172 pconn.conn.Close()
1173 return err
1174 }
1175 if pconn.cacheKey.onlyH1 {
1176 cfg.NextProtos = nil
1177 }
1178 plainConn := pconn.conn
1179 tlsConn := tls.Client(plainConn, cfg)
1180 errc := make(chan error, 2)
1181 var timer *time.Timer
1182 if d := pconn.t.TLSHandshakeTimeout; d != 0 {
1183 timer = time.AfterFunc(d, func() {
1184 errc <- tlsHandshakeTimeoutError{}
1185 })
1186 }
1187 go func() {
1188 if trace != nil && trace.TLSHandshakeStart != nil {
1189 trace.TLSHandshakeStart()
1190 }
1191 err := tlsConn.HandshakeContext(ctx)
1192 if timer != nil {
1193 timer.Stop()
1194 }
1195 errc <- err
1196 }()
1197 if err := <-errc; err != nil {
1198 plainConn.Close()
1199 if err == (tlsHandshakeTimeoutError{}) {
1200
1201
1202 <-errc
1203 }
1204 if trace != nil && trace.TLSHandshakeDone != nil {
1205 trace.TLSHandshakeDone(tls.ConnectionState{}, err)
1206 }
1207 return err
1208 }
1209 cs := tlsConn.ConnectionState()
1210 if trace != nil && trace.TLSHandshakeDone != nil {
1211 trace.TLSHandshakeDone(cs, nil)
1212 }
1213 pconn.tlsState = &cs
1214 pconn.conn = tlsConn
1215 return nil
1216 }
1217
1218 type erringRoundTripper interface {
1219 RoundTripErr() error
1220 }
1221
1222 var testHookProxyConnectTimeout = context.WithTimeout
1223
1224 func (t *Transport) dialConn(ctx context.Context, cm connectMethod, isClientConn bool, internalStateHook func() error) (_ genericClientConn, err error) {
1225
1226
1227
1228
1229 if p := t.protocols(); p.http3() {
1230 if p.HTTP1() || p.HTTP2() || p.UnencryptedHTTP2() {
1231 return nil, errors.New("http: when using HTTP3, Transport.Protocols must contain only HTTP3")
1232 }
1233 if t.h3Transport == nil {
1234 return nil, errors.New("http: Transport.Protocols contains HTTP3, but Transport does not support HTTP/3")
1235 }
1236 tlsConfig, err := t.tlsConfigForDial(cm.addr())
1237 if err != nil {
1238 return nil, err
1239 }
1240 tlsConfig.NextProtos = []string{"h3"}
1241 rt, err := t.h3Transport.DialClientConn(ctx, cm.addr(), cm.proxyURL, tlsConfig, internalStateHook)
1242 if err != nil {
1243 return nil, err
1244 }
1245 return rt.(genericClientConn), nil
1246 }
1247
1248
1249 pconn := &persistConn{
1250 t: t,
1251 cacheKey: cm.key(),
1252 reqch: make(chan requestAndChan, 1),
1253 writech: make(chan writeRequest, 1),
1254 closech: make(chan struct{}),
1255 writeErrCh: make(chan error, 1),
1256 writeLoopDone: make(chan struct{}),
1257 isClientConn: isClientConn,
1258 internalStateHook: internalStateHook,
1259 }
1260 trace := httptrace.ContextClientTrace(ctx)
1261 wrapErr := func(err error) error {
1262 if cm.proxyURL != nil {
1263
1264 return &net.OpError{Op: "proxyconnect", Net: "tcp", Err: err}
1265 }
1266 return err
1267 }
1268
1269 if cc, err := t.http2ExternalDial(ctx, cm, isClientConn, internalStateHook); err != errors.ErrUnsupported {
1270 if err != nil {
1271 return nil, err
1272 }
1273 return cc, nil
1274 }
1275
1276 if cm.scheme() == "https" && t.hasCustomTLSDialer() {
1277 var err error
1278 pconn.conn, err = t.customDialTLS(ctx, "tcp", cm.addr())
1279 if err != nil {
1280 return nil, wrapErr(err)
1281 }
1282 type connectionStater interface {
1283 ConnectionState() tls.ConnectionState
1284 }
1285 type handshaker interface {
1286 HandshakeContext(context.Context) error
1287 }
1288 if cstater, ok := pconn.conn.(connectionStater); ok {
1289 if trace != nil && trace.TLSHandshakeStart != nil {
1290 trace.TLSHandshakeStart()
1291 }
1292 if handshaker, ok := cstater.(handshaker); ok {
1293
1294
1295 if err := handshaker.HandshakeContext(ctx); err != nil {
1296 go pconn.conn.Close()
1297 if trace != nil && trace.TLSHandshakeDone != nil {
1298 trace.TLSHandshakeDone(tls.ConnectionState{}, err)
1299 }
1300 return nil, err
1301 }
1302 }
1303 cs := cstater.ConnectionState()
1304 if trace != nil && trace.TLSHandshakeDone != nil {
1305 trace.TLSHandshakeDone(cs, nil)
1306 }
1307 pconn.tlsState = &cs
1308 }
1309 } else {
1310 conn, err := t.dial(ctx, "tcp", cm.addr())
1311 if err != nil {
1312 return nil, wrapErr(err)
1313 }
1314 pconn.conn = conn
1315 if cm.scheme() == "https" {
1316 if err = pconn.addTLS(ctx, cm.addr(), trace); err != nil {
1317 return nil, wrapErr(err)
1318 }
1319 }
1320 }
1321
1322
1323 switch {
1324 case cm.proxyURL == nil:
1325
1326 case cm.proxyURL.Scheme == "socks5" || cm.proxyURL.Scheme == "socks5h":
1327 conn := pconn.conn
1328 d := socksNewDialer("tcp", conn.RemoteAddr().String())
1329 if u := cm.proxyURL.User; u != nil {
1330 auth := &socksUsernamePassword{
1331 Username: u.Username(),
1332 }
1333 auth.Password, _ = u.Password()
1334 d.AuthMethods = []socksAuthMethod{
1335 socksAuthMethodNotRequired,
1336 socksAuthMethodUsernamePassword,
1337 }
1338 d.Authenticate = auth.Authenticate
1339 }
1340 if _, err := d.DialWithConn(ctx, conn, "tcp", cm.targetAddr); err != nil {
1341 conn.Close()
1342 return nil, err
1343 }
1344 case cm.targetScheme == "http":
1345 pconn.isProxy = true
1346 if pa := cm.proxyAuth(); pa != "" {
1347 pconn.mutateHeaderFunc = func(h Header) {
1348 h.Set("Proxy-Authorization", pa)
1349 }
1350 }
1351 case cm.targetScheme == "https":
1352 conn := pconn.conn
1353 var hdr Header
1354 if t.GetProxyConnectHeader != nil {
1355 var err error
1356 hdr, err = t.GetProxyConnectHeader(ctx, cm.proxyURL, cm.targetAddr)
1357 if err != nil {
1358 conn.Close()
1359 return nil, err
1360 }
1361 } else {
1362 hdr = t.ProxyConnectHeader
1363 }
1364 if hdr == nil {
1365 hdr = make(Header)
1366 }
1367 if pa := cm.proxyAuth(); pa != "" {
1368 hdr = hdr.Clone()
1369 hdr.Set("Proxy-Authorization", pa)
1370 }
1371 connectReq := &Request{
1372 Method: "CONNECT",
1373 URL: &url.URL{Opaque: cm.targetAddr},
1374 Host: cm.targetAddr,
1375 Header: hdr,
1376 }
1377
1378
1379
1380
1381 connectCtx, cancel := testHookProxyConnectTimeout(ctx, 1*time.Minute)
1382 defer cancel()
1383
1384 didReadResponse := make(chan struct{})
1385 var (
1386 resp *Response
1387 err error
1388 )
1389
1390 go func() {
1391 defer close(didReadResponse)
1392 err = connectReq.Write(conn)
1393 if err != nil {
1394 return
1395 }
1396
1397
1398 br := bufio.NewReader(&io.LimitedReader{R: conn, N: t.maxHeaderResponseSize()})
1399 resp, err = ReadResponse(br, connectReq)
1400 }()
1401 select {
1402 case <-connectCtx.Done():
1403 conn.Close()
1404 <-didReadResponse
1405 return nil, connectCtx.Err()
1406 case <-didReadResponse:
1407
1408 }
1409 if err != nil {
1410 conn.Close()
1411 return nil, err
1412 }
1413
1414 if t.OnProxyConnectResponse != nil {
1415 err = t.OnProxyConnectResponse(ctx, cm.proxyURL, connectReq, resp)
1416 if err != nil {
1417 conn.Close()
1418 return nil, err
1419 }
1420 }
1421
1422 if resp.StatusCode != 200 {
1423 _, text, ok := strings.Cut(resp.Status, " ")
1424 conn.Close()
1425 if !ok {
1426 return nil, errors.New("unknown status code")
1427 }
1428 return nil, errors.New(text)
1429 }
1430 }
1431
1432 if cm.proxyURL != nil && cm.targetScheme == "https" {
1433 if err := pconn.addTLS(ctx, cm.targetAddr, trace); err != nil {
1434 return nil, err
1435 }
1436 }
1437
1438
1439 unencryptedHTTP2 := pconn.tlsState == nil &&
1440 t.Protocols != nil &&
1441 t.Protocols.UnencryptedHTTP2() &&
1442 !t.Protocols.HTTP1()
1443
1444 http2 := unencryptedHTTP2 ||
1445 (pconn.tlsState != nil && pconn.tlsState.NegotiatedProtocol == "h2")
1446
1447 if http2 && t.h2Transport != nil {
1448
1449 cc, err := t.http2NewClientConn(pconn.conn, isClientConn, internalStateHook)
1450 if err == nil {
1451 return cc, nil
1452 }
1453 if err != errors.ErrUnsupported {
1454 return nil, err
1455 }
1456 }
1457
1458 if isClientConn && (unencryptedHTTP2 || (pconn.tlsState != nil && pconn.tlsState.NegotiatedProtocol == "h2")) {
1459
1460
1461 altProto, _ := t.altProto.Load().(map[string]RoundTripper)
1462 h2, ok := altProto["https"].(newClientConner)
1463 if !ok {
1464 return nil, errors.New("http: HTTP/2 implementation does not support NewClientConn (update golang.org/x/net?)")
1465 }
1466 var stateHook func()
1467 if internalStateHook != nil {
1468 stateHook = func() { internalStateHook() }
1469 }
1470 rt, err := h2.NewClientConn(pconn.conn, stateHook)
1471 if err != nil {
1472 pconn.conn.Close()
1473 return nil, err
1474 }
1475 cc, ok := rt.(externalClientConn)
1476 if !ok {
1477 return nil, errors.New("http: NewClientConn returned something that is not a ClientConn")
1478 }
1479 return externalHTTP2Conn{cc}, nil
1480 }
1481
1482 wrapTLSNextProtoRoundTripper := func(rt RoundTripper) (genericClientConn, error) {
1483 if e, ok := rt.(erringRoundTripper); ok {
1484
1485 return nil, e.RoundTripErr()
1486 }
1487 return &altClientConn{rt: rt}, nil
1488 }
1489 if unencryptedHTTP2 {
1490
1491 next, ok := t.TLSNextProto[nextProtoUnencryptedHTTP2]
1492 if !ok {
1493 return nil, errors.New("http: Transport does not support unencrypted HTTP/2")
1494 }
1495 alt := next(cm.targetAddr, unencryptedTLSConn(pconn.conn))
1496 return wrapTLSNextProtoRoundTripper(alt)
1497 }
1498
1499 if s := pconn.tlsState; s != nil && s.NegotiatedProtocolIsMutual && s.NegotiatedProtocol != "" {
1500
1501 tlsConn, tlsConnOK := pconn.conn.(*tls.Conn)
1502 if next, ok := t.TLSNextProto[s.NegotiatedProtocol]; tlsConnOK && ok {
1503 alt := next(cm.targetAddr, tlsConn)
1504 return wrapTLSNextProtoRoundTripper(alt)
1505 }
1506 }
1507
1508
1509 pconn.availch = make(chan struct{}, 1)
1510 pconn.availch <- struct{}{}
1511 pconn.br = bufio.NewReaderSize(pconn, t.readBufferSize())
1512 pconn.bw = bufio.NewWriterSize(persistConnWriter{pconn}, t.writeBufferSize())
1513
1514 go pconn.readLoop()
1515 go pconn.writeLoop()
1516 return pconn, nil
1517 }
1518
1519
1520
1521
1522
1523
1524
1525 type persistConnWriter struct {
1526 pc *persistConn
1527 }
1528
1529 func (w persistConnWriter) Write(p []byte) (n int, err error) {
1530 n, err = w.pc.conn.Write(p)
1531 w.pc.nwrite += int64(n)
1532 return
1533 }
1534
1535
1536
1537
1538 func (w persistConnWriter) ReadFrom(r io.Reader) (n int64, err error) {
1539 n, err = io.Copy(w.pc.conn, r)
1540 w.pc.nwrite += n
1541 return
1542 }
1543
1544 var _ io.ReaderFrom = (*persistConnWriter)(nil)
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562 type connectMethod struct {
1563 _ incomparable
1564 proxyURL *url.URL
1565 targetScheme string
1566
1567
1568
1569 targetAddr string
1570 onlyH1 bool
1571 }
1572
1573 func (cm *connectMethod) key() connectMethodKey {
1574 proxyStr := ""
1575 targetAddr := cm.targetAddr
1576 if cm.proxyURL != nil {
1577 proxyStr = cm.proxyURL.String()
1578 if (cm.proxyURL.Scheme == "http" || cm.proxyURL.Scheme == "https") && cm.targetScheme == "http" {
1579 targetAddr = ""
1580 }
1581 }
1582 return connectMethodKey{
1583 proxy: proxyStr,
1584 scheme: cm.targetScheme,
1585 addr: targetAddr,
1586 onlyH1: cm.onlyH1,
1587 }
1588 }
1589
1590
1591 func (cm *connectMethod) scheme() string {
1592 if cm.proxyURL != nil {
1593 return cm.proxyURL.Scheme
1594 }
1595 return cm.targetScheme
1596 }
1597
1598
1599 func (cm *connectMethod) addr() string {
1600 if cm.proxyURL != nil {
1601 return canonicalAddr(cm.proxyURL)
1602 }
1603 return cm.targetAddr
1604 }
1605
1606
1607
1608
1609 type connectMethodKey struct {
1610 proxy, scheme, addr string
1611 onlyH1 bool
1612 }
1613
1614 func (k connectMethodKey) String() string {
1615
1616 var h1 string
1617 if k.onlyH1 {
1618 h1 = ",h1"
1619 }
1620 return fmt.Sprintf("%s|%s%s|%s", k.proxy, k.scheme, h1, k.addr)
1621 }
1622
1623
1624
1625 type persistConn struct {
1626 t *Transport
1627 cacheKey connectMethodKey
1628 conn net.Conn
1629 tlsState *tls.ConnectionState
1630 br *bufio.Reader
1631 bw *bufio.Writer
1632 nwrite int64
1633 reqch chan requestAndChan
1634 writech chan writeRequest
1635 closech chan struct{}
1636 availch chan struct{}
1637 isProxy bool
1638 isClientConn bool
1639 sawEOF bool
1640 readLimit int64
1641
1642
1643
1644
1645 writeErrCh chan error
1646
1647 writeLoopDone chan struct{}
1648
1649 mu sync.Mutex
1650 numExpectedResponses int
1651 closed error
1652 canceledErr error
1653 reserved bool
1654 inFlight bool
1655 internalStateHook func() error
1656
1657
1658
1659
1660 mutateHeaderFunc func(Header)
1661 }
1662
1663 func (pc *persistConn) maxHeaderResponseSize() int64 {
1664 return pc.t.maxHeaderResponseSize()
1665 }
1666
1667 func (pc *persistConn) Read(p []byte) (n int, err error) {
1668 if pc.readLimit <= 0 {
1669 return 0, fmt.Errorf("read limit of %d bytes exhausted", pc.maxHeaderResponseSize())
1670 }
1671 if int64(len(p)) > pc.readLimit {
1672 p = p[:pc.readLimit]
1673 }
1674 n, err = pc.conn.Read(p)
1675 if err == io.EOF {
1676 pc.sawEOF = true
1677 }
1678 pc.readLimit -= int64(n)
1679 return
1680 }
1681
1682
1683 func (pc *persistConn) isBroken() bool {
1684 pc.mu.Lock()
1685 b := pc.closed != nil
1686 pc.mu.Unlock()
1687 return b
1688 }
1689
1690
1691 func (pc *persistConn) canceled() error {
1692 pc.mu.Lock()
1693 defer pc.mu.Unlock()
1694 return pc.canceledErr
1695 }
1696
1697 func (pc *persistConn) cancelRequest(err error) {
1698 pc.mu.Lock()
1699 defer pc.mu.Unlock()
1700 pc.canceledErr = err
1701 pc.closeLocked(errRequestCanceled)
1702 }
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712 func (pc *persistConn) mapRoundTripError(req *transportRequest, startBytesWritten int64, err error) error {
1713 if err == nil {
1714 return nil
1715 }
1716
1717
1718
1719
1720
1721
1722
1723
1724 <-pc.writeLoopDone
1725
1726
1727
1728
1729 if cerr := pc.canceled(); cerr != nil {
1730 return cerr
1731 }
1732
1733
1734 req.mu.Lock()
1735 reqErr := req.err
1736 req.mu.Unlock()
1737 if reqErr != nil {
1738 return reqErr
1739 }
1740
1741 if err == errServerClosedIdle {
1742
1743 return err
1744 }
1745
1746 if _, ok := err.(transportReadFromServerError); ok {
1747 if pc.nwrite == startBytesWritten {
1748 return nothingWrittenError{err}
1749 }
1750
1751 return err
1752 }
1753 if pc.isBroken() {
1754 if pc.nwrite == startBytesWritten {
1755 return nothingWrittenError{err}
1756 }
1757 return fmt.Errorf("net/http: HTTP/1.x transport connection broken: %w", err)
1758 }
1759 return err
1760 }
1761
1762
1763
1764
1765 var errCallerOwnsConn = errors.New("read loop ending; caller owns writable underlying conn")
1766
1767
1768
1769
1770 const maxPostCloseReadBytes = 256 << 10
1771
1772
1773
1774
1775 const maxPostCloseReadTime = 50 * time.Millisecond
1776
1777 func maybeDrainBody(r io.Reader) bool {
1778 drainedCh := make(chan bool, 1)
1779 go func() {
1780
1781
1782
1783 if b, ok := r.(*body); ok {
1784 b.discardTrailer()
1785 }
1786 if _, err := io.CopyN(io.Discard, r, maxPostCloseReadBytes+1); err == io.EOF {
1787 drainedCh <- true
1788 } else {
1789 drainedCh <- false
1790 }
1791 }()
1792 select {
1793 case drained := <-drainedCh:
1794 return drained
1795 case <-time.After(maxPostCloseReadTime):
1796 return false
1797 }
1798 }
1799
1800 type bodyReadStatus uint8
1801
1802 const (
1803 bodyReadEOF bodyReadStatus = iota
1804 bodyReadClosedEarly
1805 bodyReadError
1806 )
1807
1808 func (pc *persistConn) readLoop() {
1809 closeErr := errReadLoopExiting
1810 defer func() {
1811 pc.CloseWithError(closeErr)
1812 if pc.internalStateHook != nil {
1813 pc.internalStateHook()
1814 }
1815 }()
1816
1817 tryPutIdleConn := func(trace *httptrace.ClientTrace) bool {
1818 if err := pc.roundTripDone(trace); err != nil {
1819 closeErr = err
1820 return false
1821 }
1822 return true
1823 }
1824
1825
1826
1827
1828
1829
1830
1831 eofc := make(chan struct{})
1832 defer close(eofc)
1833
1834
1835 testHookMu.Lock()
1836 testHookReadLoopBeforeNextRead := testHookReadLoopBeforeNextRead
1837 testHookMu.Unlock()
1838
1839 alive := true
1840 for alive {
1841 pc.readLimit = pc.maxHeaderResponseSize()
1842 _, err := pc.br.Peek(1)
1843
1844 pc.mu.Lock()
1845 if pc.numExpectedResponses == 0 {
1846 pc.readLoopPeekFailLocked(err)
1847 pc.mu.Unlock()
1848 return
1849 }
1850 pc.mu.Unlock()
1851
1852 rc := <-pc.reqch
1853 trace := rc.treq.trace
1854
1855 var resp *Response
1856 if err == nil {
1857 resp, err = pc.readResponse(rc, trace)
1858 } else {
1859 err = transportReadFromServerError{err}
1860 closeErr = err
1861 }
1862
1863 if err != nil {
1864 if pc.readLimit <= 0 {
1865 err = fmt.Errorf("net/http: server response headers exceeded %d bytes; aborted", pc.maxHeaderResponseSize())
1866 }
1867
1868 select {
1869 case rc.ch <- responseAndError{err: err}:
1870 case <-rc.callerGone:
1871 return
1872 }
1873 return
1874 }
1875 pc.readLimit = maxInt64
1876
1877 pc.mu.Lock()
1878 pc.numExpectedResponses--
1879 pc.mu.Unlock()
1880
1881 bodyWritable := resp.bodyIsWritable()
1882 isConnect := rc.treq.Request.Method == "CONNECT"
1883 hasBody := rc.treq.Request.Method != "HEAD" && resp.ContentLength != 0
1884
1885 if resp.Close || rc.treq.Request.wantsClose() || resp.StatusCode <= 199 || bodyWritable || isConnect {
1886
1887
1888
1889
1890
1891
1892
1893 alive = false
1894 }
1895
1896 if !hasBody || bodyWritable {
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910 waitc := eofc
1911 select {
1912 case rc.ch <- responseAndError{res: resp, waitc: waitc}:
1913 case <-rc.callerGone:
1914 return
1915 }
1916
1917
1918
1919 alive = alive &&
1920 !pc.sawEOF &&
1921 pc.wroteRequest() &&
1922 tryPutIdleConn(trace)
1923 if bodyWritable {
1924 closeErr = errCallerOwnsConn
1925 }
1926 rc.treq.cancel(errRequestDone)
1927
1928
1929 waitc <- struct{}{}
1930
1931
1932
1933
1934 testHookReadLoopBeforeNextRead()
1935 continue
1936 }
1937
1938 waitForBodyRead := make(chan bodyReadStatus, 1)
1939 body := &bodyEOFSignal{
1940 body: resp.Body,
1941 earlyCloseFn: func() error {
1942 waitForBodyRead <- bodyReadClosedEarly
1943 <-eofc
1944 return nil
1945 },
1946 fn: func(err error) error {
1947 if err == io.EOF {
1948 waitForBodyRead <- bodyReadEOF
1949 <-eofc
1950 } else {
1951 waitForBodyRead <- bodyReadError
1952 if err != nil {
1953 if cerr := pc.canceled(); cerr != nil {
1954 return cerr
1955 }
1956 }
1957 }
1958 return err
1959 },
1960 }
1961
1962 resp.Body = body
1963 if rc.addedGzip && ascii.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
1964 resp.Body = &httpcommon.GzipReader{Body: body}
1965 resp.Header.Del("Content-Encoding")
1966 resp.Header.Del("Content-Length")
1967 resp.ContentLength = -1
1968 resp.Uncompressed = true
1969 }
1970
1971 select {
1972 case rc.ch <- responseAndError{res: resp}:
1973 case <-rc.callerGone:
1974 return
1975 }
1976
1977
1978
1979
1980 select {
1981 case status := <-waitForBodyRead:
1982 tryPutIdle := func() {
1983 alive = alive &&
1984 !pc.sawEOF &&
1985 pc.wroteRequest() &&
1986 tryPutIdleConn(trace)
1987 }
1988 switch status {
1989 case bodyReadEOF:
1990 tryPutIdle()
1991 eofc <- struct{}{}
1992 case bodyReadClosedEarly:
1993
1994
1995 tryDrain := alive && (pc.isClientConn || !pc.t.closeConnsOnIdle()) && resp.ContentLength <= maxPostCloseReadBytes
1996 eofc <- struct{}{}
1997 if tryDrain && maybeDrainBody(body.body) {
1998 tryPutIdle()
1999 } else {
2000 alive = false
2001 }
2002 case bodyReadError:
2003 alive = false
2004 }
2005 case <-rc.treq.ctx.Done():
2006 alive = false
2007 pc.cancelRequest(context.Cause(rc.treq.ctx))
2008 case <-pc.closech:
2009 alive = false
2010 }
2011
2012 rc.treq.cancel(errRequestDone)
2013 testHookReadLoopBeforeNextRead()
2014 }
2015 }
2016
2017 func (pc *persistConn) readLoopPeekFailLocked(peekErr error) {
2018 if pc.closed != nil {
2019 return
2020 }
2021 if n := pc.br.Buffered(); n > 0 {
2022 buf, _ := pc.br.Peek(n)
2023 if is408Message(buf) {
2024 pc.closeLocked(errServerClosedIdle)
2025 return
2026 } else {
2027 log.Printf("Unsolicited response received on idle HTTP channel starting with %q; err=%v", buf, peekErr)
2028 }
2029 }
2030 if peekErr == io.EOF {
2031
2032 pc.closeLocked(errServerClosedIdle)
2033 } else {
2034 pc.closeLocked(fmt.Errorf("readLoopPeekFailLocked: %w", peekErr))
2035 }
2036 }
2037
2038
2039
2040
2041 func is408Message(buf []byte) bool {
2042 if len(buf) < len("HTTP/1.x 408") {
2043 return false
2044 }
2045 if string(buf[:7]) != "HTTP/1." {
2046 return false
2047 }
2048 return string(buf[8:12]) == " 408"
2049 }
2050
2051
2052
2053
2054 func (pc *persistConn) readResponse(rc requestAndChan, trace *httptrace.ClientTrace) (resp *Response, err error) {
2055 if trace != nil && trace.GotFirstResponseByte != nil {
2056 if peek, err := pc.br.Peek(1); err == nil && len(peek) == 1 {
2057 trace.GotFirstResponseByte()
2058 }
2059 }
2060
2061 continueCh := rc.continueCh
2062 for {
2063 resp, err = ReadResponse(pc.br, rc.treq.Request)
2064 if err != nil {
2065 return
2066 }
2067 resCode := resp.StatusCode
2068 if continueCh != nil && resCode == StatusContinue {
2069 if trace != nil && trace.Got100Continue != nil {
2070 trace.Got100Continue()
2071 }
2072 continueCh <- struct{}{}
2073 continueCh = nil
2074 }
2075 is1xx := 100 <= resCode && resCode <= 199
2076
2077 is1xxNonTerminal := is1xx && resCode != StatusSwitchingProtocols
2078 if is1xxNonTerminal {
2079 if trace != nil && trace.Got1xxResponse != nil {
2080 if err := trace.Got1xxResponse(resCode, textproto.MIMEHeader(resp.Header)); err != nil {
2081 return nil, err
2082 }
2083
2084
2085
2086
2087
2088
2089
2090 pc.readLimit = pc.maxHeaderResponseSize()
2091 }
2092 continue
2093 }
2094 break
2095 }
2096 if resp.isProtocolSwitch() && isProtocolSwitchHeader(rc.treq.Request.Header) {
2097 resp.Body = newReadWriteCloserBody(pc.br, pc.conn)
2098 }
2099 if continueCh != nil {
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112 if resp.Close || rc.treq.Request.Close {
2113 close(continueCh)
2114 } else {
2115 continueCh <- struct{}{}
2116 }
2117 }
2118
2119 resp.TLS = pc.tlsState
2120 return
2121 }
2122
2123
2124
2125
2126 func (pc *persistConn) waitForContinue(continueCh <-chan struct{}) func() bool {
2127 if continueCh == nil {
2128 return nil
2129 }
2130 return func() bool {
2131 timer := time.NewTimer(pc.t.ExpectContinueTimeout)
2132 defer timer.Stop()
2133
2134 select {
2135 case _, ok := <-continueCh:
2136 return ok
2137 case <-timer.C:
2138 return true
2139 case <-pc.closech:
2140 return false
2141 }
2142 }
2143 }
2144
2145 func newReadWriteCloserBody(br *bufio.Reader, rwc io.ReadWriteCloser) io.ReadWriteCloser {
2146 body := &readWriteCloserBody{ReadWriteCloser: rwc}
2147 if br.Buffered() != 0 {
2148 body.br = br
2149 }
2150 return body
2151 }
2152
2153
2154
2155
2156
2157
2158 type readWriteCloserBody struct {
2159 _ incomparable
2160 br *bufio.Reader
2161 io.ReadWriteCloser
2162 }
2163
2164 func (b *readWriteCloserBody) Read(p []byte) (n int, err error) {
2165 if b.br != nil {
2166 if n := b.br.Buffered(); len(p) > n {
2167 p = p[:n]
2168 }
2169 n, err = b.br.Read(p)
2170 if b.br.Buffered() == 0 {
2171 b.br = nil
2172 }
2173 return n, err
2174 }
2175 return b.ReadWriteCloser.Read(p)
2176 }
2177
2178 func (b *readWriteCloserBody) CloseWrite() error {
2179 if cw, ok := b.ReadWriteCloser.(interface{ CloseWrite() error }); ok {
2180 return cw.CloseWrite()
2181 }
2182 return fmt.Errorf("CloseWrite: %w", ErrNotSupported)
2183 }
2184
2185
2186 type nothingWrittenError struct {
2187 error
2188 }
2189
2190 func (nwe nothingWrittenError) Unwrap() error {
2191 return nwe.error
2192 }
2193
2194 func (pc *persistConn) writeLoop() {
2195 defer close(pc.writeLoopDone)
2196 for {
2197 select {
2198 case wr := <-pc.writech:
2199 startBytesWritten := pc.nwrite
2200 err := wr.req.Request.write(pc.bw, pc.isProxy, wr.req.extra, pc.waitForContinue(wr.continueCh))
2201 if bre, ok := err.(requestBodyReadError); ok {
2202 err = bre.error
2203
2204
2205
2206
2207
2208
2209
2210 wr.req.setError(err)
2211 }
2212 if err == nil {
2213 err = pc.bw.Flush()
2214 }
2215 if err != nil {
2216 if pc.nwrite == startBytesWritten {
2217 err = nothingWrittenError{err}
2218 }
2219 }
2220 pc.writeErrCh <- err
2221 wr.ch <- err
2222 if err != nil {
2223
2224 return
2225 }
2226 case <-pc.closech:
2227 return
2228 }
2229 }
2230 }
2231
2232
2233
2234
2235
2236
2237
2238 var maxWriteWaitBeforeConnReuse = 50 * time.Millisecond
2239
2240
2241
2242 func (pc *persistConn) wroteRequest() bool {
2243 select {
2244 case err := <-pc.writeErrCh:
2245
2246
2247 return err == nil
2248 default:
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259 t := time.NewTimer(maxWriteWaitBeforeConnReuse)
2260 defer t.Stop()
2261 select {
2262 case err := <-pc.writeErrCh:
2263 return err == nil
2264 case <-t.C:
2265 return false
2266 }
2267 }
2268 }
2269
2270
2271
2272 type responseAndError struct {
2273 _ incomparable
2274 res *Response
2275 waitc <-chan struct{}
2276 err error
2277 }
2278
2279 type requestAndChan struct {
2280 _ incomparable
2281 treq *transportRequest
2282 ch chan responseAndError
2283
2284
2285
2286
2287 addedGzip bool
2288
2289
2290
2291
2292
2293 continueCh chan<- struct{}
2294
2295 callerGone <-chan struct{}
2296 }
2297
2298
2299
2300
2301
2302 type writeRequest struct {
2303 req *transportRequest
2304 ch chan<- error
2305
2306
2307
2308
2309 continueCh <-chan struct{}
2310 }
2311
2312
2313
2314 type timeoutError struct {
2315 err string
2316 }
2317
2318 func (e *timeoutError) Error() string { return e.err }
2319 func (e *timeoutError) Timeout() bool { return true }
2320 func (e *timeoutError) Temporary() bool { return true }
2321 func (e *timeoutError) Is(err error) bool { return err == context.DeadlineExceeded }
2322
2323 var errTimeout error = &timeoutError{"net/http: timeout awaiting response headers"}
2324
2325
2326
2327 var errRequestCanceled = internal.ErrRequestCanceled
2328 var errRequestCanceledConn = errors.New("net/http: request canceled while waiting for connection")
2329
2330
2331
2332 var errRequestDone = errors.New("net/http: request completed")
2333
2334 func nop() {}
2335
2336
2337 var (
2338 testHookEnterRoundTrip = nop
2339 testHookWaitResLoop = nop
2340 testHookRoundTripRetried = nop
2341
2342 testHookMu sync.Locker = fakeLocker{}
2343 testHookReadLoopBeforeNextRead = nop
2344 )
2345
2346 func (pc *persistConn) waitForAvailability(ctx context.Context) error {
2347 select {
2348 case <-pc.availch:
2349 return nil
2350 case <-pc.closech:
2351 return pc.closed
2352 case <-ctx.Done():
2353 return ctx.Err()
2354 }
2355 }
2356
2357 const postRequestWriteErrorWaitTime = 50 * time.Millisecond
2358
2359 func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err error) {
2360 testHookEnterRoundTrip()
2361
2362 pc.mu.Lock()
2363 if !pc.reserved {
2364 select {
2365 case <-pc.availch:
2366 default:
2367 pc.mu.Unlock()
2368 if err := pc.waitForAvailability(req.ctx); err != nil {
2369 return nil, err
2370 }
2371 pc.mu.Lock()
2372 }
2373 }
2374 pc.reserved = false
2375 pc.inFlight = true
2376 pc.numExpectedResponses++
2377 headerFn := pc.mutateHeaderFunc
2378 pc.mu.Unlock()
2379
2380 if headerFn != nil {
2381 headerFn(req.extraHeaders())
2382 }
2383
2384
2385
2386
2387
2388 requestedGzip := false
2389 if !pc.t.DisableCompression &&
2390 req.Header.Get("Accept-Encoding") == "" &&
2391 req.Header.Get("Range") == "" &&
2392 req.Method != "HEAD" {
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405 requestedGzip = true
2406 req.extraHeaders().Set("Accept-Encoding", "gzip")
2407 }
2408
2409 var continueCh chan struct{}
2410 if req.ProtoAtLeast(1, 1) && req.Body != nil && req.expectsContinue() {
2411 continueCh = make(chan struct{}, 1)
2412 }
2413
2414 if pc.t.DisableKeepAlives &&
2415 !req.wantsClose() &&
2416 !isProtocolSwitchHeader(req.Header) &&
2417 !pc.isClientConn {
2418 req.extraHeaders().Set("Connection", "close")
2419 }
2420
2421 gone := make(chan struct{})
2422 defer close(gone)
2423
2424 const debugRoundTrip = false
2425
2426
2427
2428
2429 startBytesWritten := pc.nwrite
2430 writeErrCh := make(chan error, 1)
2431 pc.writech <- writeRequest{req, writeErrCh, continueCh}
2432
2433 resc := make(chan responseAndError)
2434 pc.reqch <- requestAndChan{
2435 treq: req,
2436 ch: resc,
2437 addedGzip: requestedGzip,
2438 continueCh: continueCh,
2439 callerGone: gone,
2440 }
2441
2442 handleResponse := func(re responseAndError) (*Response, error) {
2443 if (re.res == nil) == (re.err == nil) {
2444 panic(fmt.Sprintf("internal error: exactly one of res or err should be set; nil=%v", re.res == nil))
2445 }
2446 if debugRoundTrip {
2447 req.logf("resc recv: %p, %T/%#v", re.res, re.err, re.err)
2448 }
2449 if re.waitc != nil {
2450 <-re.waitc
2451 }
2452 if re.err != nil {
2453 return nil, pc.mapRoundTripError(req, startBytesWritten, re.err)
2454 }
2455 return re.res, nil
2456 }
2457
2458 var writeErr error
2459 var respHeaderTimer <-chan time.Time
2460 ctxDoneChan := req.ctx.Done()
2461 pcClosed := pc.closech
2462 for {
2463 testHookWaitResLoop()
2464 select {
2465 case err := <-writeErrCh:
2466 if debugRoundTrip {
2467 req.logf("writeErrCh recv: %T/%#v", err, err)
2468 }
2469 timeout := pc.t.ResponseHeaderTimeout
2470 if err != nil {
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480 if _, ok := errors.AsType[*net.OpError](err); !ok {
2481 pc.CloseWithError(fmt.Errorf("write error: %w", err))
2482 return nil, pc.mapRoundTripError(req, startBytesWritten, err)
2483 }
2484 writeErr = err
2485 if timeout == 0 {
2486 timeout = postRequestWriteErrorWaitTime
2487 } else {
2488 timeout = min(timeout, postRequestWriteErrorWaitTime)
2489 }
2490 }
2491 if timeout > 0 {
2492 if debugRoundTrip {
2493 req.logf("starting timer for %v", timeout)
2494 }
2495 timer := time.NewTimer(timeout)
2496 respHeaderTimer = timer.C
2497 }
2498 case <-pcClosed:
2499 select {
2500 case re := <-resc:
2501
2502
2503
2504 return handleResponse(re)
2505 default:
2506 }
2507 if debugRoundTrip {
2508 req.logf("closech recv: %T %#v", pc.closed, pc.closed)
2509 }
2510 return nil, pc.mapRoundTripError(req, startBytesWritten, pc.closed)
2511 case <-respHeaderTimer:
2512 if debugRoundTrip {
2513 req.logf("timeout waiting for response headers.")
2514 }
2515 if writeErr != nil {
2516
2517
2518 pc.CloseWithError(fmt.Errorf("write error: %w", writeErr))
2519 return nil, pc.mapRoundTripError(req, startBytesWritten, writeErr)
2520 }
2521 pc.CloseWithError(errTimeout)
2522 return nil, errTimeout
2523 case re := <-resc:
2524 return handleResponse(re)
2525 case <-ctxDoneChan:
2526 select {
2527 case re := <-resc:
2528
2529
2530
2531 return handleResponse(re)
2532 default:
2533 }
2534 pc.cancelRequest(context.Cause(req.ctx))
2535 }
2536 }
2537 }
2538
2539
2540
2541 type tLogKey struct{}
2542
2543 func (tr *transportRequest) logf(format string, args ...any) {
2544 if logf, ok := tr.Request.Context().Value(tLogKey{}).(func(string, ...any)); ok {
2545 logf(time.Now().Format(time.RFC3339Nano)+": "+format, args...)
2546 }
2547 }
2548
2549
2550
2551
2552
2553
2554 func (pc *persistConn) CloseWithError(err error) {
2555 pc.mu.Lock()
2556 defer pc.mu.Unlock()
2557 pc.closeLocked(err)
2558 }
2559
2560 func (pc *persistConn) closeLocked(err error) {
2561 if err == nil {
2562 panic("nil error")
2563 }
2564 if pc.closed == nil {
2565 pc.closed = err
2566 if err != errCallerOwnsConn {
2567 pc.conn.Close()
2568 }
2569 close(pc.closech)
2570 }
2571 pc.mutateHeaderFunc = nil
2572 }
2573
2574 func schemePort(scheme string) string {
2575 switch scheme {
2576 case "http":
2577 return "80"
2578 case "https":
2579 return "443"
2580 case "socks5", "socks5h":
2581 return "1080"
2582 default:
2583 return ""
2584 }
2585 }
2586
2587 func idnaASCIIFromURL(url *url.URL) string {
2588 addr := url.Hostname()
2589 if v, err := idnaASCII(addr); err == nil && v != "" {
2590 addr = v
2591 }
2592 return addr
2593 }
2594
2595
2596 func canonicalAddr(url *url.URL) string {
2597 port := url.Port()
2598 if port == "" {
2599 port = schemePort(url.Scheme)
2600 }
2601 return net.JoinHostPort(idnaASCIIFromURL(url), port)
2602 }
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615 type bodyEOFSignal struct {
2616 body io.ReadCloser
2617 mu sync.Mutex
2618 closed bool
2619 rerr error
2620 fn func(error) error
2621 earlyCloseFn func() error
2622 }
2623
2624 var errReadOnClosedResBody = errors.New("http: read on closed response body")
2625
2626 func (es *bodyEOFSignal) Read(p []byte) (n int, err error) {
2627 es.mu.Lock()
2628 closed, rerr := es.closed, es.rerr
2629 es.mu.Unlock()
2630 if closed {
2631 return 0, errReadOnClosedResBody
2632 }
2633 if rerr != nil {
2634 return 0, rerr
2635 }
2636
2637 n, err = es.body.Read(p)
2638 if err != nil {
2639 es.mu.Lock()
2640 defer es.mu.Unlock()
2641 if es.rerr == nil {
2642 es.rerr = err
2643 }
2644 err = es.condfn(err)
2645 }
2646 return
2647 }
2648
2649 func (es *bodyEOFSignal) Close() error {
2650 es.mu.Lock()
2651 defer es.mu.Unlock()
2652 if es.closed {
2653 return nil
2654 }
2655 es.closed = true
2656 if es.earlyCloseFn != nil && es.rerr == nil {
2657 earlyCloseFn := es.earlyCloseFn
2658 es.earlyCloseFn = nil
2659 es.fn = nil
2660 return earlyCloseFn()
2661 }
2662 if es.rerr != nil && es.rerr != io.EOF {
2663
2664
2665 return nil
2666 }
2667 err := es.body.Close()
2668 return es.condfn(err)
2669 }
2670
2671
2672 func (es *bodyEOFSignal) condfn(err error) error {
2673 if es.fn == nil {
2674 return err
2675 }
2676 fn := es.fn
2677 es.fn = nil
2678 es.earlyCloseFn = nil
2679 return fn(err)
2680 }
2681
2682 type tlsHandshakeTimeoutError struct{}
2683
2684 func (tlsHandshakeTimeoutError) Timeout() bool { return true }
2685 func (tlsHandshakeTimeoutError) Temporary() bool { return true }
2686 func (tlsHandshakeTimeoutError) Error() string { return "net/http: TLS handshake timeout" }
2687
2688
2689
2690
2691 type fakeLocker struct{}
2692
2693 func (fakeLocker) Lock() {}
2694 func (fakeLocker) Unlock() {}
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709 func cloneTLSConfig(cfg *tls.Config) *tls.Config {
2710 if cfg == nil {
2711 return &tls.Config{}
2712 }
2713 return cfg.Clone()
2714 }
2715
View as plain text