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