1

I am new to network programming and I am trying to implement a simple http proxy in Java that is just supposed to forward the client's requests and transfer back the response.

To handle GET requests I simply read the client socket InputStream, create a new socket to the desired host (that I read from the socket) to then write the same GET request done by the client to the OutputStream of this new socket. Same thing to retrieve the response, I write back the response from the InputStream of the host socket to the OutputStream of the client socket.

But I am having some troubles when I have to handle CONNECT requests, for instance, when I try to access www.google.com from my browser, this results in reading this from the client socket:

CONNECT www.google.com:443 HTTP/1.1
Host: www.google.com
Proxy-Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36

To my understanding, I should connect to the host www.google.com on port 443. So I create a new socket :

Socket socket = new Socket(www.google.fr, 443);

But what should be the request? I simply tried:

GET / HTTP/1.1

But using Wireshark, it seems that the host immediately terminates the connection by sending back FIN and RST TCP segments.

How should I proceed to correctly handle these requests and retrieve the content of the webpage?

1 Answer 1

2

The request should be whatever the client sends next after the CONNECT.

You have to respond to the CONNECT request with an HTTP status line, then really all you have to do is start copying bytes in both directions. You don't need to care about the request and the response any further.

Sign up to request clarification or add additional context in comments.

3 Comments

So I need to keep reading the InputStream of the same client socket for another request (because of the Proxy-Connection: keep-alive)? Or will this request be made using another connection/socket?
The request will be made with this socket, of course, otherwise what exactly is the point of it? You need to send whatever follows the CONNECT to the server, and you need to send whatever the server sends you back to the client. Simultaneously. It will be SSL so you won't understand it anyway, so you won't get to know anything about keepalive. Just copy the bytes like I said.
I wasn't sending the HTTP status line back to the client and therefore I didn't get the request that I wanted after the CONNECT. Working fine now, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.