I am writing somewhat of a proxy program in Java. Here's how it works:
- The browser will be connected to the program.
- Any requests by the browser will be first be printed out to standard out, then forwarded to the server.
- The server then returns a response, which is also printed to standard out, then forwarded back to the browser.
My problem is, step 1 works, step two works, but step 3 fails. The program can get a response, and its printed off to standard out properly, but the browser can't seem to get it. I've modified the program to isolate the problem.
All its doing here, is printing the response directly to the browser:
ServerSocket client = null;
try {
client = new ServerSocket(snoopPort);
} catch (IOException e) {
System.out.println("ERROR: Could not listen on port: " + snoopPort);
System.exit(-1);
}
Socket clientSocket = null;
try {
clientSocket = client.accept();
} catch (IOException e) {
System.out.println("ERROR: Accept failed on port: " + snoopPort);
System.exit(-1);
}
PrintWriter snoopOut = new PrintWriter(clientSocket.getOutputStream(), true);
snoopOut.print("HTTP/1.1 200 OK\r\n");
snoopOut.print("Date: Thu, 05 Feb 2009 06:37:28 GMT\r\n");
snoopOut.print("Server: Apache\r\n");
snoopOut.print("Set-Cookie: Apache=99.245.58.244.1233815848703045; path=/\r\n");
snoopOut.print("Accept-Ranges: bytes\r\n");
snoopOut.print("Transfer-Encoding: chunked\r\n");
snoopOut.print("Content-Type: text/html\r\n");
snoopOut.print("\r\n");
snoopOut.print("<html><head><title>test</head><body>hello world!</body></html>\r\n");
snoopOut.close();
clientSocket.close();
client.close();