55public 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]\n Arguments:\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]\n Arguments:\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}
0 commit comments