author | michaelm |
Wed, 23 Jul 2008 12:05:19 +0100 | |
changeset 907 | 11f377f9319d |
parent 905 | 8445a646acfb |
child 1247 | b4c26443dee5 |
permissions | -rw-r--r-- |
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 |
} |
|
905 | 123 |
Thread t = new Thread (dispatcher); |
2 | 124 |
started = true; |
125 |
t.start(); |
|
126 |
} |
|
127 |
||
128 |
public void setExecutor (Executor executor) { |
|
129 |
if (started) { |
|
130 |
throw new IllegalStateException ("server already started"); |
|
131 |
} |
|
132 |
this.executor = executor; |
|
133 |
} |
|
134 |
||
135 |
private static class DefaultExecutor implements Executor { |
|
136 |
public void execute (Runnable task) { |
|
137 |
task.run(); |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
public Executor getExecutor () { |
|
142 |
return executor; |
|
143 |
} |
|
144 |
||
145 |
public void setHttpsConfigurator (HttpsConfigurator config) { |
|
146 |
if (config == null) { |
|
147 |
throw new NullPointerException ("null HttpsConfigurator"); |
|
148 |
} |
|
149 |
if (started) { |
|
150 |
throw new IllegalStateException ("server already started"); |
|
151 |
} |
|
152 |
this.httpsConfig = config; |
|
153 |
sslContext = config.getSSLContext(); |
|
154 |
} |
|
155 |
||
156 |
public HttpsConfigurator getHttpsConfigurator () { |
|
157 |
return httpsConfig; |
|
158 |
} |
|
159 |
||
160 |
public void stop (int delay) { |
|
161 |
if (delay < 0) { |
|
162 |
throw new IllegalArgumentException ("negative delay parameter"); |
|
163 |
} |
|
164 |
terminating = true; |
|
165 |
try { schan.close(); } catch (IOException e) {} |
|
166 |
selector.wakeup(); |
|
167 |
long latest = System.currentTimeMillis() + delay * 1000; |
|
168 |
while (System.currentTimeMillis() < latest) { |
|
169 |
delay(); |
|
170 |
if (finished) { |
|
171 |
break; |
|
172 |
} |
|
173 |
} |
|
174 |
finished = true; |
|
175 |
selector.wakeup(); |
|
176 |
synchronized (allConnections) { |
|
177 |
for (HttpConnection c : allConnections) { |
|
178 |
c.close(); |
|
179 |
} |
|
180 |
} |
|
181 |
allConnections.clear(); |
|
182 |
idleConnections.clear(); |
|
183 |
timer.cancel(); |
|
184 |
} |
|
185 |
||
186 |
Dispatcher dispatcher; |
|
187 |
||
188 |
public synchronized HttpContextImpl createContext (String path, HttpHandler handler) { |
|
189 |
if (handler == null || path == null) { |
|
190 |
throw new NullPointerException ("null handler, or path parameter"); |
|
191 |
} |
|
192 |
HttpContextImpl context = new HttpContextImpl (protocol, path, handler, this); |
|
193 |
contexts.add (context); |
|
194 |
logger.config ("context created: " + path); |
|
195 |
return context; |
|
196 |
} |
|
197 |
||
198 |
public synchronized HttpContextImpl createContext (String path) { |
|
199 |
if (path == null) { |
|
200 |
throw new NullPointerException ("null path parameter"); |
|
201 |
} |
|
202 |
HttpContextImpl context = new HttpContextImpl (protocol, path, null, this); |
|
203 |
contexts.add (context); |
|
204 |
logger.config ("context created: " + path); |
|
205 |
return context; |
|
206 |
} |
|
207 |
||
208 |
public synchronized void removeContext (String path) throws IllegalArgumentException { |
|
209 |
if (path == null) { |
|
210 |
throw new NullPointerException ("null path parameter"); |
|
211 |
} |
|
212 |
contexts.remove (protocol, path); |
|
213 |
logger.config ("context removed: " + path); |
|
214 |
} |
|
215 |
||
216 |
public synchronized void removeContext (HttpContext context) throws IllegalArgumentException { |
|
217 |
if (!(context instanceof HttpContextImpl)) { |
|
218 |
throw new IllegalArgumentException ("wrong HttpContext type"); |
|
219 |
} |
|
220 |
contexts.remove ((HttpContextImpl)context); |
|
221 |
logger.config ("context removed: " + context.getPath()); |
|
222 |
} |
|
223 |
||
224 |
public InetSocketAddress getAddress() { |
|
225 |
return (InetSocketAddress)schan.socket().getLocalSocketAddress(); |
|
226 |
} |
|
227 |
||
228 |
Selector getSelector () { |
|
229 |
return selector; |
|
230 |
} |
|
231 |
||
232 |
void addEvent (Event r) { |
|
233 |
synchronized (lolock) { |
|
234 |
events.add (r); |
|
235 |
selector.wakeup(); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
int resultSize () { |
|
240 |
synchronized (lolock) { |
|
241 |
return events.size (); |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
||
246 |
/* main server listener task */ |
|
247 |
||
248 |
class Dispatcher implements Runnable { |
|
249 |
||
250 |
private void handleEvent (Event r) { |
|
251 |
ExchangeImpl t = r.exchange; |
|
252 |
HttpConnection c = t.getConnection(); |
|
253 |
try { |
|
254 |
if (r instanceof WriteFinishedEvent) { |
|
255 |
||
256 |
int exchanges = endExchange(); |
|
257 |
if (terminating && exchanges == 0) { |
|
258 |
finished = true; |
|
259 |
} |
|
260 |
SocketChannel chan = c.getChannel(); |
|
261 |
LeftOverInputStream is = t.getOriginalInputStream(); |
|
262 |
if (!is.isEOF()) { |
|
263 |
t.close = true; |
|
264 |
} |
|
265 |
if (t.close || idleConnections.size() >= MAX_IDLE_CONNECTIONS) { |
|
266 |
c.close(); |
|
267 |
allConnections.remove (c); |
|
268 |
} else { |
|
269 |
if (is.isDataBuffered()) { |
|
270 |
/* don't re-enable the interestops, just handle it */ |
|
271 |
handle (c.getChannel(), c); |
|
272 |
} else { |
|
273 |
/* re-enable interestops */ |
|
274 |
SelectionKey key = c.getSelectionKey(); |
|
275 |
if (key.isValid()) { |
|
276 |
key.interestOps ( |
|
277 |
key.interestOps()|SelectionKey.OP_READ |
|
278 |
); |
|
279 |
} |
|
280 |
c.time = getTime() + IDLE_INTERVAL; |
|
281 |
idleConnections.add (c); |
|
282 |
} |
|
283 |
} |
|
284 |
} |
|
285 |
} catch (IOException e) { |
|
286 |
logger.log ( |
|
287 |
Level.FINER, "Dispatcher (1)", e |
|
288 |
); |
|
289 |
c.close(); |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
public void run() { |
|
294 |
while (!finished) { |
|
295 |
try { |
|
296 |
||
297 |
/* process the events list first */ |
|
298 |
||
299 |
while (resultSize() > 0) { |
|
300 |
Event r; |
|
301 |
synchronized (lolock) { |
|
302 |
r = events.remove(0); |
|
303 |
handleEvent (r); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
selector.select(1000); |
|
308 |
||
309 |
/* process the selected list now */ |
|
310 |
||
311 |
Set<SelectionKey> selected = selector.selectedKeys(); |
|
312 |
Iterator<SelectionKey> iter = selected.iterator(); |
|
313 |
while (iter.hasNext()) { |
|
314 |
SelectionKey key = iter.next(); |
|
315 |
iter.remove (); |
|
316 |
if (key.equals (listenerKey)) { |
|
317 |
if (terminating) { |
|
318 |
continue; |
|
319 |
} |
|
320 |
SocketChannel chan = schan.accept(); |
|
321 |
if (chan == null) { |
|
322 |
continue; /* cancel something ? */ |
|
323 |
} |
|
324 |
chan.configureBlocking (false); |
|
325 |
SelectionKey newkey = chan.register (selector, SelectionKey.OP_READ); |
|
326 |
HttpConnection c = new HttpConnection (); |
|
327 |
c.selectionKey = newkey; |
|
328 |
c.setChannel (chan); |
|
329 |
newkey.attach (c); |
|
330 |
allConnections.add (c); |
|
331 |
} else { |
|
332 |
try { |
|
333 |
if (key.isReadable()) { |
|
334 |
boolean closed; |
|
335 |
SocketChannel chan = (SocketChannel)key.channel(); |
|
336 |
HttpConnection conn = (HttpConnection)key.attachment(); |
|
337 |
// interestOps will be restored at end of read |
|
338 |
key.interestOps (0); |
|
339 |
handle (chan, conn); |
|
340 |
} else { |
|
341 |
assert false; |
|
342 |
} |
|
343 |
} catch (IOException e) { |
|
344 |
HttpConnection conn = (HttpConnection)key.attachment(); |
|
345 |
logger.log ( |
|
346 |
Level.FINER, "Dispatcher (2)", e |
|
347 |
); |
|
348 |
conn.close(); |
|
349 |
} |
|
350 |
} |
|
351 |
} |
|
907
11f377f9319d
6728076: Test case for 6536211 is failing on all platforms
michaelm
parents:
905
diff
changeset
|
352 |
} catch (Exception e) { |
2 | 353 |
logger.log (Level.FINER, "Dispatcher (3)", e); |
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
||
358 |
public void handle (SocketChannel chan, HttpConnection conn) |
|
359 |
throws IOException |
|
360 |
{ |
|
361 |
try { |
|
362 |
Exchange t = new Exchange (chan, protocol, conn); |
|
363 |
executor.execute (t); |
|
364 |
} catch (HttpError e1) { |
|
907
11f377f9319d
6728076: Test case for 6536211 is failing on all platforms
michaelm
parents:
905
diff
changeset
|
365 |
logger.log (Level.FINER, "Dispatcher (4)", e1); |
2 | 366 |
conn.close(); |
367 |
} catch (IOException e) { |
|
907
11f377f9319d
6728076: Test case for 6536211 is failing on all platforms
michaelm
parents:
905
diff
changeset
|
368 |
logger.log (Level.FINER, "Dispatcher (5)", e); |
2 | 369 |
conn.close(); |
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
||
374 |
static boolean debug = ServerConfig.debugEnabled (); |
|
375 |
||
376 |
static synchronized void dprint (String s) { |
|
377 |
if (debug) { |
|
378 |
System.out.println (s); |
|
379 |
} |
|
380 |
} |
|
381 |
||
382 |
static synchronized void dprint (Exception e) { |
|
383 |
if (debug) { |
|
384 |
System.out.println (e); |
|
385 |
e.printStackTrace(); |
|
386 |
} |
|
387 |
} |
|
388 |
||
389 |
Logger getLogger () { |
|
390 |
return logger; |
|
391 |
} |
|
392 |
||
393 |
/* per exchange task */ |
|
394 |
||
395 |
class Exchange implements Runnable { |
|
396 |
SocketChannel chan; |
|
397 |
HttpConnection connection; |
|
398 |
HttpContextImpl context; |
|
399 |
InputStream rawin; |
|
400 |
OutputStream rawout; |
|
401 |
String protocol; |
|
402 |
ExchangeImpl tx; |
|
403 |
HttpContextImpl ctx; |
|
404 |
boolean rejected = false; |
|
405 |
||
406 |
Exchange (SocketChannel chan, String protocol, HttpConnection conn) throws IOException { |
|
407 |
this.chan = chan; |
|
408 |
this.connection = conn; |
|
409 |
this.protocol = protocol; |
|
410 |
} |
|
411 |
||
412 |
public void run () { |
|
413 |
/* context will be null for new connections */ |
|
414 |
context = connection.getHttpContext(); |
|
415 |
boolean newconnection; |
|
416 |
SSLEngine engine = null; |
|
417 |
String requestLine = null; |
|
418 |
SSLStreams sslStreams = null; |
|
419 |
try { |
|
420 |
if (context != null ) { |
|
421 |
this.rawin = connection.getInputStream(); |
|
422 |
this.rawout = connection.getRawOutputStream(); |
|
423 |
newconnection = false; |
|
424 |
} else { |
|
425 |
/* figure out what kind of connection this is */ |
|
426 |
newconnection = true; |
|
427 |
if (https) { |
|
428 |
if (sslContext == null) { |
|
429 |
logger.warning ("SSL connection received. No https contxt created"); |
|
430 |
throw new HttpError ("No SSL context established"); |
|
431 |
} |
|
432 |
sslStreams = new SSLStreams (ServerImpl.this, sslContext, chan); |
|
433 |
rawin = sslStreams.getInputStream(); |
|
434 |
rawout = sslStreams.getOutputStream(); |
|
435 |
engine = sslStreams.getSSLEngine(); |
|
436 |
} else { |
|
437 |
rawin = new BufferedInputStream( |
|
438 |
new Request.ReadStream ( |
|
439 |
ServerImpl.this, chan |
|
440 |
)); |
|
441 |
rawout = new Request.WriteStream ( |
|
442 |
ServerImpl.this, chan |
|
443 |
); |
|
444 |
} |
|
445 |
} |
|
446 |
Request req = new Request (rawin, rawout); |
|
447 |
requestLine = req.requestLine(); |
|
448 |
if (requestLine == null) { |
|
449 |
/* connection closed */ |
|
450 |
connection.close(); |
|
451 |
return; |
|
452 |
} |
|
453 |
int space = requestLine.indexOf (' '); |
|
454 |
if (space == -1) { |
|
455 |
reject (Code.HTTP_BAD_REQUEST, |
|
456 |
requestLine, "Bad request line"); |
|
457 |
return; |
|
458 |
} |
|
459 |
String method = requestLine.substring (0, space); |
|
460 |
int start = space+1; |
|
461 |
space = requestLine.indexOf(' ', start); |
|
462 |
if (space == -1) { |
|
463 |
reject (Code.HTTP_BAD_REQUEST, |
|
464 |
requestLine, "Bad request line"); |
|
465 |
return; |
|
466 |
} |
|
467 |
String uriStr = requestLine.substring (start, space); |
|
468 |
URI uri = new URI (uriStr); |
|
469 |
start = space+1; |
|
470 |
String version = requestLine.substring (start); |
|
471 |
Headers headers = req.headers(); |
|
472 |
String s = headers.getFirst ("Transfer-encoding"); |
|
473 |
int clen = 0; |
|
474 |
if (s !=null && s.equalsIgnoreCase ("chunked")) { |
|
475 |
clen = -1; |
|
476 |
} else { |
|
477 |
s = headers.getFirst ("Content-Length"); |
|
478 |
if (s != null) { |
|
479 |
clen = Integer.parseInt (s); |
|
480 |
} |
|
481 |
} |
|
482 |
ctx = contexts.findContext (protocol, uri.getPath()); |
|
483 |
if (ctx == null) { |
|
484 |
reject (Code.HTTP_NOT_FOUND, |
|
485 |
requestLine, "No context found for request"); |
|
486 |
return; |
|
487 |
} |
|
488 |
connection.setContext (ctx); |
|
489 |
if (ctx.getHandler() == null) { |
|
490 |
reject (Code.HTTP_INTERNAL_ERROR, |
|
491 |
requestLine, "No handler for context"); |
|
492 |
return; |
|
493 |
} |
|
494 |
tx = new ExchangeImpl ( |
|
495 |
method, uri, req, clen, connection |
|
496 |
); |
|
497 |
String chdr = headers.getFirst("Connection"); |
|
498 |
Headers rheaders = tx.getResponseHeaders(); |
|
499 |
||
500 |
if (chdr != null && chdr.equalsIgnoreCase ("close")) { |
|
501 |
tx.close = true; |
|
502 |
} |
|
503 |
if (version.equalsIgnoreCase ("http/1.0")) { |
|
504 |
tx.http10 = true; |
|
505 |
if (chdr == null) { |
|
506 |
tx.close = true; |
|
507 |
rheaders.set ("Connection", "close"); |
|
508 |
} else if (chdr.equalsIgnoreCase ("keep-alive")) { |
|
509 |
rheaders.set ("Connection", "keep-alive"); |
|
510 |
int idle=(int)ServerConfig.getIdleInterval()/1000; |
|
511 |
int max=(int)ServerConfig.getMaxIdleConnections(); |
|
512 |
String val = "timeout="+idle+", max="+max; |
|
513 |
rheaders.set ("Keep-Alive", val); |
|
514 |
} |
|
515 |
} |
|
516 |
||
517 |
if (newconnection) { |
|
518 |
connection.setParameters ( |
|
519 |
rawin, rawout, chan, engine, sslStreams, |
|
520 |
sslContext, protocol, ctx, rawin |
|
521 |
); |
|
522 |
} |
|
523 |
/* check if client sent an Expect 100 Continue. |
|
524 |
* In that case, need to send an interim response. |
|
525 |
* In future API may be modified to allow app to |
|
526 |
* be involved in this process. |
|
527 |
*/ |
|
528 |
String exp = headers.getFirst("Expect"); |
|
529 |
if (exp != null && exp.equalsIgnoreCase ("100-continue")) { |
|
530 |
logReply (100, requestLine, null); |
|
531 |
sendReply ( |
|
532 |
Code.HTTP_CONTINUE, false, null |
|
533 |
); |
|
534 |
} |
|
535 |
/* uf is the list of filters seen/set by the user. |
|
536 |
* sf is the list of filters established internally |
|
537 |
* and which are not visible to the user. uc and sc |
|
538 |
* are the corresponding Filter.Chains. |
|
539 |
* They are linked together by a LinkHandler |
|
540 |
* so that they can both be invoked in one call. |
|
541 |
*/ |
|
542 |
List<Filter> sf = ctx.getSystemFilters(); |
|
543 |
List<Filter> uf = ctx.getFilters(); |
|
544 |
||
545 |
Filter.Chain sc = new Filter.Chain(sf, ctx.getHandler()); |
|
546 |
Filter.Chain uc = new Filter.Chain(uf, new LinkHandler (sc)); |
|
547 |
||
548 |
/* set up the two stream references */ |
|
549 |
tx.getRequestBody(); |
|
550 |
tx.getResponseBody(); |
|
551 |
if (https) { |
|
552 |
uc.doFilter (new HttpsExchangeImpl (tx)); |
|
553 |
} else { |
|
554 |
uc.doFilter (new HttpExchangeImpl (tx)); |
|
555 |
} |
|
556 |
||
557 |
} catch (IOException e1) { |
|
558 |
logger.log (Level.FINER, "ServerImpl.Exchange (1)", e1); |
|
559 |
connection.close(); |
|
560 |
} catch (NumberFormatException e3) { |
|
561 |
reject (Code.HTTP_BAD_REQUEST, |
|
562 |
requestLine, "NumberFormatException thrown"); |
|
563 |
} catch (URISyntaxException e) { |
|
564 |
reject (Code.HTTP_BAD_REQUEST, |
|
565 |
requestLine, "URISyntaxException thrown"); |
|
566 |
} catch (Exception e4) { |
|
567 |
logger.log (Level.FINER, "ServerImpl.Exchange (2)", e4); |
|
568 |
connection.close(); |
|
569 |
} |
|
570 |
} |
|
571 |
||
572 |
/* used to link to 2 or more Filter.Chains together */ |
|
573 |
||
574 |
class LinkHandler implements HttpHandler { |
|
575 |
Filter.Chain nextChain; |
|
576 |
||
577 |
LinkHandler (Filter.Chain nextChain) { |
|
578 |
this.nextChain = nextChain; |
|
579 |
} |
|
580 |
||
581 |
public void handle (HttpExchange exchange) throws IOException { |
|
582 |
nextChain.doFilter (exchange); |
|
583 |
} |
|
584 |
} |
|
585 |
||
586 |
void reject (int code, String requestStr, String message) { |
|
587 |
rejected = true; |
|
588 |
logReply (code, requestStr, message); |
|
589 |
sendReply ( |
|
590 |
code, true, "<h1>"+code+Code.msg(code)+"</h1>"+message |
|
591 |
); |
|
592 |
} |
|
593 |
||
594 |
void sendReply ( |
|
595 |
int code, boolean closeNow, String text) |
|
596 |
{ |
|
597 |
try { |
|
598 |
String s = "HTTP/1.1 " + code + Code.msg(code) + "\r\n"; |
|
599 |
if (text != null && text.length() != 0) { |
|
600 |
s = s + "Content-Length: "+text.length()+"\r\n"; |
|
601 |
s = s + "Content-Type: text/html\r\n"; |
|
602 |
} else { |
|
603 |
s = s + "Content-Length: 0\r\n"; |
|
604 |
text = ""; |
|
605 |
} |
|
606 |
if (closeNow) { |
|
607 |
s = s + "Connection: close\r\n"; |
|
608 |
} |
|
609 |
s = s + "\r\n" + text; |
|
610 |
byte[] b = s.getBytes("ISO8859_1"); |
|
611 |
rawout.write (b); |
|
612 |
rawout.flush(); |
|
613 |
if (closeNow) { |
|
614 |
connection.close(); |
|
615 |
} |
|
616 |
} catch (IOException e) { |
|
617 |
logger.log (Level.FINER, "ServerImpl.sendReply", e); |
|
618 |
connection.close(); |
|
619 |
} |
|
620 |
} |
|
621 |
||
622 |
} |
|
623 |
||
624 |
void logReply (int code, String requestStr, String text) { |
|
625 |
if (text == null) { |
|
626 |
text = ""; |
|
627 |
} |
|
628 |
String message = requestStr + " [" + code + " " + |
|
629 |
Code.msg(code) + "] ("+text+")"; |
|
630 |
logger.fine (message); |
|
631 |
} |
|
632 |
||
633 |
long getTicks() { |
|
634 |
return ticks; |
|
635 |
} |
|
636 |
||
637 |
public long getTime() { |
|
638 |
return time; |
|
639 |
} |
|
640 |
||
641 |
void delay () { |
|
642 |
Thread.yield(); |
|
643 |
try { |
|
644 |
Thread.sleep (200); |
|
645 |
} catch (InterruptedException e) {} |
|
646 |
} |
|
647 |
||
648 |
private int exchangeCount = 0; |
|
649 |
||
650 |
synchronized void startExchange () { |
|
651 |
exchangeCount ++; |
|
652 |
} |
|
653 |
||
654 |
synchronized int endExchange () { |
|
655 |
exchangeCount --; |
|
656 |
assert exchangeCount >= 0; |
|
657 |
return exchangeCount; |
|
658 |
} |
|
659 |
||
660 |
HttpServer getWrapper () { |
|
661 |
return wrapper; |
|
662 |
} |
|
663 |
||
664 |
/** |
|
665 |
* TimerTask run every CLOCK_TICK ms |
|
666 |
*/ |
|
667 |
class ServerTimerTask extends TimerTask { |
|
668 |
public void run () { |
|
669 |
LinkedList<HttpConnection> toClose = new LinkedList<HttpConnection>(); |
|
670 |
time = System.currentTimeMillis(); |
|
671 |
ticks ++; |
|
672 |
synchronized (idleConnections) { |
|
673 |
for (HttpConnection c : idleConnections) { |
|
674 |
if (c.time <= time) { |
|
675 |
toClose.add (c); |
|
676 |
} |
|
677 |
} |
|
678 |
for (HttpConnection c : toClose) { |
|
679 |
idleConnections.remove (c); |
|
680 |
allConnections.remove (c); |
|
681 |
c.close(); |
|
682 |
} |
|
683 |
} |
|
684 |
} |
|
685 |
} |
|
686 |
} |