Title: HTTP and Java - modified headers in gzip encoding
Tags: java;http
Question: I've got strange issue with my http communication. I wrote following very simple http server and client.

Http server:


```public class SimpleHttpServer {

    public void run() throws IOException {
        final ServerSocket serverSocket = new ServerSocket(8080);

        while(true)
        {
            final Socket connectionSocket = serverSocket.accept();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        try(BufferedReader in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                            DataOutputStream out = new DataOutputStream(connectionSocket.getOutputStream())){

                            System.out.println(in.readLine());

                            String line = null;
                            while((line = in.readLine()) != null &amp;&amp; !line.trim().isEmpty()){
                                System.out.println(line);
                            }

                            byte[] message = loadFile(); //load some gzipped jpg image from disk

                            out.write(ascii("HTTP/1.1 200 OK\n"));
                            out.write(ascii("Content-Type: image/jpg\n"));
                            out.write(ascii(String.format("Content-Length: %s\n", message.length)));
                            out.write(ascii("content-encoding: gzip\n"));
                            out.write(ascii("\n"));
                            out.write(message);

                        }
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
```

Http client:


```public class SimpleHttpClient {

    public void run() throws IOException {
        String host = "localhost";
        int port = 8080;

        Socket socket = new Socket(host, port);
        try (OutputStream out = socket.getOutputStream()) {
            out.write(ascii("GET / HTTP/1.1\n"));
            out.write(ascii("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/681.350.8737 Safari/537.36\n"));
            out.write(ascii("Accept-Encoding: gzip, deflate, sdch\n"));
            out.write(ascii("Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4\n"));
            out.write(ascii("\n"));


            try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                String line = null;

                while ((line = in.readLine()) != null &amp;&amp; !line.trim().isEmpty()) {
                    System.out.println(line);
                }
            }
        }
    }
}
```

So, when I remove line with user agent header, I get result as I expect:


```HTTP/1.1 200 OK 
Content-Type: image/jpg 
Content-Length: 378632
content-encoding: gzip
```

And this is clear and obvious.

But when this header will appear, the answer is different:

```HTTP/1.1 200 OK
Content-Type: image/jpg
Transfer-Encoding: chunked
```

I didn't change anything on server side. On every request I send the same response. The user agent was copied from chrome browser.

Both implementations are in plain java socket, not on third party libriares. 
So my question is: how it happend? Something was cached? But where?

Please don't look at code quality. This is just an example. I don't want to use it on production :)

EDIT 1:

```private byte[] ascii(String value) throws UnsupportedEncodingException {
    return value.getBytes(StandardCharsets.US_ASCII);
}
```

EDIT 2:

I've done some research. It happens when user agent contains "AppleWebKit", protocol name is HTTP and port is 80 or 8080. 
Moreover it happens on Windows 10 (I checked on two different computers). On Linux (Ubuntu 16.04 and Debian 8) everything is fine.

Maybe I should change the title of question? But how can I explain the problem in one short sentence?

Comment: Seems impossible. Your client receives a 'Transfer-Encoding' header that is not produced by the server code. I suspect you don't connect to the right server. Make sure your server is the only process listening to port 8080 - maybe there is already one listening to 681.350.8737:8080. Use a step debugger to verify that your server code is executed.

Comment: check if there is some exception may be

Comment: Do not use general Exception class implement the catch for every type of exception your code can throw

Comment: post the ascii method

Comment: How `ascii` method is defined?

Comment: I was add implementation of `ascii` method. 
@Cristian - Of course you right. I shouldn't use general Exception class, but this is a simple example, only for testing :)

Comment: @blafasel I thought the same :) I even changed content type header to `Content-Type: image/impossible` on server side and reran client. Everything is correct. I'm connecting to right server. Maybe this is configuration of my computer or something was cached because earlier on this port was running netty http server? But where and why? Previously I saw the issue on chrome debug console.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:10/15/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><http>', 'Title': 'HTTP and Java - modified headers in gzip encoding', 'ViewCount': 95, 'id': 40059166, 'user_AboutMe': '', 'user_AccountId': 1464044.0, 'user_DisplayName': 'Łukasz Marciniak', 'user_Reputation': 59.0, 'user_Views': 21.0, 'user_id': 1377461.0}}