]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: using strings.CutPrefix replace strings.HasPrefix and strings.TrimPrefix
authorcuiweixie <cuiweixie@gmail.com>
Tue, 27 Sep 2022 08:06:13 +0000 (16:06 +0800)
committerGopher Robot <gobot@golang.org>
Thu, 29 Sep 2022 21:28:08 +0000 (21:28 +0000)
Change-Id: I0b7b6e4e9d2539e4fcb5c08430ba5a74733fad3c
Reviewed-on: https://go-review.googlesource.com/c/go/+/435136
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: xie cui <523516579@qq.com>

src/net/http/cgi/child.go
src/net/http/pprof/pprof.go
src/net/http/server.go

index bdb35a64e50b97ec0e542458621141cc6f85cb2a..1411f0b8e847c9561448b28500d0dafaee4059c6 100644 (file)
@@ -82,10 +82,12 @@ func RequestFromMap(params map[string]string) (*http.Request, error) {
 
        // Copy "HTTP_FOO_BAR" variables to "Foo-Bar" Headers
        for k, v := range params {
-               if !strings.HasPrefix(k, "HTTP_") || k == "HTTP_HOST" {
+               if k == "HTTP_HOST" {
                        continue
                }
-               r.Header.Add(strings.ReplaceAll(k[5:], "_", "-"), v)
+               if after, found := strings.CutPrefix(k, "HTTP_"); found {
+                       r.Header.Add(strings.ReplaceAll(after, "_", "-"), v)
+               }
        }
 
        uriStr := params["REQUEST_URI"]
index bba522768f6684aec8a0c10c27c9377f408081a9..db03af1c44bdbe4c57ee4febd188fdc4f48b4cb4 100644 (file)
@@ -371,8 +371,7 @@ type profileEntry struct {
 // Index responds to a request for "/debug/pprof/" with an HTML page
 // listing the available profiles.
 func Index(w http.ResponseWriter, r *http.Request) {
-       if strings.HasPrefix(r.URL.Path, "/debug/pprof/") {
-               name := strings.TrimPrefix(r.URL.Path, "/debug/pprof/")
+       if name, found := strings.CutPrefix(r.URL.Path, "/debug/pprof/"); found {
                if name != "" {
                        handler(name).ServeHTTP(w, r)
                        return
index b22528dcdb54dc38adc60ff3c670ea641c102b57..8d3b0f3ad118f7d2017e48aea30493c3b1134529 100644 (file)
@@ -509,11 +509,11 @@ const TrailerPrefix = "Trailer:"
 func (w *response) finalTrailers() Header {
        var t Header
        for k, vv := range w.handlerHeader {
-               if strings.HasPrefix(k, TrailerPrefix) {
+               if kk, found := strings.CutPrefix(k, TrailerPrefix); found {
                        if t == nil {
                                t = make(Header)
                        }
-                       t[strings.TrimPrefix(k, TrailerPrefix)] = vv
+                       t[kk] = vv
                }
        }
        for _, k := range w.trailers {