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

GZIP compression support #177

Closed
ritchieGitHub opened this issue May 14, 2015 · 10 comments
Closed

GZIP compression support #177

ritchieGitHub opened this issue May 14, 2015 · 10 comments

Comments

@ritchieGitHub
Copy link
Member

add GZIP compression support to the server. (from #63)

@AlbinTheander
Copy link
Contributor

I wouldn't mind taking a peek at this.

I haven't been following the project for a while. How concerned are we with backwards compatibility? I would like to add this inside of the Response class, but if I add a parameter to the Response constructor (to specify the content-encoding), it would probably break a lot of usages.

@elonen
Copy link
Member

elonen commented May 18, 2015

Great to hear, and much appreciated! We just recently committed a major change to Response, so now would be a perfect time to introduce this. Response is now created with a factory function instead of directly with a constructor.

@ritchieGitHub
Copy link
Member Author

Yes, great just take case to keep the factory methods "clean".

@elonen
Copy link
Member

elonen commented May 18, 2015

Maybe a separate toggle function, like public Deflater setCompression(bool use_gzip) instead of a boolean in the constructors? (The idea behind returning Deflater was that you can set compression level and some other parameters through it.)

@AlbinTheander
Copy link
Contributor

I guess the factory methods in question are newFixedLengthResponse and newChunkedResponse.

Fixed length responses may be a problem, since you would need to zip the entire InputStream before knowing the final size.

I'll see what I can do. Let me know if you have any thoughts about this.

@AlbinTheander
Copy link
Contributor

I submitted a pull request now. I don't expose the Deflater for the simple reason that it was easier to just use the GzipOutputStream directly instead of keeping track of headers and trailers manually.

@ritchieGitHub
Copy link
Member Author

Just checked the changes, they sound good thanks. @elonen do you concur that we can merge this?

@elonen
Copy link
Member

elonen commented May 23, 2015

Yes.

@ritchieGitHub
Copy link
Member Author

Merged with thanks!

@eduard93
Copy link

eduard93 commented Aug 28, 2018

How can user enable GZIP compression?
My code:

   public Response serve(IHTTPSession session) {
        ByteArrayInputStream resBody = new ByteArrayInputStream(new byte[0]);
        try {
            resBody = new ByteArrayInputStream("{\"response\":1}".getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
        }
        Response res = newChunkedResponse(OK, "application/json", resBody);

        res.setGzipEncoding(true);
        return res;
    }

And this request:

GET / HTTP/1.1
Host: localhost:8080
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,ru;q=0.8

Produces this response:

HTTP/1.1 200 OK 
Content-Type: application/json
Date: Tue, 28 Aug 2018 11:39:12 GMT
Connection: keep-alive
Transfer-Encoding: chunked

{"response":1}

UPD:

Add something like this to your server:

protected boolean useGzipWhenAccepted(Response r) {
    return true;
}

Also there's no need to use res.setGzipEncoding(true); as it's called automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants