/
HttpChannelState.java
1415 lines (1253 loc) · 40.8 KB
/
HttpChannelState.java
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.server;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.AsyncListener;
import javax.servlet.ServletContext;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandler.Context;
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.Scheduler;
import static javax.servlet.RequestDispatcher.ERROR_EXCEPTION;
import static javax.servlet.RequestDispatcher.ERROR_EXCEPTION_TYPE;
import static javax.servlet.RequestDispatcher.ERROR_MESSAGE;
import static javax.servlet.RequestDispatcher.ERROR_REQUEST_URI;
import static javax.servlet.RequestDispatcher.ERROR_SERVLET_NAME;
import static javax.servlet.RequestDispatcher.ERROR_STATUS_CODE;
/**
* Implementation of AsyncContext interface that holds the state of request-response cycle.
*/
public class HttpChannelState
{
private static final Logger LOG = Log.getLogger(HttpChannelState.class);
private static final long DEFAULT_TIMEOUT = Long.getLong("org.eclipse.jetty.server.HttpChannelState.DEFAULT_TIMEOUT", 30000L);
/*
* The state of the HttpChannel,used to control the overall lifecycle.
* <pre>
* IDLE <-----> HANDLING ----> WAITING
* | ^ /
* | \ /
* v \ v
* UPGRADED WOKEN
* </pre>
*/
public enum State
{
IDLE, // Idle request
HANDLING, // Request dispatched to filter/servlet or Async IO callback
WAITING, // Suspended and waiting
WOKEN, // Dispatch to handle from ASYNC_WAIT
UPGRADED // Request upgraded the connection
}
/*
* The state of the request processing lifecycle.
* <pre>
* BLOCKING <----> COMPLETING ---> COMPLETED
* ^ | ^ ^
* / | \ |
* | | DISPATCH |
* | | ^ ^ |
* | v / | |
* | ASYNC -------> COMPLETE
* | | | ^
* | v | |
* | EXPIRE | |
* \ | / |
* \ v / |
* EXPIRING ----------+
* </pre>
*/
private enum RequestState
{
BLOCKING, // Blocking request dispatched
ASYNC, // AsyncContext.startAsync() has been called
DISPATCH, // AsyncContext.dispatch() has been called
EXPIRE, // AsyncContext timeout has happened
EXPIRING, // AsyncListeners are being called
COMPLETE, // AsyncContext.complete() has been called
COMPLETING, // Request is being closed (maybe asynchronously)
COMPLETED // Response is completed
}
/*
* The input readiness state, which works together with {@link HttpInput.State}
*/
private enum InputState
{
IDLE, // No isReady; No data
REGISTER, // isReady()==false handling; No data
REGISTERED, // isReady()==false !handling; No data
POSSIBLE, // isReady()==false async read callback called (http/1 only)
PRODUCING, // isReady()==false READ_PRODUCE action is being handled (http/1 only)
READY // isReady() was false, onContentAdded has been called
}
/*
* The output committed state, which works together with {@link HttpOutput.State}
*/
private enum OutputState
{
OPEN,
COMMITTED,
COMPLETED,
ABORTED,
}
/**
* The actions to take as the channel moves from state to state.
*/
public enum Action
{
DISPATCH, // handle a normal request dispatch
ASYNC_DISPATCH, // handle an async request dispatch
SEND_ERROR, // Generate an error page or error dispatch
ASYNC_ERROR, // handle an async error
ASYNC_TIMEOUT, // call asyncContext onTimeout
WRITE_CALLBACK, // handle an IO write callback
READ_REGISTER, // Register for fill interest
READ_PRODUCE, // Check is a read is possible by parsing/filling
READ_CALLBACK, // handle an IO read callback
COMPLETE, // Complete the response by closing output
TERMINATED, // No further actions
WAIT, // Wait for further events
}
private final HttpChannel _channel;
private List<AsyncListener> _asyncListeners;
private State _state = State.IDLE;
private RequestState _requestState = RequestState.BLOCKING;
private OutputState _outputState = OutputState.OPEN;
private InputState _inputState = InputState.IDLE;
private boolean _initial = true;
private boolean _sendError;
private boolean _asyncWritePossible;
private long _timeoutMs = DEFAULT_TIMEOUT;
private AsyncContextEvent _event;
protected HttpChannelState(HttpChannel channel)
{
_channel = channel;
}
public State getState()
{
synchronized (this)
{
return _state;
}
}
public void addListener(AsyncListener listener)
{
synchronized (this)
{
if (_asyncListeners == null)
_asyncListeners = new ArrayList<>();
_asyncListeners.add(listener);
}
}
public boolean hasListener(AsyncListener listener)
{
synchronized (this)
{
if (_asyncListeners == null)
return false;
for (AsyncListener l : _asyncListeners)
{
if (l == listener)
return true;
if (l instanceof AsyncContextState.WrappedAsyncListener && ((AsyncContextState.WrappedAsyncListener)l).getListener() == listener)
return true;
}
return false;
}
}
public boolean isSendError()
{
synchronized (this)
{
return _sendError;
}
}
public void setTimeout(long ms)
{
synchronized (this)
{
_timeoutMs = ms;
}
}
public long getTimeout()
{
synchronized (this)
{
return _timeoutMs;
}
}
public AsyncContextEvent getAsyncContextEvent()
{
synchronized (this)
{
return _event;
}
}
@Override
public String toString()
{
synchronized (this)
{
return toStringLocked();
}
}
private String toStringLocked()
{
return String.format("%s@%x{%s}",
getClass().getSimpleName(),
hashCode(),
getStatusStringLocked());
}
private String getStatusStringLocked()
{
return String.format("s=%s rs=%s os=%s is=%s awp=%b se=%b i=%b al=%d",
_state,
_requestState,
_outputState,
_inputState,
_asyncWritePossible,
_sendError,
_initial,
_asyncListeners == null ? 0 : _asyncListeners.size());
}
public String getStatusString()
{
synchronized (this)
{
return getStatusStringLocked();
}
}
public boolean commitResponse()
{
synchronized (this)
{
switch (_outputState)
{
case OPEN:
_outputState = OutputState.COMMITTED;
return true;
default:
return false;
}
}
}
public boolean partialResponse()
{
synchronized (this)
{
switch (_outputState)
{
case COMMITTED:
_outputState = OutputState.OPEN;
return true;
default:
return false;
}
}
}
public boolean completeResponse()
{
synchronized (this)
{
switch (_outputState)
{
case OPEN:
case COMMITTED:
_outputState = OutputState.COMPLETED;
return true;
default:
return false;
}
}
}
public boolean isResponseCommitted()
{
synchronized (this)
{
switch (_outputState)
{
case OPEN:
return false;
default:
return true;
}
}
}
public boolean isResponseCompleted()
{
synchronized (this)
{
return _outputState == OutputState.COMPLETED;
}
}
public boolean abortResponse()
{
synchronized (this)
{
switch (_outputState)
{
case ABORTED:
return false;
case OPEN:
_channel.getResponse().setStatus(500);
_outputState = OutputState.ABORTED;
return true;
default:
_outputState = OutputState.ABORTED;
return true;
}
}
}
/**
* @return Next handling of the request should proceed
*/
public Action handling()
{
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("handling {}", toStringLocked());
switch (_state)
{
case IDLE:
if (_requestState != RequestState.BLOCKING)
throw new IllegalStateException(getStatusStringLocked());
_initial = true;
_state = State.HANDLING;
return Action.DISPATCH;
case WOKEN:
if (_event != null && _event.getThrowable() != null && !_sendError)
{
_state = State.HANDLING;
return Action.ASYNC_ERROR;
}
Action action = nextAction(true);
if (LOG.isDebugEnabled())
LOG.debug("nextAction(true) {} {}", action, toStringLocked());
return action;
default:
throw new IllegalStateException(getStatusStringLocked());
}
}
}
/**
* Signal that the HttpConnection has finished handling the request.
* For blocking connectors, this call may block if the request has
* been suspended (startAsync called).
*
* @return next actions
* be handled again (eg because of a resume that happened before unhandle was called)
*/
protected Action unhandle()
{
boolean readInterested = false;
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("unhandle {}", toStringLocked());
if (_state != State.HANDLING)
throw new IllegalStateException(this.getStatusStringLocked());
_initial = false;
Action action = nextAction(false);
if (LOG.isDebugEnabled())
LOG.debug("nextAction(false) {} {}", action, toStringLocked());
return action;
}
}
private Action nextAction(boolean handling)
{
// Assume we can keep going, but exceptions are below
_state = State.HANDLING;
if (_sendError)
{
switch (_requestState)
{
case BLOCKING:
case ASYNC:
case COMPLETE:
case DISPATCH:
case COMPLETING:
_requestState = RequestState.BLOCKING;
_sendError = false;
return Action.SEND_ERROR;
default:
break;
}
}
switch (_requestState)
{
case BLOCKING:
if (handling)
throw new IllegalStateException(getStatusStringLocked());
_requestState = RequestState.COMPLETING;
return Action.COMPLETE;
case ASYNC:
switch (_inputState)
{
case POSSIBLE:
_inputState = InputState.PRODUCING;
return Action.READ_PRODUCE;
case READY:
_inputState = InputState.IDLE;
return Action.READ_CALLBACK;
case REGISTER:
case PRODUCING:
_inputState = InputState.REGISTERED;
return Action.READ_REGISTER;
case IDLE:
case REGISTERED:
break;
default:
throw new IllegalStateException(getStatusStringLocked());
}
if (_asyncWritePossible)
{
_asyncWritePossible = false;
return Action.WRITE_CALLBACK;
}
Scheduler scheduler = _channel.getScheduler();
if (scheduler != null && _timeoutMs > 0 && !_event.hasTimeoutTask())
_event.setTimeoutTask(scheduler.schedule(_event, _timeoutMs, TimeUnit.MILLISECONDS));
_state = State.WAITING;
return Action.WAIT;
case DISPATCH:
_requestState = RequestState.BLOCKING;
return Action.ASYNC_DISPATCH;
case EXPIRE:
_requestState = RequestState.EXPIRING;
return Action.ASYNC_TIMEOUT;
case EXPIRING:
if (handling)
throw new IllegalStateException(getStatusStringLocked());
sendError(HttpStatus.INTERNAL_SERVER_ERROR_500, "AsyncContext timeout");
// handle sendError immediately
_requestState = RequestState.BLOCKING;
_sendError = false;
return Action.SEND_ERROR;
case COMPLETE:
_requestState = RequestState.COMPLETING;
return Action.COMPLETE;
case COMPLETING:
_state = State.WAITING;
return Action.WAIT;
case COMPLETED:
_state = State.IDLE;
return Action.TERMINATED;
default:
throw new IllegalStateException(getStatusStringLocked());
}
}
public void startAsync(AsyncContextEvent event)
{
final List<AsyncListener> lastAsyncListeners;
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("startAsync {}", toStringLocked());
if (_state != State.HANDLING || _requestState != RequestState.BLOCKING)
throw new IllegalStateException(this.getStatusStringLocked());
_requestState = RequestState.ASYNC;
_event = event;
lastAsyncListeners = _asyncListeners;
_asyncListeners = null;
}
if (lastAsyncListeners != null)
{
Runnable callback = new Runnable()
{
@Override
public void run()
{
for (AsyncListener listener : lastAsyncListeners)
{
try
{
listener.onStartAsync(event);
}
catch (Throwable e)
{
// TODO Async Dispatch Error
LOG.warn(e);
}
}
}
@Override
public String toString()
{
return "startAsync";
}
};
runInContext(event, callback);
}
}
public void dispatch(ServletContext context, String path)
{
boolean dispatch = false;
AsyncContextEvent event;
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("dispatch {} -> {}", toStringLocked(), path);
switch (_requestState)
{
case ASYNC:
case EXPIRING:
break;
default:
throw new IllegalStateException(this.getStatusStringLocked());
}
if (context != null)
_event.setDispatchContext(context);
if (path != null)
_event.setDispatchPath(path);
if (_requestState == RequestState.ASYNC && _state == State.WAITING)
{
_state = State.WOKEN;
dispatch = true;
}
_requestState = RequestState.DISPATCH;
event = _event;
}
cancelTimeout(event);
if (dispatch)
scheduleDispatch();
}
protected void timeout()
{
boolean dispatch = false;
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("Timeout {}", toStringLocked());
if (_requestState != RequestState.ASYNC)
return;
_requestState = RequestState.EXPIRE;
if (_state == State.WAITING)
{
_state = State.WOKEN;
dispatch = true;
}
}
if (dispatch)
{
if (LOG.isDebugEnabled())
LOG.debug("Dispatch after async timeout {}", this);
scheduleDispatch();
}
}
protected void onTimeout()
{
final List<AsyncListener> listeners;
AsyncContextEvent event;
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("onTimeout {}", toStringLocked());
if (_requestState != RequestState.EXPIRING || _state != State.HANDLING)
throw new IllegalStateException(toStringLocked());
event = _event;
listeners = _asyncListeners;
}
if (listeners != null)
{
Runnable task = new Runnable()
{
@Override
public void run()
{
for (AsyncListener listener : listeners)
{
try
{
listener.onTimeout(event);
}
catch (Throwable x)
{
LOG.warn(x + " while invoking onTimeout listener " + listener);
LOG.debug(x);
}
}
}
@Override
public String toString()
{
return "onTimeout";
}
};
runInContext(event, task);
}
}
public void complete()
{
boolean handle = false;
AsyncContextEvent event;
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("complete {}", toStringLocked());
event = _event;
switch (_requestState)
{
case EXPIRING:
case ASYNC:
_requestState = _sendError ? RequestState.BLOCKING : RequestState.COMPLETE;
break;
case COMPLETE:
return;
default:
throw new IllegalStateException(this.getStatusStringLocked());
}
if (_state == State.WAITING)
{
handle = true;
_state = State.WOKEN;
}
}
cancelTimeout(event);
if (handle)
runInContext(event, _channel);
}
public void asyncError(Throwable failure)
{
// This method is called when an failure occurs asynchronously to
// normal handling. If the request is async, we arrange for the
// exception to be thrown from the normal handling loop and then
// actually handled by #thrownException
AsyncContextEvent event = null;
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("asyncError " + toStringLocked(), failure);
if (_state == State.WAITING && _requestState == RequestState.ASYNC)
{
_state = State.WOKEN;
_event.addThrowable(failure);
event = _event;
}
else
{
LOG.warn(failure.toString());
LOG.debug(failure);
}
}
if (event != null)
{
cancelTimeout(event);
runInContext(event, _channel);
}
}
protected void onError(Throwable th)
{
final AsyncContextEvent asyncEvent;
final List<AsyncListener> asyncListeners;
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("thrownException " + getStatusStringLocked(), th);
// This can only be called from within the handle loop
if (_state != State.HANDLING)
throw new IllegalStateException(getStatusStringLocked());
// If sendError has already been called, we can only handle one failure at a time!
if (_sendError)
{
LOG.warn("unhandled due to prior sendError", th);
return;
}
// Check async state to determine type of handling
switch (_requestState)
{
case BLOCKING:
// handle the exception with a sendError
sendError(th);
return;
case DISPATCH: // Dispatch has already been called but we ignore and handle exception below
case COMPLETE: // Complete has already been called but we ignore and handle exception below
case ASYNC:
if (_asyncListeners == null || _asyncListeners.isEmpty())
{
sendError(th);
return;
}
asyncEvent = _event;
asyncEvent.addThrowable(th);
asyncListeners = _asyncListeners;
break;
default:
LOG.warn("unhandled in state " + _requestState, new IllegalStateException(th));
return;
}
}
// If we are async and have async listeners
// call onError
runInContext(asyncEvent, () ->
{
for (AsyncListener listener : asyncListeners)
{
try
{
listener.onError(asyncEvent);
}
catch (Throwable x)
{
LOG.warn(x + " while invoking onError listener " + listener);
LOG.debug(x);
}
}
});
// check the actions of the listeners
synchronized (this)
{
// If we are still async and nobody has called sendError
if (_requestState == RequestState.ASYNC && !_sendError)
// Then the listeners did not invoke API methods
// and the container must provide a default error dispatch.
sendError(th);
else
LOG.warn("unhandled in state " + _requestState, new IllegalStateException(th));
}
}
private void sendError(Throwable th)
{
// No sync as this is always called with lock held
// Determine the actual details of the exception
final Request request = _channel.getRequest();
final int code;
final String message;
Throwable cause = _channel.unwrap(th, BadMessageException.class, UnavailableException.class);
if (cause == null)
{
code = HttpStatus.INTERNAL_SERVER_ERROR_500;
message = th.toString();
}
else if (cause instanceof BadMessageException)
{
BadMessageException bme = (BadMessageException)cause;
code = bme.getCode();
message = bme.getReason();
}
else if (cause instanceof UnavailableException)
{
message = cause.toString();
if (((UnavailableException)cause).isPermanent())
code = HttpStatus.NOT_FOUND_404;
else
code = HttpStatus.SERVICE_UNAVAILABLE_503;
}
else
{
code = HttpStatus.INTERNAL_SERVER_ERROR_500;
message = null;
}
sendError(code, message);
// No ISE, so good to modify request/state
request.setAttribute(ERROR_EXCEPTION, th);
request.setAttribute(ERROR_EXCEPTION_TYPE, th.getClass());
// Ensure any async lifecycle is ended!
_requestState = RequestState.BLOCKING;
}
public void sendError(int code, String message)
{
// This method is called by Response.sendError to organise for an error page to be generated when it is possible:
// + The response is reset and temporarily closed.
// + The details of the error are saved as request attributes
// + The _sendError boolean is set to true so that an ERROR_DISPATCH action will be generated:
// - after unhandle for sync
// - after both unhandle and complete for async
final Request request = _channel.getRequest();
final Response response = _channel.getResponse();
if (message == null)
message = HttpStatus.getMessage(code);
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("sendError {}", toStringLocked());
switch (_state)
{
case HANDLING:
case WOKEN:
case WAITING:
break;
default:
throw new IllegalStateException(getStatusStringLocked());
}
if (_outputState != OutputState.OPEN)
throw new IllegalStateException("Response is " + _outputState);
response.setStatus(code);
response.softClose();
request.setAttribute(ErrorHandler.ERROR_CONTEXT, request.getErrorContext());
request.setAttribute(ERROR_REQUEST_URI, request.getRequestURI());
request.setAttribute(ERROR_SERVLET_NAME, request.getServletName());
request.setAttribute(ERROR_STATUS_CODE, code);
request.setAttribute(ERROR_MESSAGE, message);
_sendError = true;
if (_event != null)
{
Throwable cause = (Throwable)request.getAttribute(ERROR_EXCEPTION);
if (cause != null)
_event.addThrowable(cause);
}
}
}
protected void completing()
{
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("completing {}", toStringLocked());
switch (_requestState)
{
case COMPLETED:
throw new IllegalStateException(getStatusStringLocked());
default:
_requestState = RequestState.COMPLETING;
}
}
}
protected void completed()
{
final List<AsyncListener> aListeners;
final AsyncContextEvent event;
boolean handle = false;
synchronized (this)
{
if (LOG.isDebugEnabled())
LOG.debug("completed {}", toStringLocked());
if (_requestState != RequestState.COMPLETING)
throw new IllegalStateException(this.getStatusStringLocked());
if (_event == null)
{
_requestState = RequestState.COMPLETED;
aListeners = null;
event = null;
if (_state == State.WAITING)
{
_state = State.WOKEN;
handle = true;
}
}
else
{
aListeners = _asyncListeners;
event = _event;
}
}
// release any aggregate buffer from a closing flush
_channel.getResponse().getHttpOutput().closed();
if (event != null)
{
cancelTimeout(event);
if (aListeners != null)
{
runInContext(event, () ->
{
for (AsyncListener listener : aListeners)
{
try
{
listener.onComplete(event);
}
catch (Throwable e)
{
LOG.warn(e + " while invoking onComplete listener " + listener);
LOG.debug(e);
}
}
});
}
event.completed();
synchronized (this)
{
_requestState = RequestState.COMPLETED;
if (_state == State.WAITING)