Repository navigation
perf: parse WebSocket handshake headers lazily #610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "iter" | ||
| "log" | ||
| "net/http" | ||
| "net/textproto" | ||
|
|
@@ -264,9 +265,8 @@ func match(pattern, s string) (bool, error) { | |
| } | ||
|
|
||
| func selectSubprotocol(r *http.Request, subprotocols []string) string { | ||
| cps := headerTokens(r.Header, "Sec-WebSocket-Protocol") | ||
| for _, sp := range subprotocols { | ||
| for _, cp := range cps { | ||
| for cp := range headerTokens(r.Header, "Sec-WebSocket-Protocol") { | ||
| if strings.EqualFold(sp, cp) { | ||
| return cp | ||
| } | ||
|
|
@@ -275,11 +275,11 @@ func selectSubprotocol(r *http.Request, subprotocols []string) string { | |
| return "" | ||
| } | ||
|
|
||
| func selectDeflate(extensions []websocketExtension, mode CompressionMode) (*compressionOptions, bool) { | ||
| func selectDeflate(extensions iter.Seq[websocketExtension], mode CompressionMode) (*compressionOptions, bool) { | ||
| if mode == CompressionDisabled { | ||
| return nil, false | ||
| } | ||
| for _, ext := range extensions { | ||
| for ext := range extensions { | ||
| switch ext.name { | ||
| // We used to implement x-webkit-deflate-frame too for Safari but Safari has bugs... | ||
| // See https://github.com/coder/websocket/issues/218 | ||
|
|
@@ -295,7 +295,7 @@ func selectDeflate(extensions []websocketExtension, mode CompressionMode) (*comp | |
|
|
||
| func acceptDeflate(ext websocketExtension, mode CompressionMode) (*compressionOptions, bool) { | ||
| copts := mode.opts() | ||
| for _, p := range ext.params { | ||
| for p := range ext.params { | ||
| switch p { | ||
| case "client_no_context_takeover": | ||
| copts.clientNoContextTakeover = true | ||
|
|
@@ -318,7 +318,7 @@ func acceptDeflate(ext websocketExtension, mode CompressionMode) (*compressionOp | |
| } | ||
|
|
||
| func headerContainsTokenIgnoreCase(h http.Header, key, token string) bool { | ||
| for _, t := range headerTokens(h, key) { | ||
| for t := range headerTokens(h, key) { | ||
| if strings.EqualFold(t, token) { | ||
| return true | ||
| } | ||
|
|
@@ -328,43 +328,42 @@ func headerContainsTokenIgnoreCase(h http.Header, key, token string) bool { | |
|
|
||
| type websocketExtension struct { | ||
| name string | ||
| params []string | ||
| params iter.Seq[string] | ||
| } | ||
|
|
||
| func websocketExtensions(h http.Header) []websocketExtension { | ||
| var exts []websocketExtension | ||
| extStrs := headerTokens(h, "Sec-WebSocket-Extensions") | ||
| for _, extStr := range extStrs { | ||
| if extStr == "" { | ||
| continue | ||
| } | ||
|
|
||
| vals := strings.Split(extStr, ";") | ||
| for i := range vals { | ||
| vals[i] = strings.TrimSpace(vals[i]) | ||
| } | ||
|
|
||
| e := websocketExtension{ | ||
| name: vals[0], | ||
| params: vals[1:], | ||
| func websocketExtensions(h http.Header) iter.Seq[websocketExtension] { | ||
| return func(yield func(websocketExtension) bool) { | ||
| for token := range headerTokens(h, "Sec-WebSocket-Extensions") { | ||
| name, rawParams, hasParams := strings.Cut(token, ";") | ||
| params := func(yieldParam func(string) bool) { | ||
| if !hasParams { | ||
| return | ||
| } | ||
| for param := range strings.SplitSeq(rawParams, ";") { | ||
| if !yieldParam(strings.TrimSpace(param)) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| if !yield(websocketExtension{name: strings.TrimSpace(name), params: params}) { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| exts = append(exts, e) | ||
| } | ||
| return exts | ||
| } | ||
|
Comment on lines
329
to
353
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Storing Storing A brief comment on
|
||
|
|
||
| func headerTokens(h http.Header, key string) []string { | ||
| func headerTokens(h http.Header, key string) iter.Seq[string] { | ||
| key = textproto.CanonicalMIMEHeaderKey(key) | ||
| var tokens []string | ||
| for _, v := range h[key] { | ||
| v = strings.TrimSpace(v) | ||
| for _, t := range strings.Split(v, ",") { | ||
| t = strings.TrimSpace(t) | ||
| tokens = append(tokens, t) | ||
| return func(yield func(string) bool) { | ||
| for _, v := range h[key] { | ||
| for token := range strings.SplitSeq(v, ",") { | ||
| token = strings.TrimSpace(token) | ||
| if token != "" && !yield(token) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return tokens | ||
| } | ||
|
|
||
| var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -560,6 +560,12 @@ func Test_selectDeflate(t *testing.T) { | |
| header: "permessage-deflate; meow", | ||
| expOK: false, | ||
| }, | ||
| { | ||
| name: "permessage-deflate/empty-parameter", | ||
| mode: CompressionNoContextTakeover, | ||
| header: "permessage-deflate;;", | ||
| expOK: false, | ||
| }, | ||
| { | ||
| name: "permessage-deflate/unknown-parameter", | ||
| mode: CompressionNoContextTakeover, | ||
|
|
@@ -586,6 +592,44 @@ func Test_selectDeflate(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func BenchmarkAcceptDelimiterHeaders(b *testing.B) { | ||
| cases := []struct { | ||
| name string | ||
| header string | ||
| value string | ||
| }{ | ||
| {"connection-commas", "Connection", strings.Repeat(",", http.DefaultMaxHeaderBytes) + "Upgrade"}, | ||
| {"extension-commas", "Sec-WebSocket-Extensions", strings.Repeat(",", http.DefaultMaxHeaderBytes)}, | ||
| {"extension-semicolons", "Sec-WebSocket-Extensions", strings.Repeat(";", http.DefaultMaxHeaderBytes)}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| b.Run(tc.name, func(b *testing.B) { | ||
| r := httptest.NewRequest("GET", "/", nil) | ||
| r.Header.Set("Connection", "Upgrade") | ||
| r.Header.Set("Upgrade", "websocket") | ||
| r.Header.Set("Sec-WebSocket-Version", "13") | ||
| r.Header.Set("Sec-WebSocket-Key", xrand.Base64(16)) | ||
| r.Header.Set(tc.header, tc.value) | ||
|
|
||
| errHijack := errors.New("hijack error") | ||
| b.ReportAllocs() | ||
| for b.Loop() { | ||
| w := mockHijacker{ | ||
| ResponseWriter: httptest.NewRecorder(), | ||
| hijack: func() (net.Conn, *bufio.ReadWriter, error) { | ||
| return nil, nil, errHijack | ||
| }, | ||
| } | ||
| _, err := Accept(w, r, &AcceptOptions{CompressionMode: CompressionNoContextTakeover}) | ||
| if !errors.Is(err, errHijack) { | ||
| b.Fatalf("expected hijack error, got %v", err) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
Comment on lines
+595
to
+631
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both new unit tests already pass on Could we expand coverage to include:
|
||
|
|
||
| type mockHijacker struct { | ||
| http.ResponseWriter | ||
| hijack func() (net.Conn, *bufio.ReadWriter, error) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "crypto/rand" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "encoding/base64" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -315,20 +316,28 @@ func verifySubprotocol(subprotos []string, resp *http.Response) error { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func verifyServerExtensions(copts *compressionOptions, h http.Header) (*compressionOptions, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exts := websocketExtensions(h) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(exts) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var ext websocketExtension | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count := 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for e := range websocketExtensions(h) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if count == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ext = e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if count == 2 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if count == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ext := exts[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ext.name != "permessage-deflate" || len(exts) > 1 || copts == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("WebSocket protcol violation: unsupported extensions from server: %+v", exts[1:]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ext.name != "permessage-deflate" || count > 1 || copts == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, errors.New("WebSocket protcol violation: unsupported extensions from server") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+319
to
335
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: a
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _copts := *copts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| copts = &_copts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, p := range ext.params { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for p := range ext.params { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch p { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "client_no_context_takeover": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| copts.clientNoContextTakeover = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
headerTokensinside thesubprotocolsloop re-canonicalizes the header key and re-scansSec-WebSocket-Protocolonce per configured subprotocol, which can be slower thanmasterwhen several subprotocols are configured and miss.A single pass over
headerTokenstracking the best-ranked match preserves server preference order and client casing while scanning the header once: