Benjamin
Benjamin

Reputation: 234

Tomcat server is always compressing as gzip (even when Accept-Encoding field is absent)

I recently started working with Apache Tomcat, and one of my assignments was to enable gzip compression on our dynamic web content. While I found a number of implementations for this feature online, in the end I chose to follow the instructions here (http://viralpatel.net/blogs/enable-gzip-compression-in-tomcat/) and add the following lines to the connector section of my server.xml file.

compression="on" 
noCompressionUserAgents="gozilla, traviata" 
compressionMinSize="2048" 
compressableMimeType="text/html,text/xml,application/json"

Once I edited the file and restarted my server I started testing one of my servlets using Postman. I found that everything of the correct MIME types, larger than 2048, was getting compressed – even when I didn’t send the Accept-Encoding:gzip header. I tested this several ways, including toggling compression a few times, adjusting the compressableMimeType value, and fiddling with the compressionMinSize, but as near as I can tell this implementation is not checking to see if the Accept-Encoding header has been included.

While I can believe this may be an unnecessary feature since modern browsers started supporting compression back in 2000, I wanted to ensure I was covering all of my bases. I am assuming I am missing something trivial here because I haven’t been able to find the answer to this question online. I have included a copy of my server.xml, I am running Apache Tomcat 7.0.65, and I would appreciate any and all help as far as determining why I can’t seem to opt out of receiving compressed responses from my newly configured server.

<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
          type="org.apache.catalina.UserDatabase"
          description="User database that can be updated and saved"
          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

<Service name="Catalina">

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"
    compression="on" 
    noCompressionUserAgents="gozilla, traviata" 
    compressionMinSize="2048" 
    compressableMimeType="text/html,text/xml" />

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<Engine name="Catalina" defaultHost="localhost">

  <Realm className="org.apache.catalina.realm.LockOutRealm">
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
           resourceName="UserDatabase"/>
  </Realm>

  <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

Upvotes: 0

Views: 2916

Answers (1)

Benjamin
Benjamin

Reputation: 234

So I finally figured out what was going on with the gzip compression. As Postman is built on top of Chrome (being a Chrome app) all of the Postman communications are being sent through Chrome. This is important because Chrome has a number of Header values that it adds by default. In fact, these “Restricted Http Headers” are overwritten by Chrome. Accept-Encoding is one such header, so it didn’t matter what I was setting it to – It was getting outright ignored.

Luckily Chrome also released a plugin for Postman to handle Restricted Http Headers. Once I downloaded the Interceptor plugin and turned it on I was able to see that my Tomcat server was actually working properly all along. Lesson learned: it doesn’t matter if your answer is correct if you are measuring the results with an improperly calibrated tool.

Upvotes: 4

Related Questions