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