From 44a15a7262b14d517fefab5b7c13ca97ab099a30 Mon Sep 17 00:00:00 2001 From: Dai Jie Date: Fri, 2 Oct 2020 09:09:24 +0000 Subject: net/http: remove duplicate declaration of error there is no need to declare a error variable here. Change-Id: I9ea5bcf568d800efed19c90caf751aaf9abe5555 GitHub-Last-Rev: 538d1f9cee0b8564a8bec262529f567da847f1b0 GitHub-Pull-Request: golang/go#41751 Reviewed-on: https://go-review.googlesource.com/c/go/+/259037 Reviewed-by: Rob Pike Trust: Alberto Donizetti --- src/net/rpc/client.go | 1 - 1 file changed, 1 deletion(-) (limited to 'src/net') diff --git a/src/net/rpc/client.go b/src/net/rpc/client.go index 25f2a004e4..60bb2cc99f 100644 --- a/src/net/rpc/client.go +++ b/src/net/rpc/client.go @@ -245,7 +245,6 @@ func DialHTTP(network, address string) (*Client, error) { // DialHTTPPath connects to an HTTP RPC server // at the specified network address and path. func DialHTTPPath(network, address, path string) (*Client, error) { - var err error conn, err := net.Dial(network, address) if err != nil { return nil, err -- cgit v1.2.1 From db428ad7b61ed757671162054252b4326045e96c Mon Sep 17 00:00:00 2001 From: Cherry Zhang Date: Thu, 17 Sep 2020 15:02:26 -0400 Subject: all: enable more tests on macOS/ARM64 Updates #38485. Change-Id: Iac96f5ffe88521fcb11eab306d0df6463bdce046 Reviewed-on: https://go-review.googlesource.com/c/go/+/256920 Trust: Cherry Zhang Reviewed-by: Dmitri Shuralyov Reviewed-by: Ian Lance Taylor --- src/net/dial_test.go | 2 +- src/net/platform_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/net') diff --git a/src/net/dial_test.go b/src/net/dial_test.go index 2706de4442..57cf5554ad 100644 --- a/src/net/dial_test.go +++ b/src/net/dial_test.go @@ -990,7 +990,7 @@ func TestDialerControl(t *testing.T) { // except that it won't skip testing on non-mobile builders. func mustHaveExternalNetwork(t *testing.T) { t.Helper() - mobile := runtime.GOOS == "android" || (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && runtime.GOARCH == "arm64" + mobile := runtime.GOOS == "android" || runtime.GOOS == "ios" if testenv.Builder() == "" || mobile { testenv.MustHaveExternalNetwork(t) } diff --git a/src/net/platform_test.go b/src/net/platform_test.go index 4b92bb6df0..2da23dedce 100644 --- a/src/net/platform_test.go +++ b/src/net/platform_test.go @@ -82,7 +82,7 @@ func testableNetwork(network string) bool { } func iOS() bool { - return (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && runtime.GOARCH == "arm64" + return runtime.GOOS == "ios" } // testableAddress reports whether address of network is testable on -- cgit v1.2.1 From 930fa890c9b6a75700bda3dc4043de81350749ea Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 6 Oct 2020 10:53:11 -0700 Subject: net/http: add Transport.GetProxyConnectHeader Fixes golang/go#41048 Change-Id: I38e01605bffb6f85100c098051b0c416dd77f261 Reviewed-on: https://go-review.googlesource.com/c/go/+/259917 Trust: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Go Bot Reviewed-by: Damien Neil --- src/net/http/transport.go | 23 ++++++++++++++++++- src/net/http/transport_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) (limited to 'src/net') diff --git a/src/net/http/transport.go b/src/net/http/transport.go index b97c4268b5..4546166430 100644 --- a/src/net/http/transport.go +++ b/src/net/http/transport.go @@ -240,8 +240,18 @@ type Transport struct { // ProxyConnectHeader optionally specifies headers to send to // proxies during CONNECT requests. + // To set the header dynamically, see GetProxyConnectHeader. ProxyConnectHeader Header + // GetProxyConnectHeader optionally specifies a func to return + // headers to send to proxyURL during a CONNECT request to the + // ip:port target. + // If it returns an error, the Transport's RoundTrip fails with + // that error. It can return (nil, nil) to not add headers. + // If GetProxyConnectHeader is non-nil, ProxyConnectHeader is + // ignored. + GetProxyConnectHeader func(ctx context.Context, proxyURL *url.URL, target string) (Header, error) + // MaxResponseHeaderBytes specifies a limit on how many // response bytes are allowed in the server's response // header. @@ -313,6 +323,7 @@ func (t *Transport) Clone() *Transport { ResponseHeaderTimeout: t.ResponseHeaderTimeout, ExpectContinueTimeout: t.ExpectContinueTimeout, ProxyConnectHeader: t.ProxyConnectHeader.Clone(), + GetProxyConnectHeader: t.GetProxyConnectHeader, MaxResponseHeaderBytes: t.MaxResponseHeaderBytes, ForceAttemptHTTP2: t.ForceAttemptHTTP2, WriteBufferSize: t.WriteBufferSize, @@ -1623,7 +1634,17 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *pers } case cm.targetScheme == "https": conn := pconn.conn - hdr := t.ProxyConnectHeader + var hdr Header + if t.GetProxyConnectHeader != nil { + var err error + hdr, err = t.GetProxyConnectHeader(ctx, cm.proxyURL, cm.targetAddr) + if err != nil { + conn.Close() + return nil, err + } + } else { + hdr = t.ProxyConnectHeader + } if hdr == nil { hdr = make(Header) } diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go index f4b7623630..a1c9e822b4 100644 --- a/src/net/http/transport_test.go +++ b/src/net/http/transport_test.go @@ -5174,6 +5174,57 @@ func TestTransportProxyConnectHeader(t *testing.T) { } } +func TestTransportProxyGetConnectHeader(t *testing.T) { + defer afterTest(t) + reqc := make(chan *Request, 1) + ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { + if r.Method != "CONNECT" { + t.Errorf("method = %q; want CONNECT", r.Method) + } + reqc <- r + c, _, err := w.(Hijacker).Hijack() + if err != nil { + t.Errorf("Hijack: %v", err) + return + } + c.Close() + })) + defer ts.Close() + + c := ts.Client() + c.Transport.(*Transport).Proxy = func(r *Request) (*url.URL, error) { + return url.Parse(ts.URL) + } + // These should be ignored: + c.Transport.(*Transport).ProxyConnectHeader = Header{ + "User-Agent": {"foo"}, + "Other": {"bar"}, + } + c.Transport.(*Transport).GetProxyConnectHeader = func(ctx context.Context, proxyURL *url.URL, target string) (Header, error) { + return Header{ + "User-Agent": {"foo2"}, + "Other": {"bar2"}, + }, nil + } + + res, err := c.Get("https://dummy.tld/") // https to force a CONNECT + if err == nil { + res.Body.Close() + t.Errorf("unexpected success") + } + select { + case <-time.After(3 * time.Second): + t.Fatal("timeout") + case r := <-reqc: + if got, want := r.Header.Get("User-Agent"), "foo2"; got != want { + t.Errorf("CONNECT request User-Agent = %q; want %q", got, want) + } + if got, want := r.Header.Get("Other"), "bar2"; got != want { + t.Errorf("CONNECT request Other = %q; want %q", got, want) + } + } +} + var errFakeRoundTrip = errors.New("fake roundtrip") type funcRoundTripper func() @@ -5842,6 +5893,7 @@ func TestTransportClone(t *testing.T) { ResponseHeaderTimeout: time.Second, ExpectContinueTimeout: time.Second, ProxyConnectHeader: Header{}, + GetProxyConnectHeader: func(context.Context, *url.URL, string) (Header, error) { return nil, nil }, MaxResponseHeaderBytes: 1, ForceAttemptHTTP2: true, TLSNextProto: map[string]func(authority string, c *tls.Conn) RoundTripper{ -- cgit v1.2.1 From 5c1567cdc064b68210aeeddc6bf76bf0a146a626 Mon Sep 17 00:00:00 2001 From: Ayan George Date: Tue, 6 Oct 2020 18:40:40 +0000 Subject: net/http/pprof: use Request.Context, not the deprecated CloseNotifier Prior to this commit, the profiling code had a sleep() function that waits and unblocks on either time.After() or a channel provided by an http.CloseNotifier derived from a supplied http.ResponseWriter. According to the documentation, http.CloseNotifier is deprecated: Deprecated: the CloseNotifier interface predates Go's context package. New code should use Request.Context instead. This patch does just that -- sleep() now takes an *http.Request and uses http.Request.Context() to signal when a request has been cancelled. Change-Id: I98702314addf494f5743a4f99172dc607389dbb8 GitHub-Last-Rev: c1e37a03ca28417ed5833618d3eeddb2eecccd09 GitHub-Pull-Request: golang/go#41756 Reviewed-on: https://go-review.googlesource.com/c/go/+/259157 Reviewed-by: Bryan C. Mills Reviewed-by: Hyang-Ah Hana Kim Reviewed-by: Emmanuel Odeke Trust: Bryan C. Mills Trust: Emmanuel Odeke Run-TryBot: Bryan C. Mills TryBot-Result: Go Bot --- src/net/http/pprof/pprof.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'src/net') diff --git a/src/net/http/pprof/pprof.go b/src/net/http/pprof/pprof.go index 81df0448e9..5ff7fdc3de 100644 --- a/src/net/http/pprof/pprof.go +++ b/src/net/http/pprof/pprof.go @@ -93,14 +93,10 @@ func Cmdline(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, strings.Join(os.Args, "\x00")) } -func sleep(w http.ResponseWriter, d time.Duration) { - var clientGone <-chan bool - if cn, ok := w.(http.CloseNotifier); ok { - clientGone = cn.CloseNotify() - } +func sleep(r *http.Request, d time.Duration) { select { case <-time.After(d): - case <-clientGone: + case <-r.Context().Done(): } } @@ -142,7 +138,7 @@ func Profile(w http.ResponseWriter, r *http.Request) { fmt.Sprintf("Could not enable CPU profiling: %s", err)) return } - sleep(w, time.Duration(sec)*time.Second) + sleep(r, time.Duration(sec)*time.Second) pprof.StopCPUProfile() } @@ -171,7 +167,7 @@ func Trace(w http.ResponseWriter, r *http.Request) { fmt.Sprintf("Could not enable tracing: %s", err)) return } - sleep(w, time.Duration(sec*float64(time.Second))) + sleep(r, time.Duration(sec*float64(time.Second))) trace.Stop() } -- cgit v1.2.1 From 542693e00529fbb4248fac614ece68b127a5ec4d Mon Sep 17 00:00:00 2001 From: Roberto Clapis Date: Tue, 22 Sep 2020 17:57:06 +0200 Subject: net/http: make SameSiteDefaultMode behavior match the specification The current specification does not foresee a SameSite attribute without a value. While the existing implementation would serialize SameSite in a way that would likely be ignored by well-impelemented clients, it is better to not rely on this kind of quirks. Specification: https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-05#section-4.1.1 Fixes #36990 Change-Id: Ie51152741d7e84bab64d3e4e4f780286932acbde Reviewed-on: https://go-review.googlesource.com/c/go/+/256498 Trust: Roberto Clapis Reviewed-by: Filippo Valsorda --- src/net/http/cookie.go | 2 +- src/net/http/cookie_test.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'src/net') diff --git a/src/net/http/cookie.go b/src/net/http/cookie.go index d7a8f5e94e..141bc947f6 100644 --- a/src/net/http/cookie.go +++ b/src/net/http/cookie.go @@ -220,7 +220,7 @@ func (c *Cookie) String() string { } switch c.SameSite { case SameSiteDefaultMode: - b.WriteString("; SameSite") + // Skip, default mode is obtained by not emitting the attribute. case SameSiteNoneMode: b.WriteString("; SameSite=None") case SameSiteLaxMode: diff --git a/src/net/http/cookie_test.go b/src/net/http/cookie_test.go index 9e8196ebce..959713a0dc 100644 --- a/src/net/http/cookie_test.go +++ b/src/net/http/cookie_test.go @@ -67,7 +67,7 @@ var writeSetCookiesTests = []struct { }, { &Cookie{Name: "cookie-12", Value: "samesite-default", SameSite: SameSiteDefaultMode}, - "cookie-12=samesite-default; SameSite", + "cookie-12=samesite-default", }, { &Cookie{Name: "cookie-13", Value: "samesite-lax", SameSite: SameSiteLaxMode}, @@ -282,6 +282,15 @@ var readSetCookiesTests = []struct { Raw: "samesitedefault=foo; SameSite", }}, }, + { + Header{"Set-Cookie": {"samesiteinvalidisdefault=foo; SameSite=invalid"}}, + []*Cookie{{ + Name: "samesiteinvalidisdefault", + Value: "foo", + SameSite: SameSiteDefaultMode, + Raw: "samesiteinvalidisdefault=foo; SameSite=invalid", + }}, + }, { Header{"Set-Cookie": {"samesitelax=foo; SameSite=Lax"}}, []*Cookie{{ -- cgit v1.2.1 From 33511fb959f8f0edd5e831a4b41523daf9d84e87 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Wed, 23 Sep 2020 14:06:28 +0300 Subject: net/http/pprof: remove html/template dependency html/template indirectly uses reflect MethodByName, this causes linker to use conservative mode resulting in larger binaries. The template here is trivial and can be replaced by string manipulation. This reduces a binary using only net/http/pprof by ~2.5MB. Fixes #41569 Change-Id: I240e1daa6376182ff4961997ee3ec7b96cb07be8 Reviewed-on: https://go-review.googlesource.com/c/go/+/256900 Reviewed-by: Brad Fitzpatrick Reviewed-by: Hajime Hoshi Run-TryBot: Brad Fitzpatrick TryBot-Result: Go Bot Trust: Hajime Hoshi Trust: Brad Fitzpatrick --- src/net/http/pprof/pprof.go | 60 ++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 25 deletions(-) (limited to 'src/net') diff --git a/src/net/http/pprof/pprof.go b/src/net/http/pprof/pprof.go index 5ff7fdc3de..2bfcfb9545 100644 --- a/src/net/http/pprof/pprof.go +++ b/src/net/http/pprof/pprof.go @@ -61,11 +61,12 @@ import ( "bytes" "context" "fmt" - "html/template" + "html" "internal/profile" "io" "log" "net/http" + "net/url" "os" "runtime" "runtime/pprof" @@ -352,6 +353,13 @@ var profileDescriptions = map[string]string{ "trace": "A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.", } +type profileEntry struct { + Name string + Href string + Desc string + Count int +} + // Index responds with the pprof-formatted profile named by the request. // For example, "/debug/pprof/heap" serves the "heap" profile. // Index responds to a request for "/debug/pprof/" with an HTML page @@ -368,17 +376,11 @@ func Index(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("Content-Type", "text/html; charset=utf-8") - type profile struct { - Name string - Href string - Desc string - Count int - } - var profiles []profile + var profiles []profileEntry for _, p := range pprof.Profiles() { - profiles = append(profiles, profile{ + profiles = append(profiles, profileEntry{ Name: p.Name(), - Href: p.Name() + "?debug=1", + Href: p.Name(), Desc: profileDescriptions[p.Name()], Count: p.Count(), }) @@ -386,7 +388,7 @@ func Index(w http.ResponseWriter, r *http.Request) { // Adding other profiles exposed from within this package for _, p := range []string{"cmdline", "profile", "trace"} { - profiles = append(profiles, profile{ + profiles = append(profiles, profileEntry{ Name: p, Href: p, Desc: profileDescriptions[p], @@ -397,12 +399,14 @@ func Index(w http.ResponseWriter, r *http.Request) { return profiles[i].Name < profiles[j].Name }) - if err := indexTmpl.Execute(w, profiles); err != nil { + if err := indexTmplExecute(w, profiles); err != nil { log.Print(err) } } -var indexTmpl = template.Must(template.New("index").Parse(` +func indexTmplExecute(w io.Writer, profiles []profileEntry) error { + var b bytes.Buffer + b.WriteString(` /debug/pprof/