Hi,
I'm trying to make a web proxy server in Java. As this needs to be demonstrated in college, my proxy server will have to operate behind the college proxy.
I'm trying to get basic all round functionality first, so I'm trying for basic HTTP/1.0 functionality for now.
The problem is that the college webserver seems to reply to HTTP/1.0 requests with a HTTP/1.1 header.
$ telnet localhost 8000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get / http/1.0
host: www.tcd.ie
HTTP/1.1 200 OK
Date: Thu, 01 Apr 2010 01:23:00 GMT
Server: Apache/2.2.10
Content-Type: text/html; charset=ISO-8859-1
Connection: close
This has the problem of blocking the return of content from the server to my client. The connection remains open as per HTTP/1.1, but I can't see a clean way of detecting when the file is transferred.
I was originally trying to load the headers into a string vector for easy editing, but then the content stream was empty, so I abandoned that idea for the time being.
As you can see from the output, the fromServerStream.read() method will block until the server closes out the connection, but the stream is bursty so terminating when available() == 0 is not an option.
private void ServerContentToClient(){
//buffer
byte[] buffer = new byte[1024];
//amount of bytes in the buffer
int bufferSize = 0;
try{
InputStream fromServerStream = ServerSocket.getInputStream();
OutputStream toClientStream = ClientSocket.getOutputStream();
System.out.println("Available: "+fromServerStream.available());
while(true)
{
System.out.println("Start loop");
//Estimated amount of bytes that can be read from the server
System.out.println("Available: "+fromServerStream.available());
//Read the bytes into the buffer
bufferSize = fromServerStream.read(buffer, 0, 1024);
//End of stream
if(bufferSize == -1)
{
System.out.println("Closing Sockets");
ServerSocket.close();
ClientSocket.close();
break;
}
toClientStream.write(buffer, 0, bufferSize);
System.out.println("bufferSize: "+bufferSize);
System.out.println("End loop");
System.out.println();
}
}
catch(IOException e)
{
System.err.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println(e);
}
}
Start loop
Available: 0
bufferSize: 1024
End loop
Start loop
Available: 1712
bufferSize: 1024
End loop
Start loop
Available: 688
bufferSize: 688
End loop
Start loop
Available: 0
bufferSize: 555
End loop
Start loop
Available: 0
bufferSize: 1024
End loop
Start loop
Available: 3972
bufferSize: 1024
End loop
Start loop
Available: 2948
bufferSize: 1024
End loop
Start loop
Available: 1924
bufferSize: 1024
End loop
Start loop
Available: 900
bufferSize: 900
End loop
Start loop
Available: 0
Can anyone see a simple solution to this problem?
Thanks