PNG  IHDR  8] PLTE S =tRNS   PNG  IHDR  8] PLTE S =tRNS   REDROOM
PHP 7.4.33
Preview: transport_internal_test.go Size: 6.05 KB
/proc/thread-self/root/opt/golang/1.22.0/src/net/http/transport_internal_test.go

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// White-box tests for transport.go (in package http instead of http_test).

package http

import (
	"bytes"
	"crypto/tls"
	"errors"
	"io"
	"net"
	"net/http/internal/testcert"
	"strings"
	"testing"
)

// Issue 15446: incorrect wrapping of errors when server closes an idle connection.
func TestTransportPersistConnReadLoopEOF(t *testing.T) {
	ln := newLocalListener(t)
	defer ln.Close()

	connc := make(chan net.Conn, 1)
	go func() {
		defer close(connc)
		c, err := ln.Accept()
		if err != nil {
			t.Error(err)
			return
		}
		connc <- c
	}()

	tr := new(Transport)
	req, _ := NewRequest("GET", "http://"+ln.Addr().String(), nil)
	req = req.WithT(t)
	treq := &transportRequest{Request: req}
	cm := connectMethod{targetScheme: "http", targetAddr: ln.Addr().String()}
	pc, err := tr.getConn(treq, cm)
	if err != nil {
		t.Fatal(err)
	}
	defer pc.close(errors.New("test over"))

	conn := <-connc
	if conn == nil {
		// Already called t.Error in the accept goroutine.
		return
	}
	conn.Close() // simulate the server hanging up on the client

	_, err = pc.roundTrip(treq)
	if !isNothingWrittenError(err) && !isTransportReadFromServerError(err) && err != errServerClosedIdle {
		t.Errorf("roundTrip = %#v, %v; want errServerClosedIdle, transportReadFromServerError, or nothingWrittenError", err, err)
	}

	<-pc.closech
	err = pc.closed
	if !isTransportReadFromServerError(err) && err != errServerClosedIdle {
		t.Errorf("pc.closed = %#v, %v; want errServerClosedIdle or transportReadFromServerError", err, err)
	}
}

func isNothingWrittenError(err error) bool {
	_, ok := err.(nothingWrittenError)
	return ok
}

func isTransportReadFromServerError(err error) bool {
	_, ok := err.(transportReadFromServerError)
	return ok
}

func newLocalListener(t *testing.T) net.Listener {
	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		ln, err = net.Listen("tcp6", "[::1]:0")
	}
	if err != nil {
		t.Fatal(err)
	}
	return ln
}

func dummyRequest(method string) *Request {
	req, err := NewRequest(method, "http://fake.tld/", nil)
	if err != nil {
		panic(err)
	}
	return req
}
func dummyRequestWithBody(method string) *Request {
	req, err := NewRequest(method, "http://fake.tld/", strings.NewReader("foo"))
	if err != nil {
		panic(err)
	}
	return req
}

func dummyRequestWithBodyNoGetBody(method string) *Request {
	req := dummyRequestWithBody(method)
	req.GetBody = nil
	return req
}

// issue22091Error acts like a golang.org/x/net/http2.ErrNoCachedConn.
type issue22091Error struct{}

func (issue22091Error) IsHTTP2NoCachedConnError() {}
func (issue22091Error) Error() string             { return "issue22091Error" }

func TestTransportShouldRetryRequest(t *testing.T) {
	tests := []struct {
		pc  *persistConn
		req *Request

		err  error
		want bool
	}{
		0: {
			pc:   &persistConn{reused: false},
			req:  dummyRequest("POST"),
			err:  nothingWrittenError{},
			want: false,
		},
		1: {
			pc:   &persistConn{reused: true},
			req:  dummyRequest("POST"),
			err:  nothingWrittenError{},
			want: true,
		},
		2: {
			pc:   &persistConn{reused: true},
			req:  dummyRequest("POST"),
			err:  http2ErrNoCachedConn,
			want: true,
		},
		3: {
			pc:   nil,
			req:  nil,
			err:  issue22091Error{}, // like an external http2ErrNoCachedConn
			want: true,
		},
		4: {
			pc:   &persistConn{reused: true},
			req:  dummyRequest("POST"),
			err:  errMissingHost,
			want: false,
		},
		5: {
			pc:   &persistConn{reused: true},
			req:  dummyRequest("POST"),
			err:  transportReadFromServerError{},
			want: false,
		},
		6: {
			pc:   &persistConn{reused: true},
			req:  dummyRequest("GET"),
			err:  transportReadFromServerError{},
			want: true,
		},
		7: {
			pc:   &persistConn{reused: true},
			req:  dummyRequest("GET"),
			err:  errServerClosedIdle,
			want: true,
		},
		8: {
			pc:   &persistConn{reused: true},
			req:  dummyRequestWithBody("POST"),
			err:  nothingWrittenError{},
			want: true,
		},
		9: {
			pc:   &persistConn{reused: true},
			req:  dummyRequestWithBodyNoGetBody("POST"),
			err:  nothingWrittenError{},
			want: false,
		},
	}
	for i, tt := range tests {
		got := tt.pc.shouldRetryRequest(tt.req, tt.err)
		if got != tt.want {
			t.Errorf("%d. shouldRetryRequest = %v; want %v", i, got, tt.want)
		}
	}
}

type roundTripFunc func(r *Request) (*Response, error)

func (f roundTripFunc) RoundTrip(r *Request) (*Response, error) {
	return f(r)
}

// Issue 25009
func TestTransportBodyAltRewind(t *testing.T) {
	cert, err := tls.X509KeyPair(testcert.LocalhostCert, testcert.LocalhostKey)
	if err != nil {
		t.Fatal(err)
	}
	ln := newLocalListener(t)
	defer ln.Close()

	go func() {
		tln := tls.NewListener(ln, &tls.Config{
			NextProtos:   []string{"foo"},
			Certificates: []tls.Certificate{cert},
		})
		for i := 0; i < 2; i++ {
			sc, err := tln.Accept()
			if err != nil {
				t.Error(err)
				return
			}
			if err := sc.(*tls.Conn).Handshake(); err != nil {
				t.Error(err)
				return
			}
			sc.Close()
		}
	}()

	addr := ln.Addr().String()
	req, _ := NewRequest("POST", "https://example.org/", bytes.NewBufferString("request"))
	roundTripped := false
	tr := &Transport{
		DisableKeepAlives: true,
		TLSNextProto: map[string]func(string, *tls.Conn) RoundTripper{
			"foo": func(authority string, c *tls.Conn) RoundTripper {
				return roundTripFunc(func(r *Request) (*Response, error) {
					n, _ := io.Copy(io.Discard, r.Body)
					if n == 0 {
						t.Error("body length is zero")
					}
					if roundTripped {
						return &Response{
							Body:       NoBody,
							StatusCode: 200,
						}, nil
					}
					roundTripped = true
					return nil, http2noCachedConnError{}
				})
			},
		},
		DialTLS: func(_, _ string) (net.Conn, error) {
			tc, err := tls.Dial("tcp", addr, &tls.Config{
				InsecureSkipVerify: true,
				NextProtos:         []string{"foo"},
			})
			if err != nil {
				return nil, err
			}
			if err := tc.Handshake(); err != nil {
				return nil, err
			}
			return tc, nil
		},
	}
	c := &Client{Transport: tr}
	_, err = c.Do(req)
	if err != nil {
		t.Error(err)
	}
}

Directory Contents

Dirs: 9 × Files: 64

