Solution for the Proxy Cache Lab
The following is one solution for the proxy cache lab. It contains code for all the three
classes in the lab.
ProxyCache.java
/**
* ProxyCache.java Simple caching proxy
*
public static void init(int p) {
port = p;
try {
socket = new ServerSocket(port);
} catch (IOException e) {
try {
BufferedReader fromClient =
new BufferedReader(new
InputStreamReader(client.getInputStream()));
System.out.println(“Reading request…”);
new DataOutputStream(server.getOutputStream());
toServer.writeBytes(request.toString());
} catch (UnknownHostException e) {
System.out.println(“Unknown host: ” + request.getHost());
System.out.println(e);
//response = new HttpResponse(is);
DataOutputStream toClient =
new DataOutputStream(client.getOutputStream());
toClient.writeBytes(response.toString());
toClient.write(response.body);
myPort = Integer.parseInt(args[0]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“Need port number as argument”);
System.exit(1);
} catch (NumberFormatException e) {
handle(client);
//ProxyCacheThread request = new ProxyCacheThread(client);
//request.run();
} catch (IOException e) {
System.out.println(“Error reading request from client: ” + e);
/* Definitely cannot continue, so skip to next
* iteration of while loop. */
continue;
}
}
}
}
HttpRequest.java
/**
* HttpRequest HTTP request container and parser
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class HttpRequest {
/** Help variables */
final static String CRLF = “\r\n”;
final static int HTTP_PORT = 80;
/** Store the request parameters */
String method;
firstLine = from.readLine();
} catch (IOException e) {
System.out.println(“Error reading request line: ” + e);
}
String[] tmp = firstLine.split(” “);
method = tmp[0];
* contact in case the request URI is not complete. */
if (line.startsWith(“Host:”)) {
tmp = line.split(” “);
if (tmp[1].indexOf(‘:’) > 0) {
String[] tmp2 = tmp[1].split(“:”);
port);
}
/** Return host for which this request is intended */
public String getHost() {
return host;
}
/** Return port for server */
return req;
}
}
HttpResponse.java
/**
* HttpResponse Handle HTTP replies
*
* $Id: HttpResponse.java,v 1.3 2004/02/16 15:21:46 kangasha Exp $
*
*/
import java.io.*;
String version;
int status;
String statusLine = “”;
String headers = “”;
/* Body of reply */
/* First read status line and response headers */
try {
String line = fromServer.readLine();
while (line.length() != 0) {
if (!gotStatusLine) {
}
length = Integer.parseInt(tmp[1]);
}
line = fromServer.readLine();
}
} catch (IOException e) {
System.out.println(“Error reading headers from server: ” + e);
return;
}
* than BUF_SIZE. The whileloop ends when either we have
* read ContentLength bytes or when the connection is
* closed (when there is no ConnectionLength in the
* response. */
while (bytesRead < length || loop) {
body[bytesRead + i] = buf[i];
}
bytesRead += res;
}
} catch (IOException e) {
System.out.println(“Error reading response body: ” + e);
return;
}
}
/**
}
}