aboutsummaryrefslogtreecommitdiff
path: root/header.go
diff options
context:
space:
mode:
authorGravatar Valentin Paz Marcolla <valchulen.pm@gmail.com> 2021-10-22 12:53:35 -0300
committerGravatar GitHub <noreply@github.com> 2021-10-22 17:53:35 +0200
commit556aa814e4d862f33c875cf9f0ebdf2867005092 (patch)
treed64d156edb7c11619e7846526134fb5250f74117 /header.go
parentfeat: make public Server.TLSConfig (#1128) (diff)
downloadfasthttp-556aa814e4d862f33c875cf9f0ebdf2867005092.tar.gz
fasthttp-556aa814e4d862f33c875cf9f0ebdf2867005092.tar.bz2
fasthttp-556aa814e4d862f33c875cf9f0ebdf2867005092.zip
feat: ability to edit status messages (#1126)
* SetStatusMessage * Docstring * statusLine in header * Use statusLine as []byte + ResponseHeader parsing * status line getter
Diffstat (limited to 'header.go')
-rw-r--r--header.go26
1 files changed, 25 insertions, 1 deletions
diff --git a/header.go b/header.go
index f03e648..4cbe9f3 100644
--- a/header.go
+++ b/header.go
@@ -33,6 +33,7 @@ type ResponseHeader struct {
noDefaultDate bool
statusCode int
+ statusLine []byte
contentLength int
contentLengthBytes []byte
secureErrorLogMessage bool
@@ -136,6 +137,19 @@ 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
+ }
+ return statusLine(h.StatusCode())
+}
+
+// SetStatusLine sets response status line bytes.
+func (h *ResponseHeader) SetStatusLine(statusLine []byte) {
+ h.statusLine = append(h.statusLine[:0], statusLine...)
+}
+
// 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)
@@ -683,6 +697,7 @@ func (h *ResponseHeader) resetSkipNormalize() {
h.connectionClose = false
h.statusCode = 0
+ h.statusLine = h.statusLine[:0]
h.contentLength = 0
h.contentLengthBytes = h.contentLengthBytes[:0]
@@ -731,6 +746,7 @@ func (h *ResponseHeader) CopyTo(dst *ResponseHeader) {
dst.noDefaultDate = h.noDefaultDate
dst.statusCode = h.statusCode
+ dst.statusLine = append(dst.statusLine[:0], h.statusLine...)
dst.contentLength = h.contentLength
dst.contentLengthBytes = append(dst.contentLengthBytes, h.contentLengthBytes...)
dst.contentType = append(dst.contentType, h.contentType...)
@@ -1639,7 +1655,12 @@ func (h *ResponseHeader) AppendBytes(dst []byte) []byte {
if statusCode < 0 {
statusCode = StatusOK
}
- dst = append(dst, statusLine(statusCode)...)
+
+ if len(h.statusLine) > 0 {
+ dst = append(dst, h.statusLine...)
+ } else {
+ dst = append(dst, statusLine(statusCode)...)
+ }
server := h.Server()
if len(server) != 0 {
@@ -1859,6 +1880,9 @@ 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:])
+ }
return len(buf) - len(bNext), nil
}