I want to write a simple http proxy server in Java using sockets. I wrote a test prototype composed of several tutorials which I had found in the internet. So long I have come with this:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleProxyServer
{
public static final int portNumber = 55558;
public static final int maxConnections = 100;
public static void main( String[] args )
{
SimpleProxyServer proxyServer = new SimpleProxyServer();
proxyServer.start();
}
public void start()
{
System.out.println("Starting the SimpleProxyServer ...");
try
{
ServerSocket serverSocket = new ServerSocket( portNumber, maxConnections );
byte[] buffer = new byte[10000];
boolean run = true;
while( run )
{
Socket clientSocket = serverSocket.accept();
InputStream clientInputStream = clientSocket.getInputStream();
// reading the request and put it into buffer
final int readBytesCount = clientInputStream.read( buffer );
if( readBytesCount < 0)
continue;
String browserRequest = new String( buffer, 0, readBytesCount );
System.out.println( browserRequest );
// extract the host to connect to
final int hostNameStart = browserRequest.indexOf( "Host: " ) + 6;
final int hostNameEnd = browserRequest.indexOf( '\n', hostNameStart );
final String hostName = browserRequest.substring( hostNameStart, hostNameEnd - 1 );
System.out.println( "Connecting to host " + hostName );
// forward the response from the proxy to the server
Socket hostSocket = new Socket( hostName, 80 );
OutputStream hostOutputStream = hostSocket.getOutputStream();
System.out.println( "Forwarding request to server" );
hostOutputStream.write( buffer, 0, readBytesCount );
hostOutputStream.flush();
ProxyThread thread1 = new ProxyThread( clientSocket, hostSocket );
thread1.start();
ProxyThread thread2 = new ProxyThread( hostSocket, clientSocket );
thread2.start();
}
serverSocket.close();
}
catch( IOException e )
{
System.err.println( "IO Error: " + e.getMessage() );
e.printStackTrace();
}
}
}
class ProxyThread extends Thread
{
private Socket incoming, outgoing;
ProxyThread( Socket in, Socket out )
{
incoming = in;
outgoing = out;
}
// Overwritten run() method of thread,
// does the data transfers
public void run()
{
System.out.println( "Starting proxy thread" );
try
{
OutputStream toClient = outgoing.getOutputStream();
InputStream fromClient = incoming.getInputStream();
int numberRead = 0;
byte[] buffer = new byte[10000];
do
{
numberRead = fromClient.read( buffer );
System.out.println( "Read " + numberRead + " bytes" );
System.out.println( "Buffer: " + buffer );
if( numberRead > 0 )
{
toClient.write( buffer, 0, numberRead );
System.out.println( "Sent " + numberRead + " bytes" );
}
}
while( numberRead > 0 );
System.out.println( "Closing all streams and sockets" );
toClient.flush();
incoming.close();
outgoing.close();
}
catch( IOException e )
{
System.err.println( "IO Error: " + e.getMessage() );
}
catch( ArrayIndexOutOfBoundsException e )
{
System.err.println( "Index error: " + e.getMessage() );
}
}
}
While testing with a browser (Firefox if it helps) it hangs and freezes on the fromClient.read(buffer) call for a long time and returns -1, but the browser shows a connection refuse much earlier. What's the cause of it? Is it caused by the InputStream.read() blocking or it's a kinda race? Maybe the whole approach is wrong? Or it's some snag with threads?
P.S. My "native" language is C++. Though I have some occasional experience with the Java programming language, I still don't have enough experience with it's libs and particularly with sockets.
P.P.S. I thought that such thing as an http proxy server has multiple tutorials and how-to on the internet, but all I've found is single threaded or don't read the destintion URL and just transmits the request to the next proxy. So maybe the post will help someone in writing own one. If it ever gets fixed...