Ouroborus
Ouroborus

Reputation: 16875

Detect compression in GAE?

Normally one would detect if compression was requested by checking the Accept-Encoding request header. Google App Engine strips this header. Is there any other way to detect this kind of thing? I would like to know if GAE will compress particular responses before they're sent.

Upvotes: 1

Views: 285

Answers (2)

Ouroborus
Ouroborus

Reputation: 16875

My current solution is to use another server to detect compression.

I've broken my pages into an initialization stub and the content. The stub includes a bit that causes the browser to make a request to the third-party server solely for compression detection. If that passes, it then loads and injects the content.

This is not optimal, but it does work and has cut traffic from irresponsible bots by quite a bit without having significant impact on users.

Edit: It occurs to me that it may be possible to infer this from the client side. Using AJAX and checking the Content-Encoding response header for gzip would mean that GAE thinks the client wanted compression which implies that the client asked for compression.

// Certain files aren't compressed by GAE even if requested so 'canary' 
// needs to be of a type and size to trigger compression
$.get('canary').done(function(data, textStatus, jqXHR){
  // request succeeded
  var contentEncoding = jqXHR.getResponseHeader('Content-Encoding');
  if(contentEncoding != null) {
    if(!['gzip','deflate','sdch','br'].every(function(v,i,a){
      return contentEncoding.indexOf(v) < 0;
    })) {
      // accepted compression found, initiate remainder of page load
      return;
    }
  }
  // no acceptable compression, kill page
}).fail(function( jqXHR, textStatus, errorThrown ) {
  // request failed, kill page
});

Upvotes: 0

Dan Cornilescu
Dan Cornilescu

Reputation: 39824

GAE does this automatically, you don't need to worry about it in your app:

Google App Engine does its best to serve gzipped content to browsers that support it. Taking advantage of this scheme is automatic and requires no modifications to applications.

We use a combination of request headers (Accept-Encoding, User-Agent) and response headers (Content-Type) to determine whether or not the end-user can take advantage of gzipped content. This approach avoids some well-known bugs with gzipped content in popular browsers. To force gzipped content to be served, clients may supply 'gzip' as the value of both the Accept-Encoding and User-Agent request headers. Content will never be gzipped if no Accept-Encoding header is present.

And headers can be checked on the actual msgs to/from a GAE app (in production only, the dev server doesn't perform the compression). I see (in firefox):

On the request:

Accept-Encoding: "gzip, deflate"

On the response:

Content-Enconding: "gzip"

Upvotes: 2

Related Questions