2
|
1 |
/*
|
|
2 |
* Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.net.httpserver;
|
|
27 |
|
|
28 |
import java.net.*;
|
|
29 |
import java.io.*;
|
|
30 |
import java.nio.*;
|
|
31 |
import java.security.*;
|
|
32 |
import java.nio.channels.*;
|
|
33 |
import java.util.*;
|
|
34 |
import java.util.concurrent.*;
|
|
35 |
import java.util.logging.Logger;
|
|
36 |
import java.util.logging.Level;
|
|
37 |
import javax.net.ssl.*;
|
|
38 |
import com.sun.net.httpserver.*;
|
|
39 |
import com.sun.net.httpserver.spi.*;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Provides implementation for both HTTP and HTTPS
|
|
43 |
*/
|
|
44 |
class ServerImpl implements TimeSource {
|
|
45 |
|
|
46 |
private String protocol;
|
|
47 |
private boolean https;
|
|
48 |
private Executor executor;
|
|
49 |
private HttpsConfigurator httpsConfig;
|
|
50 |
private SSLContext sslContext;
|
|
51 |
private ContextList contexts;
|
|
52 |
private InetSocketAddress address;
|
|
53 |
private ServerSocketChannel schan;
|
|
54 |
private Selector selector;
|
|
55 |
private SelectionKey listenerKey;
|
|
56 |
private Set<HttpConnection> idleConnections;
|
|
57 |
private Set<HttpConnection> allConnections;
|
|
58 |
private List<Event> events;
|
|
59 |
private Object lolock = new Object();
|
|
60 |
private volatile boolean finished = false;
|
|
61 |
private volatile boolean terminating = false;
|
|
62 |
private boolean bound = false;
|
|
63 |
private boolean started = false;
|
|
64 |
private volatile long time; /* current time */
|
|
65 |
private volatile long ticks; /* number of clock ticks since server started */
|
|
66 |
private HttpServer wrapper;
|
|
67 |
|
|
68 |
final static int CLOCK_TICK = ServerConfig.getClockTick();
|
|
69 |
final static long IDLE_INTERVAL = ServerConfig.getIdleInterval();
|
|
70 |
final static int MAX_IDLE_CONNECTIONS = ServerConfig.getMaxIdleConnections();
|
|
71 |
|
|
72 |
private Timer timer;
|
|
73 |
private Logger logger;
|
|
74 |
|
|
75 |
ServerImpl (
|
|
76 |
HttpServer wrapper, String protocol, InetSocketAddress addr, int backlog
|
|
77 |
) throws IOException {
|
|
78 |
|
|
79 |
this.protocol = protocol;
|
|
80 |
this.wrapper = wrapper;
|
|
81 |
this.logger = Logger.getLogger ("com.sun.net.httpserver");
|
|
82 |
https = protocol.equalsIgnoreCase ("https");
|
|
83 |
this.address = addr;
|
|
84 |
contexts = new ContextList();
|
|
85 |
schan = ServerSocketChannel.open();
|
|
86 |
if (addr != null) {
|
|
87 |
ServerSocket socket = schan.socket();
|
|
88 |
socket.bind (addr, backlog);
|
|
89 |
bound = true;
|
|
90 |
}
|
|
91 |
selector = Selector.open ();
|
|
92 |
schan.configureBlocking (false);
|
|
93 |
listenerKey = schan.register (selector, SelectionKey.OP_ACCEPT);
|
|
94 |
dispatcher = new Dispatcher();
|
|
95 |
idleConnections = Collections.synchronizedSet (new HashSet<HttpConnection>());
|
|
96 |
allConnections = Collections.synchronizedSet (new HashSet<HttpConnection>());
|
|
97 |
time = System.currentTimeMillis();
|
|
98 |
timer = new Timer ("server-timer", true);
|
|
99 |
timer.schedule (new ServerTimerTask(), CLOCK_TICK, CLOCK_TICK);
|
|
100 |
events = new LinkedList<Event>();
|
|
101 |
logger.config ("HttpServer created "+protocol+" "+ addr);
|
|
102 |
}
|
|
103 |
|
|
104 |
public void bind (InetSocketAddress addr, int backlog) throws IOException {
|
|
105 |
if (bound) {
|
|
106 |
throw new BindException ("HttpServer already bound");
|
|
107 |
}
|
|
108 |
if (addr == null) {
|
|
109 |
throw new NullPointerException ("null address");
|
|
110 |
}
|
|
111 |
ServerSocket socket = schan.socket();
|
|
112 |
socket.bind (addr, backlog);
|
|
113 |
bound = true;
|
|
114 |
}
|
|
115 |
|
|
116 |
public void start () {
|
|
117 |
if (!bound || started || finished) {
|
|
118 |
throw new IllegalStateException ("server in wrong state");
|
|
119 |
}
|
|
120 |
if (executor == null) {
|
|
121 |
executor = new DefaultExecutor();
|
|
122 |
}
|
|
123 |
started = true;
|
|
124 |
final Dispatcher d = dispatcher;
|
|
125 |
Thread t = AccessController.doPrivileged(new PrivilegedAction<Thread>() {
|
|
126 |
public Thread run() {
|
|
127 |
Thread t = new Thread (d);
|
|
128 |
return t;
|
|
129 |
}
|
|
130 |
});
|
|
131 |
t.start();
|
|
132 |
}
|
|
133 |
|
|
134 |
public void setExecutor (Executor executor) {
|
|
135 |
if (started) {
|
|
136 |
throw new IllegalStateException ("server already started");
|
|
137 |
}
|
|
138 |
this.executor = executor;
|
|
139 |
}
|
|
140 |
|
|
141 |
private static class DefaultExecutor implements Executor {
|
|
142 |
public void execute (Runnable task) {
|
|
143 |
task.run();
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
public Executor getExecutor () {
|
|
148 |
return executor;
|
|
149 |
}
|
|
150 |
|
|
151 |
public void setHttpsConfigurator (HttpsConfigurator config) {
|
|
152 |
if (config == null) {
|
|
153 |
throw new NullPointerException ("null HttpsConfigurator");
|
|
154 |
}
|
|
155 |
if (started) {
|
|
156 |
throw new IllegalStateException ("server already started");
|
|
157 |
}
|
|
158 |
this.httpsConfig = config;
|
|
159 |
sslContext = config.getSSLContext();
|
|
160 |
}
|
|
161 |
|
|
162 |
public HttpsConfigurator getHttpsConfigurator () {
|
|
163 |
return httpsConfig;
|
|
164 |
}
|
|
165 |
|
|
166 |
public void stop (int delay) {
|
|
167 |
if (delay < 0) {
|
|
168 |
throw new IllegalArgumentException ("negative delay parameter");
|
|
169 |
}
|
|
170 |
terminating = true;
|
|
171 |
try { schan.close(); } catch (IOException e) {}
|
|
172 |
selector.wakeup();
|
|
173 |
long latest = System.currentTimeMillis() + delay * 1000;
|
|
174 |
while (System.currentTimeMillis() < latest) {
|
|
175 |
delay();
|
|
176 |
if (finished) {
|
|
177 |
break;
|
|
178 |
}
|
|
179 |
}
|
|
180 |
finished = true;
|
|
181 |
selector.wakeup();
|
|
182 |
synchronized (allConnections) {
|
|
183 |
for (HttpConnection c : allConnections) {
|
|
184 |
c.close();
|
|
185 |
}
|
|
186 |
}
|
|
187 |
allConnections.clear();
|
|
188 |
idleConnections.clear();
|
|
189 |
timer.cancel();
|
|
190 |
}
|
|
191 |
|
|
192 |
Dispatcher dispatcher;
|
|
193 |
|
|
194 |
public synchronized HttpContextImpl createContext (String path, HttpHandler handler) {
|
|
195 |
if (handler == null || path == null) {
|
|
196 |
throw new NullPointerException ("null handler, or path parameter");
|
|
197 |
}
|
|
198 |
HttpContextImpl context = new HttpContextImpl (protocol, path, handler, this);
|
|
199 |
contexts.add (context);
|
|
200 |
logger.config ("context created: " + path);
|
|
201 |
return context;
|
|
202 |
}
|
|
203 |
|
|
204 |
public synchronized HttpContextImpl createContext (String path) {
|
|
205 |
if (path == null) {
|
|
206 |
throw new NullPointerException ("null path parameter");
|
|
207 |
}
|
|
208 |
HttpContextImpl context = new HttpContextImpl (protocol, path, null, this);
|
|
209 |
contexts.add (context);
|
|
210 |
logger.config ("context created: " + path);
|
|
211 |
return context;
|
|
212 |
}
|
|
213 |
|
|
214 |
public synchronized void removeContext (String path) throws IllegalArgumentException {
|
|
215 |
if (path == null) {
|
|
216 |
throw new NullPointerException ("null path parameter");
|
|
217 |
}
|
|
218 |
contexts.remove (protocol, path);
|
|
219 |
logger.config ("context removed: " + path);
|
|
220 |
}
|
|
221 |
|
|
222 |
public synchronized void removeContext (HttpContext context) throws IllegalArgumentException {
|
|
223 |
if (!(context instanceof HttpContextImpl)) {
|
|
224 |
throw new IllegalArgumentException ("wrong HttpContext type");
|
|
225 |
}
|
|
226 |
contexts.remove ((HttpContextImpl)context);
|
|
227 |
logger.config ("context removed: " + context.getPath());
|
|
228 |
}
|
|
229 |
|
|
230 |
public InetSocketAddress getAddress() {
|
|
231 |
return (InetSocketAddress)schan.socket().getLocalSocketAddress();
|
|
232 |
}
|
|
233 |
|
|
234 |
Selector getSelector () {
|
|
235 |
return selector;
|
|
236 |
}
|
|
237 |
|
|
238 |
void addEvent (Event r) {
|
|
239 |
synchronized (lolock) {
|
|
240 |
events.add (r);
|
|
241 |
selector.wakeup();
|
|
242 |
}
|
|
243 |
}
|
|
244 |
|
|
245 |
int resultSize () {
|
|
246 |
synchronized (lolock) {
|
|
247 |
return events.size ();
|
|
248 |
}
|
|
249 |
}
|
|
250 |
|
|
251 |
|
|
252 |
/* main server listener task */
|
|
253 |
|
|
254 |
class Dispatcher implements Runnable {
|
|
255 |
|
|
256 |
private void handleEvent (Event r) {
|
|
257 |
ExchangeImpl t = r.exchange;
|
|
258 |
HttpConnection c = t.getConnection();
|
|
259 |
try {
|
|
260 |
if (r instanceof WriteFinishedEvent) {
|
|
261 |
|
|
262 |
int exchanges = endExchange();
|
|
263 |
if (terminating && exchanges == 0) {
|
|
264 |
finished = true;
|
|
265 |
}
|
|
266 |
SocketChannel chan = c.getChannel();
|
|
267 |
LeftOverInputStream is = t.getOriginalInputStream();
|
|
268 |
if (!is.isEOF()) {
|
|
269 |
t.close = true;
|
|
270 |
}
|
|
271 |
if (t.close || idleConnections.size() >= MAX_IDLE_CONNECTIONS) {
|
|
272 |
c.close();
|
|
273 |
allConnections.remove (c);
|
|
274 |
} else {
|
|
275 |
if (is.isDataBuffered()) {
|
|
276 |
/* don't re-enable the interestops, just handle it */
|
|
277 |
handle (c.getChannel(), c);
|
|
278 |
} else {
|
|
279 |
/* re-enable interestops */
|
|
280 |
SelectionKey key = c.getSelectionKey();
|
|
281 |
if (key.isValid()) {
|
|
282 |
key.interestOps (
|
|
283 |
key.interestOps()|SelectionKey.OP_READ
|
|
284 |
);
|
|
285 |
}
|
|
286 |
c.time = getTime() + IDLE_INTERVAL;
|
|
287 |
idleConnections.add (c);
|
|
288 |
}
|
|
289 |
}
|
|
290 |
}
|
|
291 |
} catch (IOException e) {
|
|
292 |
logger.log (
|
|
293 |
Level.FINER, "Dispatcher (1)", e
|
|
294 |
);
|
|
295 |
c.close();
|
|
296 |
}
|
|
297 |
}
|
|
298 |
|
|
299 |
public void run() {
|
|
300 |
while (!finished) {
|
|
301 |
try {
|
|
302 |
|
|
303 |
/* process the events list first */
|
|
304 |
|
|
305 |
while (resultSize() > 0) {
|
|
306 |
Event r;
|
|
307 |
synchronized (lolock) {
|
|
308 |
r = events.remove(0);
|
|
309 |
handleEvent (r);
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
selector.select(1000);
|
|
314 |
|
|
315 |
/* process the selected list now */
|
|
316 |
|
|
317 |
Set<SelectionKey> selected = selector.selectedKeys();
|
|
318 |
Iterator<SelectionKey> iter = selected.iterator();
|
|
319 |
while (iter.hasNext()) {
|
|
320 |
SelectionKey key = iter.next();
|
|
321 |
iter.remove ();
|
|
322 |
if (key.equals (listenerKey)) {
|
|
323 |
if (terminating) {
|
|
324 |
continue;
|
|
325 |
}
|
|
326 |
SocketChannel chan = schan.accept();
|
|
327 |
if (chan == null) {
|
|
328 |
continue; /* cancel something ? */
|
|
329 |
}
|
|
330 |
chan.configureBlocking (false);
|
|
331 |
SelectionKey newkey = chan.register (selector, SelectionKey.OP_READ);
|
|
332 |
HttpConnection c = new HttpConnection ();
|
|
333 |
c.selectionKey = newkey;
|
|
334 |
c.setChannel (chan);
|
|
335 |
newkey.attach (c);
|
|
336 |
allConnections.add (c);
|
|
337 |
} else {
|
|
338 |
try {
|
|
339 |
if (key.isReadable()) {
|
|
340 |
boolean closed;
|
|
341 |
SocketChannel chan = (SocketChannel)key.channel();
|
|
342 |
HttpConnection conn = (HttpConnection)key.attachment();
|
|
343 |
// interestOps will be restored at end of read
|
|
344 |
key.interestOps (0);
|
|
345 |
handle (chan, conn);
|
|
346 |
} else {
|
|
347 |
assert false;
|
|
348 |
}
|
|
349 |
} catch (IOException e) {
|
|
350 |
HttpConnection conn = (HttpConnection)key.attachment();
|
|
351 |
logger.log (
|
|
352 |
Level.FINER, "Dispatcher (2)", e
|
|
353 |
);
|
|
354 |
conn.close();
|
|
355 |
}
|
|
356 |
}
|
|
357 |
}
|
|
358 |
} catch (CancelledKeyException e) {
|
|
359 |
logger.log (Level.FINER, "Dispatcher (3)", e);
|
|
360 |
} catch (IOException e) {
|
|
361 |
logger.log (Level.FINER, "Dispatcher (4)", e);
|
|
362 |
}
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
public void handle (SocketChannel chan, HttpConnection conn)
|
|
367 |
throws IOException
|
|
368 |
{
|
|
369 |
try {
|
|
370 |
Exchange t = new Exchange (chan, protocol, conn);
|
|
371 |
executor.execute (t);
|
|
372 |
} catch (HttpError e1) {
|
|
373 |
logger.log (Level.FINER, "Dispatcher (5)", e1);
|
|
374 |
conn.close();
|
|
375 |
} catch (IOException e) {
|
|
376 |
logger.log (Level.FINER, "Dispatcher (6)", e);
|
|
377 |
conn.close();
|
|
378 |
}
|
|
379 |
}
|
|
380 |
}
|
|
381 |
|
|
382 |
static boolean debug = ServerConfig.debugEnabled ();
|
|
383 |
|
|
384 |
static synchronized void dprint (String s) {
|
|
385 |
if (debug) {
|
|
386 |
System.out.println (s);
|
|
387 |
}
|
|
388 |
}
|
|
389 |
|
|
390 |
static synchronized void dprint (Exception e) {
|
|
391 |
if (debug) {
|
|
392 |
System.out.println (e);
|
|
393 |
e.printStackTrace();
|
|
394 |
}
|
|
395 |
}
|
|
396 |
|
|
397 |
Logger getLogger () {
|
|
398 |
return logger;
|
|
399 |
}
|
|
400 |
|
|
401 |
/* per exchange task */
|
|
402 |
|
|
403 |
class Exchange implements Runnable {
|
|
404 |
SocketChannel chan;
|
|
405 |
HttpConnection connection;
|
|
406 |
HttpContextImpl context;
|
|
407 |
InputStream rawin;
|
|
408 |
OutputStream rawout;
|
|
409 |
String protocol;
|
|
410 |
ExchangeImpl tx;
|
|
411 |
HttpContextImpl ctx;
|
|
412 |
boolean rejected = false;
|
|
413 |
|
|
414 |
Exchange (SocketChannel chan, String protocol, HttpConnection conn) throws IOException {
|
|
415 |
this.chan = chan;
|
|
416 |
this.connection = conn;
|
|
417 |
this.protocol = protocol;
|
|
418 |
}
|
|
419 |
|
|
420 |
public void run () {
|
|
421 |
/* context will be null for new connections */
|
|
422 |
context = connection.getHttpContext();
|
|
423 |
boolean newconnection;
|
|
424 |
SSLEngine engine = null;
|
|
425 |
String requestLine = null;
|
|
426 |
SSLStreams sslStreams = null;
|
|
427 |
try {
|
|
428 |
if (context != null ) {
|
|
429 |
this.rawin = connection.getInputStream();
|
|
430 |
this.rawout = connection.getRawOutputStream();
|
|
431 |
newconnection = false;
|
|
432 |
} else {
|
|
433 |
/* figure out what kind of connection this is */
|
|
434 |
newconnection = true;
|
|
435 |
if (https) {
|
|
436 |
if (sslContext == null) {
|
|
437 |
logger.warning ("SSL connection received. No https contxt created");
|
|
438 |
throw new HttpError ("No SSL context established");
|
|
439 |
}
|
|
440 |
sslStreams = new SSLStreams (ServerImpl.this, sslContext, chan);
|
|
441 |
rawin = sslStreams.getInputStream();
|
|
442 |
rawout = sslStreams.getOutputStream();
|
|
443 |
engine = sslStreams.getSSLEngine();
|
|
444 |
} else {
|
|
445 |
rawin = new BufferedInputStream(
|
|
446 |
new Request.ReadStream (
|
|
447 |
ServerImpl.this, chan
|
|
448 |
));
|
|
449 |
rawout = new Request.WriteStream (
|
|
450 |
ServerImpl.this, chan
|
|
451 |
);
|
|
452 |
}
|
|
453 |
}
|
|
454 |
Request req = new Request (rawin, rawout);
|
|
455 |
requestLine = req.requestLine();
|
|
456 |
if (requestLine == null) {
|
|
457 |
/* connection closed */
|
|
458 |
connection.close();
|
|
459 |
return;
|
|
460 |
}
|
|
461 |
int space = requestLine.indexOf (' ');
|
|
462 |
if (space == -1) {
|
|
463 |
reject (Code.HTTP_BAD_REQUEST,
|
|
464 |
requestLine, "Bad request line");
|
|
465 |
return;
|
|
466 |
}
|
|
467 |
String method = requestLine.substring (0, space);
|
|
468 |
int start = space+1;
|
|
469 |
space = requestLine.indexOf(' ', start);
|
|
470 |
if (space == -1) {
|
|
471 |
reject (Code.HTTP_BAD_REQUEST,
|
|
472 |
requestLine, "Bad request line");
|
|
473 |
return;
|
|
474 |
}
|
|
475 |
String uriStr = requestLine.substring (start, space);
|
|
476 |
URI uri = new URI (uriStr);
|
|
477 |
start = space+1;
|
|
478 |
String version = requestLine.substring (start);
|
|
479 |
Headers headers = req.headers();
|
|
480 |
String s = headers.getFirst ("Transfer-encoding");
|
|
481 |
int clen = 0;
|
|
482 |
if (s !=null && s.equalsIgnoreCase ("chunked")) {
|
|
483 |
clen = -1;
|
|
484 |
} else {
|
|
485 |
s = headers.getFirst ("Content-Length");
|
|
486 |
if (s != null) {
|
|
487 |
clen = Integer.parseInt (s);
|
|
488 |
}
|
|
489 |
}
|
|
490 |
ctx = contexts.findContext (protocol, uri.getPath());
|
|
491 |
if (ctx == null) {
|
|
492 |
reject (Code.HTTP_NOT_FOUND,
|
|
493 |
requestLine, "No context found for request");
|
|
494 |
return;
|
|
495 |
}
|
|
496 |
connection.setContext (ctx);
|
|
497 |
if (ctx.getHandler() == null) {
|
|
498 |
reject (Code.HTTP_INTERNAL_ERROR,
|
|
499 |
requestLine, "No handler for context");
|
|
500 |
return;
|
|
501 |
}
|
|
502 |
tx = new ExchangeImpl (
|
|
503 |
method, uri, req, clen, connection
|
|
504 |
);
|
|
505 |
String chdr = headers.getFirst("Connection");
|
|
506 |
Headers rheaders = tx.getResponseHeaders();
|
|
507 |
|
|
508 |
if (chdr != null && chdr.equalsIgnoreCase ("close")) {
|
|
509 |
tx.close = true;
|
|
510 |
}
|
|
511 |
if (version.equalsIgnoreCase ("http/1.0")) {
|
|
512 |
tx.http10 = true;
|
|
513 |
if (chdr == null) {
|
|
514 |
tx.close = true;
|
|
515 |
rheaders.set ("Connection", "close");
|
|
516 |
} else if (chdr.equalsIgnoreCase ("keep-alive")) {
|
|
517 |
rheaders.set ("Connection", "keep-alive");
|
|
518 |
int idle=(int)ServerConfig.getIdleInterval()/1000;
|
|
519 |
int max=(int)ServerConfig.getMaxIdleConnections();
|
|
520 |
String val = "timeout="+idle+", max="+max;
|
|
521 |
rheaders.set ("Keep-Alive", val);
|
|
522 |
}
|
|
523 |
}
|
|
524 |
|
|
525 |
if (newconnection) {
|
|
526 |
connection.setParameters (
|
|
527 |
rawin, rawout, chan, engine, sslStreams,
|
|
528 |
sslContext, protocol, ctx, rawin
|
|
529 |
);
|
|
530 |
}
|
|
531 |
/* check if client sent an Expect 100 Continue.
|
|
532 |
* In that case, need to send an interim response.
|
|
533 |
* In future API may be modified to allow app to
|
|
534 |
* be involved in this process.
|
|
535 |
*/
|
|
536 |
String exp = headers.getFirst("Expect");
|
|
537 |
if (exp != null && exp.equalsIgnoreCase ("100-continue")) {
|
|
538 |
logReply (100, requestLine, null);
|
|
539 |
sendReply (
|
|
540 |
Code.HTTP_CONTINUE, false, null
|
|
541 |
);
|
|
542 |
}
|
|
543 |
/* uf is the list of filters seen/set by the user.
|
|
544 |
* sf is the list of filters established internally
|
|
545 |
* and which are not visible to the user. uc and sc
|
|
546 |
* are the corresponding Filter.Chains.
|
|
547 |
* They are linked together by a LinkHandler
|
|
548 |
* so that they can both be invoked in one call.
|
|
549 |
*/
|
|
550 |
List<Filter> sf = ctx.getSystemFilters();
|
|
551 |
List<Filter> uf = ctx.getFilters();
|
|
552 |
|
|
553 |
Filter.Chain sc = new Filter.Chain(sf, ctx.getHandler());
|
|
554 |
Filter.Chain uc = new Filter.Chain(uf, new LinkHandler (sc));
|
|
555 |
|
|
556 |
/* set up the two stream references */
|
|
557 |
tx.getRequestBody();
|
|
558 |
tx.getResponseBody();
|
|
559 |
if (https) {
|
|
560 |
uc.doFilter (new HttpsExchangeImpl (tx));
|
|
561 |
} else {
|
|
562 |
uc.doFilter (new HttpExchangeImpl (tx));
|
|
563 |
}
|
|
564 |
|
|
565 |
} catch (IOException e1) {
|
|
566 |
logger.log (Level.FINER, "ServerImpl.Exchange (1)", e1);
|
|
567 |
connection.close();
|
|
568 |
} catch (NumberFormatException e3) {
|
|
569 |
reject (Code.HTTP_BAD_REQUEST,
|
|
570 |
requestLine, "NumberFormatException thrown");
|
|
571 |
} catch (URISyntaxException e) {
|
|
572 |
reject (Code.HTTP_BAD_REQUEST,
|
|
573 |
requestLine, "URISyntaxException thrown");
|
|
574 |
} catch (Exception e4) {
|
|
575 |
logger.log (Level.FINER, "ServerImpl.Exchange (2)", e4);
|
|
576 |
connection.close();
|
|
577 |
}
|
|
578 |
}
|
|
579 |
|
|
580 |
/* used to link to 2 or more Filter.Chains together */
|
|
581 |
|
|
582 |
class LinkHandler implements HttpHandler {
|
|
583 |
Filter.Chain nextChain;
|
|
584 |
|
|
585 |
LinkHandler (Filter.Chain nextChain) {
|
|
586 |
this.nextChain = nextChain;
|
|
587 |
}
|
|
588 |
|
|
589 |
public void handle (HttpExchange exchange) throws IOException {
|
|
590 |
nextChain.doFilter (exchange);
|
|
591 |
}
|
|
592 |
}
|
|
593 |
|
|
594 |
void reject (int code, String requestStr, String message) {
|
|
595 |
rejected = true;
|
|
596 |
logReply (code, requestStr, message);
|
|
597 |
sendReply (
|
|
598 |
code, true, "<h1>"+code+Code.msg(code)+"</h1>"+message
|
|
599 |
);
|
|
600 |
}
|
|
601 |
|
|
602 |
void sendReply (
|
|
603 |
int code, boolean closeNow, String text)
|
|
604 |
{
|
|
605 |
try {
|
|
606 |
String s = "HTTP/1.1 " + code + Code.msg(code) + "\r\n";
|
|
607 |
if (text != null && text.length() != 0) {
|
|
608 |
s = s + "Content-Length: "+text.length()+"\r\n";
|
|
609 |
s = s + "Content-Type: text/html\r\n";
|
|
610 |
} else {
|
|
611 |
s = s + "Content-Length: 0\r\n";
|
|
612 |
text = "";
|
|
613 |
}
|
|
614 |
if (closeNow) {
|
|
615 |
s = s + "Connection: close\r\n";
|
|
616 |
}
|
|
617 |
s = s + "\r\n" + text;
|
|
618 |
byte[] b = s.getBytes("ISO8859_1");
|
|
619 |
rawout.write (b);
|
|
620 |
rawout.flush();
|
|
621 |
if (closeNow) {
|
|
622 |
connection.close();
|
|
623 |
}
|
|
624 |
} catch (IOException e) {
|
|
625 |
logger.log (Level.FINER, "ServerImpl.sendReply", e);
|
|
626 |
connection.close();
|
|
627 |
}
|
|
628 |
}
|
|
629 |
|
|
630 |
}
|
|
631 |
|
|
632 |
void logReply (int code, String requestStr, String text) {
|
|
633 |
if (text == null) {
|
|
634 |
text = "";
|
|
635 |
}
|
|
636 |
String message = requestStr + " [" + code + " " +
|
|
637 |
Code.msg(code) + "] ("+text+")";
|
|
638 |
logger.fine (message);
|
|
639 |
}
|
|
640 |
|
|
641 |
long getTicks() {
|
|
642 |
return ticks;
|
|
643 |
}
|
|
644 |
|
|
645 |
public long getTime() {
|
|
646 |
return time;
|
|
647 |
}
|
|
648 |
|
|
649 |
void delay () {
|
|
650 |
Thread.yield();
|
|
651 |
try {
|
|
652 |
Thread.sleep (200);
|
|
653 |
} catch (InterruptedException e) {}
|
|
654 |
}
|
|
655 |
|
|
656 |
private int exchangeCount = 0;
|
|
657 |
|
|
658 |
synchronized void startExchange () {
|
|
659 |
exchangeCount ++;
|
|
660 |
}
|
|
661 |
|
|
662 |
synchronized int endExchange () {
|
|
663 |
exchangeCount --;
|
|
664 |
assert exchangeCount >= 0;
|
|
665 |
return exchangeCount;
|
|
666 |
}
|
|
667 |
|
|
668 |
HttpServer getWrapper () {
|
|
669 |
return wrapper;
|
|
670 |
}
|
|
671 |
|
|
672 |
/**
|
|
673 |
* TimerTask run every CLOCK_TICK ms
|
|
674 |
*/
|
|
675 |
class ServerTimerTask extends TimerTask {
|
|
676 |
public void run () {
|
|
677 |
LinkedList<HttpConnection> toClose = new LinkedList<HttpConnection>();
|
|
678 |
time = System.currentTimeMillis();
|
|
679 |
ticks ++;
|
|
680 |
synchronized (idleConnections) {
|
|
681 |
for (HttpConnection c : idleConnections) {
|
|
682 |
if (c.time <= time) {
|
|
683 |
toClose.add (c);
|
|
684 |
}
|
|
685 |
}
|
|
686 |
for (HttpConnection c : toClose) {
|
|
687 |
idleConnections.remove (c);
|
|
688 |
allConnections.remove (c);
|
|
689 |
c.close();
|
|
690 |
}
|
|
691 |
}
|
|
692 |
}
|
|
693 |
}
|
|
694 |
}
|