Commit f9cf8e5a authored by Daniel Martí's avatar Daniel Martí Committed by Tom Bergan

net/http: various small cleanups

* Remove an unnecessary type conversion
* Make golint happier about consistent receiver names
* Make golint happier about a foo_bar var name

Change-Id: I5223808109f6f8b69ed4be76de82faf2478c6a2e
Reviewed-on: https://go-review.googlesource.com/54530
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarTom Bergan <tombergan@google.com>
parent cc4aac2b
...@@ -317,7 +317,7 @@ func scanETag(s string) (etag string, remain string) { ...@@ -317,7 +317,7 @@ func scanETag(s string) (etag string, remain string) {
// Character values allowed in ETags. // Character values allowed in ETags.
case c == 0x21 || c >= 0x23 && c <= 0x7E || c >= 0x80: case c == 0x21 || c >= 0x23 && c <= 0x7E || c >= 0x80:
case c == '"': case c == '"':
return string(s[:i+1]), s[i+1:] return s[:i+1], s[i+1:]
default: default:
return "", "" return "", ""
} }
......
...@@ -490,8 +490,8 @@ var errMissingHost = errors.New("http: Request.Write on Request with no Host or ...@@ -490,8 +490,8 @@ var errMissingHost = errors.New("http: Request.Write on Request with no Host or
// extraHeaders may be nil // extraHeaders may be nil
// waitForContinue may be nil // waitForContinue may be nil
func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitForContinue func() bool) (err error) { func (r *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitForContinue func() bool) (err error) {
trace := httptrace.ContextClientTrace(req.Context()) trace := httptrace.ContextClientTrace(r.Context())
if trace != nil && trace.WroteRequest != nil { if trace != nil && trace.WroteRequest != nil {
defer func() { defer func() {
trace.WroteRequest(httptrace.WroteRequestInfo{ trace.WroteRequest(httptrace.WroteRequestInfo{
...@@ -504,12 +504,12 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai ...@@ -504,12 +504,12 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai
// is not given, use the host from the request URL. // is not given, use the host from the request URL.
// //
// Clean the host, in case it arrives with unexpected stuff in it. // Clean the host, in case it arrives with unexpected stuff in it.
host := cleanHost(req.Host) host := cleanHost(r.Host)
if host == "" { if host == "" {
if req.URL == nil { if r.URL == nil {
return errMissingHost return errMissingHost
} }
host = cleanHost(req.URL.Host) host = cleanHost(r.URL.Host)
} }
// According to RFC 6874, an HTTP client, proxy, or other // According to RFC 6874, an HTTP client, proxy, or other
...@@ -517,10 +517,10 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai ...@@ -517,10 +517,10 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai
// to an outgoing URI. // to an outgoing URI.
host = removeZone(host) host = removeZone(host)
ruri := req.URL.RequestURI() ruri := r.URL.RequestURI()
if usingProxy && req.URL.Scheme != "" && req.URL.Opaque == "" { if usingProxy && r.URL.Scheme != "" && r.URL.Opaque == "" {
ruri = req.URL.Scheme + "://" + host + ruri ruri = r.URL.Scheme + "://" + host + ruri
} else if req.Method == "CONNECT" && req.URL.Path == "" { } else if r.Method == "CONNECT" && r.URL.Path == "" {
// CONNECT requests normally give just the host and port, not a full URL. // CONNECT requests normally give just the host and port, not a full URL.
ruri = host ruri = host
} }
...@@ -536,7 +536,7 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai ...@@ -536,7 +536,7 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai
w = bw w = bw
} }
_, err = fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(req.Method, "GET"), ruri) _, err = fmt.Fprintf(w, "%s %s HTTP/1.1\r\n", valueOrDefault(r.Method, "GET"), ruri)
if err != nil { if err != nil {
return err return err
} }
...@@ -550,8 +550,8 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai ...@@ -550,8 +550,8 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai
// Use the defaultUserAgent unless the Header contains one, which // Use the defaultUserAgent unless the Header contains one, which
// may be blank to not send the header. // may be blank to not send the header.
userAgent := defaultUserAgent userAgent := defaultUserAgent
if _, ok := req.Header["User-Agent"]; ok { if _, ok := r.Header["User-Agent"]; ok {
userAgent = req.Header.Get("User-Agent") userAgent = r.Header.Get("User-Agent")
} }
if userAgent != "" { if userAgent != "" {
_, err = fmt.Fprintf(w, "User-Agent: %s\r\n", userAgent) _, err = fmt.Fprintf(w, "User-Agent: %s\r\n", userAgent)
...@@ -561,7 +561,7 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai ...@@ -561,7 +561,7 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai
} }
// Process Body,ContentLength,Close,Trailer // Process Body,ContentLength,Close,Trailer
tw, err := newTransferWriter(req) tw, err := newTransferWriter(r)
if err != nil { if err != nil {
return err return err
} }
...@@ -570,7 +570,7 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai ...@@ -570,7 +570,7 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai
return err return err
} }
err = req.Header.WriteSubset(w, reqWriteExcludeHeader) err = r.Header.WriteSubset(w, reqWriteExcludeHeader)
if err != nil { if err != nil {
return err return err
} }
...@@ -603,7 +603,7 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai ...@@ -603,7 +603,7 @@ func (req *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, wai
trace.Wait100Continue() trace.Wait100Continue()
} }
if !waitForContinue() { if !waitForContinue() {
req.closeBody() r.closeBody()
return nil return nil
} }
} }
......
...@@ -663,9 +663,8 @@ func fixLength(isResponse bool, status int, requestMethod string, header Header, ...@@ -663,9 +663,8 @@ func fixLength(isResponse bool, status int, requestMethod string, header Header,
return -1, err return -1, err
} }
return n, nil return n, nil
} else {
header.Del("Content-Length")
} }
header.Del("Content-Length")
if isRequest { if isRequest {
// RFC 2616 neither explicitly permits nor forbids an // RFC 2616 neither explicitly permits nor forbids an
......
...@@ -1224,8 +1224,8 @@ func useProxy(addr string) bool { ...@@ -1224,8 +1224,8 @@ func useProxy(addr string) bool {
} }
} }
no_proxy := noProxyEnv.Get() noProxy := noProxyEnv.Get()
if no_proxy == "*" { if noProxy == "*" {
return false return false
} }
...@@ -1234,7 +1234,7 @@ func useProxy(addr string) bool { ...@@ -1234,7 +1234,7 @@ func useProxy(addr string) bool {
addr = addr[:strings.LastIndex(addr, ":")] addr = addr[:strings.LastIndex(addr, ":")]
} }
for _, p := range strings.Split(no_proxy, ",") { for _, p := range strings.Split(noProxy, ",") {
p = strings.ToLower(strings.TrimSpace(p)) p = strings.ToLower(strings.TrimSpace(p))
if len(p) == 0 { if len(p) == 0 {
continue continue
...@@ -2021,8 +2021,8 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err ...@@ -2021,8 +2021,8 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
// a t.Logf func. See export_test.go's Request.WithT method. // a t.Logf func. See export_test.go's Request.WithT method.
type tLogKey struct{} type tLogKey struct{}
func (r *transportRequest) logf(format string, args ...interface{}) { func (tr *transportRequest) logf(format string, args ...interface{}) {
if logf, ok := r.Request.Context().Value(tLogKey{}).(func(string, ...interface{})); ok { if logf, ok := tr.Request.Context().Value(tLogKey{}).(func(string, ...interface{})); ok {
logf(time.Now().Format(time.RFC3339Nano)+": "+format, args...) logf(time.Now().Format(time.RFC3339Nano)+": "+format, args...)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment