View | Details | Raw Unified | Return to bug 54095
Collapse All | Expand All

(-)conf/web.xml (+4 lines)
Lines 104-109 Link Here
104
            <param-name>listings</param-name>
104
            <param-name>listings</param-name>
105
            <param-value>false</param-value>
105
            <param-value>false</param-value>
106
        </init-param>
106
        </init-param>
107
        <init-param>
108
            <param-name>gzip</param-name>
109
            <param-value>true</param-value>
110
        </init-param>
107
        <load-on-startup>1</load-on-startup>
111
        <load-on-startup>1</load-on-startup>
108
    </servlet>
112
    </servlet>
109
113
(-)java/org/apache/catalina/servlets/DefaultServlet.java (+41 lines)
Lines 32-37 Link Here
32
import java.io.StringReader;
32
import java.io.StringReader;
33
import java.io.StringWriter;
33
import java.io.StringWriter;
34
import java.util.ArrayList;
34
import java.util.ArrayList;
35
import java.util.Enumeration;
35
import java.util.Iterator;
36
import java.util.Iterator;
36
import java.util.StringTokenizer;
37
import java.util.StringTokenizer;
37
38
Lines 137-142 Link Here
137
     * Read only flag. By default, it's set to true.
138
     * Read only flag. By default, it's set to true.
138
     */
139
     */
139
    protected boolean readOnly = true;
140
    protected boolean readOnly = true;
141
    
142
    
143
    /**
144
     * Should be serve gzip versions of files. By default, it's set to true.
145
     */
146
    protected boolean gzip = true;
140
147
141
148
142
    /**
149
    /**
Lines 276-281 Link Here
276
283
277
        if (getServletConfig().getInitParameter("readonly") != null)
284
        if (getServletConfig().getInitParameter("readonly") != null)
278
            readOnly = Boolean.parseBoolean(getServletConfig().getInitParameter("readonly"));
285
            readOnly = Boolean.parseBoolean(getServletConfig().getInitParameter("readonly"));
286
        
287
        if (getServletConfig().getInitParameter("gzip") != null)
288
            gzip = Boolean.parseBoolean(getServletConfig().getInitParameter("gzip"));
279
289
280
        if (getServletConfig().getInitParameter("sendfileSize") != null)
290
        if (getServletConfig().getInitParameter("sendfileSize") != null)
281
            sendfileSize =
291
            sendfileSize =
Lines 750-755 Link Here
750
            contentType = getServletContext().getMimeType(resource.getName());
760
            contentType = getServletContext().getMimeType(resource.getName());
751
            resource.setMimeType(contentType);
761
            resource.setMimeType(contentType);
752
        }
762
        }
763
        
764
        // Serve a gzipped version of the file if present 
765
        if (gzip
766
                && checkIfGzip(request)
767
                && resource.isFile()
768
                && !path.endsWith(".gz")) {
769
            WebResource gzipResource = resources.getResource(path + ".gz");
770
            if (gzipResource.exists() && gzipResource.isFile()) {
771
                gzipResource.setMimeType(contentType);
772
                response.addHeader("Content-Encoding", "gzip");
773
                resource = gzipResource;
774
            }
775
        }
753
776
754
        ArrayList<Range> ranges = null;
777
        ArrayList<Range> ranges = null;
755
        long contentLength = -1L;
778
        long contentLength = -1L;
Lines 1708-1713 Link Here
1708
        }
1731
        }
1709
        return true;
1732
        return true;
1710
    }
1733
    }
1734
    
1735
    /**
1736
     * Check if the user agent supports gzip encoding.
1737
     *
1738
     * @param request   The servlet request we are processing
1739
     * @return boolean true if the user agent supports gzip encoding,
1740
     * and false if the user agent does not support gzip encoding
1741
     */
