Skip to content

Commit fd8938c

Browse files
Julien Cretelgopherbot
Julien Cretel
authored andcommitted
net/http: speed up cookie and method validation
Fixes #67031 Change-Id: I1d764afdc7e50d61007f5f71a674eb6872ce507a GitHub-Last-Rev: 869535e GitHub-Pull-Request: #71798 Reviewed-on: https://go-review.googlesource.com/c/go/+/650195 Auto-Submit: Sean Liao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Sean Liao <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 32fdcd7 commit fd8938c

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

Diff for: src/net/http/cookie.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func ParseCookie(line string) ([]*Cookie, error) {
7979
if !found {
8080
return nil, errEqualNotFoundInCookie
8181
}
82-
if !isCookieNameValid(name) {
82+
if !isToken(name) {
8383
return nil, errInvalidCookieName
8484
}
8585
value, quoted, found := parseCookieValue(value, true)
@@ -104,7 +104,7 @@ func ParseSetCookie(line string) (*Cookie, error) {
104104
return nil, errEqualNotFoundInCookie
105105
}
106106
name = textproto.TrimString(name)
107-
if !isCookieNameValid(name) {
107+
if !isToken(name) {
108108
return nil, errInvalidCookieName
109109
}
110110
value, quoted, ok := parseCookieValue(value, true)
@@ -225,7 +225,7 @@ func SetCookie(w ResponseWriter, cookie *Cookie) {
225225
// header (if other fields are set).
226226
// If c is nil or c.Name is invalid, the empty string is returned.
227227
func (c *Cookie) String() string {
228-
if c == nil || !isCookieNameValid(c.Name) {
228+
if c == nil || !isToken(c.Name) {
229229
return ""
230230
}
231231
// extraCookieLength derived from typical length of cookie attributes
@@ -295,7 +295,7 @@ func (c *Cookie) Valid() error {
295295
if c == nil {
296296
return errors.New("http: nil Cookie")
297297
}
298-
if !isCookieNameValid(c.Name) {
298+
if !isToken(c.Name) {
299299
return errors.New("http: invalid Cookie.Name")
300300
}
301301
if !c.Expires.IsZero() && !validCookieExpires(c.Expires) {
@@ -349,7 +349,7 @@ func readCookies(h Header, filter string) []*Cookie {
349349
}
350350
name, val, _ := strings.Cut(part, "=")
351351
name = textproto.TrimString(name)
352-
if !isCookieNameValid(name) {
352+
if !isToken(name) {
353353
continue
354354
}
355355
if filter != "" && filter != name {
@@ -526,10 +526,3 @@ func parseCookieValue(raw string, allowDoubleQuote bool) (value string, quoted,
526526
}
527527
return raw, quoted, true
528528
}
529-
530-
func isCookieNameValid(raw string) bool {
531-
if raw == "" {
532-
return false
533-
}
534-
return strings.IndexFunc(raw, isNotToken) < 0
535-
}

Diff for: src/net/http/http.go

+6
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++ {

Diff for: src/net/http/request.go

+1-1
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)