-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyClientThread.java
247 lines (223 loc) · 8.43 KB
/
ProxyClientThread.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import java.net.Socket;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.io.IOException;
import java.net.URL;
import java.util.StringTokenizer;
public class ProxyClientThread extends Thread{
private Socket socket;
private Socket socketServer;
private InputStream fromBrowser;
private DataInputStream fromHost;
private OutputStream toBrowser;
private DataOutputStream toHost;
private String get;
private String host;
private ArrayList pedido;
private int porta;
HTTPRequest request;
ClientToServerThread clientToServer;
Cache cache;
private DataInputStream fromCache;
private DataOutputStream toCache;
byte line[];
URL url;
/*
* Recebe os dados da class proxythread para poder depois tratar os mesmos.
*/
public ProxyClientThread(Socket socket, Cache cache) {
this.socket = socket;
this.cache = cache;
porta = socket.getPort();
try {
fromBrowser = socket.getInputStream();
toBrowser = socket.getOutputStream();
start();
}
catch (IOException e) {
System.out.println("Erro: " + e);
}
}
/*
* Faz o tratamento do pedido
* Vê se exite na cache e caso existe devolve para o browser, nesse caso o cliente
* Caso não existe vai buscar as informações ao host, mete as na cache e depois devolve as mesma para o browser, , nesse caso o cliente.
*/
private void readRequete() throws Exception {
request = HTTPRequest.readLine(fromBrowser);
//request = HTTPRequest.parseHTTPRequestAs1_0(fromBrowser);
String requestedObject = request.requestedObject();
String post = request.requestPost();
String contentype = request.requestContentType();
String contentlength = request.requestContentLength();
String type = request.requestType();
// identifica o metodo
if(type.equals("GET"))
{ // reply to a GET request
//System.out.println("browser sent a GET operation");
if(cache.IsCachable(requestedObject))
{
if(cache.IsCached(requestedObject))
{
fromCache = new DataInputStream(cache.getFileInputStream(requestedObject));
sendToBrowser(fromCache);
}else{
discoverHost(requestedObject);
String lineString = fromHost.readLine();
StringTokenizer s = new StringTokenizer(lineString);
//aqui retCode fica com a versão do http/1.0
String retCode = s.nextToken();
//aqui retCode fica com o codigo (ex: 204 ou 200 ou ....)
retCode = s.nextToken();
if (!retCode.equals("200") || !retCode.equals("302")
|| !retCode.equals("304"))
{
toCache = new DataOutputStream(cache.getFileOutputStream(requestedObject));
String tempStr = new String(lineString+"\r\n");
toHost.writeBytes(tempStr);
line = new byte[tempStr.length()];
tempStr.getBytes(0,tempStr.length(),line,0);
toCache.write(line);
if (lineString.length() > 0)
{
while (true)
{
lineString = fromHost.readLine();
tempStr = new String(lineString+"\r\n");
toHost.writeBytes(tempStr);
line = new byte[tempStr.length()];
tempStr.getBytes(0,tempStr.length(),line,0);
//toCache.write(line);
if (lineString.length() <= 0)
break;
}
}
toHost.flush();
byte data[] = new byte[4096];
int count;
while (( count = fromHost.read(data)) != -1)
{
line = new byte[count];
System.arraycopy(data,0,line,0,count);
toCache.write(line);
toBrowser.write(data, 0, count);
}
toBrowser.flush();
toCache.close();
cache.AddToTable(requestedObject);
}
}
}else{
discoverHost(requestedObject);
sendToBrowser(fromHost);
}
}else if(type.equals("PUT"))
{ // PUT request
System.out.println("browser sent a PUT operation");
pedido.add("PUT " + requestedObject + " " + "HTTP/1.0");
}
else if(type.equals("POST"))
{ // POST request
//System.out.println("browser sent a POST operation");
discoverHost(requestedObject);
pedido = new ArrayList();
pedido.add("POST" + requestedObject + " " + "HTTP/1.0");
pedido.add(contentype);
pedido.add(contentlength);
pedido.add(post);
// for(int i=0;i<pedido.size();i++)
// {
// System.out.println(pedido.get(i));
// }
new ClientToServerThread(toHost, pedido);
sendToBrowser(fromHost);
}
}
/*
* Vai permitir saber qual a porta e com qual host vamos enviar e receber os dados
*/
private void discoverHost(String requestedObject)
{
try
{
url = new URL("http://"+request.urlHost);
int portaServer = url.getPort() > 0 ? url.getPort() : 80;
socketServer = new Socket(url.getHost(),portaServer);
fromHost = new DataInputStream(socketServer.getInputStream());
toHost = new DataOutputStream(socketServer.getOutputStream());
pedido.add("GET " + requestedObject + " " + "HTTP/1.0");
new ClientToServerThread(toHost, pedido);
}catch (Exception e)
{
//System.out.println("Erro : "+e);
}
}
/*
* Recebe dados e envia para o browser
*/
private void sendToBrowser(DataInputStream from)
{
try
{
byte data[] = new byte[4096];
int count;
while ((count = from.read(data)) != -1)
{
toBrowser.write(data,0,count);
}
}
catch (IOException e)
{
//System.out.println("Erro 1 Thread Proxy : " + porta);
}
finally
{
try
{
toBrowser.flush();
fromCache.close();
}
catch (Exception e) {
//System.out.println("Erro 2 Thread Proxy : " + porta);
}
}
}
/*
* Lança o metodoa reaRequete e ao fim fecha todas as linhas de dados.
*/
public void run() {
try
{
pedido = new ArrayList();
readRequete();
}
catch (Exception e)
{
//System.out.println("Erro 3 Thread Proxy : " + porta);
}
finally
{
try
{
if (fromBrowser != null)
fromBrowser.close();
if (toBrowser != null)
toBrowser.close();
if (socket != null)
socket.close();
if (socketServer != null)
socketServer.close();
if (socket != null)
socket.close();
if (socketServer != null)
socketServer.close();
}
catch (Exception e) {
//System.out.println("Erro 4 Thread Proxy : " + porta);
}
}
}
}