1 |
|
package org.qedeq.kernel.bo.test; |
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 |
|
import java.io.*; |
56 |
|
import java.net.*; |
57 |
|
import java.lang.reflect.Array; |
58 |
|
|
|
|
| 0% |
Uncovered Elements: 77 (77) |
Complexity: 18 |
Complexity Density: 0.32 |
|
59 |
|
public class HttpProxy extends Thread |
60 |
|
{ |
61 |
|
public static final int DEFAULT_PORT = 8080; |
62 |
|
|
63 |
|
private ServerSocket server = null; |
64 |
|
private int thisPort = DEFAULT_PORT; |
65 |
|
private String fwdServer = ""; |
66 |
|
private int fwdPort = 0; |
67 |
|
private int ptTimeout = ProxyThread.DEFAULT_TIMEOUT; |
68 |
|
private int debugLevel = 0; |
69 |
|
private PrintStream debugOut = System.out; |
70 |
|
|
71 |
|
|
72 |
|
|
|
|
| 0% |
Uncovered Elements: 28 (28) |
Complexity: 4 |
Complexity Density: 0.17 |
|
73 |
0
|
public static void main (String args[])... |
74 |
|
{ |
75 |
0
|
int port = 0; |
76 |
0
|
String fwdProxyServer = ""; |
77 |
0
|
int fwdProxyPort = 0; |
78 |
|
|
79 |
0
|
if (args.length == 0) |
80 |
|
{ |
81 |
0
|
System.err.println("USAGE: java jProxy <port number> [<fwd proxy> <fwd port>]"); |
82 |
0
|
System.err.println(" <port number> the port this service listens on"); |
83 |
0
|
System.err.println(" <fwd proxy> optional proxy server to forward requests to"); |
84 |
0
|
System.err.println(" <fwd port> the port that the optional proxy server is on"); |
85 |
0
|
System.err.println("\nHINT: if you don't want to see all the debug information flying by,"); |
86 |
0
|
System.err.println("you can pipe the output to a file or to 'nul' using \">\". For example:"); |
87 |
0
|
System.err.println(" to send output to the file prox.txt: java jProxy 8080 > prox.txt"); |
88 |
0
|
System.err.println(" to make the output go away: java jProxy 8080 > nul"); |
89 |
0
|
return; |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
0
|
port = Integer.parseInt(args[0]); |
94 |
0
|
if (args.length > 2) |
95 |
|
{ |
96 |
0
|
fwdProxyServer = args[1]; |
97 |
0
|
fwdProxyPort = Integer.parseInt(args[2]); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
0
|
System.err.println(" ** Starting jProxy on port " + port + ". Press CTRL-C to end. **\n"); |
103 |
0
|
HttpProxy jp = new HttpProxy(port, fwdProxyServer, fwdProxyPort, 20); |
104 |
0
|
jp.setDebug(1, System.out); |
105 |
0
|
jp.start(); |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
0
|
while (true) |
113 |
|
{ |
114 |
0
|
try { Thread.sleep(3000); } catch (Exception e) {} |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
128 |
0
|
public HttpProxy (int port)... |
129 |
|
{ |
130 |
0
|
thisPort = port; |
131 |
|
} |
132 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
133 |
0
|
public HttpProxy (int port, String proxyServer, int proxyPort)... |
134 |
|
{ |
135 |
0
|
thisPort = port; |
136 |
0
|
fwdServer = proxyServer; |
137 |
0
|
fwdPort = proxyPort; |
138 |
|
} |
139 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
140 |
0
|
public HttpProxy (int port, String proxyServer, int proxyPort, int timeout)... |
141 |
|
{ |
142 |
0
|
thisPort = port; |
143 |
0
|
fwdServer = proxyServer; |
144 |
0
|
fwdPort = proxyPort; |
145 |
0
|
ptTimeout = timeout; |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
152 |
0
|
public void setDebug (int level, PrintStream out)... |
153 |
|
{ |
154 |
0
|
debugLevel = level; |
155 |
0
|
debugOut = out; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
161 |
0
|
public int getPort ()... |
162 |
|
{ |
163 |
0
|
return thisPort; |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
169 |
0
|
public boolean isRunning ()... |
170 |
|
{ |
171 |
0
|
if (server == null) |
172 |
0
|
return false; |
173 |
|
else |
174 |
0
|
return true; |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
181 |
0
|
public void closeSocket ()... |
182 |
|
{ |
183 |
0
|
try { |
184 |
|
|
185 |
0
|
server.close(); |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
} catch(Exception e) { |
194 |
0
|
if (debugLevel > 0) |
195 |
0
|
debugOut.println(e); |
196 |
|
} |
197 |
|
|
198 |
0
|
server = null; |
199 |
|
} |
200 |
|
|
201 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
202 |
0
|
public void run()... |
203 |
|
{ |
204 |
0
|
try { |
205 |
|
|
206 |
|
|
207 |
0
|
server = new ServerSocket(thisPort); |
208 |
0
|
if (debugLevel > 0) |
209 |
0
|
debugOut.println("Started jProxy on port " + thisPort); |
210 |
|
|
211 |
0
|
while (true) |
212 |
|
{ |
213 |
0
|
Socket client = server.accept(); |
214 |
0
|
ProxyThread t = new ProxyThread(client, fwdServer, fwdPort); |
215 |
0
|
t.setDebug(debugLevel, debugOut); |
216 |
0
|
t.setTimeout(ptTimeout); |
217 |
0
|
t.start(); |
218 |
|
} |
219 |
|
} catch (Exception e) { |
220 |
0
|
if (debugLevel > 0) |
221 |
0
|
debugOut.println("jProxy Thread error: " + e); |
222 |
|
} |
223 |
|
|
224 |
0
|
closeSocket(); |
225 |
|
} |
226 |
|
|
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
|
|
| 0% |
Uncovered Elements: 200 (200) |
Complexity: 54 |
Complexity Density: 0.4 |
|
235 |
|
class ProxyThread extends Thread |
236 |
|
{ |
237 |
|
private Socket pSocket; |
238 |
|
private String fwdServer = ""; |
239 |
|
private int fwdPort = 0; |
240 |
|
private int debugLevel = 0; |
241 |
|
private PrintStream debugOut = System.out; |
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
public static final int DEFAULT_TIMEOUT = 20 * 1000; |
248 |
|
private int socketTimeout = DEFAULT_TIMEOUT; |
249 |
|
|
250 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
251 |
0
|
public ProxyThread(Socket s)... |
252 |
|
{ |
253 |
0
|
pSocket = s; |
254 |
|
} |
255 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
256 |
0
|
public ProxyThread(Socket s, String proxy, int port)... |
257 |
|
{ |
258 |
0
|
pSocket = s; |
259 |
0
|
fwdServer = proxy; |
260 |
0
|
fwdPort = port; |
261 |
|
} |
262 |
|
|
263 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
264 |
0
|
public void setTimeout (int timeout)... |
265 |
|
{ |
266 |
|
|
267 |
|
|
268 |
0
|
socketTimeout = timeout * 1000; |
269 |
|
} |
270 |
|
|
271 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
272 |
0
|
public void setDebug (int level, PrintStream out)... |
273 |
|
{ |
274 |
0
|
debugLevel = level; |
275 |
0
|
debugOut = out; |
276 |
|
} |
277 |
|
|
278 |
|
|
|
|
| 0% |
Uncovered Elements: 70 (70) |
Complexity: 13 |
Complexity Density: 0.24 |
|
279 |
0
|
public void run()... |
280 |
|
{ |
281 |
0
|
try |
282 |
|
{ |
283 |
0
|
long startTime = System.currentTimeMillis(); |
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
0
|
BufferedInputStream clientIn = new BufferedInputStream(pSocket.getInputStream()); |
289 |
0
|
BufferedOutputStream clientOut = new BufferedOutputStream(pSocket.getOutputStream()); |
290 |
|
|
291 |
|
|
292 |
0
|
Socket server = null; |
293 |
|
|
294 |
|
|
295 |
0
|
byte[] request = null; |
296 |
0
|
byte[] response = null; |
297 |
0
|
int requestLength = 0; |
298 |
0
|
int responseLength = 0; |
299 |
0
|
int pos = -1; |
300 |
0
|
StringBuffer host = new StringBuffer(""); |
301 |
0
|
String hostName = ""; |
302 |
0
|
int hostPort = 80; |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
0
|
request = getHTTPData(clientIn, host, false); |
308 |
0
|
requestLength = Array.getLength(request); |
309 |
|
|
310 |
|
|
311 |
|
|
312 |
0
|
hostName = host.toString(); |
313 |
0
|
pos = hostName.indexOf(":"); |
314 |
0
|
if (pos > 0) |
315 |
|
{ |
316 |
0
|
try { hostPort = Integer.parseInt(hostName.substring(pos + 1)); |
317 |
|
} catch (Exception e) { } |
318 |
0
|
hostName = hostName.substring(0, pos); |
319 |
|
} |
320 |
|
|
321 |
|
|
322 |
|
|
323 |
0
|
try |
324 |
|
{ |
325 |
0
|
if ((fwdServer.length() > 0) && (fwdPort > 0)) |
326 |
|
{ |
327 |
0
|
server = new Socket(fwdServer, fwdPort); |
328 |
|
} else { |
329 |
0
|
server = new Socket(hostName, hostPort); |
330 |
|
} |
331 |
|
} catch (Exception e) { |
332 |
|
|
333 |
0
|
String errMsg = "HTTP/1.0 500\nContent Type: text/plain\n\n" + |
334 |
|
"Error connecting to the server:\n" + e + "\n"; |
335 |
0
|
clientOut.write(errMsg.getBytes(), 0, errMsg.length()); |
336 |
|
} |
337 |
|
|
338 |
0
|
if (server != null) |
339 |
|
{ |
340 |
0
|
server.setSoTimeout(socketTimeout); |
341 |
0
|
BufferedInputStream serverIn = new BufferedInputStream(server.getInputStream()); |
342 |
0
|
BufferedOutputStream serverOut = new BufferedOutputStream(server.getOutputStream()); |
343 |
|
|
344 |
|
|
345 |
0
|
serverOut.write(request, 0, requestLength); |
346 |
0
|
serverOut.flush(); |
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
0
|
if (debugLevel > 1) |
357 |
|
{ |
358 |
0
|
response = getHTTPData(serverIn, true); |
359 |
0
|
responseLength = Array.getLength(response); |
360 |
|
} else { |
361 |
0
|
responseLength = streamHTTPData(serverIn, clientOut, true); |
362 |
|
} |
363 |
|
|
364 |
0
|
serverIn.close(); |
365 |
0
|
serverOut.close(); |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
0
|
if (debugLevel > 1) |
370 |
0
|
clientOut.write(response, 0, responseLength); |
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
0
|
if (debugLevel > 0) |
376 |
|
{ |
377 |
0
|
long endTime = System.currentTimeMillis(); |
378 |
0
|
debugOut.println("Request from " + pSocket.getInetAddress().getHostAddress() + |
379 |
|
" on Port " + pSocket.getLocalPort() + |
380 |
|
" to host " + hostName + ":" + hostPort + |
381 |
|
"\n (" + requestLength + " bytes sent, " + |
382 |
|
responseLength + " bytes returned, " + |
383 |
|
Long.toString(endTime - startTime) + " ms elapsed)"); |
384 |
0
|
debugOut.flush(); |
385 |
|
} |
386 |
0
|
if (debugLevel > 1) |
387 |
|
{ |
388 |
0
|
debugOut.println("REQUEST:\n" + (new String(request))); |
389 |
0
|
debugOut.println("RESPONSE:\n" + (new String(response))); |
390 |
0
|
debugOut.flush(); |
391 |
|
} |
392 |
|
|
393 |
|
|
394 |
0
|
clientOut.close(); |
395 |
0
|
clientIn.close(); |
396 |
0
|
pSocket.close(); |
397 |
|
} catch (Exception e) { |
398 |
0
|
if (debugLevel > 0) |
399 |
0
|
debugOut.println("Error in ProxyThread: " + e); |
400 |
|
|
401 |
|
} |
402 |
|
|
403 |
|
} |
404 |
|
|
405 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
406 |
0
|
private byte[] getHTTPData (InputStream in, boolean waitForDisconnect)... |
407 |
|
{ |
408 |
|
|
409 |
|
|
410 |
|
|
411 |
|
|
412 |
|
|
413 |
0
|
StringBuffer foo = new StringBuffer(""); |
414 |
0
|
return getHTTPData(in, foo, waitForDisconnect); |
415 |
|
} |
416 |
|
|
417 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
418 |
0
|
private byte[] getHTTPData (InputStream in, StringBuffer host, boolean waitForDisconnect)... |
419 |
|
{ |
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
|
425 |
0
|
ByteArrayOutputStream bs = new ByteArrayOutputStream(); |
426 |
0
|
streamHTTPData(in, bs, host, waitForDisconnect); |
427 |
0
|
return bs.toByteArray(); |
428 |
|
} |
429 |
|
|
430 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
431 |
0
|
private int streamHTTPData (InputStream in, OutputStream out, boolean waitForDisconnect)... |
432 |
|
{ |
433 |
0
|
StringBuffer foo = new StringBuffer(""); |
434 |
0
|
return streamHTTPData(in, out, foo, waitForDisconnect); |
435 |
|
} |
436 |
|
|
|
|
| 0% |
Uncovered Elements: 76 (76) |
Complexity: 24 |
Complexity Density: 0.48 |
|
437 |
0
|
private int streamHTTPData (InputStream in, OutputStream out, ... |
438 |
|
StringBuffer host, boolean waitForDisconnect) |
439 |
|
{ |
440 |
|
|
441 |
|
|
442 |
0
|
StringBuffer header = new StringBuffer(""); |
443 |
0
|
String data = ""; |
444 |
0
|
int responseCode = 200; |
445 |
0
|
int contentLength = 0; |
446 |
0
|
int pos = -1; |
447 |
0
|
int byteCount = 0; |
448 |
|
|
449 |
0
|
try |
450 |
|
{ |
451 |
|
|
452 |
0
|
data = readLine(in); |
453 |
0
|
if (data != null) |
454 |
|
{ |
455 |
0
|
header.append(data + "\r\n"); |
456 |
0
|
pos = data.indexOf(" "); |
457 |
0
|
if ((data.toLowerCase().startsWith("http")) && |
458 |
|
(pos >= 0) && (data.indexOf(" ", pos+1) >= 0)) |
459 |
|
{ |
460 |
0
|
String rcString = data.substring(pos+1, data.indexOf(" ", pos+1)); |
461 |
0
|
try |
462 |
|
{ |
463 |
0
|
responseCode = Integer.parseInt(rcString); |
464 |
|
} catch (Exception e) { |
465 |
0
|
if (debugLevel > 0) |
466 |
0
|
debugOut.println("Error parsing response code " + rcString); |
467 |
|
} |
468 |
|
} |
469 |
|
} |
470 |
|
|
471 |
|
|
472 |
0
|
while ((data = readLine(in)) != null) |
473 |
|
{ |
474 |
|
|
475 |
0
|
if (data.length() == 0) |
476 |
0
|
break; |
477 |
0
|
header.append(data + "\r\n"); |
478 |
|
|
479 |
|
|
480 |
0
|
pos = data.toLowerCase().indexOf("host:"); |
481 |
0
|
if (pos >= 0) |
482 |
|
{ |
483 |
0
|
host.setLength(0); |
484 |
0
|
host.append(data.substring(pos + 5).trim()); |
485 |
|
} |
486 |
|
|
487 |
|
|
488 |
0
|
pos = data.toLowerCase().indexOf("content-length:"); |
489 |
0
|
if (pos >= 0) |
490 |
0
|
contentLength = Integer.parseInt(data.substring(pos + 15).trim()); |
491 |
|
} |
492 |
|
|
493 |
|
|
494 |
0
|
header.append("\r\n"); |
495 |
|
|
496 |
|
|
497 |
0
|
out.write(header.toString().getBytes(), 0, header.length()); |
498 |
|
|
499 |
|
|
500 |
|
|
501 |
|
|
502 |
0
|
if ((responseCode != 200) && (contentLength == 0)) |
503 |
|
{ |
504 |
0
|
out.flush(); |
505 |
0
|
return header.length(); |
506 |
|
} |
507 |
|
|
508 |
|
|
509 |
|
|
510 |
|
|
511 |
|
|
512 |
0
|
if (contentLength > 0) |
513 |
0
|
waitForDisconnect = false; |
514 |
|
|
515 |
0
|
if ((contentLength > 0) || (waitForDisconnect)) |
516 |
|
{ |
517 |
0
|
try { |
518 |
0
|
byte[] buf = new byte[4096]; |
519 |
0
|
int bytesIn = 0; |
520 |
0
|
while ( ((byteCount < contentLength) || (waitForDisconnect)) |
521 |
|
&& ((bytesIn = in.read(buf)) >= 0) ) |
522 |
|
{ |
523 |
0
|
out.write(buf, 0, bytesIn); |
524 |
0
|
byteCount += bytesIn; |
525 |
|
} |
526 |
|
} catch (Exception e) { |
527 |
0
|
String errMsg = "Error getting HTTP body: " + e; |
528 |
0
|
if (debugLevel > 0) |
529 |
0
|
debugOut.println(errMsg); |
530 |
|
|
531 |
|
} |
532 |
|
} |
533 |
|
} catch (Exception e) { |
534 |
0
|
if (debugLevel > 0) |
535 |
0
|
debugOut.println("Error getting HTTP data: " + e); |
536 |
|
} |
537 |
|
|
538 |
|
|
539 |
0
|
try { out.flush(); } catch (Exception e) {} |
540 |
0
|
return (header.length() + byteCount); |
541 |
|
} |
542 |
|
|
543 |
|
|
|
|
| 0% |
Uncovered Elements: 30 (30) |
Complexity: 10 |
Complexity Density: 0.56 |
|
544 |
0
|
private String readLine (InputStream in)... |
545 |
|
{ |
546 |
|
|
547 |
0
|
StringBuffer data = new StringBuffer(""); |
548 |
0
|
int c; |
549 |
|
|
550 |
0
|
try |
551 |
|
{ |
552 |
|
|
553 |
0
|
in.mark(1); |
554 |
0
|
if (in.read() == -1) |
555 |
0
|
return null; |
556 |
|
else |
557 |
0
|
in.reset(); |
558 |
|
|
559 |
0
|
while ((c = in.read()) >= 0) |
560 |
|
{ |
561 |
|
|
562 |
0
|
if ((c == 0) || (c == 10) || (c == 13)) |
563 |
0
|
break; |
564 |
|
else |
565 |
0
|
data.append((char)c); |
566 |
|
} |
567 |
|
|
568 |
|
|
569 |
0
|
if (c == 13) |
570 |
|
{ |
571 |
0
|
in.mark(1); |
572 |
0
|
if (in.read() != 10) |
573 |
0
|
in.reset(); |
574 |
|
} |
575 |
|
} catch (Exception e) { |
576 |
0
|
if (debugLevel > 0) |
577 |
0
|
debugOut.println("Error getting header: " + e); |
578 |
|
} |
579 |
|
|
580 |
|
|
581 |
0
|
return data.toString(); |
582 |
|
} |
583 |
|
|
584 |
|
} |