Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ea0d8f2b1277970018555c575ba1fc4c55d54dcc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package org.eclipse.jetty.client;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;

import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.nio.AsyncConnection;
import org.eclipse.jetty.io.nio.SslSelectChannelEndPoint;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;


public class AsyncHttpConnection extends AbstractHttpConnection implements AsyncConnection
{
    private static final Logger LOG = Log.getLogger(AsyncHttpConnection.class);
    
    private Buffer _requestContentChunk;
    private boolean _requestComplete;
    private int _status;
    
    AsyncHttpConnection(Buffers requestBuffers, Buffers responseBuffers, EndPoint endp)
    {
        super(requestBuffers,responseBuffers,endp);
    }

    protected void reset(boolean returnBuffers) throws IOException
    {
        _requestComplete = false;
        super.reset(returnBuffers);
    }
    
    public Connection handle() throws IOException
    {
        try
        {
            boolean progress=true;
            boolean failed = false;

            // While the endpoint is open 
            // AND we have more characters to read OR we made some progress 
            while (_endp.isOpen() && 
                   (_parser.isMoreInBuffer() || _endp.isBufferingInput() || progress))
            {
                
                // If no exchange, skipCRLF or close on unexpected characters
                HttpExchange exchange;
                synchronized (this)
                {
                    exchange=_exchange;
                }

                if (exchange == null)
                {
                    long filled = _parser.fill();
                    if (filled < 0)
                        close();
                    else
                    {
                        // Hopefully just space?
                        _parser.skipCRLF();
                        if (_parser.isMoreInBuffer())
                        {
                            LOG.warn("Unexpected data received but no request sent");
                            close();
                        }
                    }
                    return this;
                }

                try
                {
                    if (exchange.getStatus() == HttpExchange.STATUS_WAITING_FOR_COMMIT)
                    {
                        progress=true;
                        commitRequest();
                    }

                    _endp.flush();

                    if (_generator.isComplete())
                    {
                        if (!_requestComplete)
                        {
                            _requestComplete = true;
                            exchange.getEventListener().onRequestComplete();
                        }
                    }
                    else
                    {
                        long flushed = _generator.flushBuffer();
                        progress|=(flushed>0);
                            
                        if (_generator.isComplete())
                        {
                            InputStream in = exchange.getRequestContentSource();
                            if (in != null)
                            {
                                if (_requestContentChunk == null || _requestContentChunk.length() == 0)
                                {
                                    _requestContentChunk = _exchange.getRequestContentChunk();

                                    if (_requestContentChunk != null)
                                        _generator.addContent(_requestContentChunk,false);
                                    else
                                        _generator.complete();

                                    flushed = _generator.flushBuffer();
                                    progress|=(flushed>0);
                                }
                            }
                            else
                                _generator.complete();
                        }
                        else
                            _generator.complete();
                        
                    }

                    if (_generator.isComplete() && !_requestComplete)
                    {
                        _requestComplete = true;
                        _exchange.getEventListener().onRequestComplete();
                    }

                    // If we are not ended then parse available
                    if (!_parser.isComplete() && (_generator.isComplete() || _generator.isCommitted() && !_endp.isBlocking()))
                    {
                        if (_parser.parseAvailable())
                            progress=true;

                        if (_parser.isIdle() && (_endp.isInputShutdown() || !_endp.isOpen()))
                            throw new EOFException();
                    }
                    
                }
                catch (Throwable e)
                {
                    LOG.debug("Failure on " + _exchange, e);

                    if (e instanceof ThreadDeath)
                        throw (ThreadDeath)e;

                    failed = true;

                    synchronized (this)
                    {
                        if (_exchange != null)
                        {
                            // Cancelling the exchange causes an exception as we close the connection,
                            // but we don't report it as it is normal cancelling operation
                            if (_exchange.getStatus() != HttpExchange.STATUS_CANCELLING &&
                                    _exchange.getStatus() != HttpExchange.STATUS_CANCELLED)
                            {
                                _exchange.setStatus(HttpExchange.STATUS_EXCEPTED);
                                _exchange.getEventListener().onException(e);
                            }
                        }
                        else
                        {
                            if (e instanceof IOException)
                                throw (IOException)e;

                            if (e instanceof Error)
                                throw (Error)e;

                            if (e instanceof RuntimeException)
                                throw (RuntimeException)e;

                            throw new RuntimeException(e);
                        }
                    }
                }
                finally
                {
                    boolean complete = false;
                    boolean close = failed; // always close the connection on error
                    if (!failed)
                    {
                        // are we complete?
                        if (_generator.isComplete())
                        {
                            if (!_requestComplete)
                            {
                                _requestComplete = true;
                                _exchange.getEventListener().onRequestComplete();
                            }

                            // we need to return the HttpConnection to a state that
                            // it can be reused or closed out
                            if (_parser.isComplete())
                            {
                                _exchange.cancelTimeout(_destination.getHttpClient());
                                complete = true;
                            }
                        }

                        // if the endpoint is closed, but the parser incomplete
                        if (!_endp.isOpen() && !(_parser.isComplete()||_parser.isIdle()))
                        {
                            // we wont be called again so let the parser see the close
                            complete=true;
                            _parser.parseAvailable();
                            // TODO should not need this
                            if (!(_parser.isComplete()||_parser.isIdle()))
                            {
                                LOG.warn("Incomplete {} {}",_parser,_endp);
                                if (_exchange!=null && !_exchange.isDone())
                                {
                                    _exchange.setStatus(HttpExchange.STATUS_EXCEPTED);
                                    _exchange.getEventListener().onException(new EOFException("Incomplete"));
                                }
                            }
                        }
                    }

                    if (_endp.isInputShutdown() && !_parser.isComplete() && !_parser.isIdle())
                    {
                        if (_exchange!=null && !_exchange.isDone())
                        {
                            _exchange.setStatus(HttpExchange.STATUS_EXCEPTED);
                            _exchange.getEventListener().onException(new EOFException("Incomplete"));
                        }
                        _endp.close();
                    }

                    if (complete || failed)
                    {
                        synchronized (this)
                        {
                            if (!close)
                                close = shouldClose();

                            reset(true);

                            progress=true;
                            if (_exchange != null)
                            {
                                exchange=_exchange;
                                _exchange = null;

                                // Reset the maxIdleTime because it may have been changed
                                if (!close)
                                    _endp.setMaxIdleTime((int)_destination.getHttpClient().getIdleTimeout());

                                if (_status==HttpStatus.SWITCHING_PROTOCOLS_101)
                                {
                                    Connection switched=exchange.onSwitchProtocol(_endp);
                                    if (switched!=null)
                                    {
                                        // switched protocol!
                                        exchange = _pipeline;
                                        _pipeline = null;
                                        if (exchange!=null)
                                            _destination.send(exchange);

                                        return switched;
                                    }
                                }

                                if (_pipeline == null)
                                {
                                    if (!isReserved())
                                        _destination.returnConnection(this, close);
                                }
                                else
                                {
                                    if (close)
                                    {
                                        if (!isReserved())
                                            _destination.returnConnection(this,close);

                                        exchange = _pipeline;
                                        _pipeline = null;
                                        _destination.send(exchange);
                                    }
                                    else
                                    {
                                        exchange = _pipeline;
                                        _pipeline = null;
                                        send(exchange);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        finally
        {
            _parser.returnBuffers();

            // Do we have more stuff to write?
            if (!_generator.isComplete() && _generator.getBytesBuffered()>0 && _endp.isOpen() && _endp instanceof AsyncEndPoint)
            {
                // Assume we are write blocked!
                ((AsyncEndPoint)_endp).scheduleWrite();
            }
        }

        return this;
    }
    
    public void onInputShutdown() throws IOException
    {
        // TODO
    }
}

Back to the top