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

feat: ability to edit status messages #1126

Merged
merged 5 commits into from Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 12 additions & 1 deletion header.go
Expand Up @@ -33,6 +33,7 @@ type ResponseHeader struct {
noDefaultDate bool

statusCode int
statusLine []byte
contentLength int
contentLengthBytes []byte
secureErrorLogMessage bool
Expand Down Expand Up @@ -136,6 +137,11 @@ func (h *ResponseHeader) SetStatusCode(statusCode int) {
h.statusCode = statusCode
}

// SetStatusLine sets response status line bytes.
func (h *ResponseHeader) SetStatusLine(statusLine []byte) {
h.statusLine = statusLine
erikdubbelboer marked this conversation as resolved.
Show resolved Hide resolved
}

// 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)
Expand Down Expand Up @@ -1639,7 +1645,12 @@ func (h *ResponseHeader) AppendBytes(dst []byte) []byte {
if statusCode < 0 {
statusCode = StatusOK
}
dst = append(dst, statusLine(statusCode)...)

if h.statusLine != nil {
dst = append(dst, h.statusLine...)
erikdubbelboer marked this conversation as resolved.
Show resolved Hide resolved
} else {
dst = append(dst, statusLine(statusCode)...)
}

server := h.Server()
if len(server) != 0 {
Expand Down
18 changes: 18 additions & 0 deletions http_test.go
Expand Up @@ -837,6 +837,24 @@ func TestResponseSkipBody(t *testing.T) {
t.Fatalf("unexpected content-type in response %q", s)
}

// set StatusNoContent with statusLine
r.Header.SetStatusCode(StatusNoContent)
r.Header.SetStatusLine([]byte("HTTP/1.1 204 NC\r\n"))
r.SetBodyString("foobar")
s = r.String()
if strings.Contains(s, "\r\n\r\nfoobar") {
t.Fatalf("unexpected non-zero body in response %q", s)
}
if strings.Contains(s, "Content-Length: ") {
t.Fatalf("unexpected content-length in response %q", s)
}
if strings.Contains(s, "Content-Type: ") {
t.Fatalf("unexpected content-type in response %q", s)
}
if !strings.HasPrefix(s, "HTTP/1.1 204 NC\r\n") {
t.Fatalf("expecting non-default status line in response %q", s)
}

// explicitly skip body
r.Header.SetStatusCode(StatusOK)
r.SkipBody = true
Expand Down