1742
    protected boolean checkIfGzip(HttpServletRequest request) {
1743
        Enumeration<String> headers = request.getHeaders("Accept-Encoding");
1744
        while (headers.hasMoreElements()) {
1745
            String header = headers.nextElement();
1746
            if (header.indexOf("gzip") != -1) {
1747
                return true;
1748
            }
1749
        }
1750
        return false;
1751
    }
1711
1752
1712
1753
1713
    /**
1754
    /**
(-)test/org/apache/catalina/servlets/TestDefaultServlet.java (-9 / +97 lines)
Lines 16-24 Link Here
16
 */
16
 */
17
package org.apache.catalina.servlets;
17
package org.apache.catalina.servlets;
18
18
19
import static org.apache.catalina.startup.SimpleHttpClient.CRLF;
20
import static org.junit.Assert.assertEquals;
21
import static org.junit.Assert.assertFalse;
22
import static org.junit.Assert.assertTrue;
23
import static org.junit.Assert.fail;
24
25
import java.io.ByteArrayOutputStream;
19
import java.io.File;
26
import java.io.File;
20
import java.io.FileOutputStream;
27
import java.io.FileOutputStream;
21
import java.io.IOException;
28
import java.io.IOException;
29
import java.io.OutputStream;
22
import java.io.OutputStreamWriter;
30
import java.io.OutputStreamWriter;
23
import java.io.Writer;
31
import java.io.Writer;
24
import java.text.SimpleDateFormat;
32
import java.text.SimpleDateFormat;
Lines 27-48 Link Here
27
import java.util.Locale;
35
import java.util.Locale;
28
import java.util.Map;
36
import java.util.Map;
29
import java.util.TimeZone;
37
import java.util.TimeZone;
38
import java.util.zip.GZIPOutputStream;
30
39
31
import javax.servlet.http.HttpServletResponse;
40
import javax.servlet.http.HttpServletResponse;
32
41
33
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertFalse;
35
import static org.junit.Assert.assertTrue;
36
import static org.junit.Assert.fail;
37
38
import org.junit.Test;
39
40
import static org.apache.catalina.startup.SimpleHttpClient.CRLF;
41
42
import org.apache.catalina.startup.SimpleHttpClient;
42
import org.apache.catalina.startup.SimpleHttpClient;
43
import org.apache.catalina.startup.Tomcat;
43
import org.apache.catalina.startup.Tomcat;
44
import org.apache.catalina.startup.TomcatBaseTest;
44
import org.apache.catalina.startup.TomcatBaseTest;
45
import org.apache.tomcat.util.buf.ByteChunk;
45
import org.apache.tomcat.util.buf.ByteChunk;
46
import org.junit.Test;
46
47
47
public class TestDefaultServlet extends TomcatBaseTest {
48
public class TestDefaultServlet extends TomcatBaseTest {
48
49
Lines 85-91 Link Here
85
        assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
86
        assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
86
87
87
    }
88
    }
89
    
90
    /**
91
     * Verify serving of gzipped resources from context root.
92
     */
93
    @Test
94
    public void testGzippedFile() throws Exception {
95
    	File appDir = new File(getTemporaryDirectory(), "MyApp");
96
        File webInf = new File(appDir, "WEB-INF");
97
        addDeleteOnTearDown(appDir);
98
        if (!webInf.mkdirs() && !webInf.isDirectory()) {
99
            fail("Unable to create directory [" + webInf + "]");
100
        }
101
        
102
        Writer w = new OutputStreamWriter(new FileOutputStream(new File(appDir,
103
                "index.html")), "UTF-8");
104
        try {
105
            w.write("<h1>Hello</h1>");
106
            w.flush();
107
        } finally {
108
            w.close();
109
        }
110
        OutputStream o = new GZIPOutputStream(new FileOutputStream(new File(
111
        		appDir, "index.html.gz")));
112
        try {
113
            o.write("<h1>Hello</h1>".getBytes("UTF-8"));
114
            o.flush();
115
        } finally {
116
            o.close();
117
        }
118
        
119
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
120
        GZIPOutputStream gzipstream = new GZIPOutputStream(bos);
121
        gzipstream.write("<h1>Hello</h1>".getBytes("UTF-8"));
122
        gzipstream.flush();
123
        gzipstream.close();
124
        String gzipped = new String(bos.toByteArray(), "ISO-8859-1"); // encoding used by SimpleHttpClient
125
        
126
        Tomcat tomcat = getTomcatInstance();
127
        String contextPath = "/MyApp";
128
        tomcat.addWebapp(null, contextPath, appDir.getAbsolutePath());
129
        tomcat.start();
130
        
131
        TestGzipClient gzipClient =
132
                new TestGzipClient(tomcat.getConnector().getLocalPort());
88
133
134
        gzipClient.reset();
135
        gzipClient.setRequest(new String[] {
136
                "GET /MyApp/index.html HTTP/1.1" + CRLF +
137
                "Host: localhost" + CRLF +
138
                "Connection: Close" + CRLF +
139
                "Accept-Encoding: gzip" + CRLF + CRLF });
140
        gzipClient.connect();
141
        gzipClient.processRequest();
142
        assertTrue(gzipClient.isResponse200());
143
        assertEquals(gzipped, gzipClient.getResponseBody());
144
        List<String> responseHeaders = gzipClient.getResponseHeaders();
145
        assertTrue(responseHeaders.contains("Content-Type: text/html"));
146
        assertTrue(responseHeaders.contains("Content-Encoding: gzip"));
147
        assertTrue(responseHeaders.contains("Content-Length: " + gzipped.length()));
148
        
149
        gzipClient.reset();
150
        gzipClient.setRequest(new String[] {
151
                "GET /MyApp/index.html HTTP/1.1" + CRLF +
152
                "Host: localhost" + CRLF +
153
                "Connection: Close" + CRLF+ CRLF });
154
        gzipClient.connect();
155
        gzipClient.processRequest();
156
        assertTrue(gzipClient.isResponse200());
157
        assertEquals("<h1>Hello</h1>", gzipClient.getResponseBody());
158
        responseHeaders = gzipClient.getResponseHeaders();
159
        assertTrue(responseHeaders.contains("Content-Type: text/html"));
160
        assertFalse(responseHeaders.contains("Content-Encoding: gzip"));
161
        assertTrue(responseHeaders.contains("Content-Length: "
162
        		+ "<h1>Hello</h1>".length()));
163
    }
164
89
    /**
165
    /**
90
     * Test https://issues.apache.org/bugzilla/show_bug.cgi?id=50026
166
     * Test https://issues.apache.org/bugzilla/show_bug.cgi?id=50026
91
     * Verify serving of resources from context root with subpath mapping.
167
     * Verify serving of resources from context root with subpath mapping.
Lines 304-307 Link Here
304
            return true;
380
            return true;
305
        }
381
        }
306
    }
382
    }
383
    
384
    private static class TestGzipClient extends SimpleHttpClient {
385
    	
386
    	public TestGzipClient(int port) {
387
    		setPort(port);
388
    	}
389
    	
390
    	@Override
391
    	public boolean isResponseBodyOK() {
392
    		return true;
393
    	}
394
    }
307
}
395
}
(-)webapps/docs/default-servlet.xml (+8 lines)
Lines 99-104 Link Here
99
    </td>
99
    </td>
100
  </tr>
100
  </tr>
101
  <tr>
101
  <tr>
102
    <th valign='top'>gzip</th>
103
    <td valign='top'>
104
        If you have a gzipped version of a file ending with <code>.gz</code>
105
        Tomcat will serve the gzipped one if the user agent supports gzip
106
        and this option is enabled. [true]
107
    </td>
108
  </tr>
109
  <tr>
102
    <th valign='top'>readmeFile</th>
110
    <th valign='top'>readmeFile</th>
103
    <td valign='top'>
111
    <td valign='top'>
104
        If a directory listing is presented, a readme file may also
112
        If a directory listing is presented, a readme file may also

Return to bug 54095