Skip to content

Commit

Permalink
net/http: use slices to simplify the code
Browse files Browse the repository at this point in the history
"strSliceContains" is replaced by "slices.Contains".

Replace "sort.Strings" with "slices.Sort" since it becomes a wrapper
of "slices.Sort" from Go 1.22.

"headerSorter" no longer has to implement "sort.Interface".
We use "slice.SortFunc" to sort kvs.

Change-Id: Ic29b4c3db147c16079575eca7ad6ff6c0f581188
GitHub-Last-Rev: 78221d5
GitHub-Pull-Request: #66440
Reviewed-on: https://go-review.googlesource.com/c/go/+/573275
Reviewed-by: Ian Lance Taylor <[email protected]>
Reviewed-by: qiulaidongfeng <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Reviewed-by: Emmanuel Odeke <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Emmanuel Odeke <[email protected]>
  • Loading branch information
apocelipes authored and gopherbot committed Mar 21, 2024
1 parent 41bd9a5 commit 4f0408a
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/net/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"net/http/internal/ascii"
"net/url"
"reflect"
"sort"
"slices"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -787,7 +787,7 @@ func (c *Client) makeHeadersCopier(ireq *Request) func(*Request) {
ss = append(ss, c.Name+"="+c.Value)
}
}
sort.Strings(ss) // Ensure deterministic headers
slices.Sort(ss) // Ensure deterministic headers
ireqhdr.Set("Cookie", strings.Join(ss, "; "))
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/net/http/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"fmt"
"net"
"net/url"
"sort"
"slices"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -111,7 +111,7 @@ func (t *Transport) IdleConnKeysForTesting() (keys []string) {
for key := range t.idleConn {
keys = append(keys, key.String())
}
sort.Strings(keys)
slices.Sort(keys)
return
}

Expand All @@ -130,7 +130,7 @@ func (t *Transport) IdleConnStrsForTesting() []string {
ret = append(ret, pc.conn.LocalAddr().String()+"/"+pc.conn.RemoteAddr().String())
}
}
sort.Strings(ret)
slices.Sort(ret)
return ret
}

Expand All @@ -150,7 +150,7 @@ func (t *Transport) IdleConnStrsForTesting_h2() []string {
}
}

sort.Strings(ret)
slices.Sort(ret)
return ret
}

Expand Down
12 changes: 3 additions & 9 deletions src/net/http/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"net/http/httptrace"
"net/http/internal/ascii"
"net/textproto"
"sort"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -152,17 +152,11 @@ type keyValues struct {
values []string
}

// A headerSorter implements sort.Interface by sorting a []keyValues
// by key. It's used as a pointer, so it can fit in a sort.Interface
// interface value without allocation.
// headerSorter contains a slice of keyValues sorted by keyValues.key.
type headerSorter struct {
kvs []keyValues
}

func (s *headerSorter) Len() int { return len(s.kvs) }
func (s *headerSorter) Swap(i, j int) { s.kvs[i], s.kvs[j] = s.kvs[j], s.kvs[i] }
func (s *headerSorter) Less(i, j int) bool { return s.kvs[i].key < s.kvs[j].key }

var headerSorterPool = sync.Pool{
New: func() any { return new(headerSorter) },
}
Expand All @@ -182,7 +176,7 @@ func (h Header) sortedKeyValues(exclude map[string]bool) (kvs []keyValues, hs *h
}
}
hs.kvs = kvs
sort.Sort(hs)
slices.SortFunc(hs.kvs, func(a, b keyValues) int { return strings.Compare(a.key, b.key) })
return kvs, hs
}

Expand Down
4 changes: 2 additions & 2 deletions src/net/http/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"net/http"
"os"
"runtime"
"sort"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -50,7 +50,7 @@ func interestingGoroutines() (gs []string) {
}
gs = append(gs, stack)
}
sort.Strings(gs)
slices.Sort(gs)
return
}

Expand Down
5 changes: 2 additions & 3 deletions src/net/http/routing_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package http
import (
"fmt"
"slices"
"sort"
"strings"
"testing"
)
Expand Down Expand Up @@ -35,7 +34,7 @@ func trueConflicts(pat *pattern, pats []*pattern) []string {
s = append(s, p.String())
}
}
sort.Strings(s)
slices.Sort(s)
return s
}

Expand All @@ -47,7 +46,7 @@ func indexConflicts(pat *pattern, idx *routingIndex) []string {
}
return nil
})
sort.Strings(s)
slices.Sort(s)
return slices.Compact(s)
}

Expand Down
5 changes: 2 additions & 3 deletions src/net/http/routing_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package http
import (
"fmt"
"io"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -261,7 +260,7 @@ func TestMatchingMethods(t *testing.T) {
ms := map[string]bool{}
test.tree.matchingMethods(test.host, test.path, ms)
keys := mapKeys(ms)
sort.Strings(keys)
slices.Sort(keys)
got := strings.Join(keys, ",")
if got != test.want {
t.Errorf("got %s, want %s", got, test.want)
Expand All @@ -285,7 +284,7 @@ func (n *routingNode) print(w io.Writer, level int) {
keys = append(keys, k)
return true
})
sort.Strings(keys)
slices.Sort(keys)

for _, k := range keys {
fmt.Fprintf(w, "%s%q:\n", indent, k)
Expand Down
17 changes: 4 additions & 13 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
urlpkg "net/url"
"path"
"runtime"
"sort"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -2652,7 +2652,7 @@ func (mux *ServeMux) matchingMethods(host, path string) []string {
// matchOrRedirect will try appending a trailing slash if there is no match.
mux.tree.matchingMethods(host, path+"/", ms)
methods := mapKeys(ms)
sort.Strings(methods)
slices.Sort(methods)
return methods
}

Expand Down Expand Up @@ -3206,7 +3206,7 @@ func (srv *Server) shouldConfigureHTTP2ForServe() bool {
// passed this tls.Config to tls.NewListener. And if they did,
// it's too late anyway to fix it. It would only be potentially racy.
// See Issue 15908.
return strSliceContains(srv.TLSConfig.NextProtos, http2NextProtoTLS)
return slices.Contains(srv.TLSConfig.NextProtos, http2NextProtoTLS)
}

// ErrServerClosed is returned by the [Server.Serve], [ServeTLS], [ListenAndServe],
Expand Down Expand Up @@ -3308,7 +3308,7 @@ func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error {
}

config := cloneTLSConfig(srv.TLSConfig)
if !strSliceContains(config.NextProtos, "http/1.1") {
if !slices.Contains(config.NextProtos, "http/1.1") {
config.NextProtos = append(config.NextProtos, "http/1.1")
}

Expand Down Expand Up @@ -3815,15 +3815,6 @@ func numLeadingCRorLF(v []byte) (n int) {
return
}

func strSliceContains(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}

// tlsRecordHeaderLooksLikeHTTP reports whether a TLS record header
// looks like it might've been a misdirected plaintext HTTP request.
func tlsRecordHeaderLooksLikeHTTP(hdr [5]byte) bool {
Expand Down
4 changes: 2 additions & 2 deletions src/net/http/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"net/http/internal/ascii"
"net/textproto"
"reflect"
"sort"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -318,7 +318,7 @@ func (t *transferWriter) writeHeader(w io.Writer, trace *httptrace.ClientTrace)
keys = append(keys, k)
}
if len(keys) > 0 {
sort.Strings(keys)
slices.Sort(keys)
// TODO: could do better allocation-wise here, but trailers are rare,
// so being lazy for now.
if _, err := io.WriteString(w, "Trailer: "+strings.Join(keys, ",")+"\r\n"); err != nil {
Expand Down

0 comments on commit 4f0408a

Please sign in to comment.