Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response.ContentEncoding(): store as field and avoid using Header.SetCanonical() #1311

Merged
merged 2 commits into from Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions brotli_test.go
Expand Up @@ -120,7 +120,7 @@ func TestCompressHandlerBrotliLevel(t *testing.T) {
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}
ce := resp.Header.Peek(HeaderContentEncoding)
ce := resp.Header.ContentEncoding()
if string(ce) != "" {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "")
}
Expand All @@ -140,7 +140,7 @@ func TestCompressHandlerBrotliLevel(t *testing.T) {
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}
ce = resp.Header.Peek(HeaderContentEncoding)
ce = resp.Header.ContentEncoding()
if string(ce) != "gzip" {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "gzip")
}
Expand All @@ -163,7 +163,7 @@ func TestCompressHandlerBrotliLevel(t *testing.T) {
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
}
ce = resp.Header.Peek(HeaderContentEncoding)
ce = resp.Header.ContentEncoding()
if string(ce) != "br" {
t.Fatalf("unexpected Content-Encoding: %q. Expecting %q", ce, "br")
}
Expand Down
8 changes: 4 additions & 4 deletions fs.go
Expand Up @@ -898,16 +898,16 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
hdr := &ctx.Response.Header
if ff.compressed {
if fileEncoding == "br" {
hdr.SetCanonical(strContentEncoding, strBr)
hdr.SetContentEncodingBytes(strBr)
} else if fileEncoding == "gzip" {
hdr.SetCanonical(strContentEncoding, strGzip)
hdr.SetContentEncodingBytes(strGzip)
}
}

