1 import java.io.*;
2 import java.net.*;
3
4 /**
5 * tail -n 100 -f /var/squid/logs/access.log
6 * telnet develop 3128
7 * GET http://194.121.221.46:8080 HTTP/1.0
8 * GET http://192.168.1.2:8080 HTTP/1.0
9 *
10 'GET / HTTP/1.0' len=14
11 'Via: 1.0 develop.ruff.de:3128 (Squid/2.4.STABLE2)' len=49
12 'X-Forwarded-For: 192.168.1.2' len=28
13 'Host: 192.168.1.2:8080' len=22
14 'Cache-Control: max-age=259200' len=29
15 'Connection: keep-alive' len=22
16
17 POST http://192.168.1.2:8080 HTTP/1.0
18 From: xx@yy.com
19 User-Agent: HTTPxmlBlaster/1.0
20 Content-Type: application/x-www-form-urlencoded
21 Content-Length: 32
22
23 home=Cosby&favorite+flavor=flies
24
25
26 POST http://192.168.1.2:8080 HTTP/1.1
27 From: xx@yy.com
28 User-Agent: HTTPxmlBlaster/1.0
29 Content-Type: application/x-www-form-urlencoded
30 Content-Length: 32
31
32 home=Cosby&favorite+flavor=flies
33
34
35 * Response:
36
37 HTTP/1.0 200 OK
38 Date: Fri, 31 Dec 1999 23:59:59 GMT
39 Content-Type: text/html
40 Content-Length: 1354
41
42 <html>
43 <body>
44 <h1>Happy New Millennium!</h1>
45 (more file contents)
46 .
47 .
48 .
49 </body>
50 </html>
51 */
52 public class EchoServer extends HttpReader {
53 ServerSocket listen = null;
54 Socket client = null;
55 PrintWriter out = null;
56 BufferedInputStream in = null;
57 ByteArray array = new ByteArray();
58 int contentLength = 0;
59 int indexContent = 0;
60
61 public EchoServer(int port) throws Exception {
62 super("EchoServer");
63
64 try {
65 listen = new ServerSocket(port);
66 while (true) {
67 try {
68 System.out.println("Waiting on port " + port + " for somebody who wants to talk to me [" + new java.util.Date().toString() + "]");
69 Socket client = listen.accept();
70 client.setTcpNoDelay(true);
71 client.setSoLinger(true, 2000);
72 //client.setSoTimeout(1000*60*);
73 client.setKeepAlive(true); // JDK 1.3
74 System.out.println("\nClient accepted!");
75 out = new PrintWriter(client.getOutputStream(), true);
76 in = new BufferedInputStream(client.getInputStream());
77
78 byte curr;
79 int val;
80 int index = 0;
81 boolean isHttpHeader = false;
82
83 for (int ii=0; ii<1000000; ii++) {
84 System.out.println("\n*** Waiting for data " + ii + " [" + new java.util.Date().toString() + "]");
85 byte[] data = read(in);
86 System.out.println("*** Received [" + new java.util.Date().toString() + "]" + ": '" + new String(data) + "'");
87
88 String resp = new String(data);
89 String header = getReplyHeader(resp.length());
90 System.out.println("\n*** Sending reply\n" + header + resp);
91 out.print(header);
92 out.print(resp);
93 out.flush();
94
95 for (int jj=0; jj<COUNT_CB; jj++) {
96 //try { Thread.currentThread().sleep(500); } catch(Exception e) { }
97 String asynchCallback = " 32<callback>" + jj%10 + "</callback>";
98 header = getReplyHeader(asynchCallback.length());
99 //header = "";
100 System.out.println("\n*** Sending callback " + (ii+1)*jj + "\n" + header + new String(asynchCallback));
101 out.print(header);
102 out.print(asynchCallback);
103 out.flush();
104 }
105 //try { Thread.currentThread().sleep(500); } catch(Exception e) { }
106 }
107 }
108 catch (IOException e) {
109 System.out.println("Problem with Client: " + e.toString());
110 }
111 finally {
112 if (out != null) out.close();
113 if (in != null) in.close();
114 if (client != null) client.close();
115 System.out.println("################ Client removed!\n");
116 }
117 }
118 }
119 catch (NumberFormatException e) {
120 System.err.println("The message is corrupted, can't parse content length: " + e.toString());
121 }
122 catch (Throwable e) {
123 System.out.println("ERROR: " + e.toString());
124 e.printStackTrace();
125 }
126 finally {
127 if (listen!=null) listen.close();
128 System.out.println("Listen Socket closed!\n");
129 }
130 }
131
132
133 public static void main(String[] args) throws IOException {
134 int port = 8080;
135 try {
136 if (args.length > 0)
137 port = Integer.parseInt(args[0]);
138
139 new EchoServer(port);
140 }
141 catch (Throwable e) {
142 System.out.println("STARTUP ERROR: " + e.toString());
143 e.printStackTrace();
144 }
145 }
146 }
syntax highlighted by Code2HTML, v. 0.9.1