author | chegar |
Tue, 17 Apr 2018 08:54:17 -0700 | |
changeset 49765 | ee6f7a61f3a5 |
parent 49271 | 7b35d2a59fb3 |
child 56451 | 9585061fdb04 |
child 59311 | b42eaca7d234 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49153
5447851ff0f6
8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
48083
diff
changeset
|
2 |
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.net.httpserver; |
|
27 |
||
28 |
import java.net.*; |
|
29 |
import java.io.*; |
|
30 |
import java.nio.channels.*; |
|
31 |
import java.util.*; |
|
32 |
import java.util.concurrent.*; |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
33 |
import java.lang.System.Logger; |
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
34 |
import java.lang.System.Logger.Level; |
2 | 35 |
import javax.net.ssl.*; |
36 |
import com.sun.net.httpserver.*; |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
37 |
import java.security.AccessController; |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
38 |
import java.security.PrivilegedAction; |
7271 | 39 |
import sun.net.httpserver.HttpConnection.State; |
2 | 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; |
|
7271 | 58 |
/* following two are used to keep track of the times |
59 |
* when a connection/request is first received |
|
60 |
* and when we start to send the response |
|
61 |
*/ |
|
62 |
private Set<HttpConnection> reqConnections; |
|
63 |
private Set<HttpConnection> rspConnections; |
|
2 | 64 |
private List<Event> events; |
65 |
private Object lolock = new Object(); |
|
66 |
private volatile boolean finished = false; |
|
67 |
private volatile boolean terminating = false; |
|
68 |
private boolean bound = false; |
|
69 |
private boolean started = false; |
|
70 |
private volatile long time; /* current time */ |
|
7271 | 71 |
private volatile long subticks = 0; |
2 | 72 |
private volatile long ticks; /* number of clock ticks since server started */ |
73 |
private HttpServer wrapper; |
|
74 |
||
75 |
final static int CLOCK_TICK = ServerConfig.getClockTick(); |
|
76 |
final static long IDLE_INTERVAL = ServerConfig.getIdleInterval(); |
|
77 |
final static int MAX_IDLE_CONNECTIONS = ServerConfig.getMaxIdleConnections(); |
|
7271 | 78 |
final static long TIMER_MILLIS = ServerConfig.getTimerMillis (); |
79 |
final static long MAX_REQ_TIME=getTimeMillis(ServerConfig.getMaxReqTime()); |
|
80 |
final static long MAX_RSP_TIME=getTimeMillis(ServerConfig.getMaxRspTime()); |
|
81 |
final static boolean timer1Enabled = MAX_REQ_TIME != -1 || MAX_RSP_TIME != -1; |
|
2 | 82 |
|
7271 | 83 |
private Timer timer, timer1; |
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
84 |
private final Logger logger; |
27720
aa3983c8fbee
8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
25859
diff
changeset
|
85 |
private Thread dispatcherThread; |
2 | 86 |
|
87 |
ServerImpl ( |
|
88 |
HttpServer wrapper, String protocol, InetSocketAddress addr, int backlog |
|
89 |
) throws IOException { |
|
90 |
||
91 |
this.protocol = protocol; |
|
92 |
this.wrapper = wrapper; |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
93 |
this.logger = System.getLogger ("com.sun.net.httpserver"); |
7271 | 94 |
ServerConfig.checkLegacyProperties (logger); |
2 | 95 |
https = protocol.equalsIgnoreCase ("https"); |
96 |
this.address = addr; |
|
97 |
contexts = new ContextList(); |
|
98 |
schan = ServerSocketChannel.open(); |
|
99 |
if (addr != null) { |
|
100 |
ServerSocket socket = schan.socket(); |
|
101 |
socket.bind (addr, backlog); |
|
102 |
bound = true; |
|
103 |
} |
|
104 |
selector = Selector.open (); |
|
105 |
schan.configureBlocking (false); |
|
106 |
listenerKey = schan.register (selector, SelectionKey.OP_ACCEPT); |
|
107 |
dispatcher = new Dispatcher(); |
|
108 |
idleConnections = Collections.synchronizedSet (new HashSet<HttpConnection>()); |
|
109 |
allConnections = Collections.synchronizedSet (new HashSet<HttpConnection>()); |
|
7271 | 110 |
reqConnections = Collections.synchronizedSet (new HashSet<HttpConnection>()); |
111 |
rspConnections = Collections.synchronizedSet (new HashSet<HttpConnection>()); |
|
2 | 112 |
time = System.currentTimeMillis(); |
113 |
timer = new Timer ("server-timer", true); |
|
114 |
timer.schedule (new ServerTimerTask(), CLOCK_TICK, CLOCK_TICK); |
|
7271 | 115 |
if (timer1Enabled) { |
116 |
timer1 = new Timer ("server-timer1", true); |
|
117 |
timer1.schedule (new ServerTimerTask1(),TIMER_MILLIS,TIMER_MILLIS); |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
118 |
logger.log (Level.DEBUG, "HttpServer timer1 enabled period in ms: ", TIMER_MILLIS); |
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
119 |
logger.log (Level.DEBUG, "MAX_REQ_TIME: "+MAX_REQ_TIME); |
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
120 |
logger.log (Level.DEBUG, "MAX_RSP_TIME: "+MAX_RSP_TIME); |
7271 | 121 |
} |
2 | 122 |
events = new LinkedList<Event>(); |
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
123 |
logger.log (Level.DEBUG, "HttpServer created "+protocol+" "+ addr); |
2 | 124 |
} |
125 |
||
126 |
public void bind (InetSocketAddress addr, int backlog) throws IOException { |
|
127 |
if (bound) { |
|
128 |
throw new BindException ("HttpServer already bound"); |
|
129 |
} |
|
130 |
if (addr == null) { |
|
131 |
throw new NullPointerException ("null address"); |
|
132 |
} |
|
133 |
ServerSocket socket = schan.socket(); |
|
134 |
socket.bind (addr, backlog); |
|
135 |
bound = true; |
|
136 |
} |
|
137 |
||
138 |
public void start () { |
|
139 |
if (!bound || started || finished) { |
|
140 |
throw new IllegalStateException ("server in wrong state"); |
|
141 |
} |
|
142 |
if (executor == null) { |
|
143 |
executor = new DefaultExecutor(); |
|
144 |
} |
|
37519
43cd7a5e06b5
8153372: Remove sun.misc.ManagedLocalsThread from jdk.httpserver
chegar
parents:
35313
diff
changeset
|
145 |
dispatcherThread = new Thread(null, dispatcher, "HTTP-Dispatcher", 0, false); |
2 | 146 |
started = true; |
27720
aa3983c8fbee
8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
25859
diff
changeset
|
147 |
dispatcherThread.start(); |
2 | 148 |
} |
149 |
||
150 |
public void setExecutor (Executor executor) { |
|
151 |
if (started) { |
|
152 |
throw new IllegalStateException ("server already started"); |
|
153 |
} |
|
154 |
this.executor = executor; |
|
155 |
} |
|
156 |
||
157 |
private static class DefaultExecutor implements Executor { |
|
158 |
public void execute (Runnable task) { |
|
159 |
task.run(); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
public Executor getExecutor () { |
|
164 |
return executor; |
|
165 |
} |
|
166 |
||
167 |
public void setHttpsConfigurator (HttpsConfigurator config) { |
|
168 |
if (config == null) { |
|
169 |
throw new NullPointerException ("null HttpsConfigurator"); |
|
170 |
} |
|
171 |
if (started) { |
|
172 |
throw new IllegalStateException ("server already started"); |
|
173 |
} |
|
174 |
this.httpsConfig = config; |
|
175 |
sslContext = config.getSSLContext(); |
|
176 |
} |
|
177 |
||
178 |
public HttpsConfigurator getHttpsConfigurator () { |
|
179 |
return httpsConfig; |
|
180 |
} |
|
181 |
||
44913
1b08f0eb012e
8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents:
41592
diff
changeset
|
182 |
public final boolean isFinishing() { |
1b08f0eb012e
8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents:
41592
diff
changeset
|
183 |
return finished; |
1b08f0eb012e
8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents:
41592
diff
changeset
|
184 |
} |
1b08f0eb012e
8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents:
41592
diff
changeset
|
185 |
|
2 | 186 |
public void stop (int delay) { |
187 |
if (delay < 0) { |
|
188 |
throw new IllegalArgumentException ("negative delay parameter"); |
|
189 |
} |
|
190 |
terminating = true; |
|
191 |
try { schan.close(); } catch (IOException e) {} |
|
192 |
selector.wakeup(); |
|
193 |
long latest = System.currentTimeMillis() + delay * 1000; |
|
194 |
while (System.currentTimeMillis() < latest) { |
|
195 |
delay(); |
|
196 |
if (finished) { |
|
197 |
break; |
|
198 |
} |
|
199 |
} |
|
200 |
finished = true; |
|
201 |
selector.wakeup(); |
|
202 |
synchronized (allConnections) { |
|
203 |
for (HttpConnection c : allConnections) { |
|
204 |
c.close(); |
|
205 |
} |
|
206 |
} |
|
207 |
allConnections.clear(); |
|
208 |
idleConnections.clear(); |
|
209 |
timer.cancel(); |
|
7271 | 210 |
if (timer1Enabled) { |
211 |
timer1.cancel(); |
|
212 |
} |
|
27779
e269428daa22
8066130: com.sun.net.httpserver stop() throws NullPointerException if it is not started
msheppar
parents:
27720
diff
changeset
|
213 |
if (dispatcherThread != null) { |
e269428daa22
8066130: com.sun.net.httpserver stop() throws NullPointerException if it is not started
msheppar
parents:
27720
diff
changeset
|
214 |
try { |
e269428daa22
8066130: com.sun.net.httpserver stop() throws NullPointerException if it is not started
msheppar
parents:
27720
diff
changeset
|
215 |
dispatcherThread.join(); |
e269428daa22
8066130: com.sun.net.httpserver stop() throws NullPointerException if it is not started
msheppar
parents:
27720
diff
changeset
|
216 |
} catch (InterruptedException e) { |
e269428daa22
8066130: com.sun.net.httpserver stop() throws NullPointerException if it is not started
msheppar
parents:
27720
diff
changeset
|
217 |
Thread.currentThread().interrupt(); |
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
218 |
logger.log (Level.TRACE, "ServerImpl.stop: ", e); |
27779
e269428daa22
8066130: com.sun.net.httpserver stop() throws NullPointerException if it is not started
msheppar
parents:
27720
diff
changeset
|
219 |
} |
27720
aa3983c8fbee
8015692: java.net.BindException is thrown on Windows XP when HTTP server is started and stopped in the loop.
msheppar
parents:
25859
diff
changeset
|
220 |
} |
2 | 221 |
} |
222 |
||
223 |
Dispatcher dispatcher; |
|
224 |
||
225 |
public synchronized HttpContextImpl createContext (String path, HttpHandler handler) { |
|
226 |
if (handler == null || path == null) { |
|
227 |
throw new NullPointerException ("null handler, or path parameter"); |
|
228 |
} |
|
229 |
HttpContextImpl context = new HttpContextImpl (protocol, path, handler, this); |
|
230 |
contexts.add (context); |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
231 |
logger.log (Level.DEBUG, "context created: " + path); |
2 | 232 |
return context; |
233 |
} |
|
234 |
||
235 |
public synchronized HttpContextImpl createContext (String path) { |
|
236 |
if (path == null) { |
|
237 |
throw new NullPointerException ("null path parameter"); |
|
238 |
} |
|
239 |
HttpContextImpl context = new HttpContextImpl (protocol, path, null, this); |
|
240 |
contexts.add (context); |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
241 |
logger.log (Level.DEBUG, "context created: " + path); |
2 | 242 |
return context; |
243 |
} |
|
244 |
||
245 |
public synchronized void removeContext (String path) throws IllegalArgumentException { |
|
246 |
if (path == null) { |
|
247 |
throw new NullPointerException ("null path parameter"); |
|
248 |
} |
|
249 |
contexts.remove (protocol, path); |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
250 |
logger.log (Level.DEBUG, "context removed: " + path); |
2 | 251 |
} |
252 |
||
253 |
public synchronized void removeContext (HttpContext context) throws IllegalArgumentException { |
|
254 |
if (!(context instanceof HttpContextImpl)) { |
|
255 |
throw new IllegalArgumentException ("wrong HttpContext type"); |
|
256 |
} |
|
257 |
contexts.remove ((HttpContextImpl)context); |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
258 |
logger.log (Level.DEBUG, "context removed: " + context.getPath()); |
2 | 259 |
} |
260 |
||
261 |
public InetSocketAddress getAddress() { |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
262 |
return AccessController.doPrivileged( |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
263 |
new PrivilegedAction<InetSocketAddress>() { |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
264 |
public InetSocketAddress run() { |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
265 |
return |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
266 |
(InetSocketAddress)schan.socket() |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
267 |
.getLocalSocketAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
268 |
} |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
11014
diff
changeset
|
269 |
}); |
2 | 270 |
} |
271 |
||
272 |
Selector getSelector () { |
|
273 |
return selector; |
|
274 |
} |
|
275 |
||
276 |
void addEvent (Event r) { |
|
277 |
synchronized (lolock) { |
|
278 |
events.add (r); |
|
279 |
selector.wakeup(); |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
/* main server listener task */ |
|
284 |
||
285 |
class Dispatcher implements Runnable { |
|
286 |
||
287 |
private void handleEvent (Event r) { |
|
288 |
ExchangeImpl t = r.exchange; |
|
289 |
HttpConnection c = t.getConnection(); |
|
290 |
try { |
|
291 |
if (r instanceof WriteFinishedEvent) { |
|
292 |
||
293 |
int exchanges = endExchange(); |
|
294 |
if (terminating && exchanges == 0) { |
|
295 |
finished = true; |
|
296 |
} |
|
7271 | 297 |
responseCompleted (c); |
2 | 298 |
LeftOverInputStream is = t.getOriginalInputStream(); |
299 |
if (!is.isEOF()) { |
|
300 |
t.close = true; |
|
301 |
} |
|
302 |
if (t.close || idleConnections.size() >= MAX_IDLE_CONNECTIONS) { |
|
303 |
c.close(); |
|
304 |
allConnections.remove (c); |
|
305 |
} else { |
|
306 |
if (is.isDataBuffered()) { |
|
307 |
/* don't re-enable the interestops, just handle it */ |
|
7271 | 308 |
requestStarted (c); |
2 | 309 |
handle (c.getChannel(), c); |
310 |
} else { |
|
7271 | 311 |
connsToRegister.add (c); |
2 | 312 |
} |
313 |
} |
|
314 |
} |
|
315 |
} catch (IOException e) { |
|
316 |
logger.log ( |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
317 |
Level.TRACE, "Dispatcher (1)", e |
2 | 318 |
); |
319 |
c.close(); |
|
320 |
} |
|
321 |
} |
|
322 |
||
7271 | 323 |
final LinkedList<HttpConnection> connsToRegister = |
324 |
new LinkedList<HttpConnection>(); |
|
325 |
||
326 |
void reRegister (HttpConnection c) { |
|
327 |
/* re-register with selector */ |
|
328 |
try { |
|
329 |
SocketChannel chan = c.getChannel(); |
|
330 |
chan.configureBlocking (false); |
|
331 |
SelectionKey key = chan.register (selector, SelectionKey.OP_READ); |
|
332 |
key.attach (c); |
|
333 |
c.selectionKey = key; |
|
334 |
c.time = getTime() + IDLE_INTERVAL; |
|
335 |
idleConnections.add (c); |
|
336 |
} catch (IOException e) { |
|
337 |
dprint(e); |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
338 |
logger.log (Level.TRACE, "Dispatcher(8)", e); |
7271 | 339 |
c.close(); |
340 |
} |
|
341 |
} |
|
342 |
||
2 | 343 |
public void run() { |
344 |
while (!finished) { |
|
345 |
try { |
|
7271 | 346 |
List<Event> list = null; |
347 |
synchronized (lolock) { |
|
348 |
if (events.size() > 0) { |
|
349 |
list = events; |
|
350 |
events = new LinkedList<Event>(); |
|
351 |
} |
|
352 |
} |
|
353 |
||
354 |
if (list != null) { |
|
355 |
for (Event r: list) { |
|
2 | 356 |
handleEvent (r); |
357 |
} |
|
358 |
} |
|
359 |
||
17463
9392f1567896
8014254: Selector in HttpServer introduces a 1000 ms delay when using KeepAlive
khazra
parents:
11014
diff
changeset
|
360 |
for (HttpConnection c : connsToRegister) { |
9392f1567896
8014254: Selector in HttpServer introduces a 1000 ms delay when using KeepAlive
khazra
parents:
11014
diff
changeset
|
361 |
reRegister(c); |
9392f1567896
8014254: Selector in HttpServer introduces a 1000 ms delay when using KeepAlive
khazra
parents:
11014
diff
changeset
|
362 |
} |
9392f1567896
8014254: Selector in HttpServer introduces a 1000 ms delay when using KeepAlive
khazra
parents:
11014
diff
changeset
|
363 |
connsToRegister.clear(); |
9392f1567896
8014254: Selector in HttpServer introduces a 1000 ms delay when using KeepAlive
khazra
parents:
11014
diff
changeset
|
364 |
|
9392f1567896
8014254: Selector in HttpServer introduces a 1000 ms delay when using KeepAlive
khazra
parents:
11014
diff
changeset
|
365 |
selector.select(1000); |
9392f1567896
8014254: Selector in HttpServer introduces a 1000 ms delay when using KeepAlive
khazra
parents:
11014
diff
changeset
|
366 |
|
2 | 367 |
/* process the selected list now */ |
368 |
Set<SelectionKey> selected = selector.selectedKeys(); |
|
369 |
Iterator<SelectionKey> iter = selected.iterator(); |
|
370 |
while (iter.hasNext()) { |
|
371 |
SelectionKey key = iter.next(); |
|
372 |
iter.remove (); |
|
373 |
if (key.equals (listenerKey)) { |
|
374 |
if (terminating) { |
|
375 |
continue; |
|
376 |
} |
|
377 |
SocketChannel chan = schan.accept(); |
|
10130
254e206a89d8
7068416: Lightweight HTTP Server should support TCP_NODELAY
chegar
parents:
7668
diff
changeset
|
378 |
|
35313
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
379 |
// optimist there's a channel |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
380 |
if (chan != null) { |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
381 |
// Set TCP_NODELAY, if appropriate |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
382 |
if (ServerConfig.noDelay()) { |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
383 |
chan.socket().setTcpNoDelay(true); |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
384 |
} |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
385 |
chan.configureBlocking (false); |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
386 |
SelectionKey newkey = |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
387 |
chan.register (selector, SelectionKey.OP_READ); |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
388 |
HttpConnection c = new HttpConnection (); |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
389 |
c.selectionKey = newkey; |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
390 |
c.setChannel (chan); |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
391 |
newkey.attach (c); |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
392 |
requestStarted (c); |
9e703c485544
8147862: Null check too late in sun.net.httpserver.ServerImpl
msheppar
parents:
29920
diff
changeset
|
393 |
allConnections.add (c); |
2 | 394 |
} |
395 |
} else { |
|
396 |
try { |
|
397 |
if (key.isReadable()) { |
|
398 |
SocketChannel chan = (SocketChannel)key.channel(); |
|
399 |
HttpConnection conn = (HttpConnection)key.attachment(); |
|
7271 | 400 |
|
401 |
key.cancel(); |
|
402 |
chan.configureBlocking (true); |
|
403 |
if (idleConnections.remove(conn)) { |
|
404 |
// was an idle connection so add it |
|
405 |
// to reqConnections set. |
|
406 |
requestStarted (conn); |
|
407 |
} |
|
2 | 408 |
handle (chan, conn); |
409 |
} else { |
|
49271
7b35d2a59fb3
8199437: Improve diagnostic system assertion message in com.sun.net.httpserver impl
chegar
parents:
49153
diff
changeset
|
410 |
assert false : "Unexpected non-readable key:" + key; |
2 | 411 |
} |
7271 | 412 |
} catch (CancelledKeyException e) { |
413 |
handleException(key, null); |
|
2 | 414 |
} catch (IOException e) { |
7271 | 415 |
handleException(key, e); |
2 | 416 |
} |
417 |
} |
|
418 |
} |
|
7271 | 419 |
// call the selector just to process the cancelled keys |
420 |
selector.selectNow(); |
|
421 |
} catch (IOException e) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
422 |
logger.log (Level.TRACE, "Dispatcher (4)", e); |
907
11f377f9319d
6728076: Test case for 6536211 is failing on all platforms
michaelm
parents:
905
diff
changeset
|
423 |
} catch (Exception e) { |
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
424 |
logger.log (Level.TRACE, "Dispatcher (7)", e); |
2 | 425 |
} |
426 |
} |
|
11014 | 427 |
try {selector.close(); } catch (Exception e) {} |
2 | 428 |
} |
429 |
||
7271 | 430 |
private void handleException (SelectionKey key, Exception e) { |
431 |
HttpConnection conn = (HttpConnection)key.attachment(); |
|
432 |
if (e != null) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
433 |
logger.log (Level.TRACE, "Dispatcher (2)", e); |
7271 | 434 |
} |
435 |
closeConnection(conn); |
|
436 |
} |
|
437 |
||
2 | 438 |
public void handle (SocketChannel chan, HttpConnection conn) |
439 |
{ |
|
440 |
try { |
|
441 |
Exchange t = new Exchange (chan, protocol, conn); |
|
442 |
executor.execute (t); |
|
443 |
} catch (HttpError e1) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
444 |
logger.log (Level.TRACE, "Dispatcher (4)", e1); |
7271 | 445 |
closeConnection(conn); |
2 | 446 |
} catch (IOException e) { |
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
447 |
logger.log (Level.TRACE, "Dispatcher (5)", e); |
7271 | 448 |
closeConnection(conn); |
49153
5447851ff0f6
8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
48083
diff
changeset
|
449 |
} catch (Throwable e) { |
5447851ff0f6
8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
48083
diff
changeset
|
450 |
logger.log (Level.TRACE, "Dispatcher (6)", e); |
5447851ff0f6
8169358: httpserver does not close connections when RejectedExecutionException occurs
ykubota
parents:
48083
diff
changeset
|
451 |
closeConnection(conn); |
2 | 452 |
} |
453 |
} |
|
454 |
} |
|
455 |
||
456 |
static boolean debug = ServerConfig.debugEnabled (); |
|
457 |
||
458 |
static synchronized void dprint (String s) { |
|
459 |
if (debug) { |
|
460 |
System.out.println (s); |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
static synchronized void dprint (Exception e) { |
|
465 |
if (debug) { |
|
466 |
System.out.println (e); |
|
467 |
e.printStackTrace(); |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
Logger getLogger () { |
|
472 |
return logger; |
|
473 |
} |
|
474 |
||
7271 | 475 |
private void closeConnection(HttpConnection conn) { |
476 |
conn.close(); |
|
477 |
allConnections.remove(conn); |
|
478 |
switch (conn.getState()) { |
|
479 |
case REQUEST: |
|
480 |
reqConnections.remove(conn); |
|
481 |
break; |
|
482 |
case RESPONSE: |
|
483 |
rspConnections.remove(conn); |
|
484 |
break; |
|
485 |
case IDLE: |
|
486 |
idleConnections.remove(conn); |
|
487 |
break; |
|
488 |
} |
|
489 |
assert !reqConnections.remove(conn); |
|
490 |
assert !rspConnections.remove(conn); |
|
491 |
assert !idleConnections.remove(conn); |
|
492 |
} |
|
493 |
||
494 |
/* per exchange task */ |
|
2 | 495 |
|
496 |
class Exchange implements Runnable { |
|
497 |
SocketChannel chan; |
|
498 |
HttpConnection connection; |
|
499 |
HttpContextImpl context; |
|
500 |
InputStream rawin; |
|
501 |
OutputStream rawout; |
|
502 |
String protocol; |
|
503 |
ExchangeImpl tx; |
|
504 |
HttpContextImpl ctx; |
|
505 |
boolean rejected = false; |
|
506 |
||
507 |
Exchange (SocketChannel chan, String protocol, HttpConnection conn) throws IOException { |
|
508 |
this.chan = chan; |
|
509 |
this.connection = conn; |
|
510 |
this.protocol = protocol; |
|
511 |
} |
|
512 |
||
513 |
public void run () { |
|
514 |
/* context will be null for new connections */ |
|
515 |
context = connection.getHttpContext(); |
|
516 |
boolean newconnection; |
|
517 |
SSLEngine engine = null; |
|
518 |
String requestLine = null; |
|
519 |
SSLStreams sslStreams = null; |
|
520 |
try { |
|
521 |
if (context != null ) { |
|
522 |
this.rawin = connection.getInputStream(); |
|
523 |
this.rawout = connection.getRawOutputStream(); |
|
524 |
newconnection = false; |
|
525 |
} else { |
|
526 |
/* figure out what kind of connection this is */ |
|
527 |
newconnection = true; |
|
528 |
if (https) { |
|
529 |
if (sslContext == null) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
530 |
logger.log (Level.WARNING, |
49765 | 531 |
"SSL connection received. No https context created"); |
2 | 532 |
throw new HttpError ("No SSL context established"); |
533 |
} |
|
534 |
sslStreams = new SSLStreams (ServerImpl.this, sslContext, chan); |
|
535 |
rawin = sslStreams.getInputStream(); |
|
536 |
rawout = sslStreams.getOutputStream(); |
|
537 |
engine = sslStreams.getSSLEngine(); |
|
2612
d7fb0809c7e4
6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents:
2
diff
changeset
|
538 |
connection.sslStreams = sslStreams; |
2 | 539 |
} else { |
540 |
rawin = new BufferedInputStream( |
|
541 |
new Request.ReadStream ( |
|
542 |
ServerImpl.this, chan |
|
543 |
)); |
|
544 |
rawout = new Request.WriteStream ( |
|
545 |
ServerImpl.this, chan |
|
546 |
); |
|
547 |
} |
|
2612
d7fb0809c7e4
6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents:
2
diff
changeset
|
548 |
connection.raw = rawin; |
d7fb0809c7e4
6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents:
2
diff
changeset
|
549 |
connection.rawout = rawout; |
2 | 550 |
} |
551 |
Request req = new Request (rawin, rawout); |
|
552 |
requestLine = req.requestLine(); |
|
553 |
if (requestLine == null) { |
|
554 |
/* connection closed */ |
|
48083 | 555 |
logger.log(Level.DEBUG, "no request line: closing"); |
7271 | 556 |
closeConnection(connection); |
2 | 557 |
return; |
558 |
} |
|
48083 | 559 |
logger.log(Level.DEBUG, "Exchange request line: {0}", requestLine); |
2 | 560 |
int space = requestLine.indexOf (' '); |
561 |
if (space == -1) { |
|
562 |
reject (Code.HTTP_BAD_REQUEST, |
|
563 |
requestLine, "Bad request line"); |
|
564 |
return; |
|
565 |
} |
|
566 |
String method = requestLine.substring (0, space); |
|
567 |
int start = space+1; |
|
568 |
space = requestLine.indexOf(' ', start); |
|
569 |
if (space == -1) { |
|
570 |
reject (Code.HTTP_BAD_REQUEST, |
|
571 |
requestLine, "Bad request line"); |
|
572 |
return; |
|
573 |
} |
|
574 |
String uriStr = requestLine.substring (start, space); |
|
575 |
URI uri = new URI (uriStr); |
|
576 |
start = space+1; |
|
577 |
String version = requestLine.substring (start); |
|
578 |
Headers headers = req.headers(); |
|
579 |
String s = headers.getFirst ("Transfer-encoding"); |
|
1511
65ddd8f149f3
6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents:
1247
diff
changeset
|
580 |
long clen = 0L; |
2 | 581 |
if (s !=null && s.equalsIgnoreCase ("chunked")) { |
1511
65ddd8f149f3
6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents:
1247
diff
changeset
|
582 |
clen = -1L; |
2 | 583 |
} else { |
584 |
s = headers.getFirst ("Content-Length"); |
|
585 |
if (s != null) { |
|
1511
65ddd8f149f3
6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents:
1247
diff
changeset
|
586 |
clen = Long.parseLong(s); |
2 | 587 |
} |
7271 | 588 |
if (clen == 0) { |
589 |
requestCompleted (connection); |
|
590 |
} |
|
2 | 591 |
} |
592 |
ctx = contexts.findContext (protocol, uri.getPath()); |
|
593 |
if (ctx == null) { |
|
594 |
reject (Code.HTTP_NOT_FOUND, |
|
595 |
requestLine, "No context found for request"); |
|
596 |
return; |
|
597 |
} |
|
598 |
connection.setContext (ctx); |
|
599 |
if (ctx.getHandler() == null) { |
|
600 |
reject (Code.HTTP_INTERNAL_ERROR, |
|
601 |
requestLine, "No handler for context"); |
|
602 |
return; |
|
603 |
} |
|
604 |
tx = new ExchangeImpl ( |
|
605 |
method, uri, req, clen, connection |
|
606 |
); |
|
607 |
String chdr = headers.getFirst("Connection"); |
|
608 |
Headers rheaders = tx.getResponseHeaders(); |
|
609 |
||
610 |
if (chdr != null && chdr.equalsIgnoreCase ("close")) { |
|
611 |
tx.close = true; |
|
612 |
} |
|
613 |
if (version.equalsIgnoreCase ("http/1.0")) { |
|
614 |
tx.http10 = true; |
|
615 |
if (chdr == null) { |
|
616 |
tx.close = true; |
|
617 |
rheaders.set ("Connection", "close"); |
|
618 |
} else if (chdr.equalsIgnoreCase ("keep-alive")) { |
|
619 |
rheaders.set ("Connection", "keep-alive"); |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10130
diff
changeset
|
620 |
int idle=(int)(ServerConfig.getIdleInterval()/1000); |
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10130
diff
changeset
|
621 |
int max=ServerConfig.getMaxIdleConnections(); |
2 | 622 |
String val = "timeout="+idle+", max="+max; |
623 |
rheaders.set ("Keep-Alive", val); |
|
624 |
} |
|
625 |
} |
|
626 |
||
627 |
if (newconnection) { |
|
628 |
connection.setParameters ( |
|
629 |
rawin, rawout, chan, engine, sslStreams, |
|
630 |
sslContext, protocol, ctx, rawin |
|
631 |
); |
|
632 |
} |
|
633 |
/* check if client sent an Expect 100 Continue. |
|
634 |
* In that case, need to send an interim response. |
|
635 |
* In future API may be modified to allow app to |
|
636 |
* be involved in this process. |
|
637 |
*/ |
|
638 |
String exp = headers.getFirst("Expect"); |
|
639 |
if (exp != null && exp.equalsIgnoreCase ("100-continue")) { |
|
640 |
logReply (100, requestLine, null); |
|
641 |
sendReply ( |
|
642 |
Code.HTTP_CONTINUE, false, null |
|
643 |
); |
|
644 |
} |
|
645 |
/* uf is the list of filters seen/set by the user. |
|
646 |
* sf is the list of filters established internally |
|
647 |
* and which are not visible to the user. uc and sc |
|
648 |
* are the corresponding Filter.Chains. |
|
649 |
* They are linked together by a LinkHandler |
|
650 |
* so that they can both be invoked in one call. |
|
651 |
*/ |
|
652 |
List<Filter> sf = ctx.getSystemFilters(); |
|
653 |
List<Filter> uf = ctx.getFilters(); |
|
654 |
||
655 |
Filter.Chain sc = new Filter.Chain(sf, ctx.getHandler()); |
|
656 |
Filter.Chain uc = new Filter.Chain(uf, new LinkHandler (sc)); |
|
657 |
||
658 |
/* set up the two stream references */ |
|
659 |
tx.getRequestBody(); |
|
660 |
tx.getResponseBody(); |
|
661 |
if (https) { |
|
662 |
uc.doFilter (new HttpsExchangeImpl (tx)); |
|
663 |
} else { |
|
664 |
uc.doFilter (new HttpExchangeImpl (tx)); |
|
665 |
} |
|
666 |
||
667 |
} catch (IOException e1) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
668 |
logger.log (Level.TRACE, "ServerImpl.Exchange (1)", e1); |
7271 | 669 |
closeConnection(connection); |
2 | 670 |
} catch (NumberFormatException e3) { |
671 |
reject (Code.HTTP_BAD_REQUEST, |
|
672 |
requestLine, "NumberFormatException thrown"); |
|
673 |
} catch (URISyntaxException e) { |
|
674 |
reject (Code.HTTP_BAD_REQUEST, |
|
675 |
requestLine, "URISyntaxException thrown"); |
|
676 |
} catch (Exception e4) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
677 |
logger.log (Level.TRACE, "ServerImpl.Exchange (2)", e4); |
7271 | 678 |
closeConnection(connection); |
2 | 679 |
} |
680 |
} |
|
681 |
||
682 |
/* used to link to 2 or more Filter.Chains together */ |
|
683 |
||
684 |
class LinkHandler implements HttpHandler { |
|
685 |
Filter.Chain nextChain; |
|
686 |
||
687 |
LinkHandler (Filter.Chain nextChain) { |
|
688 |
this.nextChain = nextChain; |
|
689 |
} |
|
690 |
||
691 |
public void handle (HttpExchange exchange) throws IOException { |
|
692 |
nextChain.doFilter (exchange); |
|
693 |
} |
|
694 |
} |
|
695 |
||
696 |
void reject (int code, String requestStr, String message) { |
|
697 |
rejected = true; |
|
698 |
logReply (code, requestStr, message); |
|
699 |
sendReply ( |
|
7271 | 700 |
code, false, "<h1>"+code+Code.msg(code)+"</h1>"+message |
2 | 701 |
); |
7271 | 702 |
closeConnection(connection); |
2 | 703 |
} |
704 |
||
705 |
void sendReply ( |
|
706 |
int code, boolean closeNow, String text) |
|
707 |
{ |
|
708 |
try { |
|
7271 | 709 |
StringBuilder builder = new StringBuilder (512); |
710 |
builder.append ("HTTP/1.1 ") |
|
711 |
.append (code).append (Code.msg(code)).append ("\r\n"); |
|
712 |
||
2 | 713 |
if (text != null && text.length() != 0) { |
7271 | 714 |
builder.append ("Content-Length: ") |
715 |
.append (text.length()).append ("\r\n") |
|
716 |
.append ("Content-Type: text/html\r\n"); |
|
2 | 717 |
} else { |
7271 | 718 |
builder.append ("Content-Length: 0\r\n"); |
2 | 719 |
text = ""; |
720 |
} |
|
721 |
if (closeNow) { |
|
7271 | 722 |
builder.append ("Connection: close\r\n"); |
2 | 723 |
} |
7271 | 724 |
builder.append ("\r\n").append (text); |
725 |
String s = builder.toString(); |
|
2 | 726 |
byte[] b = s.getBytes("ISO8859_1"); |
727 |
rawout.write (b); |
|
728 |
rawout.flush(); |
|
729 |
if (closeNow) { |
|
7271 | 730 |
closeConnection(connection); |
2 | 731 |
} |
732 |
} catch (IOException e) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
733 |
logger.log (Level.TRACE, "ServerImpl.sendReply", e); |
7271 | 734 |
closeConnection(connection); |
2 | 735 |
} |
736 |
} |
|
737 |
||
738 |
} |
|
739 |
||
740 |
void logReply (int code, String requestStr, String text) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
741 |
if (!logger.isLoggable(Level.DEBUG)) { |
7271 | 742 |
return; |
743 |
} |
|
2 | 744 |
if (text == null) { |
745 |
text = ""; |
|
746 |
} |
|
7271 | 747 |
String r; |
748 |
if (requestStr.length() > 80) { |
|
749 |
r = requestStr.substring (0, 80) + "<TRUNCATED>"; |
|
750 |
} else { |
|
751 |
r = requestStr; |
|
752 |
} |
|
753 |
String message = r + " [" + code + " " + |
|
2 | 754 |
Code.msg(code) + "] ("+text+")"; |
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
755 |
logger.log (Level.DEBUG, message); |
2 | 756 |
} |
757 |
||
758 |
long getTicks() { |
|
759 |
return ticks; |
|
760 |
} |
|
761 |
||
762 |
public long getTime() { |
|
763 |
return time; |
|
764 |
} |
|
765 |
||
766 |
void delay () { |
|
767 |
Thread.yield(); |
|
768 |
try { |
|
769 |
Thread.sleep (200); |
|
770 |
} catch (InterruptedException e) {} |
|
771 |
} |
|
772 |
||
773 |
private int exchangeCount = 0; |
|
774 |
||
775 |
synchronized void startExchange () { |
|
776 |
exchangeCount ++; |
|
777 |
} |
|
778 |
||
779 |
synchronized int endExchange () { |
|
780 |
exchangeCount --; |
|
781 |
assert exchangeCount >= 0; |
|
782 |
return exchangeCount; |
|
783 |
} |
|
784 |
||
785 |
HttpServer getWrapper () { |
|
786 |
return wrapper; |
|
787 |
} |
|
788 |
||
7271 | 789 |
void requestStarted (HttpConnection c) { |
790 |
c.creationTime = getTime(); |
|
791 |
c.setState (State.REQUEST); |
|
792 |
reqConnections.add (c); |
|
793 |
} |
|
794 |
||
795 |
// called after a request has been completely read |
|
796 |
// by the server. This stops the timer which would |
|
797 |
// close the connection if the request doesn't arrive |
|
798 |
// quickly enough. It then starts the timer |
|
799 |
// that ensures the client reads the response in a timely |
|
800 |
// fashion. |
|
801 |
||
802 |
void requestCompleted (HttpConnection c) { |
|
48083 | 803 |
State s = c.getState(); |
804 |
assert s == State.REQUEST : "State is not REQUEST ("+s+")"; |
|
7271 | 805 |
reqConnections.remove (c); |
806 |
c.rspStartedTime = getTime(); |
|
807 |
rspConnections.add (c); |
|
808 |
c.setState (State.RESPONSE); |
|
809 |
} |
|
810 |
||
811 |
// called after response has been sent |
|
812 |
void responseCompleted (HttpConnection c) { |
|
48083 | 813 |
State s = c.getState(); |
814 |
assert s == State.RESPONSE : "State is not RESPONSE ("+s+")"; |
|
7271 | 815 |
rspConnections.remove (c); |
816 |
c.setState (State.IDLE); |
|
817 |
} |
|
818 |
||
2 | 819 |
/** |
820 |
* TimerTask run every CLOCK_TICK ms |
|
821 |
*/ |
|
822 |
class ServerTimerTask extends TimerTask { |
|
823 |
public void run () { |
|
824 |
LinkedList<HttpConnection> toClose = new LinkedList<HttpConnection>(); |
|
825 |
time = System.currentTimeMillis(); |
|
826 |
ticks ++; |
|
827 |
synchronized (idleConnections) { |
|
828 |
for (HttpConnection c : idleConnections) { |
|
829 |
if (c.time <= time) { |
|
830 |
toClose.add (c); |
|
831 |
} |
|
832 |
} |
|
833 |
for (HttpConnection c : toClose) { |
|
834 |
idleConnections.remove (c); |
|
835 |
allConnections.remove (c); |
|
836 |
c.close(); |
|
837 |
} |
|
838 |
} |
|
839 |
} |
|
840 |
} |
|
7271 | 841 |
|
842 |
class ServerTimerTask1 extends TimerTask { |
|
843 |
||
844 |
// runs every TIMER_MILLIS |
|
845 |
public void run () { |
|
846 |
LinkedList<HttpConnection> toClose = new LinkedList<HttpConnection>(); |
|
847 |
time = System.currentTimeMillis(); |
|
848 |
synchronized (reqConnections) { |
|
849 |
if (MAX_REQ_TIME != -1) { |
|
850 |
for (HttpConnection c : reqConnections) { |
|
851 |
if (c.creationTime + TIMER_MILLIS + MAX_REQ_TIME <= time) { |
|
852 |
toClose.add (c); |
|
853 |
} |
|
854 |
} |
|
855 |
for (HttpConnection c : toClose) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
856 |
logger.log (Level.DEBUG, "closing: no request: " + c); |
7271 | 857 |
reqConnections.remove (c); |
858 |
allConnections.remove (c); |
|
859 |
c.close(); |
|
860 |
} |
|
861 |
} |
|
862 |
} |
|
863 |
toClose = new LinkedList<HttpConnection>(); |
|
864 |
synchronized (rspConnections) { |
|
865 |
if (MAX_RSP_TIME != -1) { |
|
866 |
for (HttpConnection c : rspConnections) { |
|
867 |
if (c.rspStartedTime + TIMER_MILLIS +MAX_RSP_TIME <= time) { |
|
868 |
toClose.add (c); |
|
869 |
} |
|
870 |
} |
|
871 |
for (HttpConnection c : toClose) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
872 |
logger.log (Level.DEBUG, "closing: no response: " + c); |
7271 | 873 |
rspConnections.remove (c); |
874 |
allConnections.remove (c); |
|
875 |
c.close(); |
|
876 |
} |
|
877 |
} |
|
878 |
} |
|
879 |
} |
|
880 |
} |
|
881 |
||
882 |
void logStackTrace (String s) { |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
883 |
logger.log (Level.TRACE, s); |
7271 | 884 |
StringBuilder b = new StringBuilder (); |
885 |
StackTraceElement[] e = Thread.currentThread().getStackTrace(); |
|
886 |
for (int i=0; i<e.length; i++) { |
|
887 |
b.append (e[i].toString()).append("\n"); |
|
888 |
} |
|
41592
855537e5ad9c
8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents:
37519
diff
changeset
|
889 |
logger.log (Level.TRACE, b.toString()); |
7271 | 890 |
} |
891 |
||
892 |
static long getTimeMillis(long secs) { |
|
893 |
if (secs == -1) { |
|
894 |
return -1; |
|
895 |
} else { |
|
896 |
return secs * 1000; |
|
897 |
} |
|
898 |
} |
|
2 | 899 |
} |