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

Start plain response as soon as certain #74

Merged
merged 2 commits into from
Aug 20, 2018
Merged

Start plain response as soon as certain #74

merged 2 commits into from
Aug 20, 2018

Conversation

victorges
Copy link
Contributor

@victorges victorges commented Aug 20, 2018

Using this library in one of my projects, I came to realise our API for Server-Sent Events stopped working when the request included an Accept-Encoding: gzip header.

Investigating on the gziphandler code I found out that, even though I added only application/json as the list of supported content types to be GZIPped, 2 little issues led to SSE stop working:

  • The handleContentType function wasn't being checked until the response actually reached the specified minSize (which I left to the default). The Content-Type was already being set to an unsupported content type (text/event-stream) so we didn't need to wait all the first 1400 bytes before starting the plain response. Fixed this on the first commit.
  • The Flush func was not flushing the underlying ResponseWriter when we were not gzipping the response. This is needed as we make sure Flush is called after every event we write to the response. Fixed this on the second commit.

One thing that is still slightly bothering me is that we are parsing the response headers every time the Write function is called. I'm thinking of adding a little "state-enum" in the gzip.Writer struct, indicating whether:

  • We haven't even processed the original response headers yet
  • We are accumulating data in the buffer to determine whether a plain or gzipped response will be done
  • We are serving a plain or GZIPped response

I think this will make the logic much clearer as well. Please also let me know what you think of that idea and I could try to send it as a separate pull-request!

When we get the response headers it might be pretty
much already known whether we will have to serve plain
content instead of gzipping. That is either when
the content-length header is set with a small enough size,
there is already an encoding in the response or the
content-type is already set to a content we don't handle.
Copy link
Contributor

@fsouza fsouza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@victorges thanks for fixing that.

re: introducing a state to the writer, I agree that it would make the logic easier to follow. Can you send the PR? :D

@victorges
Copy link
Contributor Author

Great! I think I don't have access to merging this though, could you do it for me?

I'll try to do the state thing some time in the next few days :)

@jprobinson jprobinson merged commit 253f1ac into nytimes:master Aug 20, 2018
@jprobinson
Copy link
Contributor

Thanks, @victorges & @fsouza!

@victorges victorges deleted the fix/response-header-processing branch May 15, 2019 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants