JDK-7048252 : HttpUrlConnection cache reports connection encoding as "gzip" but returns unzipped byte stream
  • Type: Bug
  • Component: deploy
  • Sub-Component: plugin
  • Affected Version: 6u24
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2011-05-25
  • Updated: 2011-05-26
  • Resolved: 2011-05-26
Related Reports
Duplicate :  
Description
I have an applet that uses java.net.URL and java.net.URLConnection to make outgoing HTTP connections. The applet wants the contents to be gzipped whenever possible and therefore applies the following to each connection:

   connection.setRequestProperty("Accept-Encoding", "gzip");

If the target server is not capable of returning gzipped contents, this customization has no effect and everything works fine. However, if the server returns gzipped contents, the following problem occurs. If the resource has not been accessed and cached yet, connection.getInputStream() returns a gzipped stream as expected. However, if the resource is already cached, connection.getInputStream() unexpectedly returns an unzipped stream, while connection.getContentEncoding() still says "gzip".

Here is the code I use to reproduce the problem (it is included in the attached test project):

public class GetGzippedResourceTest extends JApplet {

    private JLabel label;

    @Override
    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    setLayout(new BorderLayout());

                    JButton button = new JButton("Test");
                    button.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            test();
                        }
                    });
                    add(button, BorderLayout.NORTH);

                    label = new JLabel();
                    add(label, BorderLayout.CENTER);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }

    private void test() {
        InputStream is = null;
        try {
            URL url = new URL("http://a2.twimg.com/a/1306276046/phoenix/css/phoenix.bundle.css");
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("Accept-Encoding", "gzip");
            connection.connect();
            is = connection.getInputStream();
            String encoding = connection.getContentEncoding();
            if (!"gzip".equalsIgnoreCase(encoding)) {
                throw new IllegalStateException("Unexpected encoding: " + encoding);
            }
            is = new GZIPInputStream(is);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = br.readLine();
            label.setText("Result: " + line);
            is.close();
            is = null;
        } catch (Exception ex) {
            label.setText("Error. Consult Java Plug-in console for more info");
            ex.printStackTrace(System.err);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignore) {}
            }
        }
    }
}


To reproduce the problem:

0) Delete all Temporary Internet Files in the Java Console Panel and your browser.
1) Build the attached project.
2) Self-sign the jar.
3) Run the applet (e.g. using the html and jnlp files included in the attached archive).
4) Click the "Test" button once. The first line of the test resource will appear in the area below the button.
5) Click the "Test" button once again. The area below the button will report an error, and the Java console will display the following, indicating that the stream returned on the second attempt is not gzipped:

network: Cache entry found [url: http://a2.twimg.com/a/1306276046/phoenix/css/phoenix.bundle.css, version: null] prevalidated=false/0
java.io.IOException: Not in GZIP format
	at java.util.zip.GZIPInputStream.readHeader(Unknown Source)
	at java.util.zip.GZIPInputStream.<init>(Unknown Source)
	at java.util.zip.GZIPInputStream.<init>(Unknown Source)
	at getgzippedresourcetest.GetGzippedResourceTest.test(GetGzippedResourceTest.java:58)
	at getgzippedresourcetest.GetGzippedResourceTest.access$000(GetGzippedResourceTest.java:18)
	at getgzippedresourcetest.GetGzippedResourceTest$1$1.actionPerformed(GetGzippedResourceTest.java:32)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        ...