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