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
53
54 Content-Type: multipart/x-mixed-replace;boundary=End
55 .....
56 --End
57 Content-Type: text/html
58
59 */
60 public class HttpReader {
61
62 ByteArray array = new ByteArray();
63 File to_file;
64 FileOutputStream file;
65 public final int CR = 13;
66 public final int LF = 10;
67 public final byte[] CRLF = {13, 10};
68 //public final String CRLFstr = new String(CRLF);
69 public final String CRLFstr = "\r\n";
70 public final int COUNT_CB = 0; // for testing only
71
72 protected HttpReader(String filename) throws FileNotFoundException {
73 to_file = new File(filename+".log");
74 file = new FileOutputStream(to_file);
75 }
76
77
78 protected String getReplyHeader(int contentLength) {
79 StringBuffer buf = new StringBuffer(512);
80 buf.append("HTTP/1.1 200 OK").append(CRLFstr);
81 //buf.append("Date: Fri, 31 Dec 1999 23:59:59 GMT").append(CRLFstr);
82 //buf.append("Expires: Tue, 31 Dec 1997 23:59:59 GMT").append(CRLFstr);
83 //buf.append("").append(CRLFstr);
84 buf.append("Server: HTTP xmlBlaster server/1.0").append(CRLFstr);
85 buf.append("Cache-Control: no-cache, no-store, must-revalidate").append(CRLFstr);
86 buf.append("Connection: Keep-alive").append(CRLFstr);
87 buf.append("Keep-Alive: 300000").append(CRLFstr);
88 buf.append("Expires: 0").append(CRLFstr);
89 //buf.append("Content-Type: multipart/x-mixed-replace;boundary=End").append(CRLFstr);
90 buf.append("Content-Type: application/octet-stream").append(CRLFstr);
91 buf.append("Content-Length: ").append(contentLength).append(CRLFstr);
92 //buf.append("Content-Length: 300000").append(CRLFstr);
93 //buf.append("Content-Type: text/plain").append(CRLFstr);
94 buf.append(CRLFstr);
95 return buf.toString();
96 }
97
98 protected String getPostHeader(String url, int contentLength) {
99 StringBuffer buf = new StringBuffer(512);
100 buf.append("POST ").append(url).append(" HTTP/1.1").append(CRLFstr);
101 buf.append("From: xx@yy.com").append(CRLFstr);
102 buf.append("User-Agent: HTTP xmlBlaster/1.0").append(CRLFstr);
103 buf.append("Cache-Control: no-cache, no-store, must-revalidate").append(CRLFstr);
104 buf.append("Expires: 0").append(CRLFstr);
105 buf.append("Connection: Keep-alive").append(CRLFstr);
106 buf.append("Keep-Alive: 30000000").append(CRLFstr);
107 //buf.append("Content-Type: application/octet-stream").append(CRLFstr);
108 buf.append("Content-Type: text/plain").append(CRLFstr);
109 buf.append("Content-Length: ").append(contentLength).append(CRLFstr);
110 buf.append(CRLFstr);
111 return buf.toString();
112 }
113
114 protected byte[] read(InputStream in) throws IOException {
115
116 int contentLength = 0;
117 int indexContent = 0;
118
119 byte curr;
120 int val;
121 int index = 0;
122 boolean isHttpHeader = false;
123
124 while (true) {
125 val = in.read();
126 index ++;
127 indexContent++;
128 /*
129 if (val == -1) {
130 System.out.println("Can't read bytes from socket, trying again");
131 continue;
132 }
133 */
134 /*
135 if (val == 0)
136 break;
137 */
138 if (val == -1)
139 throw new IOException("Can't read bytes from socket, socket closed");
140 if (index == 0 && val == 'G' || val == 'P') {
141 isHttpHeader = true;
142 System.out.println("Receiving HTTP request");
143 }
144
145 array.write(val);
146
147 //System.out.println("'" + val + "'");
148
149 if (val == CR) {
150 continue;
151 }
152 if (val == LF) {
153 String line = new String(array.toByteArray()).trim();
154 if (line.length() == 0) {
155 byte[] lenb = new byte[10];
156 int read = 0;
157 while (read < 10)
158 read += in.read(lenb, read, 10-read);
159 long len = Long.parseLong(new String(lenb).trim());
160 //System.out.println("*** Expecting raw data len=" + len);
161 byte[] data = new byte[(int)len];
162 System.arraycopy(lenb, 0, data, 0, 10);
163 //read=10 is already
164 while (read < len) {
165 read += in.read(data, read, (int)len-read);
166 }
167 //System.out.println("*** Data=\n'" + new String(data) + "'");
168 return data;
169 }
170
171 /*
172 if (val == LF) {
173 //System.out.println("Ignoring linefeed");
174 continue;
175 }
176
177 if (val == CR) {
178 String line = new String(array.toByteArray()).trim();
179 if (line.length() == 0) {
180 //System.out.println("*** Starting data section");
181 indexContent = 0;
182 byte[] data = new byte[contentLength];
183 if (contentLength == 0) {
184 //System.out.println("*** NO CONTENT");
185 return data;
186 }
187 val = in.read();
188 int offset = 0;
189 if (val != LF) {
190 System.out.println("*** MISSING LF");
191 offset++;
192 data[0] = (byte)val;
193 }
194 int read = 0;
195 while (read != contentLength) {
196 read += in.read(data, offset, contentLength);
197 }
198 String dataStr = new String(data);
199 //System.out.println("*** Data=\n'" + new String(data) + "'");
200 return data;
201 }
202 */
203 System.out.println(line);
204 file.write(line.getBytes());
205 // We ignore "HTTP/1.1 100 Continue"
206 if (line.startsWith("Content-Length:")) {
207 String tmp = line.substring(15).trim();
208 contentLength = Integer.parseInt(tmp);
209 //System.out.println("*** Content length = " + contentLength);
210 }
211 array.reset();
212 }
213 }
214 // if (file != null) file.close();
215 //return null;
216 }
217 }
syntax highlighted by Code2HTML, v. 0.9.1