author | michaelm |
Thu, 15 Oct 2009 12:03:31 +0100 | |
changeset 4047 | f5dcf30f9206 |
parent 1639 | a97859015238 |
child 5460 | 0682ab146d05 |
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.io.*; |
|
29 |
import java.nio.*; |
|
30 |
import java.nio.channels.*; |
|
31 |
import java.net.*; |
|
32 |
import javax.net.ssl.*; |
|
33 |
import java.util.*; |
|
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
34 |
import java.util.logging.Logger; |
2 | 35 |
import java.text.*; |
36 |
import sun.net.www.MessageHeader; |
|
37 |
import com.sun.net.httpserver.*; |
|
38 |
import com.sun.net.httpserver.spi.*; |
|
39 |
||
40 |
class ExchangeImpl { |
|
41 |
||
42 |
Headers reqHdrs, rspHdrs; |
|
43 |
Request req; |
|
44 |
String method; |
|
45 |
URI uri; |
|
46 |
HttpConnection connection; |
|
1511
65ddd8f149f3
6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents:
2
diff
changeset
|
47 |
long reqContentLen; |
2 | 48 |
long rspContentLen; |
49 |
/* raw streams which access the socket directly */ |
|
50 |
InputStream ris; |
|
51 |
OutputStream ros; |
|
52 |
Thread thread; |
|
53 |
/* close the underlying connection when this exchange finished */ |
|
54 |
boolean close; |
|
55 |
boolean closed; |
|
56 |
boolean http10 = false; |
|
57 |
||
58 |
/* for formatting the Date: header */ |
|
59 |
static TimeZone tz; |
|
60 |
static DateFormat df; |
|
61 |
static { |
|
62 |
String pattern = "EEE, dd MMM yyyy HH:mm:ss zzz"; |
|
63 |
tz = TimeZone.getTimeZone ("GMT"); |
|
64 |
df = new SimpleDateFormat (pattern, Locale.US); |
|
65 |
df.setTimeZone (tz); |
|
66 |
} |
|
67 |
||
68 |
/* streams which take care of the HTTP protocol framing |
|
69 |
* and are passed up to higher layers |
|
70 |
*/ |
|
71 |
InputStream uis; |
|
72 |
OutputStream uos; |
|
73 |
LeftOverInputStream uis_orig; // uis may have be a user supplied wrapper |
|
74 |
PlaceholderOutputStream uos_orig; |
|
75 |
||
76 |
boolean sentHeaders; /* true after response headers sent */ |
|
77 |
Map<String,Object> attributes; |
|
78 |
int rcode = -1; |
|
79 |
HttpPrincipal principal; |
|
80 |
ServerImpl server; |
|
81 |
||
82 |
ExchangeImpl ( |
|
1511
65ddd8f149f3
6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents:
2
diff
changeset
|
83 |
String m, URI u, Request req, long len, HttpConnection connection |
2 | 84 |
) throws IOException { |
85 |
this.req = req; |
|
86 |
this.reqHdrs = req.headers(); |
|
87 |
this.rspHdrs = new Headers(); |
|
88 |
this.method = m; |
|
89 |
this.uri = u; |
|
90 |
this.connection = connection; |
|
91 |
this.reqContentLen = len; |
|
92 |
/* ros only used for headers, body written directly to stream */ |
|
93 |
this.ros = req.outputStream(); |
|
94 |
this.ris = req.inputStream(); |
|
95 |
server = getServerImpl(); |
|
96 |
server.startExchange(); |
|
97 |
} |
|
98 |
||
99 |
public Headers getRequestHeaders () { |
|
100 |
return new UnmodifiableHeaders (reqHdrs); |
|
101 |
} |
|
102 |
||
103 |
public Headers getResponseHeaders () { |
|
104 |
return rspHdrs; |
|
105 |
} |
|
106 |
||
107 |
public URI getRequestURI () { |
|
108 |
return uri; |
|
109 |
} |
|
110 |
||
111 |
public String getRequestMethod (){ |
|
112 |
return method; |
|
113 |
} |
|
114 |
||
115 |
public HttpContextImpl getHttpContext (){ |
|
116 |
return connection.getHttpContext(); |
|
117 |
} |
|
118 |
||
119 |
public void close () { |
|
120 |
if (closed) { |
|
121 |
return; |
|
122 |
} |
|
123 |
closed = true; |
|
124 |
||
125 |
/* close the underlying connection if, |
|
126 |
* a) the streams not set up yet, no response can be sent, or |
|
127 |
* b) if the wrapper output stream is not set up, or |
|
128 |
* c) if the close of the input/outpu stream fails |
|
129 |
*/ |
|
130 |
try { |
|
131 |
if (uis_orig == null || uos == null) { |
|
132 |
connection.close(); |
|
133 |
return; |
|
134 |
} |
|
135 |
if (!uos_orig.isWrapped()) { |
|
136 |
connection.close(); |
|
137 |
return; |
|
138 |
} |
|
139 |
if (!uis_orig.isClosed()) { |
|
140 |
uis_orig.close(); |
|
141 |
} |
|
142 |
uos.close(); |
|
143 |
} catch (IOException e) { |
|
144 |
connection.close(); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
public InputStream getRequestBody () { |
|
149 |
if (uis != null) { |
|
150 |
return uis; |
|
151 |
} |
|
1511
65ddd8f149f3
6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents:
2
diff
changeset
|
152 |
if (reqContentLen == -1L) { |
2 | 153 |
uis_orig = new ChunkedInputStream (this, ris); |
154 |
uis = uis_orig; |
|
155 |
} else { |
|
156 |
uis_orig = new FixedLengthInputStream (this, ris, reqContentLen); |
|
157 |
uis = uis_orig; |
|
158 |
} |
|
159 |
return uis; |
|
160 |
} |
|
161 |
||
162 |
LeftOverInputStream getOriginalInputStream () { |
|
163 |
return uis_orig; |
|
164 |
} |
|
165 |
||
166 |
public int getResponseCode () { |
|
167 |
return rcode; |
|
168 |
} |
|
169 |
||
170 |
public OutputStream getResponseBody () { |
|
171 |
/* TODO. Change spec to remove restriction below. Filters |
|
172 |
* cannot work with this restriction |
|
173 |
* |
|
174 |
* if (!sentHeaders) { |
|
175 |
* throw new IllegalStateException ("headers not sent"); |
|
176 |
* } |
|
177 |
*/ |
|
178 |
if (uos == null) { |
|
179 |
uos_orig = new PlaceholderOutputStream (null); |
|
180 |
uos = uos_orig; |
|
181 |
} |
|
182 |
return uos; |
|
183 |
} |
|
184 |
||
185 |
||
186 |
/* returns the place holder stream, which is the stream |
|
187 |
* returned from the 1st call to getResponseBody() |
|
188 |
* The "real" ouputstream is then placed inside this |
|
189 |
*/ |
|
190 |
PlaceholderOutputStream getPlaceholderResponseBody () { |
|
191 |
getResponseBody(); |
|
192 |
return uos_orig; |
|
193 |
} |
|
194 |
||
195 |
public void sendResponseHeaders (int rCode, long contentLen) |
|
196 |
throws IOException |
|
197 |
{ |
|
198 |
if (sentHeaders) { |
|
199 |
throw new IOException ("headers already sent"); |
|
200 |
} |
|
201 |
this.rcode = rCode; |
|
202 |
String statusLine = "HTTP/1.1 "+rCode+Code.msg(rCode)+"\r\n"; |
|
203 |
OutputStream tmpout = new BufferedOutputStream (ros); |
|
204 |
PlaceholderOutputStream o = getPlaceholderResponseBody(); |
|
205 |
tmpout.write (bytes(statusLine, 0), 0, statusLine.length()); |
|
206 |
boolean noContentToSend = false; // assume there is content |
|
207 |
rspHdrs.set ("Date", df.format (new Date())); |
|
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
208 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
209 |
/* check for response type that is not allowed to send a body */ |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
210 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
211 |
if ((rCode>=100 && rCode <200) /* informational */ |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
212 |
||(rCode == 204) /* no content */ |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
213 |
||(rCode == 304)) /* not modified */ |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
214 |
{ |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
215 |
if (contentLen != -1) { |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
216 |
Logger logger = server.getLogger(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
217 |
String msg = "sendResponseHeaders: rCode = "+ rCode |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
218 |
+ ": forcing contentLen = -1"; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
219 |
logger.warning (msg); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
220 |
} |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
221 |
contentLen = -1; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
1639
diff
changeset
|
222 |
} |
2 | 223 |
if (contentLen == 0) { |
224 |
if (http10) { |
|
225 |
o.setWrappedStream (new UndefLengthOutputStream (this, ros)); |
|
226 |
close = true; |
|
227 |
} else { |
|
228 |
rspHdrs.set ("Transfer-encoding", "chunked"); |
|
229 |
o.setWrappedStream (new ChunkedOutputStream (this, ros)); |
|
230 |
} |
|
231 |
} else { |
|
232 |
if (contentLen == -1) { |
|
233 |
noContentToSend = true; |
|
234 |
contentLen = 0; |
|
235 |
} |
|
236 |
/* content len might already be set, eg to implement HEAD resp */ |
|
237 |
if (rspHdrs.getFirst ("Content-length") == null) { |
|
238 |
rspHdrs.set ("Content-length", Long.toString(contentLen)); |
|
239 |
} |
|
240 |
o.setWrappedStream (new FixedLengthOutputStream (this, ros, contentLen)); |
|
241 |
} |
|
242 |
write (rspHdrs, tmpout); |
|
243 |
this.rspContentLen = contentLen; |
|
244 |
tmpout.flush() ; |
|
245 |
tmpout = null; |
|
246 |
sentHeaders = true; |
|
247 |
if (noContentToSend) { |
|
248 |
WriteFinishedEvent e = new WriteFinishedEvent (this); |
|
249 |
server.addEvent (e); |
|
250 |
closed = true; |
|
251 |
} |
|
252 |
server.logReply (rCode, req.requestLine(), null); |
|
253 |
} |
|
254 |
||
255 |
void write (Headers map, OutputStream os) throws IOException { |
|
256 |
Set<Map.Entry<String,List<String>>> entries = map.entrySet(); |
|
257 |
for (Map.Entry<String,List<String>> entry : entries) { |
|
258 |
String key = entry.getKey(); |
|
259 |
byte[] buf; |
|
260 |
List<String> values = entry.getValue(); |
|
261 |
for (String val : values) { |
|
262 |
int i = key.length(); |
|
263 |
buf = bytes (key, 2); |
|
264 |
buf[i++] = ':'; |
|
265 |
buf[i++] = ' '; |
|
266 |
os.write (buf, 0, i); |
|
267 |
buf = bytes (val, 2); |
|
268 |
i = val.length(); |
|
269 |
buf[i++] = '\r'; |
|
270 |
buf[i++] = '\n'; |
|
271 |
os.write (buf, 0, i); |
|
272 |
} |
|
273 |
} |
|
274 |
os.write ('\r'); |
|
275 |
os.write ('\n'); |
|
276 |
} |
|
277 |
||
278 |
private byte[] rspbuf = new byte [128]; // used by bytes() |
|
279 |
||
280 |
/** |
|
281 |
* convert string to byte[], using rspbuf |
|
282 |
* Make sure that at least "extra" bytes are free at end |
|
283 |
* of rspbuf. Reallocate rspbuf if not big enough. |
|
284 |
* caller must check return value to see if rspbuf moved |
|
285 |
*/ |
|
286 |
private byte[] bytes (String s, int extra) { |
|
287 |
int slen = s.length(); |
|
288 |
if (slen+extra > rspbuf.length) { |
|
289 |
int diff = slen + extra - rspbuf.length; |
|
290 |
rspbuf = new byte [2* (rspbuf.length + diff)]; |
|
291 |
} |
|
292 |
char c[] = s.toCharArray(); |
|
293 |
for (int i=0; i<c.length; i++) { |
|
294 |
rspbuf[i] = (byte)c[i]; |
|
295 |
} |
|
296 |
return rspbuf; |
|
297 |
} |
|
298 |
||
299 |
public InetSocketAddress getRemoteAddress (){ |
|
300 |
Socket s = connection.getChannel().socket(); |
|
301 |
InetAddress ia = s.getInetAddress(); |
|
302 |
int port = s.getPort(); |
|
303 |
return new InetSocketAddress (ia, port); |
|
304 |
} |
|
305 |
||
306 |
public InetSocketAddress getLocalAddress (){ |
|
307 |
Socket s = connection.getChannel().socket(); |
|
308 |
InetAddress ia = s.getLocalAddress(); |
|
309 |
int port = s.getLocalPort(); |
|
310 |
return new InetSocketAddress (ia, port); |
|
311 |
} |
|
312 |
||
313 |
public String getProtocol (){ |
|
314 |
String reqline = req.requestLine(); |
|
315 |
int index = reqline.lastIndexOf (' '); |
|
316 |
return reqline.substring (index+1); |
|
317 |
} |
|
318 |
||
319 |
public SSLSession getSSLSession () { |
|
320 |
SSLEngine e = connection.getSSLEngine(); |
|
321 |
if (e == null) { |
|
322 |
return null; |
|
323 |
} |
|
324 |
return e.getSession(); |
|
325 |
} |
|
326 |
||
327 |
public Object getAttribute (String name) { |
|
328 |
if (name == null) { |
|
329 |
throw new NullPointerException ("null name parameter"); |
|
330 |
} |
|
331 |
if (attributes == null) { |
|
332 |
attributes = getHttpContext().getAttributes(); |
|
333 |
} |
|
334 |
return attributes.get (name); |
|
335 |
} |
|
336 |
||
337 |
public void setAttribute (String name, Object value) { |
|
338 |
if (name == null) { |
|
339 |
throw new NullPointerException ("null name parameter"); |
|
340 |
} |
|
341 |
if (attributes == null) { |
|
342 |
attributes = getHttpContext().getAttributes(); |
|
343 |
} |
|
344 |
attributes.put (name, value); |
|
345 |
} |
|
346 |
||
347 |
public void setStreams (InputStream i, OutputStream o) { |
|
348 |
assert uis != null; |
|
349 |
if (i != null) { |
|
350 |
uis = i; |
|
351 |
} |
|
352 |
if (o != null) { |
|
353 |
uos = o; |
|
354 |
} |
|
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* PP |
|
359 |
*/ |
|
360 |
HttpConnection getConnection () { |
|
361 |
return connection; |
|
362 |
} |
|
363 |
||
364 |
ServerImpl getServerImpl () { |
|
365 |
return getHttpContext().getServerImpl(); |
|
366 |
} |
|
367 |
||
368 |
public HttpPrincipal getPrincipal () { |
|
369 |
return principal; |
|
370 |
} |
|
371 |
||
372 |
void setPrincipal (HttpPrincipal principal) { |
|
373 |
this.principal = principal; |
|
374 |
} |
|
375 |
||
376 |
static ExchangeImpl get (HttpExchange t) { |
|
377 |
if (t instanceof HttpExchangeImpl) { |
|
378 |
return ((HttpExchangeImpl)t).getExchangeImpl(); |
|
379 |
} else { |
|
380 |
assert t instanceof HttpsExchangeImpl; |
|
381 |
return ((HttpsExchangeImpl)t).getExchangeImpl(); |
|
382 |
} |
|
383 |
} |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* An OutputStream which wraps another stream |
|
388 |
* which is supplied either at creation time, or sometime later. |
|
389 |
* If a caller/user tries to write to this stream before |
|
390 |
* the wrapped stream has been provided, then an IOException will |
|
391 |
* be thrown. |
|
392 |
*/ |
|
393 |
class PlaceholderOutputStream extends java.io.OutputStream { |
|
394 |
||
395 |
OutputStream wrapped; |
|
396 |
||
397 |
PlaceholderOutputStream (OutputStream os) { |
|
398 |
wrapped = os; |
|
399 |
} |
|
400 |
||
401 |
void setWrappedStream (OutputStream os) { |
|
402 |
wrapped = os; |
|
403 |
} |
|
404 |
||
405 |
boolean isWrapped () { |
|
406 |
return wrapped != null; |
|
407 |
} |
|
408 |
||
409 |
private void checkWrap () throws IOException { |
|
410 |
if (wrapped == null) { |
|
411 |
throw new IOException ("response headers not sent yet"); |
|
412 |
} |
|
413 |
} |
|
414 |
||
415 |
public void write(int b) throws IOException { |
|
416 |
checkWrap(); |
|
417 |
wrapped.write (b); |
|
418 |
} |
|
419 |
||
420 |
public void write(byte b[]) throws IOException { |
|
421 |
checkWrap(); |
|
422 |
wrapped.write (b); |
|
423 |
} |
|
424 |
||
425 |
public void write(byte b[], int off, int len) throws IOException { |
|
426 |
checkWrap(); |
|
427 |
wrapped.write (b, off, len); |
|
428 |
} |
|
429 |
||
430 |
public void flush() throws IOException { |
|
431 |
checkWrap(); |
|
432 |
wrapped.flush(); |
|
433 |
} |
|
434 |
||
435 |
public void close() throws IOException { |
|
436 |
checkWrap(); |
|
437 |
wrapped.close(); |
|
438 |
} |
|
439 |
} |