]> source.dussan.org Git - jgit.git/commitdiff
Compress large /info/refs responses on HTTP 46/7846/1
authorShawn O. Pearce <spearce@spearce.org>
Thu, 20 Sep 2012 00:33:07 +0000 (17:33 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Thu, 20 Sep 2012 00:33:07 +0000 (17:33 -0700)
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: Ia13e63ed334f594d5e1ab53a97240eb2e8f550e2

org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/InfoRefsServlet.java
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/ReceivePackServlet.java
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartOutputStream.java
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/SmartServiceInfoRefs.java
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/UploadPackServlet.java

index ffaa13153c9bbdefb317171d279e5f603d235fea..52f928548b3b06d06289741df9cbd54569c46ee8 100644 (file)
@@ -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 {
index 10cadd7bb05b7d950b1c029fc61cff88cbad3ecd..3d6c35b0b6f81438508264fa0bb13b676a4aadd6 100644 (file)
@@ -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();
index c39b78900debb00af0adbdb4d4eda049100130b0..145c63bcafcbebf6c93200ad29a8b6a923674bb7 100644 (file)
@@ -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 {
index 907b328db94decdb0e60b5d8a61768ff5925d163..48107537793f926a92ccae64347953041b8a9e62 100644 (file)
@@ -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));
 
index 046db45766108b99de80ed92c2b9d4c7403aa7c3..c5272b55eb007f83fe40dc4db5acbde503c1f77f 100644 (file)
@@ -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();