aboutsummaryrefslogtreecommitdiff
path: root/header.go
diff options
context:
space:
mode:
authorGravatar Darío <dgrripoll@gmail.com> 2021-02-16 21:53:40 +0100
committerGravatar GitHub <noreply@github.com> 2021-02-16 21:53:40 +0100
commit1b61ca2e36ab3b581d667156c6b9b9141dc69827 (patch)
tree3ff2820c53bd1628a8decfe800ce0fa497bfc81c /header.go
parentfix s2b go vet warning (#967) (diff)
downloadfasthttp-1b61ca2e36ab3b581d667156c6b9b9141dc69827.tar.gz
fasthttp-1b61ca2e36ab3b581d667156c6b9b9141dc69827.tar.bz2
fasthttp-1b61ca2e36ab3b581d667156c6b9b9141dc69827.zip
Added Protocol() as a replacement of hardcoded strHTTP11 (#969)
* Added Protocol() as a replacement of hardcoded strHTTP11 * Applied review changes * Modify h.proto in parseFirstLine
Diffstat (limited to 'header.go')
-rw-r--r--header.go30
1 files changed, 29 insertions, 1 deletions
diff --git a/header.go b/header.go
index dcc088e..d2caba2 100644
--- a/header.go
+++ b/header.go
@@ -68,6 +68,7 @@ type RequestHeader struct {
method []byte
requestURI []byte
+ proto []byte
host []byte
contentType []byte
userAgent []byte
@@ -455,6 +456,26 @@ func (h *RequestHeader) SetMethodBytes(method []byte) {
h.method = append(h.method[:0], method...)
}
+// Protocol returns HTTP protocol.
+func (h *RequestHeader) Protocol() []byte {
+ if len(h.proto) == 0 {
+ return strHTTP11
+ }
+ return h.proto
+}
+
+// SetProtocol sets HTTP request protocol.
+func (h *RequestHeader) SetProtocol(method string) {
+ h.proto = append(h.proto[:0], method...)
+ h.noHTTP11 = !bytes.Equal(h.proto, strHTTP11)
+}
+
+// SetProtocolBytes sets HTTP request protocol.
+func (h *RequestHeader) SetProtocolBytes(method []byte) {
+ h.proto = append(h.proto[:0], method...)
+ h.noHTTP11 = !bytes.Equal(h.proto, strHTTP11)
+}
+
// RequestURI returns RequestURI from the first HTTP request line.
func (h *RequestHeader) RequestURI() []byte {
requestURI := h.requestURI
@@ -680,6 +701,7 @@ func (h *RequestHeader) resetSkipNormalize() {
h.contentLengthBytes = h.contentLengthBytes[:0]
h.method = h.method[:0]
+ h.proto = h.proto[:0]
h.requestURI = h.requestURI[:0]
h.host = h.host[:0]
h.contentType = h.contentType[:0]
@@ -722,6 +744,7 @@ func (h *RequestHeader) CopyTo(dst *RequestHeader) {
dst.contentLength = h.contentLength
dst.contentLengthBytes = append(dst.contentLengthBytes[:0], h.contentLengthBytes...)
dst.method = append(dst.method[:0], h.method...)
+ dst.proto = append(dst.proto[:0], h.proto...)
dst.requestURI = append(dst.requestURI[:0], h.requestURI...)
dst.host = append(dst.host[:0], h.host...)
dst.contentType = append(dst.contentType[:0], h.contentType...)
@@ -1601,7 +1624,7 @@ func (h *RequestHeader) AppendBytes(dst []byte) []byte {
dst = append(dst, ' ')
dst = append(dst, h.RequestURI()...)
dst = append(dst, ' ')
- dst = append(dst, strHTTP11...)
+ dst = append(dst, h.Protocol()...)
dst = append(dst, strCRLF...)
userAgent := h.UserAgent()
@@ -1736,16 +1759,21 @@ 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, ' ')
if n < 0 {
h.noHTTP11 = true
n = len(b)
+ protoStr = strHTTP10
} else if n == 0 {
return 0, fmt.Errorf("requestURI cannot be empty in %q", buf)
} else if !bytes.Equal(b[n+1:], strHTTP11) {
h.noHTTP11 = true
+ protoStr = b[n+1:]
}
+
+ h.proto = append(h.proto[:0], protoStr...)
h.requestURI = append(h.requestURI[:0], b[:n]...)
return len(buf) - len(bNext), nil