-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProxyCache.java
More file actions
111 lines (93 loc) · 3.17 KB
/
Copy pathProxyCache.java
File metadata and controls
111 lines (93 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.net.*;
import java.io.*;
import java.util.*;
public class ProxyCache {
/** Port for the proxy */
private static int port;
/** Socket for client connections */
private static ServerSocket socket;
/** Create the ProxyCache object and the socket */
public static void init(int p) {
port = p;
try {
socket = new ServerSocket(port); // creating a new server socket at the port passed into the cache.
} catch (IOException e) {
System.out.println("Error creating socket: " + e);
System.exit(-1);
}
}
public static void handle(Socket client) {
Socket server = null;
HttpRequest request = null;
HttpResponse response = null;
/* Process request. If there are any exceptions, then simply
* return and end this request. This unfortunately means the
* client will hang for a while, until it timeouts. */
/* Read request */
try {
BufferedReader fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
request = new HttpRequest(fromClient);
} catch (IOException e) {
System.out.println("Error reading request from client: " + e);
return;
}
/* Send request to server */
try {
/* Open socket and write request to socket */
server = new Socket(request.getHost(), request.getPort());
DataOutputStream toServer = new DataOutputStream(server.getOutputStream());
toServer.writeBytes(request.toString()); // writing request to socket.
} catch (UnknownHostException e) {
System.out.println("Unknown host: " + request.getHost());
System.out.println(e);
return;
} catch (IOException e) {
System.out.println("Error writing request to server: " + e);
return;
}
/* Read response and forward it to client */
try {
DataInputStream fromServer = new DataInputStream(server.getInputStream());
response = new HttpResponse(fromServer);
DataOutputStream toClient = new DataOutputStream(client.getOutputStream());
toClient.writeBytes(response.toString());
toClient.write(response.body);
/* Write response to client. First headers, then body */
client.close();
server.close();
} catch (IOException e) {
System.out.println("Error writing response to client: " + e);
}
}
/* -------------------------------------------------- */
/* Read command line arguments and start proxy */
public static void main(String args[]) {
int myPort = 0;
try {
myPort = Integer.parseInt(args[0]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Need port number as argument");
System.exit(-1);
} catch (NumberFormatException e) {
System.out.println("Please give port number as integer.");
System.exit(-1);
}
init(myPort);
/** Main loop. Listen for incoming connections and spawn a new
* thread for handling them */
Socket client = null;
while (true) {
try {
client = socket.accept();
System.out.println("Connection Established "+ client);
handle(client);
} catch (IOException e) {
System.out.println("Error reading request from client: " + e);
/* Definitely cannot continue processing this request,
* so skip to next iteration of while loop. */
continue;
}
}
}
}
//