Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/Response.java @ 876:2efdb98f3543
use just one fixed Connector in Server
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 04 Oct 2016 14:05:45 -0600 |
parents | 8e9db0bbf4f9 |
children | df84a1741687 |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.server; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.io.PrintWriter; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.util.Collection; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.util.ArrayList; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import java.util.Collections; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import java.util.EnumSet; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import java.util.Enumeration; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import java.util.Locale; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import javax.servlet.RequestDispatcher; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import javax.servlet.ServletOutputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import javax.servlet.SessionTrackingMode; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import javax.servlet.http.Cookie; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import javax.servlet.http.HttpServletResponse; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import javax.servlet.http.HttpSession; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 import org.eclipse.jetty.http.HttpCookie; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 import org.eclipse.jetty.http.HttpFields; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 import org.eclipse.jetty.http.HttpGenerator; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 import org.eclipse.jetty.http.HttpHeaderValues; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 import org.eclipse.jetty.http.HttpHeaders; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 import org.eclipse.jetty.http.HttpSchemes; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 import org.eclipse.jetty.http.HttpStatus; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 import org.eclipse.jetty.http.HttpURI; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.http.HttpVersions; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 import org.eclipse.jetty.http.MimeTypes; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 import org.eclipse.jetty.io.BufferCache.CachedBuffer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 import org.eclipse.jetty.server.handler.ContextHandler; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 import org.eclipse.jetty.server.handler.ErrorHandler; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 import org.eclipse.jetty.util.ByteArrayISO8859Writer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 import org.eclipse.jetty.util.QuotedStringTokenizer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 import org.eclipse.jetty.util.StringUtil; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 import org.eclipse.jetty.util.URIUtil; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
813
diff
changeset
|
54 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
813
diff
changeset
|
55 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
56 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 /** Response. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 * <p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 * Implements {@link javax.servlet.http.HttpServletResponse} from the <code>javax.servlet.http</code> package. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 * </p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 public class Response implements HttpServletResponse |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 { |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
813
diff
changeset
|
64 private static final Logger LOG = LoggerFactory.getLogger(Response.class); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
65 |
813 | 66 |
67 public static final int | |
68 NONE=0, | |
69 STREAM=1, | |
70 WRITER=2; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 |
813 | 72 /** |
73 * If a header name starts with this string, the header (stripped of the prefix) | |
74 * can be set during include using only {@link #setHeader(String, String)} or | |
75 * {@link #addHeader(String, String)}. | |
76 */ | |
77 public final static String SET_INCLUDE_HEADER_PREFIX = "org.eclipse.jetty.server.include."; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
78 |
813 | 79 /** |
80 * If this string is found within the comment of a cookie added with {@link #addCookie(Cookie)}, then the cookie | |
81 * will be set as HTTP ONLY. | |
82 */ | |
83 public final static String HTTP_ONLY_COMMENT="__HTTP_ONLY__"; | |
84 | |
85 | |
86 /* ------------------------------------------------------------ */ | |
87 public static Response getResponse(HttpServletResponse response) | |
88 { | |
89 if (response instanceof Response) | |
90 return (Response)response; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
91 |
813 | 92 return AbstractHttpConnection.getCurrentConnection().getResponse(); |
93 } | |
94 | |
95 private final AbstractHttpConnection _connection; | |
96 private int _status=SC_OK; | |
97 private String _reason; | |
98 private Locale _locale; | |
99 private String _mimeType; | |
100 private CachedBuffer _cachedMimeType; | |
101 private String _characterEncoding; | |
102 private boolean _explicitEncoding; | |
103 private String _contentType; | |
104 private volatile int _outputState; | |
105 private PrintWriter _writer; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
106 |
813 | 107 /* ------------------------------------------------------------ */ |
108 /** | |
109 * | |
110 */ | |
111 public Response(AbstractHttpConnection connection) | |
112 { | |
113 _connection=connection; | |
114 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
115 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
116 |
813 | 117 /* ------------------------------------------------------------ */ |
118 /* | |
119 * @see javax.servlet.ServletResponse#reset() | |
120 */ | |
121 protected void recycle() | |
122 { | |
123 _status=SC_OK; | |
124 _reason=null; | |
125 _locale=null; | |
126 _mimeType=null; | |
127 _cachedMimeType=null; | |
128 _characterEncoding=null; | |
129 _explicitEncoding=false; | |
130 _contentType=null; | |
131 _writer=null; | |
132 _outputState=NONE; | |
133 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
134 |
813 | 135 /* ------------------------------------------------------------ */ |
136 /* | |
137 * @see javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http.Cookie) | |
138 */ | |
139 public void addCookie(HttpCookie cookie) | |
140 { | |
141 _connection.getResponseFields().addSetCookie(cookie); | |
142 } | |
143 | |
144 /* ------------------------------------------------------------ */ | |
145 /* | |
146 * @see javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http.Cookie) | |
147 */ | |
148 public void addCookie(Cookie cookie) | |
149 { | |
150 String comment=cookie.getComment(); | |
151 boolean http_only=false; | |
152 | |
153 if (comment!=null) | |
154 { | |
155 int i=comment.indexOf(HTTP_ONLY_COMMENT); | |
156 if (i>=0) | |
157 { | |
158 http_only=true; | |
159 comment=comment.replace(HTTP_ONLY_COMMENT,"").trim(); | |
160 if (comment.length()==0) | |
161 comment=null; | |
162 } | |
163 } | |
164 _connection.getResponseFields().addSetCookie(cookie.getName(), | |
165 cookie.getValue(), | |
166 cookie.getDomain(), | |
167 cookie.getPath(), | |
168 cookie.getMaxAge(), | |
169 comment, | |
170 cookie.getSecure(), | |
171 http_only || cookie.isHttpOnly(), | |
172 cookie.getVersion()); | |
173 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
174 |
813 | 175 /* ------------------------------------------------------------ */ |
176 /* | |
177 * @see javax.servlet.http.HttpServletResponse#containsHeader(java.lang.String) | |
178 */ | |
179 public boolean containsHeader(String name) | |
180 { | |
181 return _connection.getResponseFields().containsKey(name); | |
182 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
183 |
813 | 184 /* ------------------------------------------------------------ */ |
185 /* | |
186 * @see javax.servlet.http.HttpServletResponse#encodeURL(java.lang.String) | |
187 */ | |
188 public String encodeURL(String url) | |
189 { | |
809
09d518d313b7
remove SessionManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
190 throw new UnsupportedOperationException(); |
813 | 191 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
192 |
813 | 193 /* ------------------------------------------------------------ */ |
194 /** | |
195 * @see javax.servlet.http.HttpServletResponse#encodeRedirectURL(java.lang.String) | |
196 */ | |
197 public String encodeRedirectURL(String url) | |
198 { | |
199 return encodeURL(url); | |
200 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
201 |
813 | 202 /* ------------------------------------------------------------ */ |
203 @Deprecated | |
204 public String encodeUrl(String url) | |
205 { | |
206 return encodeURL(url); | |
207 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
208 |
813 | 209 /* ------------------------------------------------------------ */ |
210 @Deprecated | |
211 public String encodeRedirectUrl(String url) | |
212 { | |
213 return encodeRedirectURL(url); | |
214 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
215 |
813 | 216 /* ------------------------------------------------------------ */ |
217 /* | |
218 * @see javax.servlet.http.HttpServletResponse#sendError(int, java.lang.String) | |
219 */ | |
220 public void sendError(int code, String message) throws IOException | |
221 { | |
222 if (_connection.isIncluding()) | |
223 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
224 |
813 | 225 if (isCommitted()) |
226 LOG.warn("Committed before "+code+" "+message); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
227 |
813 | 228 resetBuffer(); |
229 _characterEncoding=null; | |
230 setHeader(HttpHeaders.EXPIRES,null); | |
231 setHeader(HttpHeaders.LAST_MODIFIED,null); | |
232 setHeader(HttpHeaders.CACHE_CONTROL,null); | |
233 setHeader(HttpHeaders.CONTENT_TYPE,null); | |
234 setHeader(HttpHeaders.CONTENT_LENGTH,null); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
235 |
813 | 236 _outputState=NONE; |
237 setStatus(code,message); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
238 |
813 | 239 if (message==null) |
240 message=HttpStatus.getMessage(code); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
241 |
813 | 242 // If we are allowed to have a body |
243 if (code!=SC_NO_CONTENT && | |
244 code!=SC_NOT_MODIFIED && | |
245 code!=SC_PARTIAL_CONTENT && | |
246 code>=SC_OK) | |
247 { | |
248 Request request = _connection.getRequest(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
249 |
813 | 250 ErrorHandler error_handler = null; |
251 ContextHandler.Context context = request.getContext(); | |
252 if (context!=null) | |
253 error_handler=context.getContextHandler().getErrorHandler(); | |
254 if (error_handler==null) | |
255 error_handler = _connection.getConnector().getServer().getBean(ErrorHandler.class); | |
256 if (error_handler!=null) | |
257 { | |
258 request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE,new Integer(code)); | |
259 request.setAttribute(RequestDispatcher.ERROR_MESSAGE, message); | |
260 request.setAttribute(RequestDispatcher.ERROR_REQUEST_URI, request.getRequestURI()); | |
261 error_handler.handle(null,_connection.getRequest(),_connection.getRequest(),this ); | |
262 } | |
263 else | |
264 { | |
265 setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store"); | |
266 setContentType(MimeTypes.TEXT_HTML_8859_1); | |
267 ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(2048); | |
268 if (message != null) | |
269 { | |
270 message= StringUtil.replace(message, "&", "&"); | |
271 message= StringUtil.replace(message, "<", "<"); | |
272 message= StringUtil.replace(message, ">", ">"); | |
273 } | |
274 String uri= request.getRequestURI(); | |
275 if (uri!=null) | |
276 { | |
277 uri= StringUtil.replace(uri, "&", "&"); | |
278 uri= StringUtil.replace(uri, "<", "<"); | |
279 uri= StringUtil.replace(uri, ">", ">"); | |
280 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
281 |
813 | 282 writer.write("<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\"/>\n"); |
283 writer.write("<title>Error "); | |
284 writer.write(Integer.toString(code)); | |
285 writer.write(' '); | |
286 if (message==null) | |
287 message=HttpStatus.getMessage(code); | |
288 writer.write(message); | |
289 writer.write("</title>\n</head>\n<body>\n<h2>HTTP ERROR: "); | |
290 writer.write(Integer.toString(code)); | |
291 writer.write("</h2>\n<p>Problem accessing "); | |
292 writer.write(uri); | |
293 writer.write(". Reason:\n<pre> "); | |
294 writer.write(message); | |
295 writer.write("</pre>"); | |
296 writer.write("</p>\n<hr /><i><small>Powered by Jetty://</small></i>"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
297 |
813 | 298 for (int i= 0; i < 20; i++) |
299 writer.write("\n "); | |
300 writer.write("\n</body>\n</html>\n"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
301 |
813 | 302 writer.flush(); |
303 setContentLength(writer.size()); | |
304 writer.writeTo(getOutputStream()); | |
305 writer.destroy(); | |
306 } | |
307 } | |
308 else if (code!=SC_PARTIAL_CONTENT) | |
309 { | |
310 _connection.getRequestFields().remove(HttpHeaders.CONTENT_TYPE_BUFFER); | |
311 _connection.getRequestFields().remove(HttpHeaders.CONTENT_LENGTH_BUFFER); | |
312 _characterEncoding=null; | |
313 _mimeType=null; | |
314 _cachedMimeType=null; | |
315 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
316 |
813 | 317 complete(); |
318 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
319 |
813 | 320 /* ------------------------------------------------------------ */ |
321 /* | |
322 * @see javax.servlet.http.HttpServletResponse#sendError(int) | |
323 */ | |
324 public void sendError(int sc) throws IOException | |
325 { | |
326 if (sc==102) | |
327 sendProcessing(); | |
328 else | |
329 sendError(sc,null); | |
330 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
331 |
813 | 332 /* ------------------------------------------------------------ */ |
333 /* Send a 102-Processing response. | |
334 * If the connection is a HTTP connection, the version is 1.1 and the | |
335 * request has a Expect header starting with 102, then a 102 response is | |
336 * sent. This indicates that the request still be processed and real response | |
337 * can still be sent. This method is called by sendError if it is passed 102. | |
338 * @see javax.servlet.http.HttpServletResponse#sendError(int) | |
339 */ | |
340 public void sendProcessing() throws IOException | |
341 { | |
342 if (_connection.isExpecting102Processing() && !isCommitted()) | |
343 ((HttpGenerator)_connection.getGenerator()).send1xx(HttpStatus.PROCESSING_102); | |
344 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
345 |
813 | 346 /* ------------------------------------------------------------ */ |
347 /* | |
348 * @see javax.servlet.http.HttpServletResponse#sendRedirect(java.lang.String) | |
349 */ | |
350 public void sendRedirect(String location) throws IOException | |
351 { | |
352 if (_connection.isIncluding()) | |
353 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
354 |
813 | 355 if (location==null) |
356 throw new IllegalArgumentException(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
357 |
813 | 358 if (!URIUtil.hasScheme(location)) |
359 { | |
360 StringBuilder buf = _connection.getRequest().getRootURL(); | |
361 if (location.startsWith("/")) | |
362 buf.append(location); | |
363 else | |
364 { | |
365 String path=_connection.getRequest().getRequestURI(); | |
366 String parent=(path.endsWith("/"))?path:URIUtil.parentPath(path); | |
367 location=URIUtil.addPaths(parent,location); | |
368 if(location==null) | |
369 throw new IllegalStateException("path cannot be above root"); | |
370 if (!location.startsWith("/")) | |
371 buf.append('/'); | |
372 buf.append(location); | |
373 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
374 |
813 | 375 location=buf.toString(); |
376 HttpURI uri = new HttpURI(location); | |
377 String path=uri.getDecodedPath(); | |
378 String canonical=URIUtil.canonicalPath(path); | |
379 if (canonical==null) | |
380 throw new IllegalArgumentException(); | |
381 if (!canonical.equals(path)) | |
382 { | |
383 buf = _connection.getRequest().getRootURL(); | |
384 buf.append(URIUtil.encodePath(canonical)); | |
385 String param=uri.getParam(); | |
386 if (param!=null) | |
387 { | |
388 buf.append(';'); | |
389 buf.append(param); | |
390 } | |
391 String query=uri.getQuery(); | |
392 if (query!=null) | |
393 { | |
394 buf.append('?'); | |
395 buf.append(query); | |
396 } | |
397 String fragment=uri.getFragment(); | |
398 if (fragment!=null) | |
399 { | |
400 buf.append('#'); | |
401 buf.append(fragment); | |
402 } | |
403 location=buf.toString(); | |
404 } | |
405 } | |
406 | |
407 resetBuffer(); | |
408 setHeader(HttpHeaders.LOCATION,location); | |
409 setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); | |
410 complete(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
411 |
813 | 412 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
413 |
813 | 414 /* ------------------------------------------------------------ */ |
415 /* | |
416 * @see javax.servlet.http.HttpServletResponse#setDateHeader(java.lang.String, long) | |
417 */ | |
418 public void setDateHeader(String name, long date) | |
419 { | |
420 if (!_connection.isIncluding()) | |
421 _connection.getResponseFields().putDateField(name, date); | |
422 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
423 |
813 | 424 /* ------------------------------------------------------------ */ |
425 /* | |
426 * @see javax.servlet.http.HttpServletResponse#addDateHeader(java.lang.String, long) | |
427 */ | |
428 public void addDateHeader(String name, long date) | |
429 { | |
430 if (!_connection.isIncluding()) | |
431 _connection.getResponseFields().addDateField(name, date); | |
432 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
433 |
813 | 434 /* ------------------------------------------------------------ */ |
435 /* | |
436 * @see javax.servlet.http.HttpServletResponse#setHeader(java.lang.String, java.lang.String) | |
437 */ | |
438 public void setHeader(String name, String value) | |
439 { | |
440 if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) | |
441 setContentType(value); | |
442 else | |
443 { | |
444 if (_connection.isIncluding()) | |
445 { | |
446 if (name.startsWith(SET_INCLUDE_HEADER_PREFIX)) | |
447 name=name.substring(SET_INCLUDE_HEADER_PREFIX.length()); | |
448 else | |
449 return; | |
450 } | |
451 _connection.getResponseFields().put(name, value); | |
452 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) | |
453 { | |
454 if (value==null) | |
455 _connection._generator.setContentLength(-1); | |
456 else | |
457 _connection._generator.setContentLength(Long.parseLong(value)); | |
458 } | |
459 } | |
460 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
461 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
462 |
813 | 463 /* ------------------------------------------------------------ */ |
464 public Collection<String> getHeaderNames() | |
465 { | |
466 final HttpFields fields=_connection.getResponseFields(); | |
467 return fields.getFieldNamesCollection(); | |
468 } | |
469 | |
470 /* ------------------------------------------------------------ */ | |
471 /* | |
472 */ | |
473 public String getHeader(String name) | |
474 { | |
475 return _connection.getResponseFields().getStringField(name); | |
476 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
477 |
813 | 478 /* ------------------------------------------------------------ */ |
479 /* | |
480 */ | |
481 public Collection<String> getHeaders(String name) | |
482 { | |
483 final HttpFields fields=_connection.getResponseFields(); | |
484 Collection<String> i = fields.getValuesCollection(name); | |
485 if (i==null) | |
486 return Collections.EMPTY_LIST; | |
487 return i; | |
488 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
489 |
813 | 490 /* ------------------------------------------------------------ */ |
491 /* | |
492 * @see javax.servlet.http.HttpServletResponse#addHeader(java.lang.String, java.lang.String) | |
493 */ | |
494 public void addHeader(String name, String value) | |
495 { | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
496 |
813 | 497 if (_connection.isIncluding()) |
498 { | |
499 if (name.startsWith(SET_INCLUDE_HEADER_PREFIX)) | |
500 name=name.substring(SET_INCLUDE_HEADER_PREFIX.length()); | |
501 else | |
502 return; | |
503 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
504 |
813 | 505 if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) |
506 { | |
507 setContentType(value); | |
508 return; | |
509 } | |
510 | |
511 _connection.getResponseFields().add(name, value); | |
512 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) | |
513 _connection._generator.setContentLength(Long.parseLong(value)); | |
514 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
515 |
813 | 516 /* ------------------------------------------------------------ */ |
517 /* | |
518 * @see javax.servlet.http.HttpServletResponse#setIntHeader(java.lang.String, int) | |
519 */ | |
520 public void setIntHeader(String name, int value) | |
521 { | |
522 if (!_connection.isIncluding()) | |
523 { | |
524 _connection.getResponseFields().putLongField(name, value); | |
525 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) | |
526 _connection._generator.setContentLength(value); | |
527 } | |
528 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
529 |
813 | 530 /* ------------------------------------------------------------ */ |
531 /* | |
532 * @see javax.servlet.http.HttpServletResponse#addIntHeader(java.lang.String, int) | |
533 */ | |
534 public void addIntHeader(String name, int value) | |
535 { | |
536 if (!_connection.isIncluding()) | |
537 { | |
538 _connection.getResponseFields().addLongField(name, value); | |
539 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) | |
540 _connection._generator.setContentLength(value); | |
541 } | |
542 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
543 |
813 | 544 /* ------------------------------------------------------------ */ |
545 /* | |
546 * @see javax.servlet.http.HttpServletResponse#setStatus(int) | |
547 */ | |
548 public void setStatus(int sc) | |
549 { | |
550 setStatus(sc,null); | |
551 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
552 |
813 | 553 /* ------------------------------------------------------------ */ |
554 /* | |
555 * @see javax.servlet.http.HttpServletResponse#setStatus(int, java.lang.String) | |
556 */ | |
557 public void setStatus(int sc, String sm) | |
558 { | |
559 if (sc<=0) | |
560 throw new IllegalArgumentException(); | |
561 if (!_connection.isIncluding()) | |
562 { | |
563 _status=sc; | |
564 _reason=sm; | |
565 } | |
566 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
567 |
813 | 568 /* ------------------------------------------------------------ */ |
569 /* | |
570 * @see javax.servlet.ServletResponse#getCharacterEncoding() | |
571 */ | |
572 public String getCharacterEncoding() | |
573 { | |
574 if (_characterEncoding==null) | |
575 _characterEncoding=StringUtil.__ISO_8859_1; | |
576 return _characterEncoding; | |
577 } | |
578 | |
579 /* ------------------------------------------------------------ */ | |
580 String getSetCharacterEncoding() | |
581 { | |
582 return _characterEncoding; | |
583 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
584 |
813 | 585 /* ------------------------------------------------------------ */ |
586 /* | |
587 * @see javax.servlet.ServletResponse#getContentType() | |
588 */ | |
589 public String getContentType() | |
590 { | |
591 return _contentType; | |
592 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
593 |
813 | 594 /* ------------------------------------------------------------ */ |
595 /* | |
596 * @see javax.servlet.ServletResponse#getOutputStream() | |
597 */ | |
598 public ServletOutputStream getOutputStream() throws IOException | |
599 { | |
600 if (_outputState!=NONE && _outputState!=STREAM) | |
601 throw new IllegalStateException("WRITER"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
602 |
813 | 603 ServletOutputStream out = _connection.getOutputStream(); |
604 _outputState=STREAM; | |
605 return out; | |
606 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
607 |
813 | 608 /* ------------------------------------------------------------ */ |
609 public boolean isWriting() | |
610 { | |
611 return _outputState==WRITER; | |
612 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
613 |
813 | 614 /* ------------------------------------------------------------ */ |
615 public boolean isOutputing() | |
616 { | |
617 return _outputState!=NONE; | |
618 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
619 |
813 | 620 /* ------------------------------------------------------------ */ |
621 /* | |
622 * @see javax.servlet.ServletResponse#getWriter() | |
623 */ | |
624 public PrintWriter getWriter() throws IOException | |
625 { | |
626 if (_outputState!=NONE && _outputState!=WRITER) | |
627 throw new IllegalStateException("STREAM"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
628 |
813 | 629 /* if there is no writer yet */ |
630 if (_writer==null) | |
631 { | |
632 /* get encoding from Content-Type header */ | |
633 String encoding = _characterEncoding; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
634 |
813 | 635 if (encoding==null) |
636 { | |
637 /* implementation of educated defaults */ | |
638 if(_cachedMimeType != null) | |
639 encoding = MimeTypes.getCharsetFromContentType(_cachedMimeType); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
640 |
813 | 641 if (encoding==null) |
642 encoding = StringUtil.__ISO_8859_1; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
643 |
813 | 644 setCharacterEncoding(encoding); |
645 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
646 |
813 | 647 /* construct Writer using correct encoding */ |
648 _writer = _connection.getPrintWriter(encoding); | |
649 } | |
650 _outputState=WRITER; | |
651 return _writer; | |
652 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
653 |
813 | 654 /* ------------------------------------------------------------ */ |
655 /* | |
656 * @see javax.servlet.ServletResponse#setCharacterEncoding(java.lang.String) | |
657 */ | |
658 public void setCharacterEncoding(String encoding) | |
659 { | |
660 if (_connection.isIncluding()) | |
661 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
662 |
813 | 663 if (this._outputState==0 && !isCommitted()) |
664 { | |
665 _explicitEncoding=true; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
666 |
813 | 667 if (encoding==null) |
668 { | |
669 // Clear any encoding. | |
670 if (_characterEncoding!=null) | |
671 { | |
672 _characterEncoding=null; | |
673 if (_cachedMimeType!=null) | |
674 _contentType=_cachedMimeType.toString(); | |
675 else if (_mimeType!=null) | |
676 _contentType=_mimeType; | |
677 else | |
678 _contentType=null; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
679 |
813 | 680 if (_contentType==null) |
681 _connection.getResponseFields().remove(HttpHeaders.CONTENT_TYPE_BUFFER); | |
682 else | |
683 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
684 } | |
685 } | |
686 else | |
687 { | |
688 // No, so just add this one to the mimetype | |
689 _characterEncoding=encoding; | |
690 if (_contentType!=null) | |
691 { | |
692 int i0=_contentType.indexOf(';'); | |
693 if (i0<0) | |
694 { | |
695 _contentType=null; | |
696 if(_cachedMimeType!=null) | |
697 { | |
698 CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); | |
699 if (content_type!=null) | |
700 { | |
701 _contentType=content_type.toString(); | |
702 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); | |
703 } | |
704 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
705 |
813 | 706 if (_contentType==null) |
707 { | |
708 _contentType = _mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
709 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
710 } | |
711 } | |
712 else | |
713 { | |
714 int i1=_contentType.indexOf("charset=",i0); | |
715 if (i1<0) | |
716 { | |
717 _contentType = _contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
718 } | |
719 else | |
720 { | |
721 int i8=i1+8; | |
722 int i2=_contentType.indexOf(" ",i8); | |
723 if (i2<0) | |
724 _contentType=_contentType.substring(0,i8)+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
725 else | |
726 _contentType=_contentType.substring(0,i8)+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= ")+_contentType.substring(i2); | |
727 } | |
728 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
729 } | |
730 } | |
731 } | |
732 } | |
733 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
734 |
813 | 735 /* ------------------------------------------------------------ */ |
736 /* | |
737 * @see javax.servlet.ServletResponse#setContentLength(int) | |
738 */ | |
739 public void setContentLength(int len) | |
740 { | |
741 // Protect from setting after committed as default handling | |
742 // of a servlet HEAD request ALWAYS sets _content length, even | |
743 // if the getHandling committed the response! | |
744 if (isCommitted() || _connection.isIncluding()) | |
745 return; | |
746 _connection._generator.setContentLength(len); | |
747 if (len>0) | |
748 { | |
749 _connection.getResponseFields().putLongField(HttpHeaders.CONTENT_LENGTH, len); | |
750 if (_connection._generator.isAllContentWritten()) | |
751 { | |
752 if (_outputState==WRITER) | |
753 _writer.close(); | |
754 else if (_outputState==STREAM) | |
755 { | |
756 try | |
757 { | |
758 getOutputStream().close(); | |
759 } | |
760 catch(IOException e) | |
761 { | |
762 throw new RuntimeException(e); | |
763 } | |
764 } | |
765 } | |
766 } | |
767 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
768 |
813 | 769 /* ------------------------------------------------------------ */ |
770 /* | |
771 * @see javax.servlet.ServletResponse#setContentLength(int) | |
772 */ | |
773 public void setLongContentLength(long len) | |
774 { | |
775 // Protect from setting after committed as default handling | |
776 // of a servlet HEAD request ALWAYS sets _content length, even | |
777 // if the getHandling committed the response! | |
778 if (isCommitted() || _connection.isIncluding()) | |
779 return; | |
780 _connection._generator.setContentLength(len); | |
781 _connection.getResponseFields().putLongField(HttpHeaders.CONTENT_LENGTH, len); | |
782 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
783 |
813 | 784 /* ------------------------------------------------------------ */ |
785 /* | |
786 * @see javax.servlet.ServletResponse#setContentType(java.lang.String) | |
787 */ | |
788 public void setContentType(String contentType) | |
789 { | |
790 if (isCommitted() || _connection.isIncluding()) | |
791 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
792 |
813 | 793 // Yes this method is horribly complex.... but there are lots of special cases and |
794 // as this method is called on every request, it is worth trying to save string creation. | |
795 // | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
796 |
813 | 797 if (contentType==null) |
798 { | |
799 if (_locale==null) | |
800 _characterEncoding=null; | |
801 _mimeType=null; | |
802 _cachedMimeType=null; | |
803 _contentType=null; | |
804 _connection.getResponseFields().remove(HttpHeaders.CONTENT_TYPE_BUFFER); | |
805 } | |
806 else | |
807 { | |
808 // Look for encoding in contentType | |
809 int i0=contentType.indexOf(';'); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
810 |
813 | 811 if (i0>0) |
812 { | |
813 // we have content type parameters | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
814 |
813 | 815 // Extract params off mimetype |
816 _mimeType=contentType.substring(0,i0).trim(); | |
817 _cachedMimeType=MimeTypes.CACHE.get(_mimeType); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
818 |
813 | 819 // Look for charset |
820 int i1=contentType.indexOf("charset=",i0+1); | |
821 if (i1>=0) | |
822 { | |
823 _explicitEncoding=true; | |
824 int i8=i1+8; | |
825 int i2 = contentType.indexOf(' ',i8); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
826 |
813 | 827 if (_outputState==WRITER) |
828 { | |
829 // strip the charset and ignore; | |
830 if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' ')) | |
831 { | |
832 if (_cachedMimeType!=null) | |
833 { | |
834 CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); | |
835 if (content_type!=null) | |
836 { | |
837 _contentType=content_type.toString(); | |
838 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); | |
839 } | |
840 else | |
841 { | |
842 _contentType=_mimeType+";charset="+_characterEncoding; | |
843 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
844 } | |
845 } | |
846 else | |
847 { | |
848 _contentType=_mimeType+";charset="+_characterEncoding; | |
849 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
850 } | |
851 } | |
852 else if (i2<0) | |
853 { | |
854 _contentType=contentType.substring(0,i1)+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
855 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
856 } | |
857 else | |
858 { | |
859 _contentType=contentType.substring(0,i1)+contentType.substring(i2)+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
860 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
861 } | |
862 } | |
863 else if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' ')) | |
864 { | |
865 // The params are just the char encoding | |
866 _cachedMimeType=MimeTypes.CACHE.get(_mimeType); | |
867 _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8)); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
868 |
813 | 869 if (_cachedMimeType!=null) |
870 { | |
871 CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); | |
872 if (content_type!=null) | |
873 { | |
874 _contentType=content_type.toString(); | |
875 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); | |
876 } | |
877 else | |
878 { | |
879 _contentType=contentType; | |
880 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
881 } | |
882 } | |
883 else | |
884 { | |
885 _contentType=contentType; | |
886 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
887 } | |
888 } | |
889 else if (i2>0) | |
890 { | |
891 _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8,i2)); | |
892 _contentType=contentType; | |
893 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
894 } | |
895 else | |
896 { | |
897 _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8)); | |
898 _contentType=contentType; | |
899 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
900 } | |
901 } | |
902 else // No encoding in the params. | |
903 { | |
904 _cachedMimeType=null; | |
905 _contentType=_characterEncoding==null?contentType:contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
906 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
907 } | |
908 } | |
909 else // No params at all | |
910 { | |
911 _mimeType=contentType; | |
912 _cachedMimeType=MimeTypes.CACHE.get(_mimeType); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
913 |
813 | 914 if (_characterEncoding!=null) |
915 { | |
916 if (_cachedMimeType!=null) | |
917 { | |
918 CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); | |
919 if (content_type!=null) | |
920 { | |
921 _contentType=content_type.toString(); | |
922 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); | |
923 } | |
924 else | |
925 { | |
926 _contentType=_mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
927 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
928 } | |
929 } | |
930 else | |
931 { | |
932 _contentType=contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
933 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
934 } | |
935 } | |
936 else if (_cachedMimeType!=null) | |
937 { | |
938 _contentType=_cachedMimeType.toString(); | |
939 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_cachedMimeType); | |
940 } | |
941 else | |
942 { | |
943 _contentType=contentType; | |
944 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
945 } | |
946 } | |
947 } | |
948 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
949 |
813 | 950 /* ------------------------------------------------------------ */ |
951 /* | |
952 * @see javax.servlet.ServletResponse#setBufferSize(int) | |
953 */ | |
954 public void setBufferSize(int size) | |
955 { | |
956 if (isCommitted() || getContentCount()>0) | |
957 throw new IllegalStateException("Committed or content written"); | |
958 _connection.getGenerator().increaseContentBufferSize(size); | |
959 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
960 |
813 | 961 /* ------------------------------------------------------------ */ |
962 /* | |
963 * @see javax.servlet.ServletResponse#getBufferSize() | |
964 */ | |
965 public int getBufferSize() | |
966 { | |
967 return _connection.getGenerator().getContentBufferSize(); | |
968 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
969 |
813 | 970 /* ------------------------------------------------------------ */ |
971 /* | |
972 * @see javax.servlet.ServletResponse#flushBuffer() | |
973 */ | |
974 public void flushBuffer() throws IOException | |
975 { | |
976 _connection.flushResponse(); | |
977 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
978 |
813 | 979 /* ------------------------------------------------------------ */ |
980 /* | |
981 * @see javax.servlet.ServletResponse#reset() | |
982 */ | |
983 public void reset() | |
984 { | |
985 resetBuffer(); | |
986 fwdReset(); | |
987 _status=200; | |
988 _reason=null; | |
989 | |
990 HttpFields response_fields=_connection.getResponseFields(); | |
991 | |
992 response_fields.clear(); | |
993 String connection=_connection.getRequestFields().getStringField(HttpHeaders.CONNECTION_BUFFER); | |
994 if (connection!=null) | |
995 { | |
996 String[] values = connection.split(","); | |
997 for (int i=0;values!=null && i<values.length;i++) | |
998 { | |
999 CachedBuffer cb = HttpHeaderValues.CACHE.get(values[0].trim()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1000 |
813 | 1001 if (cb!=null) |
1002 { | |
1003 switch(cb.getOrdinal()) | |
1004 { | |
1005 case HttpHeaderValues.CLOSE_ORDINAL: | |
1006 response_fields.put(HttpHeaders.CONNECTION_BUFFER,HttpHeaderValues.CLOSE_BUFFER); | |
1007 break; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1008 |
813 | 1009 case HttpHeaderValues.KEEP_ALIVE_ORDINAL: |
1010 if (HttpVersions.HTTP_1_0.equalsIgnoreCase(_connection.getRequest().getProtocol())) | |
1011 response_fields.put(HttpHeaders.CONNECTION_BUFFER,HttpHeaderValues.KEEP_ALIVE); | |
1012 break; | |
1013 case HttpHeaderValues.TE_ORDINAL: | |
1014 response_fields.put(HttpHeaders.CONNECTION_BUFFER,HttpHeaderValues.TE); | |
1015 break; | |
1016 } | |
1017 } | |
1018 } | |
1019 } | |
1020 } | |
1021 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1022 |
813 | 1023 public void reset(boolean preserveCookies) |
1024 { | |
1025 if (!preserveCookies) | |
1026 reset(); | |
1027 else | |
1028 { | |
1029 HttpFields response_fields=_connection.getResponseFields(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1030 |
813 | 1031 ArrayList<String> cookieValues = new ArrayList<String>(5); |
1032 Enumeration<String> vals = response_fields.getValues(HttpHeaders.SET_COOKIE); | |
1033 while (vals.hasMoreElements()) | |
1034 cookieValues.add((String)vals.nextElement()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1035 |
813 | 1036 reset(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1037 |
813 | 1038 for (String v:cookieValues) |
1039 response_fields.add(HttpHeaders.SET_COOKIE, v); | |
1040 } | |
1041 } | |
1042 | |
1043 | |
1044 | |
1045 /* ------------------------------------------------------------ */ | |
1046 /* | |
1047 * @see javax.servlet.ServletResponse#reset() | |
1048 */ | |
1049 public void fwdReset() | |
1050 { | |
1051 resetBuffer(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1052 |
813 | 1053 _writer=null; |
1054 _outputState=NONE; | |
1055 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1056 |
813 | 1057 /* ------------------------------------------------------------ */ |
1058 /* | |
1059 * @see javax.servlet.ServletResponse#resetBuffer() | |
1060 */ | |
1061 public void resetBuffer() | |
1062 { | |
1063 if (isCommitted()) | |
1064 throw new IllegalStateException("Committed"); | |
1065 _connection.getGenerator().resetBuffer(); | |
1066 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1067 |
813 | 1068 /* ------------------------------------------------------------ */ |
1069 /* | |
1070 * @see javax.servlet.ServletResponse#isCommitted() | |
1071 */ | |
1072 public boolean isCommitted() | |
1073 { | |
1074 return _connection.isResponseCommitted(); | |
1075 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1076 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1077 |
813 | 1078 /* ------------------------------------------------------------ */ |
1079 /* | |
1080 * @see javax.servlet.ServletResponse#setLocale(java.util.Locale) | |
1081 */ | |
1082 public void setLocale(Locale locale) | |
1083 { | |
1084 if (locale == null || isCommitted() ||_connection.isIncluding()) | |
1085 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1086 |
813 | 1087 _locale = locale; |
1088 _connection.getResponseFields().put(HttpHeaders.CONTENT_LANGUAGE_BUFFER,locale.toString().replace('_','-')); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1089 |
813 | 1090 if (_explicitEncoding || _outputState!=0 ) |
1091 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1092 |
813 | 1093 if (_connection.getRequest().getContext()==null) |
1094 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1095 |
813 | 1096 String charset = _connection.getRequest().getContext().getContextHandler().getLocaleEncoding(locale); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1097 |
813 | 1098 if (charset!=null && charset.length()>0) |
1099 { | |
1100 _characterEncoding=charset; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1101 |
813 | 1102 /* get current MIME type from Content-Type header */ |
1103 String type=getContentType(); | |
1104 if (type!=null) | |
1105 { | |
1106 _characterEncoding=charset; | |
1107 int semi=type.indexOf(';'); | |
1108 if (semi<0) | |
1109 { | |
1110 _mimeType=type; | |
1111 _contentType= type += ";charset="+charset; | |
1112 } | |
1113 else | |
1114 { | |
1115 _mimeType=type.substring(0,semi); | |
1116 _contentType= _mimeType += ";charset="+charset; | |
1117 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1118 |
813 | 1119 _cachedMimeType=MimeTypes.CACHE.get(_mimeType); |
1120 _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); | |
1121 } | |
1122 } | |
1123 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1124 |
813 | 1125 /* ------------------------------------------------------------ */ |
1126 /* | |
1127 * @see javax.servlet.ServletResponse#getLocale() | |
1128 */ | |
1129 public Locale getLocale() | |
1130 { | |
1131 if (_locale==null) | |
1132 return Locale.getDefault(); | |
1133 return _locale; | |
1134 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1135 |
813 | 1136 /* ------------------------------------------------------------ */ |
1137 /** | |
1138 * @return The HTTP status code that has been set for this request. This will be <code>200<code> | |
1139 * ({@link HttpServletResponse#SC_OK}), unless explicitly set through one of the <code>setStatus</code> methods. | |
1140 */ | |
1141 public int getStatus() | |
1142 { | |
1143 return _status; | |
1144 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1145 |
813 | 1146 /* ------------------------------------------------------------ */ |
1147 /** | |
1148 * @return The reason associated with the current {@link #getStatus() status}. This will be <code>null</code>, | |
1149 * unless one of the <code>setStatus</code> methods have been called. | |
1150 */ | |
1151 public String getReason() | |
1152 { | |
1153 return _reason; | |
1154 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1155 |
813 | 1156 /* ------------------------------------------------------------ */ |
1157 /** | |
1158 */ | |
1159 public void complete() | |
1160 throws IOException | |
1161 { | |
1162 _connection.completeResponse(); | |
1163 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1164 |
813 | 1165 /* ------------------------------------------------------------- */ |
1166 /** | |
1167 * @return the number of bytes actually written in response body | |
1168 */ | |
1169 public long getContentCount() | |
1170 { | |
1171 if (_connection==null || _connection.getGenerator()==null) | |
1172 return -1; | |
1173 return _connection.getGenerator().getContentWritten(); | |
1174 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1175 |
813 | 1176 /* ------------------------------------------------------------ */ |
1177 public HttpFields getHttpFields() | |
1178 { | |
1179 return _connection.getResponseFields(); | |
1180 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1181 |
813 | 1182 /* ------------------------------------------------------------ */ |
1183 @Override | |
1184 public String toString() | |
1185 { | |
1186 return "HTTP/1.1 "+_status+" "+ (_reason==null?"":_reason) +System.getProperty("line.separator")+ | |
1187 _connection.getResponseFields().toString(); | |
1188 } | |
1189 | |
1190 /* ------------------------------------------------------------ */ | |
1191 /* ------------------------------------------------------------ */ | |
1192 /* ------------------------------------------------------------ */ | |
1193 private static class NullOutput extends ServletOutputStream | |
1194 { | |
1195 @Override | |
1196 public void write(int b) throws IOException | |
1197 { | |
1198 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1199 |
813 | 1200 @Override |
1201 public void print(String s) throws IOException | |
1202 { | |
1203 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1204 |
813 | 1205 @Override |
1206 public void println(String s) throws IOException | |
1207 { | |
1208 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1209 |
813 | 1210 @Override |
1211 public void write(byte[] b, int off, int len) throws IOException | |
1212 { | |
1213 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1214 |
813 | 1215 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1216 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1217 } |