-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathWebServer.java
More file actions
572 lines (512 loc) · 18 KB
/
WebServer.java
File metadata and controls
572 lines (512 loc) · 18 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package processing.app;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.*;
/**
* This code is placed here in anticipation of running the reference from an
* internal web server that reads the docs from a zip file, instead of using
* thousands of .html files on the disk, which is really inefficient.
* <p/>
* This is a very simple, multi-threaded HTTP server, originally based on
* <a href="http://j.mp/6BQwpI">this</a> article on java.sun.com.
*/
public class WebServer implements HttpConstants {
/* Where worker threads stand idle */
static Vector threads = new Vector();
/* the web server's virtual root */
//static File root;
/* timeout on client connections */
static int timeout = 10000;
/* max # worker threads */
static int workers = 5;
// static PrintStream log = System.out;
/*
static void loadProps() throws IOException {
File f = new File
(System.getProperty("java.home")+File.separator+
"lib"+File.separator+"www-server.properties");
if (f.exists()) {
InputStream is =new BufferedInputStream(new
FileInputStream(f));
props.load(is);
is.close();
String r = props.getProperty("root");
if (r != null) {
root = new File(r);
if (!root.exists()) {
throw new Error(root + " doesn't exist as server root");
}
}
r = props.getProperty("timeout");
if (r != null) {
timeout = Integer.parseInt(r);
}
r = props.getProperty("workers");
if (r != null) {
workers = Integer.parseInt(r);
}
r = props.getProperty("log");
if (r != null) {
p("opening log file: " + r);
log = new PrintStream(new BufferedOutputStream(
new FileOutputStream(r)));
}
}
// if no properties were specified, choose defaults
if (root == null) {
root = new File(System.getProperty("user.dir"));
}
if (timeout <= 1000) {
timeout = 5000;
}
if (workers < 25) {
workers = 5;
}
if (log == null) {
p("logging to stdout");
log = System.out;
}
}
static void printProps() {
p("root="+root);
p("timeout="+timeout);
p("workers="+workers);
}
*/
/* print to stdout */
// protected static void p(String s) {
// System.out.println(s);
// }
/* print to the log file */
protected static void log(String s) {
if (false) {
System.out.println(s);
}
// synchronized (log) {
// log.println(s);
// log.flush();
// }
}
//public static void main(String[] a) throws Exception {
static public int launch(String zipPath) throws IOException {
final ZipFile zip = new ZipFile(zipPath);
final Map<String, ZipEntry> entries = new HashMap<String, ZipEntry>();
Enumeration en = zip.entries();
while (en.hasMoreElements()) {
ZipEntry entry = (ZipEntry) en.nextElement();
entries.put(entry.getName(), entry);
}
// if (a.length > 0) {
// port = Integer.parseInt(a[0]);
// }
// loadProps();
// printProps();
// start worker threads
for (int i = 0; i < workers; ++i) {
WebServerWorker w = new WebServerWorker(zip, entries);
Thread t = new Thread(w, "Web Server Worker #" + i);
t.start();
threads.addElement(w);
}
final int port = 8080;
//SwingUtilities.invokeLater(new Runnable() {
Runnable r = new Runnable() {
public void run() {
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
Socket s = ss.accept();
WebServerWorker w = null;
synchronized (threads) {
if (threads.isEmpty()) {
WebServerWorker ws = new WebServerWorker(zip, entries);
ws.setSocket(s);
(new Thread(ws, "additional worker")).start();
} else {
w = (WebServerWorker) threads.elementAt(0);
threads.removeElementAt(0);
w.setSocket(s);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
new Thread(r).start();
// });
return port;
}
}
class WebServerWorker /*extends WebServer*/ implements HttpConstants, Runnable {
private final ZipFile zip;
private final Map<String, ZipEntry> entries;
final static int BUF_SIZE = 2048;
static final byte[] EOL = { (byte)'\r', (byte)'\n' };
/* buffer to use for requests */
byte[] buf;
/* Socket to client we're handling */
private Socket s;
WebServerWorker(ZipFile zip, Map<String, ZipEntry> entries) {
this.entries = entries;
this.zip = zip;
buf = new byte[BUF_SIZE];
s = null;
}
// Worker() {
// buf = new byte[BUF_SIZE];
// s = null;
// }
//
synchronized void setSocket(Socket s) {
this.s = s;
notify();
}
public synchronized void run() {
while(true) {
if (s == null) {
/* nothing to do */
try {
wait();
} catch (InterruptedException e) {
/* should not happen */
continue;
}
}
try {
handleClient();
} catch (Exception e) {
e.printStackTrace();
}
/* go back in wait queue if there's fewer
* than numHandler connections.
*/
s = null;
Vector pool = WebServer.threads;
synchronized (pool) {
if (pool.size() >= WebServer.workers) {
/* too many threads, exit this one */
return;
} else {
pool.addElement(this);
}
}
}
}
void handleClient() throws IOException {
InputStream is = new BufferedInputStream(s.getInputStream());
PrintStream ps = new PrintStream(s.getOutputStream());
// we will only block in read for this many milliseconds
// before we fail with java.io.InterruptedIOException,
// at which point we will abandon the connection.
s.setSoTimeout(WebServer.timeout);
s.setTcpNoDelay(true);
// zero out the buffer from last time
for (int i = 0; i < BUF_SIZE; i++) {
buf[i] = 0;
}
try {
// We only support HTTP GET/HEAD, and don't support any fancy HTTP
// options, so we're only interested really in the first line.
int nread = 0, r = 0;
outerloop:
while (nread < BUF_SIZE) {
r = is.read(buf, nread, BUF_SIZE - nread);
if (r == -1) {
return; // EOF
}
int i = nread;
nread += r;
for (; i < nread; i++) {
if (buf[i] == (byte)'\n' || buf[i] == (byte)'\r') {
break outerloop; // read one line
}
}
}
/* are we doing a GET or just a HEAD */
boolean doingGet;
/* beginning of file name */
int index;
if (buf[0] == (byte)'G' &&
buf[1] == (byte)'E' &&
buf[2] == (byte)'T' &&
buf[3] == (byte)' ') {
doingGet = true;
index = 4;
} else if (buf[0] == (byte)'H' &&
buf[1] == (byte)'E' &&
buf[2] == (byte)'A' &&
buf[3] == (byte)'D' &&
buf[4] == (byte)' ') {
doingGet = false;
index = 5;
} else {
/* we don't support this method */
ps.print("HTTP/1.0 " + HTTP_BAD_METHOD +
" unsupported method type: ");
ps.write(buf, 0, 5);
ps.write(EOL);
ps.flush();
s.close();
return;
}
int i = 0;
/* find the file name, from:
* GET /foo/bar.html HTTP/1.0
* extract "/foo/bar.html"
*/
for (i = index; i < nread; i++) {
if (buf[i] == (byte)' ') {
break;
}
}
String fname = new String(buf, index, i-index);
// get the zip entry, remove the front slash
ZipEntry entry = entries.get(fname.substring(1));
//System.out.println(fname + " " + entry);
boolean ok = printHeaders(entry, ps);
if (entry != null) {
InputStream stream = zip.getInputStream(entry);
if (doingGet && ok) {
sendFile(stream, ps);
}
} else {
send404(ps);
}
/*
String fname =
(new String(buf, 0, index, i-index)).replace('/', File.separatorChar);
if (fname.startsWith(File.separator)) {
fname = fname.substring(1);
}
File targ = new File(WebServer.root, fname);
if (targ.isDirectory()) {
File ind = new File(targ, "index.html");
if (ind.exists()) {
targ = ind;
}
}
boolean OK = printHeaders(targ, ps);
if (doingGet) {
if (OK) {
sendFile(targ, ps);
} else {
send404(targ, ps);
}
}
*/
} finally {
s.close();
}
}
boolean printHeaders(ZipEntry targ, PrintStream ps) throws IOException {
boolean ret = false;
int rCode = 0;
if (targ == null) {
rCode = HTTP_NOT_FOUND;
ps.print("HTTP/1.0 " + HTTP_NOT_FOUND + " Not Found");
ps.write(EOL);
ret = false;
} else {
rCode = HTTP_OK;
ps.print("HTTP/1.0 " + HTTP_OK + " OK");
ps.write(EOL);
ret = true;
}
if (targ != null) {
WebServer.log("From " +s.getInetAddress().getHostAddress()+": GET " + targ.getName()+" --> "+rCode);
}
ps.print("Server: Processing Documentation Server");
ps.write(EOL);
ps.print("Date: " + (new Date()));
ps.write(EOL);
if (ret) {
if (!targ.isDirectory()) {
ps.print("Content-length: " + targ.getSize());
ps.write(EOL);
ps.print("Last Modified: " + new Date(targ.getTime()));
ps.write(EOL);
String name = targ.getName();
int ind = name.lastIndexOf('.');
String ct = null;
if (ind > 0) {
ct = map.get(name.substring(ind));
}
if (ct == null) {
//System.err.println("unknown content type " + name.substring(ind));
ct = "application/x-unknown-content-type";
}
ps.print("Content-type: " + ct);
ps.write(EOL);
} else {
ps.print("Content-type: text/html");
ps.write(EOL);
}
}
ps.write(EOL); // adding another newline here [fry]
return ret;
}
boolean printHeaders(File targ, PrintStream ps) throws IOException {
boolean ret = false;
int rCode = 0;
if (!targ.exists()) {
rCode = HTTP_NOT_FOUND;
ps.print("HTTP/1.0 " + HTTP_NOT_FOUND + " Not Found");
ps.write(EOL);
ret = false;
} else {
rCode = HTTP_OK;
ps.print("HTTP/1.0 " + HTTP_OK+" OK");
ps.write(EOL);
ret = true;
}
WebServer.log("From " +s.getInetAddress().getHostAddress()+": GET " + targ.getAbsolutePath()+"-->"+rCode);
ps.print("Server: Simple java");
ps.write(EOL);
ps.print("Date: " + (new Date()));
ps.write(EOL);
if (ret) {
if (!targ.isDirectory()) {
ps.print("Content-length: " + targ.length());
ps.write(EOL);
ps.print("Last Modified: " + new Date(targ.lastModified()));
ps.write(EOL);
String name = targ.getName();
int ind = name.lastIndexOf('.');
String ct = null;
if (ind > 0) {
ct = map.get(name.substring(ind));
}
if (ct == null) {
ct = "unknown/unknown";
}
ps.print("Content-type: " + ct);
ps.write(EOL);
} else {
ps.print("Content-type: text/html");
ps.write(EOL);
}
}
return ret;
}
void send404(PrintStream ps) throws IOException {
ps.write(EOL);
ps.write(EOL);
ps.print("<html><body><h1>404 Not Found</h1>"+
"The requested resource was not found.</body></html>");
ps.write(EOL);
ps.write(EOL);
}
void sendFile(File targ, PrintStream ps) throws IOException {
InputStream is = null;
ps.write(EOL);
if (targ.isDirectory()) {
listDirectory(targ, ps);
return;
} else {
is = new FileInputStream(targ.getAbsolutePath());
}
sendFile(is, ps);
}
void sendFile(InputStream is, PrintStream ps) throws IOException {
try {
int n;
while ((n = is.read(buf)) > 0) {
ps.write(buf, 0, n);
}
} finally {
is.close();
}
}
/* mapping of file extensions to content-types */
static Map<String, String> map = new HashMap<String, String>();
static {
fillMap();
}
static void setSuffix(String k, String v) {
map.put(k, v);
}
static void fillMap() {
setSuffix("", "content/unknown");
setSuffix(".uu", "application/octet-stream");
setSuffix(".exe", "application/octet-stream");
setSuffix(".ps", "application/postscript");
setSuffix(".zip", "application/zip");
setSuffix(".sh", "application/x-shar");
setSuffix(".tar", "application/x-tar");
setSuffix(".snd", "audio/basic");
setSuffix(".au", "audio/basic");
setSuffix(".wav", "audio/x-wav");
setSuffix(".gif", "image/gif");
setSuffix(".jpg", "image/jpeg");
setSuffix(".jpeg", "image/jpeg");
setSuffix(".htm", "text/html");
setSuffix(".html", "text/html");
setSuffix(".css", "text/css");
setSuffix(".java", "text/javascript");
setSuffix(".txt", "text/plain");
setSuffix(".java", "text/plain");
setSuffix(".c", "text/plain");
setSuffix(".cc", "text/plain");
setSuffix(".c++", "text/plain");
setSuffix(".h", "text/plain");
setSuffix(".pl", "text/plain");
}
void listDirectory(File dir, PrintStream ps) throws IOException {
ps.println("<TITLE>Directory listing</TITLE><P>\n");
ps.println("<A HREF=\"..\">Parent Directory</A><BR>\n");
String[] list = dir.list();
for (int i = 0; list != null && i < list.length; i++) {
File f = new File(dir, list[i]);
if (f.isDirectory()) {
ps.println("<A HREF=\""+list[i]+"/\">"+list[i]+"/</A><BR>");
} else {
ps.println("<A HREF=\""+list[i]+"\">"+list[i]+"</A><BR");
}
}
ps.println("<P><HR><BR><I>" + (new Date()) + "</I>");
}
}
interface HttpConstants {
/** 2XX: generally "OK" */
public static final int HTTP_OK = 200;
public static final int HTTP_CREATED = 201;
public static final int HTTP_ACCEPTED = 202;
public static final int HTTP_NOT_AUTHORITATIVE = 203;
public static final int HTTP_NO_CONTENT = 204;
public static final int HTTP_RESET = 205;
public static final int HTTP_PARTIAL = 206;
/** 3XX: relocation/redirect */
public static final int HTTP_MULT_CHOICE = 300;
public static final int HTTP_MOVED_PERM = 301;
public static final int HTTP_MOVED_TEMP = 302;
public static final int HTTP_SEE_OTHER = 303;
public static final int HTTP_NOT_MODIFIED = 304;
public static final int HTTP_USE_PROXY = 305;
/** 4XX: client error */
public static final int HTTP_BAD_REQUEST = 400;
public static final int HTTP_UNAUTHORIZED = 401;
public static final int HTTP_PAYMENT_REQUIRED = 402;
public static final int HTTP_FORBIDDEN = 403;
public static final int HTTP_NOT_FOUND = 404;
public static final int HTTP_BAD_METHOD = 405;
public static final int HTTP_NOT_ACCEPTABLE = 406;
public static final int HTTP_PROXY_AUTH = 407;
public static final int HTTP_CLIENT_TIMEOUT = 408;
public static final int HTTP_CONFLICT = 409;
public static final int HTTP_GONE = 410;
public static final int HTTP_LENGTH_REQUIRED = 411;
public static final int HTTP_PRECON_FAILED = 412;
public static final int HTTP_ENTITY_TOO_LARGE = 413;
public static final int HTTP_REQ_TOO_LONG = 414;
public static final int HTTP_UNSUPPORTED_TYPE = 415;
/** 5XX: server error */
public static final int HTTP_SERVER_ERROR = 500;
public static final int HTTP_INTERNAL_ERROR = 501;
public static final int HTTP_BAD_GATEWAY = 502;
public static final int HTTP_UNAVAILABLE = 503;
public static final int HTTP_GATEWAY_TIMEOUT = 504;
public static final int HTTP_VERSION = 505;
}