Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/Response.java @ 1052:4a2489f1d5fe
remove JBuffer.peek()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 08 Nov 2016 00:27:16 -0700 |
parents | 27f3dc761452 |
children |
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.HttpHeaderValues; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 import org.eclipse.jetty.http.HttpHeaders; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 import org.eclipse.jetty.http.HttpStatus; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 import org.eclipse.jetty.http.HttpURI; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 import org.eclipse.jetty.http.HttpVersions; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 import org.eclipse.jetty.http.MimeTypes; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.server.handler.ContextHandler; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 import org.eclipse.jetty.util.ByteArrayISO8859Writer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 import org.eclipse.jetty.util.QuotedStringTokenizer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 import org.eclipse.jetty.util.StringUtil; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 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
|
50 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
813
diff
changeset
|
51 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 /** Response. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 * <p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 * 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
|
56 * </p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 */ |
999 | 58 public final class Response implements HttpServletResponse |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 { |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
813
diff
changeset
|
60 private static final Logger LOG = LoggerFactory.getLogger(Response.class); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 |
813 | 62 |
63 public static final int | |
64 NONE=0, | |
65 STREAM=1, | |
66 WRITER=2; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
67 |
813 | 68 /** |
69 * If a header name starts with this string, the header (stripped of the prefix) | |
70 * can be set during include using only {@link #setHeader(String, String)} or | |
71 * {@link #addHeader(String, String)}. | |
72 */ | |
73 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
|
74 |
813 | 75 /** |
76 * If this string is found within the comment of a cookie added with {@link #addCookie(Cookie)}, then the cookie | |
77 * will be set as HTTP ONLY. | |
78 */ | |
79 public final static String HTTP_ONLY_COMMENT="__HTTP_ONLY__"; | |
80 | |
81 private final AbstractHttpConnection _connection; | |
999 | 82 private int _status = SC_OK; |
813 | 83 private String _reason; |
84 private Locale _locale; | |
85 private String _mimeType; | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
86 private String _cachedMimeType; |
813 | 87 private String _characterEncoding; |
88 private boolean _explicitEncoding; | |
89 private String _contentType; | |
90 private volatile int _outputState; | |
91 private PrintWriter _writer; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
92 |
813 | 93 public Response(AbstractHttpConnection connection) |
94 { | |
999 | 95 _connection = connection; |
813 | 96 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
97 |
813 | 98 public void addCookie(HttpCookie cookie) |
99 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
100 _connection._responseFields.addSetCookie(cookie); |
813 | 101 } |
102 | |
999 | 103 @Override |
813 | 104 public void addCookie(Cookie cookie) |
105 { | |
106 String comment=cookie.getComment(); | |
107 boolean http_only=false; | |
108 | |
109 if (comment!=null) | |
110 { | |
111 int i=comment.indexOf(HTTP_ONLY_COMMENT); | |
112 if (i>=0) | |
113 { | |
114 http_only=true; | |
115 comment=comment.replace(HTTP_ONLY_COMMENT,"").trim(); | |
116 if (comment.length()==0) | |
117 comment=null; | |
118 } | |
119 } | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
120 _connection._responseFields.addSetCookie(cookie.getName(), |
813 | 121 cookie.getValue(), |
122 cookie.getDomain(), | |
123 cookie.getPath(), | |
124 cookie.getMaxAge(), | |
125 comment, | |
126 cookie.getSecure(), | |
127 http_only || cookie.isHttpOnly(), | |
128 cookie.getVersion()); | |
129 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
130 |
999 | 131 @Override |
813 | 132 public boolean containsHeader(String name) |
133 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
134 return _connection._responseFields.containsKey(name); |
813 | 135 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
136 |
999 | 137 @Override |
813 | 138 public String encodeURL(String url) |
139 { | |
809
09d518d313b7
remove SessionManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
140 throw new UnsupportedOperationException(); |
813 | 141 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
142 |
999 | 143 @Override |
813 | 144 public String encodeRedirectURL(String url) |
145 { | |
999 | 146 throw new UnsupportedOperationException(); |
813 | 147 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
148 |
999 | 149 @Override |
813 | 150 @Deprecated |
151 public String encodeUrl(String url) | |
152 { | |
999 | 153 throw new UnsupportedOperationException(); |
813 | 154 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
155 |
999 | 156 @Override |
813 | 157 @Deprecated |
158 public String encodeRedirectUrl(String url) | |
159 { | |
999 | 160 throw new UnsupportedOperationException(); |
813 | 161 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
162 |
999 | 163 @Override |
813 | 164 public void sendError(int code, String message) throws IOException |
165 { | |
166 if (isCommitted()) | |
167 LOG.warn("Committed before "+code+" "+message); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
168 |
813 | 169 resetBuffer(); |
999 | 170 _characterEncoding = null; |
813 | 171 setHeader(HttpHeaders.EXPIRES,null); |
172 setHeader(HttpHeaders.LAST_MODIFIED,null); | |
173 setHeader(HttpHeaders.CACHE_CONTROL,null); | |
174 setHeader(HttpHeaders.CONTENT_TYPE,null); | |
175 setHeader(HttpHeaders.CONTENT_LENGTH,null); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
176 |
999 | 177 _outputState = NONE; |
813 | 178 setStatus(code,message); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
179 |
813 | 180 if (message==null) |
181 message=HttpStatus.getMessage(code); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
182 |
813 | 183 // If we are allowed to have a body |
184 if (code!=SC_NO_CONTENT && | |
185 code!=SC_NOT_MODIFIED && | |
186 code!=SC_PARTIAL_CONTENT && | |
187 code>=SC_OK) | |
188 { | |
189 Request request = _connection.getRequest(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
190 |
911 | 191 setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store"); |
192 setContentType(MimeTypes.TEXT_HTML_8859_1); | |
193 ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(2048); | |
194 if (message != null) | |
813 | 195 { |
911 | 196 message= StringUtil.replace(message, "&", "&"); |
197 message= StringUtil.replace(message, "<", "<"); | |
198 message= StringUtil.replace(message, ">", ">"); | |
813 | 199 } |
911 | 200 String uri= request.getRequestURI(); |
201 if (uri!=null) | |
813 | 202 { |
911 | 203 uri= StringUtil.replace(uri, "&", "&"); |
204 uri= StringUtil.replace(uri, "<", "<"); | |
205 uri= StringUtil.replace(uri, ">", ">"); | |
206 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
207 |
911 | 208 writer.write("<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\"/>\n"); |
209 writer.write("<title>Error "); | |
210 writer.write(Integer.toString(code)); | |
211 writer.write(' '); | |
212 if (message==null) | |
213 message=HttpStatus.getMessage(code); | |
214 writer.write(message); | |
215 writer.write("</title>\n</head>\n<body>\n<h2>HTTP ERROR: "); | |
216 writer.write(Integer.toString(code)); | |
217 writer.write("</h2>\n<p>Problem accessing "); | |
218 writer.write(uri); | |
219 writer.write(". Reason:\n<pre> "); | |
220 writer.write(message); | |
221 writer.write("</pre>"); | |
222 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
|
223 |
911 | 224 for (int i= 0; i < 20; i++) |
225 writer.write("\n "); | |
226 writer.write("\n</body>\n</html>\n"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
227 |
911 | 228 writer.flush(); |
229 setContentLength(writer.size()); | |
230 writer.writeTo(getOutputStream()); | |
231 writer.destroy(); | |
813 | 232 } |
233 else if (code!=SC_PARTIAL_CONTENT) | |
234 { | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
235 _connection._requestFields.remove(HttpHeaders.CONTENT_TYPE); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
236 _connection._requestFields.remove(HttpHeaders.CONTENT_LENGTH); |
999 | 237 _characterEncoding = null; |
238 _mimeType = null; | |
239 _cachedMimeType = null; | |
813 | 240 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
241 |
813 | 242 complete(); |
243 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
244 |
999 | 245 @Override |
813 | 246 public void sendError(int sc) throws IOException |
247 { | |
999 | 248 sendError(sc,null); |
813 | 249 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
250 |
999 | 251 @Override |
813 | 252 public void sendRedirect(String location) throws IOException |
253 { | |
254 if (location==null) | |
255 throw new IllegalArgumentException(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
256 |
813 | 257 if (!URIUtil.hasScheme(location)) |
258 { | |
259 StringBuilder buf = _connection.getRequest().getRootURL(); | |
260 if (location.startsWith("/")) | |
261 buf.append(location); | |
262 else | |
263 { | |
264 String path=_connection.getRequest().getRequestURI(); | |
265 String parent=(path.endsWith("/"))?path:URIUtil.parentPath(path); | |
266 location=URIUtil.addPaths(parent,location); | |
267 if(location==null) | |
268 throw new IllegalStateException("path cannot be above root"); | |
269 if (!location.startsWith("/")) | |
270 buf.append('/'); | |
271 buf.append(location); | |
272 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
273 |
813 | 274 location=buf.toString(); |
275 HttpURI uri = new HttpURI(location); | |
276 String path=uri.getDecodedPath(); | |
277 String canonical=URIUtil.canonicalPath(path); | |
278 if (canonical==null) | |
279 throw new IllegalArgumentException(); | |
280 if (!canonical.equals(path)) | |
281 { | |
282 buf = _connection.getRequest().getRootURL(); | |
283 buf.append(URIUtil.encodePath(canonical)); | |
284 String param=uri.getParam(); | |
285 if (param!=null) | |
286 { | |
287 buf.append(';'); | |
288 buf.append(param); | |
289 } | |
290 String query=uri.getQuery(); | |
291 if (query!=null) | |
292 { | |
293 buf.append('?'); | |
294 buf.append(query); | |
295 } | |
296 String fragment=uri.getFragment(); | |
297 if (fragment!=null) | |
298 { | |
299 buf.append('#'); | |
300 buf.append(fragment); | |
301 } | |
302 location=buf.toString(); | |
303 } | |
304 } | |
305 | |
306 resetBuffer(); | |
307 setHeader(HttpHeaders.LOCATION,location); | |
308 setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); | |
309 complete(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
310 |
813 | 311 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
312 |
999 | 313 @Override |
813 | 314 public void setDateHeader(String name, long date) |
315 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
316 _connection._responseFields.putDateField(name, date); |
813 | 317 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
318 |
999 | 319 @Override |
813 | 320 public void addDateHeader(String name, long date) |
321 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
322 _connection._responseFields.addDateField(name, date); |
813 | 323 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
324 |
999 | 325 @Override |
813 | 326 public void setHeader(String name, String value) |
327 { | |
328 if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) | |
329 setContentType(value); | |
330 else | |
331 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
332 _connection._responseFields.put(name, value); |
813 | 333 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) |
334 { | |
335 if (value==null) | |
336 _connection._generator.setContentLength(-1); | |
337 else | |
338 _connection._generator.setContentLength(Long.parseLong(value)); | |
339 } | |
340 } | |
341 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
342 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
343 |
999 | 344 @Override |
813 | 345 public Collection<String> getHeaderNames() |
346 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
347 final HttpFields fields=_connection._responseFields; |
813 | 348 return fields.getFieldNamesCollection(); |
349 } | |
350 | |
999 | 351 @Override |
813 | 352 public String getHeader(String name) |
353 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
354 return _connection._responseFields.getStringField(name); |
813 | 355 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
356 |
999 | 357 @Override |
813 | 358 public Collection<String> getHeaders(String name) |
359 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
360 final HttpFields fields=_connection._responseFields; |
813 | 361 Collection<String> i = fields.getValuesCollection(name); |
362 if (i==null) | |
363 return Collections.EMPTY_LIST; | |
364 return i; | |
365 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
366 |
999 | 367 @Override |
813 | 368 public void addHeader(String name, String value) |
369 { | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
370 |
813 | 371 if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) |
372 { | |
373 setContentType(value); | |
374 return; | |
375 } | |
376 | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
377 _connection._responseFields.add(name, value); |
813 | 378 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) |
379 _connection._generator.setContentLength(Long.parseLong(value)); | |
380 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
381 |
999 | 382 @Override |
813 | 383 public void setIntHeader(String name, int value) |
384 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
385 _connection._responseFields.putLongField(name, value); |
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
386 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) |
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
387 _connection._generator.setContentLength(value); |
813 | 388 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
389 |
999 | 390 @Override |
813 | 391 public void addIntHeader(String name, int value) |
392 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
393 _connection._responseFields.addLongField(name, value); |
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
394 if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) |
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
395 _connection._generator.setContentLength(value); |
813 | 396 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
397 |
999 | 398 @Override |
813 | 399 public void setStatus(int sc) |
400 { | |
401 setStatus(sc,null); | |
402 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
403 |
999 | 404 @Override |
813 | 405 public void setStatus(int sc, String sm) |
406 { | |
407 if (sc<=0) | |
408 throw new IllegalArgumentException(); | |
999 | 409 _status = sc; |
410 _reason = sm; | |
813 | 411 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
412 |
999 | 413 @Override |
813 | 414 public String getCharacterEncoding() |
415 { | |
416 if (_characterEncoding==null) | |
999 | 417 _characterEncoding = StringUtil.__ISO_8859_1; |
813 | 418 return _characterEncoding; |
419 } | |
420 | |
999 | 421 @Override |
813 | 422 public String getContentType() |
423 { | |
424 return _contentType; | |
425 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
426 |
999 | 427 @Override |
813 | 428 public ServletOutputStream getOutputStream() throws IOException |
429 { | |
430 if (_outputState!=NONE && _outputState!=STREAM) | |
431 throw new IllegalStateException("WRITER"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
432 |
813 | 433 ServletOutputStream out = _connection.getOutputStream(); |
999 | 434 _outputState = STREAM; |
813 | 435 return out; |
436 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
437 |
999 | 438 @Override |
813 | 439 public PrintWriter getWriter() throws IOException |
440 { | |
441 if (_outputState!=NONE && _outputState!=WRITER) | |
442 throw new IllegalStateException("STREAM"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
443 |
813 | 444 /* if there is no writer yet */ |
445 if (_writer==null) | |
446 { | |
447 /* get encoding from Content-Type header */ | |
448 String encoding = _characterEncoding; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
449 |
813 | 450 if (encoding==null) |
451 { | |
452 /* implementation of educated defaults */ | |
453 if(_cachedMimeType != null) | |
454 encoding = MimeTypes.getCharsetFromContentType(_cachedMimeType); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
455 |
813 | 456 if (encoding==null) |
457 encoding = StringUtil.__ISO_8859_1; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
458 |
813 | 459 setCharacterEncoding(encoding); |
460 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
461 |
813 | 462 /* construct Writer using correct encoding */ |
463 _writer = _connection.getPrintWriter(encoding); | |
464 } | |
465 _outputState=WRITER; | |
466 return _writer; | |
467 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
468 |
999 | 469 @Override |
813 | 470 public void setCharacterEncoding(String encoding) |
471 { | |
472 if (this._outputState==0 && !isCommitted()) | |
473 { | |
999 | 474 _explicitEncoding = true; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
475 |
813 | 476 if (encoding==null) |
477 { | |
478 // Clear any encoding. | |
479 if (_characterEncoding!=null) | |
480 { | |
481 _characterEncoding=null; | |
482 if (_cachedMimeType!=null) | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
483 _contentType = _cachedMimeType; |
813 | 484 else if (_mimeType!=null) |
999 | 485 _contentType = _mimeType; |
813 | 486 else |
999 | 487 _contentType = null; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
488 |
813 | 489 if (_contentType==null) |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
490 _connection._responseFields.remove(HttpHeaders.CONTENT_TYPE); |
813 | 491 else |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
492 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 493 } |
494 } | |
495 else | |
496 { | |
497 // No, so just add this one to the mimetype | |
498 _characterEncoding=encoding; | |
499 if (_contentType!=null) | |
500 { | |
501 int i0=_contentType.indexOf(';'); | |
502 if (i0<0) | |
503 { | |
504 _contentType=null; | |
505 if(_cachedMimeType!=null) | |
506 { | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
507 String content_type = MimeTypes.getAssociate(_cachedMimeType,_characterEncoding); |
813 | 508 if (content_type!=null) |
509 { | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
510 _contentType = content_type; |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
511 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 512 } |
513 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
514 |
813 | 515 if (_contentType==null) |
516 { | |
517 _contentType = _mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
518 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 519 } |
520 } | |
521 else | |
522 { | |
523 int i1=_contentType.indexOf("charset=",i0); | |
524 if (i1<0) | |
525 { | |
526 _contentType = _contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
527 } | |
528 else | |
529 { | |
530 int i8=i1+8; | |
531 int i2=_contentType.indexOf(" ",i8); | |
532 if (i2<0) | |
533 _contentType=_contentType.substring(0,i8)+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
534 else | |
535 _contentType=_contentType.substring(0,i8)+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= ")+_contentType.substring(i2); | |
536 } | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
537 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 538 } |
539 } | |
540 } | |
541 } | |
542 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
543 |
999 | 544 @Override |
813 | 545 public void setContentLength(int len) |
546 { | |
547 // Protect from setting after committed as default handling | |
548 // of a servlet HEAD request ALWAYS sets _content length, even | |
549 // if the getHandling committed the response! | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
550 if (isCommitted()) |
813 | 551 return; |
552 _connection._generator.setContentLength(len); | |
553 if (len>0) | |
554 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
555 _connection._responseFields.putLongField(HttpHeaders.CONTENT_LENGTH, len); |
813 | 556 if (_connection._generator.isAllContentWritten()) |
557 { | |
558 if (_outputState==WRITER) | |
559 _writer.close(); | |
560 else if (_outputState==STREAM) | |
561 { | |
562 try | |
563 { | |
564 getOutputStream().close(); | |
565 } | |
566 catch(IOException e) | |
567 { | |
568 throw new RuntimeException(e); | |
569 } | |
570 } | |
571 } | |
572 } | |
573 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
574 |
999 | 575 @Override |
813 | 576 public void setContentType(String contentType) |
577 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
578 if (isCommitted()) |
813 | 579 return; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
580 |
813 | 581 // Yes this method is horribly complex.... but there are lots of special cases and |
582 // as this method is called on every request, it is worth trying to save string creation. | |
583 // | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
584 |
813 | 585 if (contentType==null) |
586 { | |
587 if (_locale==null) | |
999 | 588 _characterEncoding = null; |
589 _mimeType = null; | |
590 _cachedMimeType = null; | |
813 | 591 _contentType=null; |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
592 _connection._responseFields.remove(HttpHeaders.CONTENT_TYPE); |
813 | 593 } |
594 else | |
595 { | |
596 // Look for encoding in contentType | |
597 int i0=contentType.indexOf(';'); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
598 |
813 | 599 if (i0>0) |
600 { | |
601 // we have content type parameters | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
602 |
813 | 603 // Extract params off mimetype |
999 | 604 _mimeType = contentType.substring(0,i0).trim(); |
605 _cachedMimeType = MimeTypes.CACHE.get(_mimeType); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
606 |
813 | 607 // Look for charset |
608 int i1=contentType.indexOf("charset=",i0+1); | |
609 if (i1>=0) | |
610 { | |
999 | 611 _explicitEncoding = true; |
813 | 612 int i8=i1+8; |
613 int i2 = contentType.indexOf(' ',i8); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
614 |
813 | 615 if (_outputState==WRITER) |
616 { | |
617 // strip the charset and ignore; | |
618 if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' ')) | |
619 { | |
620 if (_cachedMimeType!=null) | |
621 { | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
622 String content_type = MimeTypes.getAssociate(_cachedMimeType,_characterEncoding); |
813 | 623 if (content_type!=null) |
624 { | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
625 _contentType=content_type; |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
626 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 627 } |
628 else | |
629 { | |
999 | 630 _contentType = _mimeType+";charset="+_characterEncoding; |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
631 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 632 } |
633 } | |
634 else | |
635 { | |
999 | 636 _contentType = _mimeType+";charset="+_characterEncoding; |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
637 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 638 } |
639 } | |
640 else if (i2<0) | |
641 { | |
642 _contentType=contentType.substring(0,i1)+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
643 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 644 } |
645 else | |
646 { | |
647 _contentType=contentType.substring(0,i1)+contentType.substring(i2)+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
648 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 649 } |
650 } | |
651 else if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' ')) | |
652 { | |
653 // The params are just the char encoding | |
999 | 654 _cachedMimeType = MimeTypes.CACHE.get(_mimeType); |
813 | 655 _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8)); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
656 |
813 | 657 if (_cachedMimeType!=null) |
658 { | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
659 String content_type = MimeTypes.getAssociate(_cachedMimeType,_characterEncoding); |
813 | 660 if (content_type!=null) |
661 { | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
662 _contentType=content_type; |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
663 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 664 } |
665 else | |
666 { | |
667 _contentType=contentType; | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
668 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 669 } |
670 } | |
671 else | |
672 { | |
673 _contentType=contentType; | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
674 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 675 } |
676 } | |
677 else if (i2>0) | |
678 { | |
679 _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8,i2)); | |
680 _contentType=contentType; | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
681 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 682 } |
683 else | |
684 { | |
685 _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8)); | |
686 _contentType=contentType; | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
687 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 688 } |
689 } | |
690 else // No encoding in the params. | |
691 { | |
999 | 692 _cachedMimeType = null; |
813 | 693 _contentType=_characterEncoding==null?contentType:contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
694 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 695 } |
696 } | |
697 else // No params at all | |
698 { | |
999 | 699 _mimeType = contentType; |
700 _cachedMimeType = MimeTypes.CACHE.get(_mimeType); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
701 |
813 | 702 if (_characterEncoding!=null) |
703 { | |
704 if (_cachedMimeType!=null) | |
705 { | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
706 String content_type = MimeTypes.getAssociate(_cachedMimeType,_characterEncoding); |
813 | 707 if (content_type!=null) |
708 { | |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
709 _contentType=content_type; |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
710 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 711 } |
712 else | |
713 { | |
999 | 714 _contentType = _mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
715 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 716 } |
717 } | |
718 else | |
719 { | |
720 _contentType=contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
721 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 722 } |
723 } | |
724 else if (_cachedMimeType!=null) | |
725 { | |
999 | 726 _contentType = _cachedMimeType.toString(); |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
727 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 728 } |
729 else | |
730 { | |
731 _contentType=contentType; | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
732 _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); |
813 | 733 } |
734 } | |
735 } | |
736 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
737 |
999 | 738 @Override |
813 | 739 public void setBufferSize(int size) |
740 { | |
983
23ec25435b8c
simplify AbstractGenerator
Franklin Schmidt <fschmidt@gmail.com>
parents:
982
diff
changeset
|
741 throw new UnsupportedOperationException(); |
813 | 742 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
743 |
999 | 744 @Override |
813 | 745 public int getBufferSize() |
746 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
747 return _connection._generator.getContentBufferSize(); |
813 | 748 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
749 |
999 | 750 @Override |
813 | 751 public void flushBuffer() throws IOException |
752 { | |
753 _connection.flushResponse(); | |
754 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
755 |
999 | 756 @Override |
813 | 757 public void reset() |
758 { | |
759 resetBuffer(); | |
760 fwdReset(); | |
999 | 761 _status = 200; |
762 _reason = null; | |
813 | 763 |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
764 HttpFields response_fields=_connection._responseFields; |
813 | 765 |
766 response_fields.clear(); | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
767 String connection=_connection._requestFields.getStringField(HttpHeaders.CONNECTION); |
813 | 768 if (connection!=null) |
769 { | |
770 String[] values = connection.split(","); | |
771 for (int i=0;values!=null && i<values.length;i++) | |
772 { | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1018
diff
changeset
|
773 int cb = HttpHeaderValues.CACHE.getOrdinal(values[0].trim()); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
774 |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1018
diff
changeset
|
775 if (cb != -1) |
813 | 776 { |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1018
diff
changeset
|
777 switch(cb) |
813 | 778 { |
779 case HttpHeaderValues.CLOSE_ORDINAL: | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
780 response_fields.put(HttpHeaders.CONNECTION,HttpHeaderValues.CLOSE); |
813 | 781 break; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
782 |
813 | 783 case HttpHeaderValues.KEEP_ALIVE_ORDINAL: |
784 if (HttpVersions.HTTP_1_0.equalsIgnoreCase(_connection.getRequest().getProtocol())) | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
785 response_fields.put(HttpHeaders.CONNECTION,HttpHeaderValues.KEEP_ALIVE); |
813 | 786 break; |
787 case HttpHeaderValues.TE_ORDINAL: | |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
788 response_fields.put(HttpHeaders.CONNECTION,HttpHeaderValues.TE); |
813 | 789 break; |
790 } | |
791 } | |
792 } | |
793 } | |
794 } | |
795 | |
796 | |
999 | 797 private void fwdReset() |
813 | 798 { |
799 resetBuffer(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
800 |
813 | 801 _writer=null; |
802 _outputState=NONE; | |
803 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
804 |
999 | 805 @Override |
813 | 806 public void resetBuffer() |
807 { | |
808 if (isCommitted()) | |
809 throw new IllegalStateException("Committed"); | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
810 _connection._generator.resetBuffer(); |
813 | 811 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
812 |
999 | 813 @Override |
813 | 814 public boolean isCommitted() |
815 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
816 return _connection._generator.isCommitted(); |
813 | 817 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
818 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
819 |
999 | 820 @Override |
813 | 821 public void setLocale(Locale locale) |
822 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
823 if (locale == null || isCommitted()) |
813 | 824 return; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
825 |
813 | 826 _locale = locale; |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
827 _connection._responseFields.put(HttpHeaders.CONTENT_LANGUAGE,locale.toString().replace('_','-')); |
813 | 828 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
829 |
999 | 830 @Override |
813 | 831 public Locale getLocale() |
832 { | |
833 if (_locale==null) | |
834 return Locale.getDefault(); | |
835 return _locale; | |
836 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
837 |
813 | 838 /* ------------------------------------------------------------ */ |
839 /** | |
840 * @return The HTTP status code that has been set for this request. This will be <code>200<code> | |
841 * ({@link HttpServletResponse#SC_OK}), unless explicitly set through one of the <code>setStatus</code> methods. | |
842 */ | |
843 public int getStatus() | |
844 { | |
845 return _status; | |
846 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
847 |
813 | 848 /* ------------------------------------------------------------ */ |
849 /** | |
850 * @return The reason associated with the current {@link #getStatus() status}. This will be <code>null</code>, | |
851 * unless one of the <code>setStatus</code> methods have been called. | |
852 */ | |
853 public String getReason() | |
854 { | |
855 return _reason; | |
856 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
857 |
813 | 858 public void complete() |
859 throws IOException | |
860 { | |
861 _connection.completeResponse(); | |
862 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
863 |
813 | 864 /* ------------------------------------------------------------- */ |
865 /** | |
866 * @return the number of bytes actually written in response body | |
867 */ | |
868 public long getContentCount() | |
869 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
870 if (_connection==null) |
813 | 871 return -1; |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
872 return _connection._generator.getContentWritten(); |
813 | 873 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
874 |
813 | 875 public HttpFields getHttpFields() |
876 { | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
877 return _connection._responseFields; |
813 | 878 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
879 |
813 | 880 @Override |
881 public String toString() | |
882 { | |
883 return "HTTP/1.1 "+_status+" "+ (_reason==null?"":_reason) +System.getProperty("line.separator")+ | |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
911
diff
changeset
|
884 _connection._responseFields.toString(); |
813 | 885 } |
886 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
887 } |