3333import io .netty .channel .ChannelFutureListener ;
3434import io .netty .channel .ChannelHandlerContext ;
3535import io .netty .channel .ChannelPipeline ;
36+ import io .netty .channel .ChannelPromise ;
3637import io .netty .channel .DefaultFileRegion ;
3738import io .netty .handler .codec .http .DefaultFullHttpRequest ;
3839import io .netty .handler .codec .http .DefaultFullHttpResponse ;
5859import io .netty .handler .stream .ChunkedNioStream ;
5960import io .netty .handler .stream .ChunkedStream ;
6061import io .netty .handler .stream .ChunkedWriteHandler ;
61- import io .netty .handler .timeout .IdleState ;
6262import io .netty .handler .timeout .IdleStateHandler ;
6363import io .netty .util .ReferenceCounted ;
6464
7373import java .nio .channels .FileChannel ;
7474import java .nio .channels .ReadableByteChannel ;
7575import java .nio .charset .Charset ;
76- import java .time .Duration ;
7776import java .util .ArrayList ;
7877import java .util .Collection ;
7978import java .util .Collections ;
101100public class NettyContext implements DefaultContext , ChannelFutureListener {
102101
103102 private static final HttpHeaders NO_TRAILING = EmptyHttpHeaders .INSTANCE ;
104- final DefaultHttpHeaders setHeaders = new DefaultHttpHeaders (true );
103+ DefaultHttpHeaders setHeaders = new DefaultHttpHeaders (true );
105104 private final int bufferSize ;
106105 InterfaceHttpPostRequestDecoder decoder ;
107106 private Router router ;
@@ -436,9 +435,9 @@ private Context send(@Nonnull ByteBuf data) {
436435 data , setHeaders , NO_TRAILING );
437436 if (ctx .channel ().eventLoop ().inEventLoop ()) {
438437 needsFlush = true ;
439- ctx .write (response ). addListener (this );
438+ ctx .write (response , promise (this ) );
440439 } else {
441- ctx .writeAndFlush (response ). addListener (this );
440+ ctx .writeAndFlush (response , promise (this ) );
442441 }
443442 return this ;
444443 }
@@ -447,6 +446,11 @@ public void flush() {
447446 if (needsFlush ) {
448447 needsFlush = false ;
449448 ctx .flush ();
449+ this .ctx = null ;
450+ this .req = null ;
451+ this .route = null ;
452+ this .router = null ;
453+ this .headers = null ;
450454 }
451455 }
452456
@@ -461,7 +465,7 @@ public void flush() {
461465 // Body
462466 ctx .write (new ChunkedNioStream (channel , bufferSize ), ctx .voidPromise ());
463467 // Finish
464- ctx .writeAndFlush (EMPTY_LAST_CONTENT ). addListener (this );
468+ ctx .writeAndFlush (EMPTY_LAST_CONTENT , promise (this ) );
465469 });
466470 return this ;
467471 }
@@ -486,7 +490,7 @@ public void flush() {
486490 // Body
487491 ctx .write (chunkedStream , ctx .voidPromise ());
488492 // Finish
489- ctx .writeAndFlush (EMPTY_LAST_CONTENT ). addListener (this );
493+ ctx .writeAndFlush (EMPTY_LAST_CONTENT , promise (this ) );
490494 });
491495 return this ;
492496 } catch (Exception x ) {
@@ -515,7 +519,7 @@ public void flush() {
515519 // Headers
516520 ctx .write (rsp , ctx .voidPromise ());
517521 // Body
518- ctx .writeAndFlush (chunkedInput ). addListener (this );
522+ ctx .writeAndFlush (chunkedInput , promise (this ) );
519523 });
520524 } else {
521525 ctx .channel ().eventLoop ().execute (() -> {
@@ -525,7 +529,7 @@ public void flush() {
525529 ctx .write (new DefaultFileRegion (file , range .getStart (), range .getEnd ()),
526530 ctx .voidPromise ());
527531 // Finish
528- ctx .writeAndFlush (EMPTY_LAST_CONTENT ). addListener (this );
532+ ctx .writeAndFlush (EMPTY_LAST_CONTENT , promise (this ) );
529533 });
530534 }
531535 } catch (IOException x ) {
@@ -557,7 +561,7 @@ public void flush() {
557561 DefaultFullHttpResponse rsp = new DefaultFullHttpResponse (HTTP_1_1 ,
558562 HttpResponseStatus .valueOf (statusCode .value ()), Unpooled .EMPTY_BUFFER , setHeaders ,
559563 NO_TRAILING );
560- ctx .writeAndFlush (rsp ). addListener (this );
564+ ctx .writeAndFlush (rsp , promise (this ) );
561565 return this ;
562566 }
563567
@@ -573,17 +577,33 @@ public void flush() {
573577 }
574578
575579 private void ifSaveSession () {
576- Session session = ( Session ) getAttributes (). get ( Session . NAME );
577- if (session != null && ( session . isNew () || session . isModify ()) ) {
580+ Session session = saveSession ( );
581+ if (session != null ) {
578582 SessionStore store = router .getSessionStore ();
579583 store .saveSession (this , session );
580584 }
581585 }
582586
583- private NettyOutputStream newOutputStream () {
584- prepareChunked ();
585- return new NettyOutputStream (ctx , bufferSize ,
586- new DefaultHttpResponse (req .protocolVersion (), status , setHeaders ), this );
587+ private Session saveSession () {
588+ Session session = (Session ) getAttributes ().get (Session .NAME );
589+ if (session != null && (session .isNew () || session .isModify ())) {
590+ return session ;
591+ }
592+ return session ;
593+ }
594+
595+ private ChannelPromise promise (ChannelFutureListener listener ) {
596+ if (pendingTasks ()) {
597+ return ctx .newPromise ().addListener (listener );
598+ }
599+ return ctx .voidPromise ();
600+ }
601+
602+ private boolean pendingTasks () {
603+ return (saveSession () != null ) ||
604+ (files != null && files .size () > 0 ) ||
605+ (decoder != null ) ||
606+ shouldRelease (req );
587607 }
588608
589609 void destroy (Throwable cause ) {
@@ -619,6 +639,12 @@ void destroy(Throwable cause) {
619639 release (req );
620640 }
621641
642+ private NettyOutputStream newOutputStream () {
643+ prepareChunked ();
644+ return new NettyOutputStream (ctx , bufferSize ,
645+ new DefaultHttpResponse (req .protocolVersion (), status , setHeaders ), this );
646+ }
647+
622648 private FileUpload register (FileUpload upload ) {
623649 if (this .files == null ) {
624650 this .files = new ArrayList <>();
@@ -653,12 +679,18 @@ private void decodeForm(HttpRequest req, Formdata form) {
653679 }
654680
655681 private static void release (HttpRequest req ) {
682+ if (shouldRelease (req )) {
683+ ReferenceCounted ref = (ReferenceCounted ) req ;
684+ ref .release ();
685+ }
686+ }
687+
688+ private static boolean shouldRelease (HttpRequest req ) {
656689 if (req instanceof ReferenceCounted ) {
657690 ReferenceCounted ref = (ReferenceCounted ) req ;
658- if (ref .refCnt () > 0 ) {
659- ref .release ();
660- }
691+ return ref .refCnt () > 0 ;
661692 }
693+ return false ;
662694 }
663695
664696 private long responseLength () {
0 commit comments