aboutsummaryrefslogtreecommitdiff
path: root/header.go
diff options
context:
space:
mode:
authorGravatar Erik Dubbelboer <erik@dubbelboer.com> 2024-02-11 14:55:31 +0800
committerGravatar GitHub <noreply@github.com> 2024-02-11 07:55:31 +0100
commit332726634240b82456ce8563cd7aa4027612ce36 (patch)
tree64a7b1862d9d6089e7521a7a2c59f8ea19436fe0 /header.go
parentBump dependencies (#1718) (diff)
downloadfasthttp-332726634240b82456ce8563cd7aa4027612ce36.tar.gz
fasthttp-332726634240b82456ce8563cd7aa4027612ce36.tar.bz2
fasthttp-332726634240b82456ce8563cd7aa4027612ce36.zip
Follow RFCs 7230 and 9112 for HTTP versions (#1710)
Require that HTTP versions match the following pattern: HTTP/[0-9]\.[0-9]
Diffstat (limited to 'header.go')
-rw-r--r--header.go36
1 files changed, 26 insertions, 10 deletions
diff --git a/header.go b/header.go
index ac279d7..bdee768 100644
--- a/header.go
+++ b/header.go
@@ -2870,24 +2870,40 @@ func (h *RequestHeader) parseFirstLine(buf []byte) (int, error) {
h.method = append(h.method[:0], b[:n]...)
b = b[n+1:]
- protoStr := strHTTP11
// parse requestURI
n = bytes.LastIndexByte(b, ' ')
- switch {
- case n < 0:
- h.noHTTP11 = true
- n = len(b)
- protoStr = strHTTP10
- case n == 0:
+ if n < 0 {
+ return 0, fmt.Errorf("cannot find whitespace in the first line of request %q", buf)
+ } else if n == 0 {
if h.secureErrorLogMessage {
return 0, fmt.Errorf("requestURI cannot be empty")
}
return 0, fmt.Errorf("requestURI cannot be empty in %q", buf)
- case !bytes.Equal(b[n+1:], strHTTP11):
- h.noHTTP11 = true
- protoStr = b[n+1:]
}
+ protoStr := b[n+1:]
+
+ // Follow RFCs 7230 and 9112 and require that HTTP versions match the following pattern: HTTP/[0-9]\.[0-9]
+ if len(protoStr) != len(strHTTP11) {
+ if h.secureErrorLogMessage {
+ return 0, fmt.Errorf("unsupported HTTP version %q", protoStr)
+ }
+ return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf)
+ }
+ if !bytes.HasPrefix(protoStr, strHTTP11[:5]) {
+ if h.secureErrorLogMessage {
+ return 0, fmt.Errorf("unsupported HTTP version %q", protoStr)
+ }
+ return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf)
+ }
+ if protoStr[5] < '0' || protoStr[5] > '9' || protoStr[7] < '0' || protoStr[7] > '9' {
+ if h.secureErrorLogMessage {
+ return 0, fmt.Errorf("unsupported HTTP version %q", protoStr)
+ }
+ return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf)
+ }
+
+ h.noHTTP11 = !bytes.Equal(protoStr, strHTTP11)
h.proto = append(h.proto[:0], protoStr...)
h.requestURI = append(h.requestURI[:0], b[:n]...)