Skip to content

Commit 55ea1f3

Browse files
committed
code review
1 parent 0ac3772 commit 55ea1f3

File tree

4 files changed

+33
-72
lines changed

4 files changed

+33
-72
lines changed

HttpRequest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,6 @@ public HttpRequest(String method, String url, String version, String headers, St
9898
this.error = 0;
9999
}
100100

101-
/**
102-
* Copy constructor
103-
* <p>
104-
* Returns a deep copy of the 'other' HttpRequest.
105-
* @param other An existing HttpRequest to create the copy from.
106-
*/
107-
public HttpRequest(HttpRequest other) {
108-
this.method = other.method;
109-
this.URL = other.URL;
110-
this.version = other.version;
111-
this.headers = other.headers;
112-
this.host = other.host;
113-
this.port = other.port;
114-
this.error = other.error;
115-
}
116-
117101
public HttpResponse askServerIfvalid(Date lastModified, String etag) {
118102
if (lastModified == null || etag == null || etag.isEmpty()) {
119103
return null;

HttpResponse.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,6 @@ public HttpResponse(int statusCode) {
154154
body.write(bodyStr.getBytes(), 0, bodyStr.length());
155155
}
156156

157-
// copy constructor
158-
public HttpResponse (HttpResponse other) {
159-
this.version = other.version;
160-
this.status = other.status;
161-
this.statusMessage = other.statusMessage;
162-
this.statusLine = other.statusLine;
163-
this.headers = other.headers;
164-
this.lastModified = other.lastModified;
165-
this.expires = other.expires;
166-
this.etag = other.etag;
167-
this.body = other.body;
168-
}
169-
170157
public HttpResponse setStaus(int status) {
171158
this.status = status;
172159
return this;
@@ -207,6 +194,7 @@ public Socket send(Socket host) {
207194
// Write response to host.
208195
DataOutputStream toClient = new DataOutputStream(host.getOutputStream());
209196
toClient.writeBytes(toString());
197+
ProxyCache.verbose("<--- Response <--- \n" + toString());
210198
} catch (IOException e) {
211199
System.out.println("Error writing response to client: " + e);
212200
}

ProxyCache.java

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
public class ProxyCache {
66
private static boolean secure = false;
77
public static boolean expires = false;
8+
public static boolean verbose = false;
89

910
/** Port for the proxy */
1011
private static int port;
@@ -25,6 +26,12 @@ public static void init(int p) {
2526
}
2627
}
2728

29+
public static void verbose(String message) {
30+
if (verbose) {
31+
System.out.println(message);
32+
}
33+
}
34+
2835
public static int ifErrorSend(int errorCode, Socket client) {
2936
if (errorCode != 0) {
3037
HttpResponse errorResponse = new HttpResponse(errorCode);
@@ -47,45 +54,42 @@ public static void handle(Socket client) {
4754
/* Read request */
4855
try {
4956
BufferedReader fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
50-
System.out.println("\n**** New Request ****");
57+
verbose("\n**** New Request ****");
5158
request = new HttpRequest(fromClient);
52-
System.out.println("---> Request --->\n" + request.toString()); // Debug
59+
verbose("---> Request --->\n" + request.toString()); // Debug
5360
} catch (IOException e) {
5461
System.out.println("Error reading request from client: " + e);
5562
new HttpResponse(400).send(client);
5663
}
5764

58-
if (request.method.equals("GET")) { // Only GET requests are cached
65+
if (request.method.equals("GET")) { // Handle GET requests
5966
// Check if key is in cache
60-
if ((response = cache.get(request.getURL())) != null) {// && response.isValid()) { // get item from cache if the cache is still fresh
61-
// response is set and valid
62-
// System.out.println("Retrieved " + request.getURL());
63-
// System.out.println("Is valid? " + cache.isValid(request));
64-
} else { // handle requests
65-
/* Send request to server */
66-
response = request.send();
67-
68-
/* Read response and forward it to client */
69-
cache.put(request.getURL(), response);// Save response to cache
67+
if ((response = cache.get(request.getURL())) == null) { // get item from cache
68+
// if the item is not in the cache.
69+
70+
/* Send request to server */
71+
response = request.send();
72+
73+
/* Read response and forward it to client */
74+
cache.put(request.getURL(), response);// Save response to cache
7075
}
7176

72-
System.out.println("<--- Response <--- \n" + response.toString()); // Debug
77+
verbose("<--- Response <--- \n" + response.toString());
7378
try {
7479
/* Write response to client. First headers, then body */
7580
DataOutputStream toClient = new DataOutputStream(client.getOutputStream());
7681
toClient.writeBytes(response.toString()); // headers
7782
response.body.writeTo(toClient); // body
7883
client.close();
7984
} catch (IOException e) {
80-
System.out.println("Error writing response to client (cached): " + e);
81-
// new HttpResponse(500).send(client);
85+
System.out.println("Error writing response to client: " + e);
8286
}
8387
}
8488

8589
// http://stackoverflow.com/questions/16358589/implementing-a-simple-https-proxy-application-with-java
8690
// http://stackoverflow.com/questions/18273703/tunneling-two-socket-client-in-java#18274109
8791
else if (secure && request.method.equals("CONNECT")) {
88-
System.out.println("CONNECT found! Tunnelling HTTPS connection.");
92+
System.out.println("CONNECT found! attempt tunnel HTTPS connection.");
8993
InputStream fromClient;
9094
OutputStream toServer;
9195
try {
@@ -123,33 +127,14 @@ else if (secure && request.method.equals("CONNECT")) {
123127
} catch (SocketTimeoutException e) {
124128
System.out.println("Connection Timed out " + e);
125129
// EXIT HERE
126-
// servertToClient.interrupt();
127130
return;
128-
// new HttpResponse(404).send(client);
129131
} catch (IOException e) {
130132
System.out.println("Error tunnelling request: " + e);
131-
// new HttpResponse(500).send(client);
132-
// return;
133-
// } catch (InterruptedException e) {
134-
// System.out.println("Thread: Interrupted" + e);
135133
}
136134
} else { // e.g POST, HEAD or DELETE requests
135+
System.out.println("Ignoring " + request.method + " request.");
137136
new HttpResponse(501).send(client);
138137
return;
139-
// response = request.send();
140-
// System.out.println("<--- Response <--- \n" + response.toString()); // Debug
141-
142-
// try {
143-
// /* Write response to client. First headers, then body */
144-
// DataOutputStream toClient = new DataOutputStream(client.getOutputStream());
145-
// toClient.writeBytes(response.toString()); // headers // broken pipe /Protocol wrong type for socket
146-
// response.body.writeTo(toClient); // body
147-
// client.close();
148-
// } catch (IOException e) {
149-
// System.out.println("Error writing response to client: " + e);
150-
// e.printStackTrace();
151-
// // new HttpResponse(500).send(client);
152-
// }
153138
}
154139
}
155140

@@ -163,8 +148,7 @@ public static void main(String args[]) {
163148
try {
164149
myPort = Integer.parseInt(args[0]);
165150
} catch (ArrayIndexOutOfBoundsException e) {
166-
System.out.println("Usage: ProxyCache <Port Number> [args]\nArguments:\n -s, --secure Proxy HTTPS/TLS (experimental, can use 100% CPU)\n -e, --expires Check expires header");
167-
// System.out.println("Need port number as argument");
151+
System.out.println("Usage: ProxyCache <Port Number> [args]\nArguments:\n -v --verbose Verbose output (more logging) \n -s, --secure Proxy HTTPS/TLS (experimental, can use 100% CPU)\n -e, --expires Check expires header");
168152
System.exit(-1);
169153
} catch (NumberFormatException e) {
170154
System.out.println("Please give port number as integer.");
@@ -173,6 +157,12 @@ public static void main(String args[]) {
173157
init(myPort);
174158

175159
for (String argument: args) { // http://java.about.com/od/javasyntax/a/Using-Command-Line-Arguments.htm
160+
if(argument.equals("-v") || argument.equals("--verbose")) {
161+
// Verbose output (more logging)
162+
verbose = true;
163+
System.out.println("Verbose logging=true");
164+
165+
}
176166
if(argument.equals("-s") || argument.equals("--secure")) {
177167
// proxy secure HTTPS/TLS connections (experimental as it can course 100% CPU usage)
178168
secure = true;
@@ -194,7 +184,6 @@ public static void main(String args[]) {
194184
try {
195185
client = socket.accept();
196186
new Thread(new ProxyThread(client)).start();
197-
// new ProxyThread(client);
198187
} catch (IOException e) {
199188
System.out.println("Error reading request from client: " + e);
200189
/* Definitely cannot continue processing this request,
@@ -216,7 +205,6 @@ class ProxyThread extends Thread {
216205

217206
public void run() {
218207
ProxyCache.handle(client);
219-
// System.out.println("Handling client from thread! " + client.toString());
220208
}
221209
}
222210

@@ -246,7 +234,7 @@ public void run() {
246234
} catch (IOException e) {
247235
System.out.println("Thread: IOError: " + e);
248236
interrupt();
249-
// return;
237+
return;
250238
}
251239
}
252240
}

SCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ public boolean isValid(String requestURL, HttpResponse response) {
8383
return true; // use the cached response if new response has an unrecoverable error.
8484
}
8585
if (newResponse.status == 304) { // Not modified, so still valid
86-
System.out.println("SCACHE: 304 Not modified");
86+
System.out.println("SCACHE: recieved 304 Not modified");
8787
return true;
8888
} else { // 200 OK and other responses
89+
System.out.println("SCACHE: recieved " + newResponse.status);
8990
System.out.println("SCACHE: response was modified. Updating Cache.");
9091
put(requestURL, newResponse); // save the new response
9192
return false;

0 commit comments

Comments
 (0)