Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
rymsha committed Feb 1, 2024
1 parent da24ff1 commit d5d5d26
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/com/enonic/app/booster/BoosterApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ protected void doHandle( final HttpServletRequest req, final HttpServletResponse
cached = null;
}

if ( ("\""+ sha256 + "-gzip" + "\"").equals( req.getHeader( "If-None-Match" ) )) {
LOG.debug( "Returning 304 Not Modified" );
res.setStatus( 304 );
return;
}
// Report cache HIT or MISS. Header is not present if cache is not used
res.setHeader( "X-Booster-Cache", cached != null ? "HIT" : "MISS" );

Expand All @@ -231,12 +236,14 @@ protected void doHandle( final HttpServletRequest req, final HttpServletResponse

copyHeaders( res, cached );

res.addHeader( "Vary", "Accept-Encoding" );

if ( supportsGzip( req ) )
{
LOG.debug( "Request accepts gzip. Writing gzipped response body from cache" );
// Headers will tell Jetty to not apply compression, as it is done already
res.setHeader( "Content-Encoding", "gzip" );
res.addHeader( "Vary", "Accept-Encoding" );
res.setHeader( "etag", "\"" + sha256 + "-gzip" + "\"" );
res.setContentLength( cached.gzipData.size() );
cached.gzipData.writeTo( res.getOutputStream() );
}
Expand All @@ -245,6 +252,7 @@ protected void doHandle( final HttpServletRequest req, final HttpServletResponse
// we don't store decompressed data in cache as it is mostly waste of space
// we can recreate uncompressed response from compressed data
LOG.debug( "Request does not accept gzip. Writing plain response body from cache" );
res.setHeader( "etag", "\"" + sha256 + "\"" );
res.setContentLength( cached.contentLength );
new GZIPInputStream( new ByteArrayInputStream( cached.gzipData.toByteArray() ) ).transferTo( res.getOutputStream() );
}
Expand Down

0 comments on commit d5d5d26

Please sign in to comment.