author | asaha |
Thu, 16 Apr 2009 21:08:04 -0700 | |
changeset 2624 | 1ae5a9028dd4 |
parent 1639 | a97859015238 |
parent 2612 | d7fb0809c7e4 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1639 | 2 |
* Copyright 2005-2008 Sun Microsystems, Inc. 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 |
|
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.util.*; |
|
29 |
import java.nio.*; |
|
30 |
import java.net.*; |
|
31 |
import java.io.*; |
|
32 |
import java.nio.channels.*; |
|
33 |
import com.sun.net.httpserver.*; |
|
34 |
import com.sun.net.httpserver.spi.*; |
|
35 |
||
36 |
/** |
|
37 |
*/ |
|
38 |
class Request { |
|
39 |
||
40 |
final static int BUF_LEN = 2048; |
|
41 |
final static byte CR = 13; |
|
42 |
final static byte LF = 10; |
|
43 |
||
44 |
private String startLine; |
|
45 |
private SocketChannel chan; |
|
46 |
private InputStream is; |
|
47 |
private OutputStream os; |
|
48 |
||
49 |
Request (InputStream rawInputStream, OutputStream rawout) throws IOException { |
|
50 |
this.chan = chan; |
|
51 |
is = rawInputStream; |
|
52 |
os = rawout; |
|
53 |
do { |
|
54 |
startLine = readLine(); |
|
2612
d7fb0809c7e4
6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents:
2
diff
changeset
|
55 |
if (startLine == null) { |
d7fb0809c7e4
6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents:
2
diff
changeset
|
56 |
return; |
d7fb0809c7e4
6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents:
2
diff
changeset
|
57 |
} |
2 | 58 |
/* skip blank lines */ |
1511
65ddd8f149f3
6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents:
2
diff
changeset
|
59 |
} while (startLine == null ? false : startLine.equals ("")); |
2 | 60 |
} |
61 |
||
62 |
||
63 |
char[] buf = new char [BUF_LEN]; |
|
64 |
int pos; |
|
65 |
StringBuffer lineBuf; |
|
66 |
||
67 |
public InputStream inputStream () { |
|
68 |
return is; |
|
69 |
} |
|
70 |
||
71 |
public OutputStream outputStream () { |
|
72 |
return os; |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* read a line from the stream returning as a String. |
|
77 |
* Not used for reading headers. |
|
78 |
*/ |
|
79 |
||
80 |
public String readLine () throws IOException { |
|
81 |
boolean gotCR = false, gotLF = false; |
|
82 |
pos = 0; lineBuf = new StringBuffer(); |
|
83 |
while (!gotLF) { |
|
84 |
int c = is.read(); |
|
85 |
if (c == -1) { |
|
86 |
return null; |
|
87 |
} |
|
88 |
if (gotCR) { |
|
89 |
if (c == LF) { |
|
90 |
gotLF = true; |
|
91 |
} else { |
|
92 |
gotCR = false; |
|
93 |
consume (CR); |
|
94 |
consume (c); |
|
95 |
} |
|
96 |
} else { |
|
97 |
if (c == CR) { |
|
98 |
gotCR = true; |
|
99 |
} else { |
|
100 |
consume (c); |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
lineBuf.append (buf, 0, pos); |
|
105 |
return new String (lineBuf); |
|
106 |
} |
|
107 |
||
108 |
private void consume (int c) { |
|
109 |
if (pos == BUF_LEN) { |
|
110 |
lineBuf.append (buf); |
|
111 |
pos = 0; |
|
112 |
} |
|
113 |
buf[pos++] = (char)c; |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* returns the request line (first line of a request) |
|
118 |
*/ |
|
119 |
public String requestLine () { |
|
120 |
return startLine; |
|
121 |
} |
|
122 |
||
123 |
Headers hdrs = null; |
|
124 |
||
125 |
Headers headers () throws IOException { |
|
126 |
if (hdrs != null) { |
|
127 |
return hdrs; |
|
128 |
} |
|
129 |
hdrs = new Headers(); |
|
130 |
||
131 |
char s[] = new char[10]; |
|
132 |
int firstc = is.read(); |
|
133 |
while (firstc != LF && firstc != CR && firstc >= 0) { |
|
134 |
int len = 0; |
|
135 |
int keyend = -1; |
|
136 |
int c; |
|
137 |
boolean inKey = firstc > ' '; |
|
138 |
s[len++] = (char) firstc; |
|
139 |
parseloop:{ |
|
140 |
while ((c = is.read()) >= 0) { |
|
141 |
switch (c) { |
|
142 |
case ':': |
|
143 |
if (inKey && len > 0) |
|
144 |
keyend = len; |
|
145 |
inKey = false; |
|
146 |
break; |
|
147 |
case '\t': |
|
148 |
c = ' '; |
|
149 |
case ' ': |
|
150 |
inKey = false; |
|
151 |
break; |
|
152 |
case CR: |
|
153 |
case LF: |
|
154 |
firstc = is.read(); |
|
155 |
if (c == CR && firstc == LF) { |
|
156 |
firstc = is.read(); |
|
157 |
if (firstc == CR) |
|
158 |
firstc = is.read(); |
|
159 |
} |
|
160 |
if (firstc == LF || firstc == CR || firstc > ' ') |
|
161 |
break parseloop; |
|
162 |
/* continuation */ |
|
163 |
c = ' '; |
|
164 |
break; |
|
165 |
} |
|
166 |
if (len >= s.length) { |
|
167 |
char ns[] = new char[s.length * 2]; |
|
168 |
System.arraycopy(s, 0, ns, 0, len); |
|
169 |
s = ns; |
|
170 |
} |
|
171 |
s[len++] = (char) c; |
|
172 |
} |
|
173 |
firstc = -1; |
|
174 |
} |
|
175 |
while (len > 0 && s[len - 1] <= ' ') |
|
176 |
len--; |
|
177 |
String k; |
|
178 |
if (keyend <= 0) { |
|
179 |
k = null; |
|
180 |
keyend = 0; |
|
181 |
} else { |
|
182 |
k = String.copyValueOf(s, 0, keyend); |
|
183 |
if (keyend < len && s[keyend] == ':') |
|
184 |
keyend++; |
|
185 |
while (keyend < len && s[keyend] <= ' ') |
|
186 |
keyend++; |
|
187 |
} |
|
188 |
String v; |
|
189 |
if (keyend >= len) |
|
190 |
v = new String(); |
|
191 |
else |
|
192 |
v = String.copyValueOf(s, keyend, len - keyend); |
|
193 |
hdrs.add (k,v); |
|
194 |
} |
|
195 |
return hdrs; |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Implements blocking reading semantics on top of a non-blocking channel |
|
200 |
*/ |
|
201 |
||
202 |
static class ReadStream extends InputStream { |
|
203 |
SocketChannel channel; |
|
204 |
SelectorCache sc; |
|
205 |
Selector selector; |
|
206 |
ByteBuffer chanbuf; |
|
207 |
SelectionKey key; |
|
208 |
int available; |
|
209 |
byte[] one; |
|
210 |
boolean closed = false, eof = false; |
|
211 |
ByteBuffer markBuf; /* reads may be satisifed from this buffer */ |
|
212 |
boolean marked; |
|
213 |
boolean reset; |
|
214 |
int readlimit; |
|
215 |
static long readTimeout; |
|
216 |
ServerImpl server; |
|
217 |
||
218 |
static { |
|
219 |
readTimeout = ServerConfig.getReadTimeout(); |
|
220 |
} |
|
221 |
||
222 |
public ReadStream (ServerImpl server, SocketChannel chan) throws IOException { |
|
223 |
this.channel = chan; |
|
224 |
this.server = server; |
|
225 |
sc = SelectorCache.getSelectorCache(); |
|
226 |
selector = sc.getSelector(); |
|
227 |
chanbuf = ByteBuffer.allocate (8* 1024); |
|
228 |
key = chan.register (selector, SelectionKey.OP_READ); |
|
229 |
available = 0; |
|
230 |
one = new byte[1]; |
|
231 |
closed = marked = reset = false; |
|
232 |
} |
|
233 |
||
234 |
public synchronized int read (byte[] b) throws IOException { |
|
235 |
return read (b, 0, b.length); |
|
236 |
} |
|
237 |
||
238 |
public synchronized int read () throws IOException { |
|
239 |
int result = read (one, 0, 1); |
|
240 |
if (result == 1) { |
|
241 |
return one[0] & 0xFF; |
|
242 |
} else { |
|
243 |
return -1; |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
public synchronized int read (byte[] b, int off, int srclen) throws IOException { |
|
248 |
||
249 |
int canreturn, willreturn; |
|
250 |
||
251 |
if (closed) |
|
252 |
throw new IOException ("Stream closed"); |
|
253 |
||
254 |
if (eof) { |
|
255 |
return -1; |
|
256 |
} |
|
257 |
||
258 |
if (reset) { /* satisfy from markBuf */ |
|
259 |
canreturn = markBuf.remaining (); |
|
260 |
willreturn = canreturn>srclen ? srclen : canreturn; |
|
261 |
markBuf.get(b, off, willreturn); |
|
262 |
if (canreturn == willreturn) { |
|
263 |
reset = false; |
|
264 |
} |
|
265 |
} else { /* satisfy from channel */ |
|
266 |
canreturn = available(); |
|
267 |
while (canreturn == 0 && !eof) { |
|
268 |
block (); |
|
269 |
canreturn = available(); |
|
270 |
} |
|
271 |
if (eof) { |
|
272 |
return -1; |
|
273 |
} |
|
274 |
willreturn = canreturn>srclen ? srclen : canreturn; |
|
275 |
chanbuf.get(b, off, willreturn); |
|
276 |
available -= willreturn; |
|
277 |
||
278 |
if (marked) { /* copy into markBuf */ |
|
279 |
try { |
|
280 |
markBuf.put (b, off, willreturn); |
|
281 |
} catch (BufferOverflowException e) { |
|
282 |
marked = false; |
|
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
return willreturn; |
|
287 |
} |
|
288 |
||
289 |
public synchronized int available () throws IOException { |
|
290 |
if (closed) |
|
291 |
throw new IOException ("Stream is closed"); |
|
292 |
||
293 |
if (eof) |
|
294 |
return -1; |
|
295 |
||
296 |
if (reset) |
|
297 |
return markBuf.remaining(); |
|
298 |
||
299 |
if (available > 0) |
|
300 |
return available; |
|
301 |
||
302 |
chanbuf.clear (); |
|
303 |
available = channel.read (chanbuf); |
|
304 |
if (available > 0) { |
|
305 |
chanbuf.flip(); |
|
306 |
} else if (available == -1) { |
|
307 |
eof = true; |
|
308 |
available = 0; |
|
309 |
} |
|
310 |
return available; |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* block() only called when available==0 and buf is empty |
|
315 |
*/ |
|
316 |
private synchronized void block () throws IOException { |
|
317 |
long currtime = server.getTime(); |
|
318 |
long maxtime = currtime + readTimeout; |
|
319 |
||
320 |
while (currtime < maxtime) { |
|
321 |
if (selector.select (readTimeout) == 1) { |
|
322 |
selector.selectedKeys().clear(); |
|
323 |
available (); |
|
324 |
return; |
|
325 |
} |
|
326 |
currtime = server.getTime(); |
|
327 |
} |
|
328 |
throw new SocketTimeoutException ("no data received"); |
|
329 |
} |
|
330 |
||
331 |
public void close () throws IOException { |
|
332 |
if (closed) { |
|
333 |
return; |
|
334 |
} |
|
335 |
channel.close (); |
|
336 |
selector.selectNow(); |
|
337 |
sc.freeSelector(selector); |
|
338 |
closed = true; |
|
339 |
} |
|
340 |
||
341 |
public synchronized void mark (int readlimit) { |
|
342 |
if (closed) |
|
343 |
return; |
|
344 |
this.readlimit = readlimit; |
|
345 |
markBuf = ByteBuffer.allocate (readlimit); |
|
346 |
marked = true; |
|
347 |
reset = false; |
|
348 |
} |
|
349 |
||
350 |
public synchronized void reset () throws IOException { |
|
351 |
if (closed ) |
|
352 |
return; |
|
353 |
if (!marked) |
|
354 |
throw new IOException ("Stream not marked"); |
|
355 |
marked = false; |
|
356 |
reset = true; |
|
357 |
markBuf.flip (); |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
static class WriteStream extends java.io.OutputStream { |
|
362 |
SocketChannel channel; |
|
363 |
ByteBuffer buf; |
|
364 |
SelectionKey key; |
|
365 |
SelectorCache sc; |
|
366 |
Selector selector; |
|
367 |
boolean closed; |
|
368 |
byte[] one; |
|
369 |
ServerImpl server; |
|
370 |
static long writeTimeout; |
|
371 |
||
372 |
static { |
|
373 |
writeTimeout = ServerConfig.getWriteTimeout(); |
|
374 |
} |
|
375 |
||
376 |
public WriteStream (ServerImpl server, SocketChannel channel) throws IOException { |
|
377 |
this.channel = channel; |
|
378 |
this.server = server; |
|
379 |
sc = SelectorCache.getSelectorCache(); |
|
380 |
selector = sc.getSelector(); |
|
381 |
key = channel.register (selector, SelectionKey.OP_WRITE); |
|
382 |
closed = false; |
|
383 |
one = new byte [1]; |
|
384 |
buf = ByteBuffer.allocate (4096); |
|
385 |
} |
|
386 |
||
387 |
public synchronized void write (int b) throws IOException { |
|
388 |
one[0] = (byte)b; |
|
389 |
write (one, 0, 1); |
|
390 |
} |
|
391 |
||
392 |
public synchronized void write (byte[] b) throws IOException { |
|
393 |
write (b, 0, b.length); |
|
394 |
} |
|
395 |
||
396 |
public synchronized void write (byte[] b, int off, int len) throws IOException { |
|
397 |
int l = len; |
|
398 |
if (closed) |
|
399 |
throw new IOException ("stream is closed"); |
|
400 |
||
401 |
int cap = buf.capacity(); |
|
402 |
if (cap < len) { |
|
403 |
int diff = len - cap; |
|
404 |
buf = ByteBuffer.allocate (2*(cap+diff)); |
|
405 |
} |
|
406 |
buf.clear(); |
|
407 |
buf.put (b, off, len); |
|
408 |
buf.flip (); |
|
409 |
int n; |
|
410 |
while ((n = channel.write (buf)) < l) { |
|
411 |
l -= n; |
|
412 |
if (l == 0) |
|
413 |
return; |
|
414 |
block(); |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
void block () throws IOException { |
|
419 |
long currtime = server.getTime(); |
|
420 |
long maxtime = currtime + writeTimeout; |
|
421 |
||
422 |
while (currtime < maxtime) { |
|
423 |
if (selector.select (writeTimeout) == 1) { |
|
424 |
selector.selectedKeys().clear (); |
|
425 |
return; |
|
426 |
} |
|
427 |
currtime = server.getTime(); |
|
428 |
} |
|
429 |
throw new SocketTimeoutException ("write blocked too long"); |
|
430 |
} |
|
431 |
||
432 |
||
433 |
public void close () throws IOException { |
|
434 |
if (closed) |
|
435 |
return; |
|
436 |
channel.close (); |
|
437 |
selector.selectNow(); |
|
438 |
sc.freeSelector(selector); |
|
439 |
closed = true; |
|
440 |
} |
|
441 |
} |
|
442 |
} |