import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

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

  final private Socket clientSocket;

  public RequestHandlerThread(Socket clientSocket) {
    this.clientSocket = clientSocket;
  }

  public void run() {
    try {
      try {
        clientSocket.setSoTimeout(1000);
        HttpRequest clientRequest = new HttpRequest(clientSocket.getInputStream());
        System.out.printf("[%s] %s\n", clientSocket.getInetAddress().getCanonicalHostName(), clientRequest);

        Socket serverSocket = new Socket(clientRequest.getHost(), 80);
        clientRequest.send(serverSocket.getOutputStream());

        HttpResponse serverResponse = new HttpResponse(serverSocket.getInputStream());
        serverResponse.send(clientSocket.getOutputStream());

        serverSocket.close();
      } catch (SocketException e) {
        System.out.printf("[%s] Abandon request\n", clientSocket.getInetAddress().getCanonicalHostName());
      } catch (SocketTimeoutException e) {
        System.out.printf("Socket read timeout. Ignore\n");
      } catch (UnknownHostException e) {
        clientSocket.getOutputStream().write("HTTP/1.0 502 Bad Gateway\r\n\r\n".getBytes());
      } catch (IOException e) {
        e.printStackTrace();
      } catch (MalformedRequestException | MalformedResponseException e) {
        clientSocket.getOutputStream().write(e.getMessage().getBytes());
      } finally {
        clientSocket.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

@@ -664,295 +664,93 @@
     
-Socket serverSocket = new Socket(clientRequest.getHost(), 80);%0A        clientRequest.send(serverSocket.getOutputStream());%0A%0A        HttpResponse serverResponse = new HttpResponse(serverSocket.getInputStream());%0A        serverResponse.send(clientSocket.getOutputStream());%0A%0A        server
+ResponseCache.process(clientRequest, clientSocket.getOutputStream());%0A%0A        client
 Sock

Implement basic caching