Hi,

I tried all day to fix a really simple code and I have no idea why it isn't working. Basically I'm writing a HTTP proxy that is just receiving the client request and sends it to the appropriate server (part of the user request aka HTTP header). The response of the the server is redirected back to the client. So in this early stage it should do nothing from the point of view of the user.

My problem is that when I try to read the client request read() doesn't return -1. So the loop never ends. I'm trying to do everything on the byte level because I don't want to temper with the data just passing it through.

Java Code:
        byte[] clientRequest = null;
        Socket extSocket = null;
            
        try
        {
            byte[] buffer = new byte[2048];
            int data = 0;

            this.clientInputStream = this.socket.getInputStream();
            this.clientOutputStream = this.socket.getOutputStream();

            // receive HTTP header from client
            ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
            while ((data = this.clientInputStream.read()) != -1)
            {
                byteBuffer.write(data);
            }
            clientRequest = byteBuffer.toByteArray();

            HttpHeader header = this.parseHeader(new String(clientRequest));

            extSocket = new Socket(header.getAddress(), 80);
            
            // send HTTP header to server
            OutputStream serverOuputStream = extSocket.getOutputStream();
            serverOuputStream.write(clientRequest);
            serverOuputStream.flush();

            // receive response from server and send to client
            InputStream serverInputStream = extSocket.getInputStream();
            while ((data = serverInputStream.read()) > -1)
            {
                this.clientOutputStream.write(data);
            }

            System.out.println("done");
        }
        catch(Exception e)
        {
            err.printStackTrace();
        }
        finally
        {
            try
            {
                extSocket.close();
                this.socket.close();
            }
            catch (Exception err)
            {err.printStackTrace();}
        }
Both while loops will never end. The method is part of class used as a thread for a basic client-server application.

Does anyone have a clue what I am doing wrong? Or a good example how to implement an HTTP proxy server (without caching)? Would be glad about any help since I wasted several hours without any results.