Enable streaming compression for any response that is bigger than the 32 KiB buffer used by SmartOutputStream. This is useful on the info/refs file which can have many branches and tags listed, and is often bigger than 32 KiB, but also compresses by at least 50%. Disable streaming compression on large git-upload-pack responses, as these are usually highly compressed Git pack data. Trying to compress these with gzip will only waste CPU time and additional transfer space with the gzip wrapper. Small git-upload-pack data is usually text based negotiation responses and can be squeezed smaller with a little bit of CPU usage. Change-Id: Ia13e63ed334f594d5e1ab53a97240eb2e8f550e2tags/v2.2.0.201212191850-r
@@ -72,7 +72,8 @@ class InfoRefsServlet extends HttpServlet { | |||
final Repository db = getRepository(req); | |||
final OutputStreamWriter out = new OutputStreamWriter( | |||
new SmartOutputStream(req, rsp), Constants.CHARSET); | |||
new SmartOutputStream(req, rsp, true), | |||
Constants.CHARSET); | |||
final RefAdvertiser adv = new RefAdvertiser() { | |||
@Override | |||
protected void writeOne(final CharSequence line) throws IOException { |
@@ -171,7 +171,7 @@ class ReceivePackServlet extends HttpServlet { | |||
return; | |||
} | |||
SmartOutputStream out = new SmartOutputStream(req, rsp) { | |||
SmartOutputStream out = new SmartOutputStream(req, rsp, true) { | |||
@Override | |||
public void flush() throws IOException { | |||
doFlush(); |
@@ -70,22 +70,29 @@ class SmartOutputStream extends TemporaryBuffer { | |||
private static final int LIMIT = 32 * 1024; | |||
private final HttpServletRequest req; | |||
private final HttpServletResponse rsp; | |||
private boolean compressStream; | |||
private boolean startedOutput; | |||
SmartOutputStream(final HttpServletRequest req, | |||
final HttpServletResponse rsp) { | |||
final HttpServletResponse rsp, | |||
boolean compressStream) { | |||
super(LIMIT); | |||
this.req = req; | |||
this.rsp = rsp; | |||
this.compressStream = compressStream; | |||
} | |||
@Override | |||
protected OutputStream overflow() throws IOException { | |||
startedOutput = true; | |||
return rsp.getOutputStream(); | |||
OutputStream out = rsp.getOutputStream(); | |||
if (compressStream && acceptsGzipEncoding(req)) { | |||
rsp.setHeader(HDR_CONTENT_ENCODING, ENCODING_GZIP); | |||
out = new GZIPOutputStream(out); | |||
} | |||
return out; | |||
} | |||
public void close() throws IOException { |
@@ -122,7 +122,7 @@ abstract class SmartServiceInfoRefs implements Filter { | |||
throws IOException { | |||
final HttpServletRequest req = (HttpServletRequest) request; | |||
final HttpServletResponse res = (HttpServletResponse) response; | |||
final SmartOutputStream buf = new SmartOutputStream(req, res); | |||
final SmartOutputStream buf = new SmartOutputStream(req, res, true); | |||
try { | |||
res.setContentType(infoRefsResultType(svc)); | |||
@@ -172,7 +172,7 @@ class UploadPackServlet extends HttpServlet { | |||
return; | |||
} | |||
SmartOutputStream out = new SmartOutputStream(req, rsp) { | |||
SmartOutputStream out = new SmartOutputStream(req, rsp, false) { | |||
@Override | |||
public void flush() throws IOException { | |||
doFlush(); |