Skip to content

Commit

Permalink
feat: add GetProtocol and SetProtocol api for ResponseHeader (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzekin authored Apr 27, 2023
1 parent 2e9df0d commit 9d0b519
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 54 deletions.
49 changes: 36 additions & 13 deletions pkg/protocol/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ type RequestHeader struct {
noCopy nocopy.NoCopy //lint:ignore U1000 until noCopy is used

disableNormalizing bool
noHTTP11 bool
protocol string
connectionClose bool
noDefaultContentType bool

Expand All @@ -82,8 +80,10 @@ type RequestHeader struct {
requestURI []byte
host []byte
contentType []byte
userAgent []byte
mulHeader [][]byte

userAgent []byte
mulHeader [][]byte
protocol string

h []argsKV
bufKV argsKV
Expand Down Expand Up @@ -111,7 +111,6 @@ type ResponseHeader struct {
noCopy nocopy.NoCopy //lint:ignore U1000 until noCopy is used

disableNormalizing bool
noHTTP11 bool
connectionClose bool
noDefaultContentType bool
noDefaultDate bool
Expand All @@ -124,6 +123,7 @@ type ResponseHeader struct {
contentType []byte
server []byte
mulHeader [][]byte
protocol string

h []argsKV
bufKV argsKV
Expand Down Expand Up @@ -173,8 +173,17 @@ func (h *ResponseHeader) PeekArgBytes(key []byte) []byte {
return peekArgBytes(h.h, key)
}

// Deprecated: Use ResponseHeader.SetProtocol(consts.HTTP11) instead
//
// Now SetNoHTTP11(true) equal to SetProtocol(consts.HTTP10)
// SetNoHTTP11(false) equal to SetProtocol(consts.HTTP11)
func (h *ResponseHeader) SetNoHTTP11(b bool) {
h.noHTTP11 = b
if b {
h.protocol = consts.HTTP10
return
}

h.protocol = consts.HTTP11
}

// Cookie fills cookie for the given cookie.Key.
Expand All @@ -196,7 +205,7 @@ func (h *ResponseHeader) FullCookie() []byte {

// IsHTTP11 returns true if the response is HTTP/1.1.
func (h *ResponseHeader) IsHTTP11() bool {
return !h.noHTTP11
return h.protocol == consts.HTTP11
}

// SetContentType sets Content-Type header value.
Expand All @@ -222,7 +231,6 @@ func (h *ResponseHeader) CopyTo(dst *ResponseHeader) {
dst.Reset()

dst.disableNormalizing = h.disableNormalizing
dst.noHTTP11 = h.noHTTP11
dst.connectionClose = h.connectionClose
dst.noDefaultContentType = h.noDefaultContentType
dst.noDefaultDate = h.noDefaultDate
Expand Down Expand Up @@ -289,7 +297,7 @@ func (h *ResponseHeader) VisitAll(f func(key, value []byte)) {

// IsHTTP11 returns true if the request is HTTP/1.1.
func (h *RequestHeader) IsHTTP11() bool {
return !h.noHTTP11
return h.protocol == consts.HTTP11
}

func (h *RequestHeader) SetProtocol(p string) {
Expand All @@ -300,8 +308,17 @@ func (h *RequestHeader) GetProtocol() string {
return h.protocol
}

// Deprecated: Use RequestHeader.SetProtocol(consts.HTTP11) instead
//
// Now SetNoHTTP11(true) equal to SetProtocol(consts.HTTP10)
// SetNoHTTP11(false) equal to SetProtocol(consts.HTTP11)
func (h *RequestHeader) SetNoHTTP11(b bool) {
h.noHTTP11 = b
if b {
h.protocol = consts.HTTP10
return
}

h.protocol = consts.HTTP11
}

func (h *RequestHeader) InitBufValue(size int) {
Expand Down Expand Up @@ -486,7 +503,7 @@ func checkWriteHeaderCode(code int) {
}

func (h *ResponseHeader) ResetSkipNormalize() {
h.noHTTP11 = false
h.protocol = ""
h.connectionClose = false

h.statusCode = 0
Expand Down Expand Up @@ -1075,7 +1092,6 @@ func (h *RequestHeader) CopyTo(dst *RequestHeader) {
dst.Reset()

dst.disableNormalizing = h.disableNormalizing
dst.noHTTP11 = h.noHTTP11
dst.connectionClose = h.connectionClose
dst.noDefaultContentType = h.noDefaultContentType

Expand Down Expand Up @@ -1395,7 +1411,6 @@ func (h *RequestHeader) SetCanonical(key, value []byte) {
}

func (h *RequestHeader) ResetSkipNormalize() {
h.noHTTP11 = false
h.connectionClose = false
h.protocol = ""
h.noDefaultContentType = false
Expand Down Expand Up @@ -1803,3 +1818,11 @@ func (h *RequestHeader) Trailer() *Trailer {
}
return h.trailer
}

func (h *ResponseHeader) SetProtocol(p string) {
h.protocol = p
}

func (h *ResponseHeader) GetProtocol() string {
return h.protocol
}
8 changes: 4 additions & 4 deletions pkg/protocol/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ func TestResponseHeaderSetHeaderLength(t *testing.T) {
func TestSetNoHTTP11(t *testing.T) {
rh := ResponseHeader{}
rh.SetNoHTTP11(true)
assert.True(t, rh.noHTTP11)
assert.DeepEqual(t, consts.HTTP10, rh.protocol)

rh.SetNoHTTP11(false)
assert.False(t, rh.noHTTP11)
assert.DeepEqual(t, consts.HTTP11, rh.protocol)
assert.True(t, rh.IsHTTP11())

h := RequestHeader{}
h.SetNoHTTP11(true)
assert.True(t, h.noHTTP11)
assert.DeepEqual(t, consts.HTTP10, h.protocol)

h.SetNoHTTP11(false)
assert.False(t, h.noHTTP11)
assert.DeepEqual(t, consts.HTTP11, h.protocol)
assert.True(t, h.IsHTTP11())
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/protocol/http1/req/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,11 @@ func parseFirstLine(h *protocol.RequestHeader, buf []byte) (int, error) {
// parse requestURI
n = bytes.LastIndexByte(b, ' ')
if n < 0 {
h.SetNoHTTP11(true)
h.SetProtocol(consts.HTTP10)
n = len(b)
} else if n == 0 {
return 0, fmt.Errorf("requestURI cannot be empty in %q", buf)
} else if !bytes.Equal(b[n+1:], bytestr.StrHTTP11) {
h.SetNoHTTP11(true)
h.SetProtocol(consts.HTTP10)
}
h.SetRequestURIBytes(b[:n])
Expand Down
9 changes: 8 additions & 1 deletion pkg/protocol/http1/resp/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,14 @@ func parseFirstLine(h *protocol.ResponseHeader, buf []byte) (int, error) {
if n < 0 {
return 0, fmt.Errorf("cannot find whitespace in the first line of response %q", buf)
}
h.SetNoHTTP11(!bytes.Equal(b[:n], bytestr.StrHTTP11))

isHTTP11 := bytes.Equal(b[:n], bytestr.StrHTTP11)
if !isHTTP11 {
h.SetProtocol(consts.HTTP10)
} else {
h.SetProtocol(consts.HTTP11)
}

b = b[n+1:]

// parse status code
Expand Down
Loading

0 comments on commit 9d0b519

Please sign in to comment.