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