You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
my fiber handler, say GET /foo, uses c.Context().SetBodyStreamWriter(func(w *bufio.Writer)) to stream the response body to the client in chunks.
Response Headers With slogfiber
HTTP/1.1 200 OK
Date: Thu, 02 Jan 2025 01:11:41 GMT
Content-Type: text/event-stream // set by me
Content-Length: 25044 // crux of the issue
X-Request-Id: e8f629e6-d90b-498c-8d0b-f2aa148532ab // from the `WithRequestID: true`
Cache-Control: no-cache // set by me
Transfer-Encoding header missing despite being set with c.Set("Transfer-Encoding", "chunked")
Response Headers Without slogfiber
HTTP/1.1 200 OK
Date: Wed, 01 Jan 2025 01:12:31 GMT
Content-Type: text/event-stream // set by me
Cache-Control: no-cache // set by me
Transfer-Encoding: chunked
Explanation of the Issue
to stream to the client in chunks using theTransfer-Encoding: chunked header, there is one key note:
SetBodyStreamWriter sets the contentLength to the bodySize of -1, however, slogfiber's call to c.Response().Body() repopulates contentLength, thus the fasthttp.ResponseHeader.SetContentLength func deletes the Transfer-Encoding header
Solution
i can open a pr moving the filter check to the top of the returned handler (middleware), before the potentially problematic calls
The text was updated successfully, but these errors were encountered:
I don't remember why the filter is as far in the code. It must be escaped ASAP in the middleware. i'm going to fix that by myself. (see v1.17.0)
However, I wonder if this middleware should wait for the body to be written before triggering a log record. Either by blocking the middleware chain, or by building the record in a goroutine.
I don't really like the second option, since it might cost a goroutine for each http request. WDYT?
I suppose we can overwrite the body.Close() method and add our own logic.
The Issue
the following config leads to some odd interactions with my streaming fiber handler.
config:
my fiber handler, say
GET /foo
, usesc.Context().SetBodyStreamWriter(func(w *bufio.Writer))
to stream the response body to the client in chunks.Response Headers With slogfiber
Transfer-Encoding
header missing despite being set withc.Set("Transfer-Encoding", "chunked")
Response Headers Without slogfiber
Explanation of the Issue
to stream to the client in chunks using the
Transfer-Encoding: chunked
header, there is one key note:per the mdn web docs,
because slogfiber includes response information of the form
this call
responseAttributes = append(responseAttributes, slog.Int("length", len(c.Response().Body())))
causes the
Content-Length: 123
header to be populated, messing up theTransfer-Encoding: chunked
header and thus breaking my streaming implementation.Attempted Workarounds
i tried to add a filter to my config for the impacted streaming route
however, since the filter check occurs after the problem call (
responseAttributes = append(responseAttributes, slog.Int("length", len(c.Response().Body())))
), theContent-Length: 123
header is still populated.SetBodyStreamWriter
sets the contentLength to the bodySize of -1, however, slogfiber's call toc.Response().Body()
repopulates contentLength, thus thefasthttp.ResponseHeader.SetContentLength
func deletes theTransfer-Encoding
headerSolution
i can open a pr moving the filter check to the top of the returned handler (middleware), before the potentially problematic calls
The text was updated successfully, but these errors were encountered: