Skip to content

Commit f962005

Browse files
committed
net/http: speed up cookie and method validation
Fixes #67031
1 parent d524e1e commit f962005

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/net/http/cookie.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,5 @@ func parseCookieValue(raw string, allowDoubleQuote bool) (value string, quoted,
528528
}
529529

530530
func isCookieNameValid(raw string) bool {
531-
if raw == "" {
532-
return false
533-
}
534-
return strings.IndexFunc(raw, isNotToken) < 0
531+
return isToken(raw)
535532
}

src/net/http/http.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ func isNotToken(r rune) bool {
123123
return !httpguts.IsTokenRune(r)
124124
}
125125

126+
// isToken reports whether v is a valid token (https://www.rfc-editor.org/rfc/rfc2616#section-2.2).
127+
func isToken(v string) bool {
128+
// For historical reasons, this function is called ValidHeaderFieldName (see issue #67031).
129+
return httpguts.ValidHeaderFieldName(v)
130+
}
131+
126132
// stringContainsCTLByte reports whether s contains any ASCII control character.
127133
func stringContainsCTLByte(s string) bool {
128134
for i := 0; i < len(s); i++ {

src/net/http/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ func validMethod(method string) bool {
855855
extension-method = token
856856
token = 1*<any CHAR except CTLs or separators>
857857
*/
858-
return len(method) > 0 && strings.IndexFunc(method, isNotToken) == -1
858+
return isToken(method)
859859
}
860860

861861
// NewRequest wraps [NewRequestWithContext] using [context.Background].

0 commit comments

Comments
 (0)