Skip to content

Commit

Permalink
net/http: speed up cookie and method validation
Browse files Browse the repository at this point in the history
Fixes #67031
  • Loading branch information
Julien Cretel committed Feb 17, 2025
1 parent d524e1e commit f962005
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/net/http/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,5 @@ func parseCookieValue(raw string, allowDoubleQuote bool) (value string, quoted,
}

func isCookieNameValid(raw string) bool {
if raw == "" {
return false
}
return strings.IndexFunc(raw, isNotToken) < 0
return isToken(raw)
}
6 changes: 6 additions & 0 deletions src/net/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func isNotToken(r rune) bool {
return !httpguts.IsTokenRune(r)
}

// isToken reports whether v is a valid token (https://www.rfc-editor.org/rfc/rfc2616#section-2.2).
func isToken(v string) bool {
// For historical reasons, this function is called ValidHeaderFieldName (see issue #67031).
return httpguts.ValidHeaderFieldName(v)
}

// stringContainsCTLByte reports whether s contains any ASCII control character.
func stringContainsCTLByte(s string) bool {
for i := 0; i < len(s); i++ {
Expand Down
2 changes: 1 addition & 1 deletion src/net/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ func validMethod(method string) bool {
extension-method = token
token = 1*<any CHAR except CTLs or separators>
*/
return len(method) > 0 && strings.IndexFunc(method, isNotToken) == -1
return isToken(method)
}

// NewRequest wraps [NewRequestWithContext] using [context.Background].
Expand Down

0 comments on commit f962005

Please sign in to comment.