Name Size Perms Modified Actions
cgi DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
cookiejar DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
fcgi DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
httptest DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
httptrace DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
httputil DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
internal DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
pprof DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
testdata DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
3.01 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
33.40 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
46.07 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
63.24 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.56 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
11.53 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
19.26 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.42 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.04 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
560 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.38 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
8.43 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.52 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.62 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
30.36 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
46.70 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
357.28 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
812 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.04 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.90 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.05 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.13 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.22 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
900 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
4.94 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.68 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.95 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
517 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.91 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
15.17 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
14.50 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.16 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
2.38 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
9.70 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
48.34 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
23.30 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
42.39 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
11.10 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
4.17 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
9.81 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.89 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
23.63 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
566 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
11.80 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.95 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
4.02 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.48 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.89 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
5.60 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
120.67 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.95 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
192.54 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.90 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
9.56 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
12.90 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.45 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
30.89 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
9.13 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
87.81 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
362 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
364 B lrw-r--r-- 2024-02-02 18:09:55
Edit Download
6.05 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
186.06 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.22 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).
 !"#$%&'(()*+,-./00123456789 t\ wIDATx ]ys  47Y ƒ -  "  Rv  < f{Ɛ $k l L > L  ~h^ 1  [  r G t& h  l F z3O Y ! p A(_g̷ E8 )S 8 c  Kb"z ~ 5 J xAL WU <  *  5 m;W a pB h ~P J 2 3 6 ҙ .Ƹ P i  4g F R L P ΪK/D  M v (a3 k J Œ4N5* SH ` SdJ z  O J Xՠ V>u ߱ BE&L b2 ?2` tX+  c CB A$ i b C ĀMB E : /  # Dx &l =q Ty  0 \p I ( L Ǎ { e 4k ;`u^ヲ eP!( d {  )T A 8 O;Ě n >;s6 !  :Nx `[S D HU ~ q›J F} a g*D 49 / pn k h (t 8NxƐF _!r չ7 ZR R׷ q/5") Ӎ NY 0 x sZ!   o  fu  ,  K"$ ? pg  㕣=  1» {h " fh7    y  } € +7  $ y " X —ą - G P u 4 m >J 5 L =V ' ^@I p ?MS xЌ XV P ! h "C NS9B8̢ ]!K  e   zA , ӏkbY  !< XQ ٿyS| *" f { w  4@[S <  # 0 ! js [m  =,~ o "ݎ DHf Wo $ g ! Vԅ t mB /y Wf V4񺍸 c+@x?  B ~u " xUN e 0 BĂ) ~J pz! 7y6]l Ԥ@ P a< O /DHC `≻  N m"$  0ObB }{ x AO FCG D R ^ "B  { WDH  UR l@ T #  +"d T ; 0 i  D}. 7 ` ' ] w rE &S i ƕiTD EL P _ u h $ Ա FG wVD G L R Zf ' .!] J /ZR oGЍs Mr Ĥ ʬ 3 Q [3 cL ` ^ p + ( F;# B 5 '  2Y f [  ϶R0e }  E 7 6M aۮ H <& n % L] E}Up x紉, Uw' Q  Ǯշo k ވۙ 0N94 VX5 xEDE l D #֤ } C o )W :  ^ s 9  bRf iX5u ཱི 4 :[  T 1. | [E 2ؽ Iy\ : o x K G 5 ylP ' uK E ftb/i[3 .g _  [3M n G, #NwQ5~  ؚ) | n =Ц"x qg gB ` 듘 ~ x w ? ? R~  _ u. &VQ K˻   H C ( TN˄+ `C dA nB׭ D 3"Z G ê ^k H_ /- ~ " R_  .8 Z_ 6@ o  xg  uP ? 3լ @7AM!  E7^ - =V L  x  g-D0  CtmW 7  O  G _ WD0 g C  w1 r d w : a | \  *" f nֳ ^ H# f L ` Z ۽hV  }S F r0Ù Bć5r] @! NL iQ]{s^=4 d  WD  "  "   M; t" 8 5 e dL| "-*st" ) SWD ?R[S e ooF  20.D ? bo =) A i o d ģ ZҰaO @E =) i D a &ܟa CϞ y6 ,<%{^x%{f8? `iw^ ?/ M * IEND B`