import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Created by Xinan on 14/9/15.
 */
public class RequestHandlerThread extends Thread {

  final private Socket clientSocket;
  final private int BUFFER_SIZE;

  public RequestHandlerThread(Socket clientSocket) {
    this(clientSocket, 65536);
  }

  public RequestHandlerThread(Socket clientSocket, int bufferSize) {
    this.clientSocket = clientSocket;
    BUFFER_SIZE = bufferSize;
  }

  public void run() {
    try {
      System.out.printf("Connected to client: %s\n", clientSocket.getInetAddress());
      byte[] buffer = new byte[BUFFER_SIZE];
      BufferedInputStream in = new BufferedInputStream(clientSocket.getInputStream());
      PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
      int bytesRead;
      do {
        bytesRead = in.read(buffer);
        out.print(new String(buffer, 0, bytesRead));
      } while (in.available() > 0);
      out.flush();
      in.close();
      out.close();
      clientSocket.close();
    } catch (IOException e) {
      System.out.printf("Error: %s\n", e.getMessage());
    }
  }
}

@@ -12,83 +12,33 @@
 .io.
-BufferedInputStream;%0Aimport java.io.IOException;%0Aimport java.io.PrintWriter
+*;%0Aimport java.net.Socket
 ;%0Aim
@@ -57,16 +57,25 @@
 t.Socket
+Exception
 ;%0A%0A/**%0A 
@@ -505,47 +505,257 @@
     
-System.out.printf(%22Connected to client:
+try %7B%0A        HttpRequest clientRequest = new HttpRequest(clientSocket.getInputStream());%0A        BufferedOutputStream clientOut = new BufferedOutputStream(clientSocket.getOutputStream(), BUFFER_SIZE);%0A        System.out.printf(%22Client %25s requesting
  %25s%5C
@@ -791,56 +791,110 @@
 ss()
+, clientRequest.getUrl()
 );%0A
+%0A
       
-byte%5B%5D buffer = new byte%5BBUFFER_SIZE%5D;%0A
+  Socket serverSocket = new Socket(clientRequest.getHost(), 80);%0A%0A  
     
@@ -915,17 +915,23 @@
 tStream 
-i
+serverI
 n = new 
@@ -942,38 +942,38 @@
 eredInputStream(
-client
+server
 Socket.getInputS
@@ -983,115 +983,288 @@
 am()
-);%0A      PrintWriter out = new PrintWriter(clientSocket.getOutputStream());%0A      int bytesRead;%0A      do %7B
+, BUFFER_SIZE);%0A        BufferedOutputStream serverOut = new BufferedOutputStream(serverSocket.getOutputStream(), BUFFER_SIZE);%0A        serverOut.write(clientRequest.toByteBuffer());%0A        serverOut.flush();%0A%0A        int bytesRead;%0A        byte%5B%5D buffer = new byte%5BBUFFER_SIZE%5D;
 %0A   
@@ -1264,24 +1264,32 @@
 E%5D;%0A        
+while ((
 bytesRead = 
@@ -1288,17 +1288,23 @@
 sRead = 
-i
+serverI
 n.read(b
@@ -1309,17 +1309,24 @@
 (buffer)
-;
+) %3E 0) %7B
 %0A       
@@ -1330,28 +1330,25 @@
     
-out.print(new String
+  clientOut.write
 (buf
@@ -1365,25 +1365,24 @@
 tesRead)
-)
 ;%0A      
 %7D while 
@@ -1377,83 +1377,61 @@
     
-%7D while (in.available() %3E 0);%0A      out.flush();%0A      in.close();%0A      ou
+    clientOut.flush();%0A        %7D%0A%0A        serverSocke
 t.cl
@@ -1439,24 +1439,26 @@
 se();%0A      
+  
 clientSocket
@@ -1471,16 +1471,18 @@
 ();%0A    
+  
 %7D catch 
@@ -1486,10 +1486,14 @@
 ch (
-IO
+Socket
 Exce
@@ -1499,24 +1499,26 @@
 eption e) %7B%0A
+  
       System
@@ -1534,36 +1534,311 @@
 tf(%22
-Error: %25s%5Cn%22, e.getMessage()
+Client closed connection.%5Cn%22);%0A      %7D catch (IOException e) %7B%0A        e.printStackTrace();%0A      %7D catch (MalformedRequestException e) %7B%0A        clientSocket.getOutputStream().write(e.getMessage().getBytes());%0A        clientSocket.close();%0A      %7D%0A    %7D catch (IOException e) %7B%0A      e.printStackTrace(
 );%0A 

Implement basic proxy feature