Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/AbstractHttpConnection.java @ 997:7d28be82ab75
simplify Request
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 18 Oct 2016 22:43:17 -0600 |
| parents | d9cfec64899c |
| children | 74b9daf2826c |
| 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.InputStream; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.io.PrintWriter; |
| 985 | 24 import java.io.Writer; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import javax.servlet.ServletInputStream; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import javax.servlet.ServletOutputStream; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import javax.servlet.http.HttpServletRequest; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import javax.servlet.http.HttpServletResponse; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import org.eclipse.jetty.http.EncodedHttpURI; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import org.eclipse.jetty.http.HttpBuffers; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import org.eclipse.jetty.http.HttpException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import org.eclipse.jetty.http.HttpFields; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import org.eclipse.jetty.http.HttpGenerator; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 import org.eclipse.jetty.http.HttpHeaderValues; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 import org.eclipse.jetty.http.HttpHeaders; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 import org.eclipse.jetty.http.HttpMethods; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 import org.eclipse.jetty.http.HttpParser; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 import org.eclipse.jetty.http.HttpStatus; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 import org.eclipse.jetty.http.HttpURI; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 import org.eclipse.jetty.http.HttpVersions; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 import org.eclipse.jetty.http.MimeTypes; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 import org.eclipse.jetty.io.AbstractConnection; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.io.Buffer; |
|
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.io.Buffers; |
| 985 | 48 import org.eclipse.jetty.io.ByteArrayBuffer; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 import org.eclipse.jetty.io.EndPoint; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 import org.eclipse.jetty.io.EofException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 import org.eclipse.jetty.io.RuntimeIOException; |
| 985 | 52 import org.eclipse.jetty.util.ByteArrayOutputStream2; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 import org.eclipse.jetty.util.QuotedStringTokenizer; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 import org.eclipse.jetty.util.StringUtil; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 import org.eclipse.jetty.util.URIUtil; |
|
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
56 import org.slf4j.Logger; |
|
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
57 import org.slf4j.LoggerFactory; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 import org.eclipse.jetty.util.resource.Resource; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 /** |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 * <p>A HttpConnection represents the connection of a HTTP client to the server |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 * and is created by an instance of a {@link Connector}. It's prime function is |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 * to associate {@link Request} and {@link Response} instances with a {@link EndPoint}. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
64 * </p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
65 * <p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
66 * A connection is also the prime mechanism used by jetty to recycle objects without |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
67 * pooling. The {@link Request}, {@link Response}, {@link HttpParser}, {@link HttpGenerator} |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
68 * and {@link HttpFields} instances are all recycled for the duraction of |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
69 * a connection. Where appropriate, allocated buffers are also kept associated |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
70 * with the connection via the parser and/or generator. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 * </p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
72 * <p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
73 * The connection state is held by 3 separate state machines: The request state, the |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
74 * response state and the continuation state. All three state machines must be driven |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
75 * to completion for every request, and all three can complete in any order. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
76 * </p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
77 * <p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
78 * The HttpConnection support protocol upgrade. If on completion of a request, the |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
79 * response code is 101 (switch protocols), then the org.eclipse.jetty.io.Connection |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
80 * request attribute is checked to see if there is a new Connection instance. If so, |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
81 * the new connection is returned from {@link #handle()} and is used for future |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
82 * handling of the underlying connection. Note that for switching protocols that |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
83 * don't use 101 responses (eg CONNECT), the response should be sent and then the |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
84 * status code changed to 101 before returning from the handler. Implementors |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
85 * of new Connection types should be careful to extract any buffered data from |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
86 * (HttpParser)http.getParser()).getHeaderBuffer() and |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
87 * (HttpParser)http.getParser()).getBodyBuffer() to initialise their new connection. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
88 * </p> |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
89 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
90 */ |
| 982 | 91 public abstract class AbstractHttpConnection extends AbstractConnection |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
92 { |
| 826 | 93 private static final Logger LOG = LoggerFactory.getLogger(AbstractHttpConnection.class); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
94 |
| 826 | 95 private int _requests; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
96 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
97 private final Connector _connector; |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
98 private final Server _server; |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
99 protected final HttpURI _uri = StringUtil.__UTF8.equals(URIUtil.__CHARSET)?new HttpURI():new EncodedHttpURI(URIUtil.__CHARSET); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
100 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
101 protected final HttpParser _parser; |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
102 protected final HttpFields _requestFields = new HttpFields(); |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
103 private final Request _request; |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
104 private volatile ServletInputStream _in; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
105 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
106 protected final HttpGenerator _generator; |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
107 final HttpFields _responseFields = new HttpFields(); |
| 826 | 108 protected final Response _response; |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
109 private volatile Output _out; |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
110 private volatile HttpWriter _writer; |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
111 private volatile PrintWriter _printWriter; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
112 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
113 private int _version = -2; // UNKNOWN |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
114 |
| 826 | 115 private String _charset; |
| 116 private boolean _expect = false; | |
| 117 private boolean _expect100Continue = false; | |
| 118 private boolean _expect102Processing = false; | |
| 119 private boolean _head = false; | |
| 120 private boolean _host = false; | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
121 private boolean _delayedHandling = false; |
| 826 | 122 private boolean _earlyEOF = false; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
123 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
124 protected AbstractHttpConnection(Connector connector, EndPoint endpoint) |
| 826 | 125 { |
| 126 super(endpoint); | |
| 127 _connector = connector; | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
128 HttpBuffers ab = _connector; |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
129 _parser = new HttpParser(ab.getRequestBuffers(), endpoint, new RequestHandler()); |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
130 _request = new Request(this); |
| 826 | 131 _response = new Response(this); |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
132 _generator = new HttpGenerator(ab.getResponseBuffers(), endpoint); |
|
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
133 _server = connector.server; |
| 826 | 134 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
135 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
136 public final Connector getConnector() |
| 826 | 137 { |
| 138 return _connector; | |
| 139 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
140 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
141 public final Request getRequest() |
| 826 | 142 { |
| 143 return _request; | |
| 144 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
145 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
146 public final Response getResponse() |
| 826 | 147 { |
| 148 return _response; | |
| 149 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
150 |
| 826 | 151 /* ------------------------------------------------------------ */ |
| 152 /** | |
| 153 * Get the inputStream from the connection. | |
| 154 * <p> | |
| 155 * If the associated response has the Expect header set to 100 Continue, | |
| 156 * then accessing the input stream indicates that the handler/servlet | |
| 157 * is ready for the request body and thus a 100 Continue response is sent. | |
| 158 * | |
| 159 * @return The input stream for this connection. | |
| 160 * The stream will be created if it does not already exist. | |
| 161 * @throws IOException if the input stream cannot be retrieved | |
| 162 */ | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
163 public final ServletInputStream getInputStream() throws IOException |
| 826 | 164 { |
| 165 // If the client is expecting 100 CONTINUE, then send it now. | |
| 166 if (_expect100Continue) | |
| 167 { | |
| 168 // is content missing? | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
169 if (_parser.getHeaderBuffer()==null || _parser.getHeaderBuffer().length()<2) |
| 826 | 170 { |
| 171 if (_generator.isCommitted()) | |
| 172 throw new IllegalStateException("Committed before 100 Continues"); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
173 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
174 _generator.send1xx(HttpStatus.CONTINUE_100); |
| 826 | 175 } |
| 176 _expect100Continue=false; | |
| 177 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
178 |
| 826 | 179 if (_in == null) |
| 981 | 180 _in = new HttpInput(); |
| 826 | 181 return _in; |
| 182 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
183 |
| 826 | 184 /* ------------------------------------------------------------ */ |
| 185 /** | |
| 186 * @return The output stream for this connection. The stream will be created if it does not already exist. | |
| 187 */ | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
188 public final ServletOutputStream getOutputStream() |
| 826 | 189 { |
| 190 if (_out == null) | |
| 191 _out = new Output(); | |
| 192 return _out; | |
| 193 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
194 |
| 826 | 195 /* ------------------------------------------------------------ */ |
| 196 /** | |
| 197 * @param encoding the PrintWriter encoding | |
| 198 * @return A {@link PrintWriter} wrapping the {@link #getOutputStream output stream}. The writer is created if it | |
| 199 * does not already exist. | |
| 200 */ | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
201 public final PrintWriter getPrintWriter(String encoding) |
| 826 | 202 { |
| 203 getOutputStream(); | |
| 204 if (_writer==null) | |
| 205 { | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
206 _writer = new HttpWriter(_out); |
|
882
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
207 _printWriter = new PrintWriter(_writer) |
|
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
208 { |
|
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
209 public void close() |
| 826 | 210 { |
|
882
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
211 synchronized (lock) |
| 826 | 212 { |
|
882
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
213 try |
| 826 | 214 { |
|
882
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
215 out.close(); |
|
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
216 } |
|
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
217 catch (IOException e) |
|
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
218 { |
|
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
219 setError(); |
| 826 | 220 } |
| 221 } | |
|
882
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
222 } |
|
d850a5e6ff15
remove UncheckedPrintWriter
Franklin Schmidt <fschmidt@gmail.com>
parents:
878
diff
changeset
|
223 }; |
| 826 | 224 } |
| 225 _writer.setCharacterEncoding(encoding); | |
| 226 return _printWriter; | |
| 227 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
228 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
229 protected void reset() |
| 826 | 230 { |
| 231 _parser.reset(); | |
| 232 _parser.returnBuffers(); // TODO maybe only on unhandle | |
| 233 _requestFields.clear(); | |
| 234 _request.recycle(); | |
| 235 _generator.reset(); | |
| 236 _generator.returnBuffers();// TODO maybe only on unhandle | |
| 237 _responseFields.clear(); | |
| 238 _response.recycle(); | |
| 239 _uri.clear(); | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
240 _writer = null; |
| 826 | 241 _earlyEOF = false; |
| 242 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
243 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
244 private void handleRequest() throws IOException |
| 826 | 245 { |
| 246 boolean error = false; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
247 |
| 826 | 248 try |
| 249 { | |
| 250 // Loop here to handle async request redispatches. | |
| 251 // The loop is controlled by the call to async.unhandle in the | |
| 252 // finally block below. If call is from a non-blocking connector, | |
| 253 // then the unhandle will return false only if an async dispatch has | |
| 254 // already happened when unhandle is called. For a blocking connector, | |
| 255 // the wait for the asynchronous dispatch or timeout actually happens | |
| 256 // within the call to unhandle(). | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
257 |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
258 _request.setHandled(false); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
259 |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
260 String info=null; |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
261 try |
| 826 | 262 { |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
263 _uri.getPort(); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
264 String path = null; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
265 |
| 826 | 266 try |
| 267 { | |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
268 path = _uri.getDecodedPath(); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
269 } |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
270 catch (Exception e) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
271 { |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
272 LOG.warn("Failed UTF-8 decode for request path, trying ISO-8859-1"); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
273 LOG.trace("",e); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
274 path = _uri.getDecodedPath(StringUtil.__ISO_8859_1); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
275 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
276 |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
277 info = URIUtil.canonicalPath(path); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
278 if (info==null && !_request.getMethod().equals(HttpMethods.CONNECT)) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
279 { |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
280 if (path==null && _uri.getScheme()!=null && _uri.getHost()!=null) |
| 826 | 281 { |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
282 info="/"; |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
283 _request.setRequestURI(""); |
| 826 | 284 } |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
285 else |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
286 throw new HttpException(400); |
| 826 | 287 } |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
288 _request.setPathInfo(info); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
289 |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
290 if (_out!=null) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
291 _out.reopen(); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
292 |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
293 _connector.customize(_endp, _request); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
294 _server.handle(this); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
295 } |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
296 catch (EofException e) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
297 { |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
298 LOG.debug("",e); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
299 error=true; |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
300 _request.setHandled(true); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
301 if (!_response.isCommitted()) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
302 _generator.sendError(500, null, null, true); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
303 } |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
304 catch (RuntimeIOException e) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
305 { |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
306 LOG.debug("",e); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
307 error=true; |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
308 _request.setHandled(true); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
309 } |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
310 catch (HttpException e) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
311 { |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
312 LOG.debug("",e); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
313 error=true; |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
314 _request.setHandled(true); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
315 _response.sendError(e.getStatus(), e.getReason()); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
316 } |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
317 catch (Throwable e) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
318 { |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
319 LOG.warn(String.valueOf(_uri),e); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
320 error=true; |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
321 _request.setHandled(true); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
322 _generator.sendError(info==null?400:500, null, null, true); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
323 |
| 826 | 324 } |
| 325 } | |
| 326 finally | |
| 327 { | |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
328 if (_expect100Continue) |
| 826 | 329 { |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
330 LOG.debug("100 continues not sent"); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
331 // We didn't send 100 continues, but the latest interpretation |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
332 // of the spec (see httpbis) is that the client will either |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
333 // send the body anyway, or close. So we no longer need to |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
334 // do anything special here other than make the connection not persistent |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
335 _expect100Continue = false; |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
336 if (!_response.isCommitted()) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
337 _generator.setPersistent(false); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
338 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
339 |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
340 if(_endp.isOpen()) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
341 { |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
342 if (error) |
| 826 | 343 { |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
344 _endp.shutdownOutput(); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
345 _generator.setPersistent(false); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
346 if (!_generator.isComplete()) |
| 826 | 347 _response.complete(); |
| 348 } | |
| 349 else | |
| 350 { | |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
351 if (!_response.isCommitted() && !_request.isHandled()) |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
352 _response.sendError(HttpServletResponse.SC_NOT_FOUND); |
| 826 | 353 _response.complete(); |
| 354 } | |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
355 } |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
356 else |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
357 { |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
358 _response.complete(); |
|
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
359 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
360 |
|
939
8db5996c8c89
remove AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
934
diff
changeset
|
361 _request.setHandled(true); |
| 826 | 362 } |
| 363 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
364 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
365 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
366 public final void commitResponse(boolean last) throws IOException |
| 826 | 367 { |
| 368 if (!_generator.isCommitted()) | |
| 369 { | |
| 370 _generator.setResponse(_response.getStatus(), _response.getReason()); | |
| 371 try | |
| 372 { | |
| 373 // If the client was expecting 100 continues, but we sent something | |
| 374 // else, then we need to close the connection | |
| 375 if (_expect100Continue && _response.getStatus()!=100) | |
| 376 _generator.setPersistent(false); | |
| 377 _generator.completeHeader(_responseFields, last); | |
| 378 } | |
| 379 catch(RuntimeException e) | |
| 380 { | |
| 381 LOG.warn("header full: " + e); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
382 |
| 826 | 383 _response.reset(); |
| 384 _generator.reset(); | |
| 385 _generator.setResponse(HttpStatus.INTERNAL_SERVER_ERROR_500,null); | |
| 982 | 386 _generator.completeHeader(_responseFields,HttpGenerator.LAST); |
| 826 | 387 _generator.complete(); |
| 388 throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR_500); | |
| 389 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
390 |
| 826 | 391 } |
| 392 if (last) | |
| 393 _generator.complete(); | |
| 394 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
395 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
396 public final void completeResponse() throws IOException |
| 826 | 397 { |
| 398 if (!_generator.isCommitted()) | |
| 399 { | |
| 400 _generator.setResponse(_response.getStatus(), _response.getReason()); | |
| 401 try | |
| 402 { | |
| 982 | 403 _generator.completeHeader(_responseFields, HttpGenerator.LAST); |
| 826 | 404 } |
| 405 catch(RuntimeException e) | |
| 406 { | |
| 407 LOG.warn("header full: "+e); | |
| 408 LOG.debug("",e); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
409 |
| 826 | 410 _response.reset(); |
| 411 _generator.reset(); | |
| 412 _generator.setResponse(HttpStatus.INTERNAL_SERVER_ERROR_500,null); | |
| 982 | 413 _generator.completeHeader(_responseFields,HttpGenerator.LAST); |
| 826 | 414 _generator.complete(); |
| 415 throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR_500); | |
| 416 } | |
| 417 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
418 |
| 826 | 419 _generator.complete(); |
| 420 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
421 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
422 public final void flushResponse() throws IOException |
| 826 | 423 { |
| 424 try | |
| 425 { | |
| 982 | 426 commitResponse(HttpGenerator.MORE); |
| 826 | 427 _generator.flushBuffer(); |
| 428 } | |
| 429 catch(IOException e) | |
| 430 { | |
| 431 throw (e instanceof EofException) ? e:new EofException(e); | |
| 432 } | |
| 433 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
434 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
435 public final boolean isExpecting102Processing() |
| 826 | 436 { |
| 437 return _expect102Processing; | |
| 438 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
439 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
440 public final int getMaxIdleTime() |
| 826 | 441 { |
| 996 | 442 if (_connector.server.isLowOnThreads() && _endp.getMaxIdleTime()==_connector.getMaxIdleTime()) |
| 913 | 443 return 0; |
| 826 | 444 if (_endp.getMaxIdleTime()>0) |
| 445 return _endp.getMaxIdleTime(); | |
| 446 return _connector.getMaxIdleTime(); | |
| 447 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
448 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
449 @Override |
| 826 | 450 public String toString() |
| 451 { | |
| 452 return String.format("%s,g=%s,p=%s,r=%d", | |
| 453 super.toString(), | |
| 454 _generator, | |
| 455 _parser, | |
| 456 _requests); | |
| 457 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
458 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
459 private void startRequest(Buffer method, Buffer uri, Buffer version) throws IOException |
| 826 | 460 { |
| 461 uri=uri.asImmutableBuffer(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
462 |
| 826 | 463 _host = false; |
| 464 _expect = false; | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
465 _expect100Continue = false; |
| 826 | 466 _expect102Processing=false; |
| 467 _delayedHandling=false; | |
| 468 _charset=null; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
469 |
| 826 | 470 if(_request.getTimeStamp()==0) |
| 471 _request.setTimeStamp(System.currentTimeMillis()); | |
| 472 _request.setMethod(method.toString()); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
473 |
| 826 | 474 try |
| 475 { | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
476 _head = false; |
| 826 | 477 switch (HttpMethods.CACHE.getOrdinal(method)) |
| 478 { | |
| 479 case HttpMethods.CONNECT_ORDINAL: | |
| 480 _uri.parseConnect(uri.array(), uri.getIndex(), uri.length()); | |
| 481 break; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
482 |
| 826 | 483 case HttpMethods.HEAD_ORDINAL: |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
484 _head = true; |
| 826 | 485 _uri.parse(uri.array(), uri.getIndex(), uri.length()); |
| 486 break; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
487 |
| 826 | 488 default: |
| 489 _uri.parse(uri.array(), uri.getIndex(), uri.length()); | |
| 490 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
491 |
| 826 | 492 _request.setUri(_uri); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
493 |
| 826 | 494 if (version==null) |
| 495 { | |
| 496 _request.setProtocol(HttpVersions.HTTP_0_9); | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
497 _version = HttpVersions.HTTP_0_9_ORDINAL; |
| 826 | 498 } |
| 499 else | |
| 500 { | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
501 version = HttpVersions.CACHE.get(version); |
| 826 | 502 if (version==null) |
| 503 throw new HttpException(HttpStatus.BAD_REQUEST_400,null); | |
| 504 _version = HttpVersions.CACHE.getOrdinal(version); | |
| 505 if (_version <= 0) _version = HttpVersions.HTTP_1_0_ORDINAL; | |
| 506 _request.setProtocol(version.toString()); | |
| 507 } | |
| 508 } | |
| 509 catch (Exception e) | |
| 510 { | |
| 511 LOG.debug("",e); | |
| 512 if (e instanceof HttpException) | |
| 513 throw (HttpException)e; | |
| 514 throw new HttpException(HttpStatus.BAD_REQUEST_400,null,e); | |
| 515 } | |
| 516 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
517 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
518 private void parsedHeader(Buffer name, Buffer value) throws IOException |
| 826 | 519 { |
| 520 int ho = HttpHeaders.CACHE.getOrdinal(name); | |
| 521 switch (ho) | |
| 522 { | |
| 523 case HttpHeaders.HOST_ORDINAL: | |
| 524 // TODO check if host matched a host in the URI. | |
| 525 _host = true; | |
| 526 break; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
527 |
| 826 | 528 case HttpHeaders.EXPECT_ORDINAL: |
| 529 if (_version>=HttpVersions.HTTP_1_1_ORDINAL) | |
| 530 { | |
| 531 value = HttpHeaderValues.CACHE.lookup(value); | |
| 532 switch(HttpHeaderValues.CACHE.getOrdinal(value)) | |
| 533 { | |
| 534 case HttpHeaderValues.CONTINUE_ORDINAL: | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
535 _expect100Continue = true; |
| 826 | 536 break; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
537 |
| 826 | 538 case HttpHeaderValues.PROCESSING_ORDINAL: |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
539 _expect102Processing = true; |
| 826 | 540 break; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
541 |
| 826 | 542 default: |
| 543 String[] values = value.toString().split(","); | |
| 544 for (int i=0;values!=null && i<values.length;i++) | |
| 545 { | |
| 546 CachedBuffer cb=HttpHeaderValues.CACHE.get(values[i].trim()); | |
| 547 if (cb==null) | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
548 _expect = true; |
| 826 | 549 else |
| 550 { | |
| 551 switch(cb.getOrdinal()) | |
| 552 { | |
| 553 case HttpHeaderValues.CONTINUE_ORDINAL: | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
554 _expect100Continue = true; |
| 826 | 555 break; |
| 556 case HttpHeaderValues.PROCESSING_ORDINAL: | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
557 _expect102Processing = true; |
| 826 | 558 break; |
| 559 default: | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
560 _expect = true; |
| 826 | 561 } |
| 562 } | |
| 563 } | |
| 564 } | |
| 565 } | |
| 566 break; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
567 |
| 826 | 568 case HttpHeaders.ACCEPT_ENCODING_ORDINAL: |
| 569 case HttpHeaders.USER_AGENT_ORDINAL: | |
| 570 value = HttpHeaderValues.CACHE.lookup(value); | |
| 571 break; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
572 |
| 826 | 573 case HttpHeaders.CONTENT_TYPE_ORDINAL: |
| 574 value = MimeTypes.CACHE.lookup(value); | |
| 575 _charset=MimeTypes.getCharsetFromContentType(value); | |
| 576 break; | |
| 577 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
578 |
| 826 | 579 _requestFields.add(name, value); |
| 580 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
581 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
582 private void headerComplete() throws IOException |
| 826 | 583 { |
| 584 // Handle idle race | |
| 585 if (_endp.isOutputShutdown()) | |
| 586 { | |
| 587 _endp.close(); | |
| 588 return; | |
| 589 } | |
| 590 | |
| 591 _requests++; | |
| 592 _generator.setVersion(_version); | |
| 593 switch (_version) | |
| 594 { | |
| 595 case HttpVersions.HTTP_0_9_ORDINAL: | |
| 596 break; | |
| 597 case HttpVersions.HTTP_1_0_ORDINAL: | |
| 598 _generator.setHead(_head); | |
| 599 if (_parser.isPersistent()) | |
| 600 { | |
| 601 _responseFields.add(HttpHeaders.CONNECTION_BUFFER, HttpHeaderValues.KEEP_ALIVE_BUFFER); | |
| 602 _generator.setPersistent(true); | |
| 603 } | |
| 604 else if (HttpMethods.CONNECT.equals(_request.getMethod())) | |
| 605 { | |
| 606 _generator.setPersistent(true); | |
| 607 _parser.setPersistent(true); | |
| 608 } | |
| 609 break; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
610 |
| 826 | 611 case HttpVersions.HTTP_1_1_ORDINAL: |
| 612 _generator.setHead(_head); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
613 |
| 826 | 614 if (!_parser.isPersistent()) |
| 615 { | |
| 616 _responseFields.add(HttpHeaders.CONNECTION_BUFFER,HttpHeaderValues.CLOSE_BUFFER); | |
| 617 _generator.setPersistent(false); | |
| 618 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
619 |
| 826 | 620 if (!_host) |
| 621 { | |
| 622 LOG.debug("!host {}",this); | |
| 623 _generator.setResponse(HttpStatus.BAD_REQUEST_400, null); | |
| 624 _responseFields.put(HttpHeaders.CONNECTION_BUFFER, HttpHeaderValues.CLOSE_BUFFER); | |
| 625 _generator.completeHeader(_responseFields, true); | |
| 626 _generator.complete(); | |
| 627 return; | |
| 628 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
629 |
| 826 | 630 if (_expect) |
| 631 { | |
| 632 LOG.debug("!expectation {}",this); | |
| 633 _generator.setResponse(HttpStatus.EXPECTATION_FAILED_417, null); | |
| 634 _responseFields.put(HttpHeaders.CONNECTION_BUFFER, HttpHeaderValues.CLOSE_BUFFER); | |
| 635 _generator.completeHeader(_responseFields, true); | |
| 636 _generator.complete(); | |
| 637 return; | |
| 638 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
639 |
| 826 | 640 break; |
| 641 default: | |
| 642 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
643 |
| 826 | 644 if(_charset!=null) |
| 645 _request.setCharacterEncodingUnchecked(_charset); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
646 |
| 826 | 647 // Either handle now or wait for first content |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
648 if ((_parser.getContentLength()<=0 && !_parser.isChunking())||_expect100Continue) |
| 826 | 649 handleRequest(); |
| 650 else | |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
651 _delayedHandling = true; |
| 826 | 652 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
653 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
654 private void content(Buffer buffer) throws IOException |
| 826 | 655 { |
| 656 if (_delayedHandling) | |
| 657 { | |
|
974
7422ca1ae146
remove Connection.isIdle()
Franklin Schmidt <fschmidt@gmail.com>
parents:
972
diff
changeset
|
658 _delayedHandling = false; |
| 826 | 659 handleRequest(); |
| 660 } | |
| 661 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
662 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
663 private void messageComplete(long contentLength) throws IOException |
| 826 | 664 { |
| 665 if (_delayedHandling) | |
| 666 { | |
|
974
7422ca1ae146
remove Connection.isIdle()
Franklin Schmidt <fschmidt@gmail.com>
parents:
972
diff
changeset
|
667 _delayedHandling = false; |
| 826 | 668 handleRequest(); |
| 669 } | |
| 670 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
671 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
672 private void earlyEOF() |
| 826 | 673 { |
| 674 _earlyEOF = true; | |
| 675 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
676 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
677 |
| 980 | 678 private class RequestHandler implements HttpParser.EventHandler |
| 826 | 679 { |
| 680 @Override | |
| 681 public void startRequest(Buffer method, Buffer uri, Buffer version) throws IOException | |
| 682 { | |
| 683 AbstractHttpConnection.this.startRequest(method, uri, version); | |
| 684 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
685 |
| 826 | 686 @Override |
| 687 public void parsedHeader(Buffer name, Buffer value) throws IOException | |
| 688 { | |
| 689 AbstractHttpConnection.this.parsedHeader(name, value); | |
| 690 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
691 |
| 826 | 692 @Override |
| 693 public void headerComplete() throws IOException | |
| 694 { | |
| 695 AbstractHttpConnection.this.headerComplete(); | |
| 696 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
697 |
| 826 | 698 @Override |
| 699 public void content(Buffer ref) throws IOException | |
| 700 { | |
| 701 AbstractHttpConnection.this.content(ref); | |
| 702 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
703 |
| 826 | 704 @Override |
| 705 public void messageComplete(long contentLength) throws IOException | |
| 706 { | |
| 707 AbstractHttpConnection.this.messageComplete(contentLength); | |
| 708 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
709 |
| 826 | 710 @Override |
| 711 public void earlyEOF() | |
| 712 { | |
| 713 AbstractHttpConnection.this.earlyEOF(); | |
| 714 } | |
| 715 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
716 |
|
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
965
diff
changeset
|
717 |
| 985 | 718 public final class Output extends ServletOutputStream |
| 826 | 719 { |
| 985 | 720 private boolean _closed; |
| 721 private ByteArrayBuffer _onebyte; | |
| 722 | |
| 723 // These are held here for reuse by Writer | |
| 724 String _characterEncoding; | |
| 725 Writer _converter; | |
| 726 char[] _chars; | |
| 727 ByteArrayOutputStream2 _bytes; | |
| 728 | |
| 729 public final void reopen() | |
| 730 { | |
| 731 _closed = false; | |
| 732 } | |
| 733 | |
| 734 @Override | |
| 735 public final void write(byte[] b, int off, int len) throws IOException | |
| 736 { | |
| 737 write(new ByteArrayBuffer(b,off,len)); | |
| 738 } | |
| 739 | |
| 740 @Override | |
| 741 public final void write(byte[] b) throws IOException | |
| 742 { | |
| 743 write(new ByteArrayBuffer(b)); | |
| 744 } | |
| 745 | |
| 746 @Override | |
| 747 public final void write(int b) throws IOException | |
| 826 | 748 { |
| 985 | 749 if (_onebyte==null) |
| 750 _onebyte = new ByteArrayBuffer(1); | |
| 751 else | |
| 752 _onebyte.clear(); | |
| 753 _onebyte.put((byte)b); | |
| 754 write(_onebyte); | |
| 826 | 755 } |
| 985 | 756 |
| 757 private void write(Buffer buffer) throws IOException | |
| 758 { | |
| 759 if (_closed) | |
| 760 throw new IOException("Closed"); | |
| 761 if (!_generator.isOpen()) | |
| 762 throw new EofException(); | |
| 763 | |
| 764 // Block until we can add _content. | |
| 765 while (_generator.isBufferFull()) | |
| 766 { | |
| 767 _generator.blockForOutput(getMaxIdleTime()); | |
| 768 if (_closed) | |
| 769 throw new IOException("Closed"); | |
| 770 if (!_generator.isOpen()) | |
| 771 throw new EofException(); | |
| 772 } | |
| 773 | |
| 774 // Add the _content | |
| 775 _generator.addContent(buffer, HttpGenerator.MORE); | |
| 776 | |
| 777 // Have to flush and complete headers? | |
| 778 | |
| 779 if (_generator.isAllContentWritten()) | |
| 780 { | |
| 781 flush(); | |
| 782 close(); | |
| 783 } | |
| 784 else if (_generator.isBufferFull()) | |
| 785 commitResponse(HttpGenerator.MORE); | |
| 786 | |
| 787 // Block until our buffer is free | |
| 788 while (buffer.length() > 0 && _generator.isOpen()) | |
| 789 { | |
| 790 _generator.blockForOutput(getMaxIdleTime()); | |
| 791 } | |
| 792 } | |
| 793 | |
| 794 | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
795 |
| 826 | 796 /* ------------------------------------------------------------ */ |
| 797 /* | |
| 798 * @see java.io.OutputStream#close() | |
| 799 */ | |
| 800 @Override | |
| 801 public void close() throws IOException | |
| 802 { | |
| 985 | 803 if (_closed) |
| 826 | 804 return; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
805 |
| 985 | 806 if (!_generator.isCommitted()) |
| 982 | 807 commitResponse(HttpGenerator.LAST); |
| 826 | 808 else |
| 809 flushResponse(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
810 |
| 985 | 811 _closed = true; |
| 826 | 812 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
813 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
814 |
| 826 | 815 /* ------------------------------------------------------------ */ |
| 816 /* | |
| 817 * @see java.io.OutputStream#flush() | |
| 818 */ | |
| 819 @Override | |
| 820 public void flush() throws IOException | |
| 821 { | |
| 985 | 822 if (!_generator.isCommitted()) |
| 982 | 823 commitResponse(HttpGenerator.MORE); |
| 985 | 824 _generator.flush(getMaxIdleTime()); |
| 826 | 825 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
826 |
| 826 | 827 /* ------------------------------------------------------------ */ |
| 828 /* | |
| 829 * @see javax.servlet.ServletOutputStream#print(java.lang.String) | |
| 830 */ | |
| 831 @Override | |
| 832 public void print(String s) throws IOException | |
| 833 { | |
| 985 | 834 if (_closed) |
| 826 | 835 throw new IOException("Closed"); |
| 985 | 836 PrintWriter writer = getPrintWriter(null); |
| 826 | 837 writer.print(s); |
| 838 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
839 |
| 826 | 840 public void sendContent(Object content) throws IOException |
| 841 { | |
| 842 Resource resource=null; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
843 |
| 985 | 844 if (_closed) |
| 826 | 845 throw new IOException("Closed"); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
846 |
| 985 | 847 if (_generator.isWritten()) |
| 826 | 848 throw new IllegalStateException("!empty"); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
849 |
| 826 | 850 if (content instanceof Resource) |
| 851 { | |
| 852 resource=(Resource)content; | |
| 853 _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER, resource.lastModified()); | |
| 854 content=resource.getInputStream(); | |
| 855 } | |
| 856 | |
| 857 // Process content. | |
| 858 if (content instanceof Buffer) | |
| 859 { | |
| 985 | 860 _generator.addContent((Buffer) content, HttpGenerator.LAST); |
| 982 | 861 commitResponse(HttpGenerator.LAST); |
| 826 | 862 } |
| 863 else if (content instanceof InputStream) | |
| 864 { | |
| 865 InputStream in = (InputStream)content; | |
| 866 | |
| 867 try | |
| 868 { | |
| 985 | 869 int max = _generator.prepareUncheckedAddContent(); |
| 870 Buffer buffer = _generator.getUncheckedBuffer(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
871 |
| 826 | 872 int len=buffer.readFrom(in,max); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
873 |
| 826 | 874 while (len>=0) |
| 875 { | |
| 985 | 876 _generator.completeUncheckedAddContent(); |
| 826 | 877 _out.flush(); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
878 |
| 985 | 879 max = _generator.prepareUncheckedAddContent(); |
| 880 buffer = _generator.getUncheckedBuffer(); | |
| 826 | 881 len=buffer.readFrom(in,max); |
| 882 } | |
| 985 | 883 _generator.completeUncheckedAddContent(); |
| 826 | 884 _out.flush(); |
| 885 } | |
| 886 finally | |
| 887 { | |
| 888 if (resource!=null) | |
| 889 resource.release(); | |
| 890 else | |
| 891 in.close(); | |
| 892 } | |
| 893 } | |
| 894 else | |
| 895 throw new IllegalArgumentException("unknown content type?"); | |
| 981 | 896 } |
| 897 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
898 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
899 |
| 981 | 900 private final class HttpInput extends ServletInputStream |
| 901 { | |
| 902 /* ------------------------------------------------------------ */ | |
| 903 /* | |
| 904 * @see java.io.InputStream#read() | |
| 905 */ | |
| 906 @Override | |
| 907 public int read() throws IOException | |
| 908 { | |
| 909 byte[] bytes = new byte[1]; | |
| 910 int read = read(bytes, 0, 1); | |
| 911 return read < 0 ? -1 : 0xff & bytes[0]; | |
| 912 } | |
| 913 | |
| 914 /* ------------------------------------------------------------ */ | |
| 915 /* | |
| 916 * @see java.io.InputStream#read(byte[], int, int) | |
| 917 */ | |
| 918 @Override | |
| 919 public int read(byte[] b, int off, int len) throws IOException | |
| 920 { | |
| 921 int l = -1; | |
| 922 Buffer content = _parser.blockForContent(getMaxIdleTime()); | |
| 923 if (content!=null) | |
| 924 l = content.get(b, off, len); | |
| 925 else if (_earlyEOF) | |
| 926 throw new EofException("early EOF"); | |
| 927 return l; | |
| 928 } | |
| 929 | |
| 930 @Override | |
| 931 public int available() throws IOException | |
| 932 { | |
| 933 return _parser.available(); | |
| 826 | 934 } |
| 935 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
936 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
937 } |
