Skip to content

Commit

Permalink
header.go Response.putHeader() and Request.putHeader() skip check for…
Browse files Browse the repository at this point in the history
… basic headers

The change should improve performance because the setSpecialHeader() call is omitted.
As a downside on adding a new basic header field all putHeader() must be replaced with a direct getter and setter.
  • Loading branch information
stokito committed Jun 4, 2022
1 parent 6a8d763 commit 6e82826
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions fs.go
Expand Up @@ -907,7 +907,7 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
statusCode := StatusOK
contentLength := ff.contentLength
if h.acceptByteRange {
hdr.SetCanonical(strAcceptRanges, strBytes)
hdr.putHeader(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.putHeader(strLastModified, ff.lastModifiedStr)
if !ctx.IsHead() {
ctx.SetBodyStream(r, contentLength)
} else {
Expand Down
28 changes: 18 additions & 10 deletions header.go
Expand Up @@ -104,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.putHeader(strContentRange, h.bufKV.value)
}

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

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

// StatusCode returns response status code.
Expand Down Expand Up @@ -168,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.putHeader(strLastModified, h.bufKV.value)
}

// ConnectionClose returns true if 'Connection: close' header is set.
Expand Down Expand Up @@ -596,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.putHeader(strReferer, referer)
}

// Method returns HTTP request method.
Expand Down Expand Up @@ -1256,7 +1256,7 @@ func (h *ResponseHeader) setSpecialHeader(key, value []byte) bool {
h.SetConnectionClose()
} else {
h.ResetConnectionClose()
h.h = setArgBytes(h.h, key, value, argsHasValue)
h.putHeader(key, value)
}
return true
}
Expand Down Expand Up @@ -1289,6 +1289,11 @@ func (h *ResponseHeader) setSpecialHeader(key, value []byte) bool {
return false
}

// putHeader directly put into map i.e. not a basic header
func (h *ResponseHeader) putHeader(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 @@ -1311,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.putHeader(key, value)
}
return true
} else if caseInsensitiveCompare(strCookie, key) {
Expand Down Expand Up @@ -1342,6 +1347,11 @@ func (h *RequestHeader) setSpecialHeader(key, value []byte) bool {
return false
}

// putHeader directly put into map i.e. not a basic header
func (h *RequestHeader) putHeader(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 @@ -1461,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.putHeader(key, value)
}

// SetCookie sets the given response cookie.
Expand Down Expand Up @@ -1673,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.putHeader(key, value)
}

// Peek returns header value for the given key.
Expand Down
4 changes: 2 additions & 2 deletions server.go
Expand Up @@ -1305,7 +1305,7 @@ func (ctx *RequestCtx) RedirectBytes(uri []byte, statusCode int) {
}

func (ctx *RequestCtx) redirect(uri []byte, statusCode int) {
ctx.Response.Header.SetCanonical(strLocation, uri)
ctx.Response.Header.putHeader(strLocation, uri)
statusCode = getRedirectStatusCode(statusCode)
ctx.Response.SetStatusCode(statusCode)
}
Expand Down Expand Up @@ -2378,7 +2378,7 @@ func (s *Server) serveConn(c net.Conn) (err error) {
// Set 'Connection: keep-alive' response header for non-HTTP/1.1 request.
// There is no need in setting this header for http/1.1, since in http/1.1
// connections are keep-alive by default.
ctx.Response.Header.SetCanonical(strConnection, strKeepAlive)
ctx.Response.Header.putHeader(strConnection, strKeepAlive)
}

if serverName != nil && len(ctx.Response.Header.Server()) == 0 {
Expand Down

0 comments on commit 6e82826

Please sign in to comment.