Skip to content

Commit

Permalink
Added Protocol() as a replacement of hardcoded strHTTP11 (#969)
Browse files Browse the repository at this point in the history
* Added Protocol() as a replacement of hardcoded strHTTP11

* Applied review changes

* Modify h.proto in parseFirstLine
  • Loading branch information
dgrr authored Feb 16, 2021
1 parent 52a8ab6 commit 1b61ca2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
30 changes: 29 additions & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type RequestHeader struct {

method []byte
requestURI []byte
proto []byte
host []byte
contentType []byte
userAgent []byte
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ type Server struct {
// which will close it when needed.
KeepHijackedConns bool


// CloseOnShutdown when true adds a `Connection: close` header when when the server is shutting down.
CloseOnShutdown bool

Expand All @@ -384,7 +383,6 @@ type Server struct {
// larger then the current limit.
StreamRequestBody bool


tlsConfig *tls.Config
nextProtos map[string]ServeHandler

Expand Down
1 change: 1 addition & 0 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
strCRLF = []byte("\r\n")
strHTTP = []byte("http")
strHTTPS = []byte("https")
strHTTP10 = []byte("HTTP/1.0")
strHTTP11 = []byte("HTTP/1.1")
strColon = []byte(":")
strColonSlashSlash = []byte("://")
Expand Down
2 changes: 1 addition & 1 deletion uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,4 @@ func TestURIWithQuerystringOverride(t *testing.T) {
if uriString != "/?q1=foo&q2=bar&q4=quux" {
t.Fatalf("Expected Querystring to be overriden but was %s ", uriString)
}
}
}

0 comments on commit 1b61ca2

Please sign in to comment.