statusCode := StatusOK
contentLength := ff.contentLength
if h.acceptByteRange {
hdr.SetCanonical(strAcceptRanges, strBytes)
hdr.setNonSpecial(strAcceptRanges, strBytes)
if len(byteRange) > 0 {
startPos, endPos, err := ParseByteRange(byteRange, contentLength)
if err != nil {
Expand All @@ -930,7 +930,7 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
}
}

hdr.SetCanonical(strLastModified, ff.lastModifiedStr)
hdr.setNonSpecial(strLastModified, ff.lastModifiedStr)
if !ctx.IsHead() {
ctx.SetBodyStream(r, contentLength)
} else {
Expand Down
14 changes: 7 additions & 7 deletions fs_test.go
Expand Up @@ -133,7 +133,7 @@ func TestServeFileHead(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

ce := resp.Header.Peek(HeaderContentEncoding)
ce := resp.Header.ContentEncoding()
if len(ce) > 0 {
t.Fatalf("Unexpected 'Content-Encoding' %q", ce)
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestServeFileCompressed(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

ce := resp.Header.Peek(HeaderContentEncoding)
ce := resp.Header.ContentEncoding()
if string(ce) != "gzip" {
t.Fatalf("Unexpected 'Content-Encoding' %q. Expecting %q", ce, "gzip")
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestServeFileCompressed(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

ce = resp.Header.Peek(HeaderContentEncoding)
ce = resp.Header.ContentEncoding()
if string(ce) != "br" {
t.Fatalf("Unexpected 'Content-Encoding' %q. Expecting %q", ce, "br")
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestServeFileUncompressed(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

ce := resp.Header.Peek(HeaderContentEncoding)
ce := resp.Header.ContentEncoding()
if len(ce) > 0 {
t.Fatalf("Unexpected 'Content-Encoding' %q", ce)
}
Expand Down Expand Up @@ -567,7 +567,7 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
if resp.StatusCode() != StatusOK {
t.Errorf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce := resp.Header.Peek(HeaderContentEncoding)
ce := resp.Header.ContentEncoding()
if string(ce) != "" {
t.Errorf("unexpected content-encoding %q. Expecting empty string. filePath=%q", ce, filePath)
}
Expand All @@ -586,7 +586,7 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
if resp.StatusCode() != StatusOK {
t.Errorf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce = resp.Header.Peek(HeaderContentEncoding)
ce = resp.Header.ContentEncoding()
if string(ce) != "gzip" {
t.Errorf("unexpected content-encoding %q. Expecting %q. filePath=%q", ce, "gzip", filePath)
}
Expand All @@ -611,7 +611,7 @@ func testFSCompress(t *testing.T, h RequestHandler, filePath string) {
if resp.StatusCode() != StatusOK {
t.Errorf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusOK, filePath)
}
ce = resp.Header.Peek(HeaderContentEncoding)
ce = resp.Header.ContentEncoding()
if string(ce) != "br" {
t.Errorf("unexpected content-encoding %q. Expecting %q. filePath=%q", ce, "br", filePath)
}
Expand Down
69 changes: 57 additions & 12 deletions header.go
Expand Up @@ -39,8 +39,9 @@ type ResponseHeader struct {
contentLengthBytes []byte
secureErrorLogMessage bool

contentType []byte
server []byte
contentType []byte
contentEncoding []byte
server []byte

h []argsKV
trailer []argsKV
Expand Down Expand Up @@ -103,7 +104,7 @@ func (h *ResponseHeader) SetContentRange(startPos, endPos, contentLength int) {
b = AppendUint(b, contentLength)
h.bufKV.value = b

h.SetCanonical(strContentRange, h.bufKV.value)
h.setNonSpecial(strContentRange, h.bufKV.value)
}

// SetByteRange sets 'Range: bytes=startPos-endPos' header.
Expand All @@ -125,7 +126,7 @@ func (h *RequestHeader) SetByteRange(startPos, endPos int) {
}
h.bufKV.value = b

h.SetCanonical(strRange, h.bufKV.value)
h.setNonSpecial(strRange, h.bufKV.value)
}

// StatusCode returns response status code.
Expand Down Expand Up @@ -167,7 +168,7 @@ func (h *ResponseHeader) SetProtocol(protocol []byte) {
// SetLastModified sets 'Last-Modified' header to the given value.
func (h *ResponseHeader) SetLastModified(t time.Time) {
h.bufKV.value = AppendHTTPDate(h.bufKV.value[:0], t)
h.SetCanonical(strLastModified, h.bufKV.value)
h.setNonSpecial(strLastModified, h.bufKV.value)
}

// ConnectionClose returns true if 'Connection: close' header is set.
Expand Down Expand Up @@ -325,6 +326,21 @@ func (h *ResponseHeader) SetContentTypeBytes(contentType []byte) {
h.contentType = append(h.contentType[:0], contentType...)
}

// ContentEncoding returns Content-Encoding header value.
func (h *ResponseHeader) ContentEncoding() []byte {
return h.contentEncoding
}

// SetContentEncoding sets Content-Encoding header value.
func (h *ResponseHeader) SetContentEncoding(contentEncoding string) {
h.contentEncoding = append(h.contentEncoding[:0], contentEncoding...)
}

// SetContentEncodingBytes sets Content-Encoding header value.
func (h *ResponseHeader) SetContentEncodingBytes(contentEncoding []byte) {
h.contentEncoding = append(h.contentEncoding[:0], contentEncoding...)
}

// Server returns Server header value.
func (h *ResponseHeader) Server() []byte {
return h.server
Expand Down Expand Up @@ -580,7 +596,7 @@ func (h *RequestHeader) SetReferer(referer string) {

// SetRefererBytes sets Referer header value.
func (h *RequestHeader) SetRefererBytes(referer []byte) {
h.SetCanonical(strReferer, referer)
h.setNonSpecial(strReferer, referer)
}

// Method returns HTTP request method.
Expand Down Expand Up @@ -937,6 +953,7 @@ func (h *ResponseHeader) resetSkipNormalize() {
h.contentLengthBytes = h.contentLengthBytes[:0]

h.contentType = h.contentType[:0]
h.contentEncoding = h.contentEncoding[:0]
h.server = h.server[:0]

h.h = h.h[:0]
Expand Down Expand Up @@ -994,6 +1011,7 @@ func (h *ResponseHeader) CopyTo(dst *ResponseHeader) {
dst.contentLength = h.contentLength
dst.contentLengthBytes = append(dst.contentLengthBytes, h.contentLengthBytes...)
dst.contentType = append(dst.contentType, h.contentType...)
dst.contentEncoding = append(dst.contentEncoding, h.contentEncoding...)
dst.server = append(dst.server, h.server...)
dst.h = copyArgs(dst.h, h.h)
dst.cookies = copyArgs(dst.cookies, h.cookies)
Expand Down Expand Up @@ -1035,6 +1053,10 @@ func (h *ResponseHeader) VisitAll(f func(key, value []byte)) {
if len(contentType) > 0 {
f(strContentType, contentType)
}
contentEncoding := h.ContentEncoding()
if len(contentEncoding) > 0 {
f(strContentEncoding, contentEncoding)
}
server := h.Server()
if len(server) > 0 {
f(strServer, server)
Expand Down Expand Up @@ -1158,6 +1180,8 @@ func (h *ResponseHeader) del(key []byte) {
switch string(key) {
case HeaderContentType:
h.contentType = h.contentType[:0]
case HeaderContentEncoding:
h.contentEncoding = h.contentEncoding[:0]
case HeaderServer:
h.server = h.server[:0]
case HeaderSetCookie:
Expand Down Expand Up @@ -1224,12 +1248,15 @@ func (h *ResponseHeader) setSpecialHeader(key, value []byte) bool {
h.contentLengthBytes = append(h.contentLengthBytes[:0], value...)
}
return true
} else if caseInsensitiveCompare(strContentEncoding, key) {
h.SetContentEncodingBytes(value)
return true
} else if caseInsensitiveCompare(strConnection, key) {
if bytes.Equal(strClose, value) {
h.SetConnectionClose()
} else {
h.ResetConnectionClose()
h.h = setArgBytes(h.h, key, value, argsHasValue)
h.setNonSpecial(key, value)
}
return true
}
Expand Down Expand Up @@ -1262,6 +1289,11 @@ func (h *ResponseHeader) setSpecialHeader(key, value []byte) bool {
return false
}

// setNonSpecial directly put into map i.e. not a basic header
func (h *ResponseHeader) setNonSpecial(key []byte, value []byte) {
h.h = setArgBytes(h.h, key, value, argsHasValue)
}

// setSpecialHeader handles special headers and return true when a header is processed.
func (h *RequestHeader) setSpecialHeader(key, value []byte) bool {
if len(key) == 0 {
Expand All @@ -1284,7 +1316,7 @@ func (h *RequestHeader) setSpecialHeader(key, value []byte) bool {
h.SetConnectionClose()
} else {
h.ResetConnectionClose()
h.h = setArgBytes(h.h, key, value, argsHasValue)
h.setNonSpecial(key, value)
}
return true
} else if caseInsensitiveCompare(strCookie, key) {
Expand Down Expand Up @@ -1315,6 +1347,11 @@ func (h *RequestHeader) setSpecialHeader(key, value []byte) bool {
return false
}

// setNonSpecial directly put into map i.e. not a basic header
func (h *RequestHeader) setNonSpecial(key []byte, value []byte) {
h.h = setArgBytes(h.h, key, value, argsHasValue)
}

// Add adds the given 'key: value' header.
//
// Multiple headers with the same key may be added with this function.
Expand Down Expand Up @@ -1434,8 +1471,7 @@ func (h *ResponseHeader) SetCanonical(key, value []byte) {
if h.setSpecialHeader(key, value) {
return
}

h.h = setArgBytes(h.h, key, value, argsHasValue)
h.setNonSpecial(key, value)
}

// SetCookie sets the given response cookie.
Expand Down Expand Up @@ -1646,8 +1682,7 @@ func (h *RequestHeader) SetCanonical(key, value []byte) {
if h.setSpecialHeader(key, value) {
return
}

h.h = setArgBytes(h.h, key, value, argsHasValue)
h.setNonSpecial(key, value)
}

// Peek returns header value for the given key.
Expand Down Expand Up @@ -1696,6 +1731,8 @@ func (h *ResponseHeader) peek(key []byte) []byte {
switch string(key) {
case HeaderContentType:
return h.ContentType()
case HeaderContentEncoding:
return h.ContentEncoding()
case HeaderServer:
return h.Server()
case HeaderConnection:
Expand Down Expand Up @@ -2151,6 +2188,10 @@ func (h *ResponseHeader) AppendBytes(dst []byte) []byte {
dst = appendHeaderLine(dst, strContentType, contentType)
}
}
contentEncoding := h.ContentEncoding()
if len(contentEncoding) > 0 {
dst = appendHeaderLine(dst, strContentEncoding, contentEncoding)
}

if len(h.contentLengthBytes) > 0 {
dst = appendHeaderLine(dst, strContentLength, h.contentLengthBytes)
Expand Down Expand Up @@ -2616,6 +2657,10 @@ func (h *ResponseHeader) parseHeaders(buf []byte) (int, error) {
h.contentType = append(h.contentType[:0], s.value...)
continue
}
if caseInsensitiveCompare(s.key, strContentEncoding) {
h.contentEncoding = append(h.contentEncoding[:0], s.value...)
continue
}
if caseInsensitiveCompare(s.key, strContentLength) {
if h.contentLength != -1 {
if h.contentLength, err = parseContentLength(s.value); err != nil {
Expand Down