aboutsummaryrefslogtreecommitdiff
path: root/header.go
diff options
context:
space:
mode:
authorGravatar Shivansh Vij <shivanshvij@loopholelabs.io> 2021-11-08 00:44:02 -0800
committerGravatar GitHub <noreply@github.com> 2021-11-08 09:44:02 +0100
commit2ca01c7efb6dcd2d75b36571c857847b9a1b2592 (patch)
tree2c8c8013bb8a59500e1e78b0f75869a48111705d /header.go
parentFix lint (diff)
downloadfasthttp-2ca01c7efb6dcd2d75b36571c857847b9a1b2592.tar.gz
fasthttp-2ca01c7efb6dcd2d75b36571c857847b9a1b2592.tar.bz2
fasthttp-2ca01c7efb6dcd2d75b36571c857847b9a1b2592.zip
fix: Status Line parsing and writing (#1135)
* Adding zero-allocation uint64 to byte slice conversion and fixing the ResponseHeader.SetStatusLine function call signature * Removing unnecessary i2b function * Fixing various bugs * Adding test cases * Commenting AppendStatusLine * Update status.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Update header.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Cleaning up references to strHTTP11, using formatStatusLine for invalidStatusLine, and making `appendStatusLine` an unexported function Issue: https://github.com/valyala/fasthttp/issues/1132 * Fixing merge conflicts Issue: https://github.com/valyala/fasthttp/issues/1132 * Replacing []byte{} with nil in some test cases Issue: https://github.com/valyala/fasthttp/issues/1132 * Cleaning up parsing first line, and improving StatusMessage function Issue: https://github.com/valyala/fasthttp/issues/1132 * Fixing as per PR * Update header.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Update header.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Fixing as per requested changes * Update header_test.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
Diffstat (limited to 'header.go')
-rw-r--r--header.go54
1 files changed, 34 insertions, 20 deletions
diff --git a/header.go b/header.go
index fb6f005..c14532c 100644
--- a/header.go
+++ b/header.go
@@ -33,7 +33,8 @@ type ResponseHeader struct {
noDefaultDate bool
statusCode int
- statusLine []byte
+ statusMessage []byte
+ protocol []byte
contentLength int
contentLengthBytes []byte
secureErrorLogMessage bool
@@ -137,17 +138,27 @@ func (h *ResponseHeader) SetStatusCode(statusCode int) {
h.statusCode = statusCode
}
-// StatusLine returns response status line.
-func (h *ResponseHeader) StatusLine() []byte {
- if len(h.statusLine) > 0 {
- return h.statusLine
+// StatusMessage returns response status message.
+func (h *ResponseHeader) StatusMessage() []byte {
+ return h.statusMessage
+}
+
+// SetStatusMessage sets response status message bytes.
+func (h *ResponseHeader) SetStatusMessage(statusMessage []byte) {
+ h.statusMessage = append(h.statusMessage[:0], statusMessage...)
+}
+
+// Protocol returns response protocol bytes.
+func (h *ResponseHeader) Protocol() []byte {
+ if len(h.protocol) > 0 {
+ return h.protocol
}
- return statusLine(h.StatusCode())
+ return strHTTP11
}
-// SetStatusLine sets response status line bytes.
-func (h *ResponseHeader) SetStatusLine(statusLine []byte) {
- h.statusLine = append(h.statusLine[:0], statusLine...)
+// SetProtocol sets response protocol bytes.
+func (h *ResponseHeader) SetProtocol(protocol []byte) {
+ h.protocol = append(h.protocol[:0], protocol...)
}
// SetLastModified sets 'Last-Modified' header to the given value.
@@ -697,7 +708,8 @@ func (h *ResponseHeader) resetSkipNormalize() {
h.connectionClose = false
h.statusCode = 0
- h.statusLine = h.statusLine[:0]
+ h.statusMessage = h.statusMessage[:0]
+ h.protocol = h.protocol[:0]
h.contentLength = 0
h.contentLengthBytes = h.contentLengthBytes[:0]
@@ -746,7 +758,8 @@ func (h *ResponseHeader) CopyTo(dst *ResponseHeader) {
dst.noDefaultDate = h.noDefaultDate
dst.statusCode = h.statusCode
- dst.statusLine = append(dst.statusLine, h.statusLine...)
+ dst.statusMessage = append(dst.statusMessage, h.statusMessage...)
+ dst.protocol = append(dst.protocol, h.protocol...)
dst.contentLength = h.contentLength
dst.contentLengthBytes = append(dst.contentLengthBytes, h.contentLengthBytes...)
dst.contentType = append(dst.contentType, h.contentType...)
@@ -1648,19 +1661,20 @@ func (h *ResponseHeader) String() string {
return string(h.Header())
}
-// AppendBytes appends response header representation to dst and returns
+// appendStatusLine appends the response status line to dst and returns
// the extended dst.
-func (h *ResponseHeader) AppendBytes(dst []byte) []byte {
+func (h *ResponseHeader) appendStatusLine(dst []byte) []byte {
statusCode := h.StatusCode()
if statusCode < 0 {
statusCode = StatusOK
}
+ return formatStatusLine(dst, h.Protocol(), statusCode, h.StatusMessage())
+}
- if len(h.statusLine) > 0 {
- dst = append(dst, h.statusLine...)
- } else {
- dst = append(dst, statusLine(statusCode)...)
- }
+// AppendBytes appends response header representation to dst and returns
+// the extended dst.
+func (h *ResponseHeader) AppendBytes(dst []byte) []byte {
+ dst = h.appendStatusLine(dst[:0])
server := h.Server()
if len(server) != 0 {
@@ -1880,8 +1894,8 @@ func (h *ResponseHeader) parseFirstLine(buf []byte) (int, error) {
}
return 0, fmt.Errorf("unexpected char at the end of status code. Response %q", buf)
}
- if len(b) > n+1 && !bytes.Equal(b[n+1:], statusLine(h.statusCode)) {
- h.SetStatusLine(b[n+1:])
+ if len(b) > n+1 {
+ h.SetStatusMessage(b[n+1:])
}
return len(buf) - len(bNext), nil