Skip to content

Commit

Permalink
fix: "identity" has been deprecated #1909 (#1919)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksw2000 authored Dec 15, 2024
1 parent db50dee commit bdcbf61
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 10 additions & 8 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,13 @@ func (h *ResponseHeader) SetContentLength(contentLength int) {
if contentLength >= 0 {
h.contentLengthBytes = AppendUint(h.contentLengthBytes[:0], contentLength)
h.h = delAllArgsBytes(h.h, strTransferEncoding)
} else {
return
} else if contentLength == -1 {
h.contentLengthBytes = h.contentLengthBytes[:0]
value := strChunked
if contentLength == -2 {
h.SetConnectionClose()
value = strIdentity
}
h.h = setArgBytes(h.h, strTransferEncoding, value, argsHasValue)
h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
return
}
h.SetConnectionClose()
}

func (h *ResponseHeader) mustSkipContentLength() bool {
Expand Down Expand Up @@ -3096,7 +3094,11 @@ func (h *ResponseHeader) parseHeaders(buf []byte) (int, error) {
h.contentLengthBytes = h.contentLengthBytes[:0]
}
if h.contentLength == -2 && !h.ConnectionUpgrade() && !h.mustSkipContentLength() {
h.h = setArgBytes(h.h, strTransferEncoding, strIdentity, argsHasValue)
// According to modern HTTP/1.1 specifications (RFC 7230):
// `identity` as a value for `Transfer-Encoding` was removed
// in the errata to RFC 2616.
// Therefore, we do not include `Transfer-Encoding: identity` in the header.
// See: https://github.com/valyala/fasthttp/issues/1909
h.connectionClose = true
}
if h.noHTTP11 && !h.connectionClose {
Expand Down
4 changes: 4 additions & 0 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,10 @@ func TestResponseHeaderReadSuccess(t *testing.T) {
if !h.ConnectionClose() {
t.Fatalf("expecting connection: close for identity response")
}
// See https://github.com/valyala/fasthttp/issues/1909
if hasArg(h.h, HeaderTransferEncoding) {
t.Fatalf("unexpected header: 'Transfer-Encoding' should not be present in parsed headers")
}

// no content-type
testResponseHeaderReadSuccess(t, h, "HTTP/1.1 400 OK\r\nContent-Length: 123\r\n\r\nfoiaaa",
Expand Down

0 comments on commit bdcbf61

Please sign in to comment.