|
27 | 27 | import io.netty.handler.codec.http.websocketx.WebSocketFrame; |
28 | 28 | import io.netty.handler.timeout.IdleStateEvent; |
29 | 29 | import io.netty.util.AsciiString; |
| 30 | +import io.netty.util.ReferenceCountUtil; |
| 31 | +import io.netty.util.ReferenceCounted; |
30 | 32 | import org.slf4j.Logger; |
31 | 33 |
|
32 | 34 | import java.nio.charset.StandardCharsets; |
@@ -72,51 +74,59 @@ public NettyHandler(ScheduledExecutorService scheduler, Router router, long maxR |
72 | 74 |
|
73 | 75 | @Override |
74 | 76 | public void channelRead(ChannelHandlerContext ctx, Object msg) { |
75 | | - if (msg instanceof HttpRequest) { |
76 | | - HttpRequest req = (HttpRequest) msg; |
77 | | - context = new NettyContext(ctx, req, router, pathOnly(req.uri()), bufferSize); |
| 77 | + try { |
| 78 | + if (msg instanceof HttpRequest) { |
| 79 | + HttpRequest req = (HttpRequest) msg; |
| 80 | + context = new NettyContext(ctx, req, router, pathOnly(req.uri()), bufferSize); |
78 | 81 |
|
79 | | - if (defaultHeaders) { |
80 | | - context.setHeaders.set(HttpHeaderNames.DATE, date(scheduler)); |
81 | | - context.setHeaders.set(HttpHeaderNames.SERVER, server); |
82 | | - } |
83 | | - context.setHeaders.set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN); |
| 82 | + if (defaultHeaders) { |
| 83 | + context.setHeaders.set(HttpHeaderNames.DATE, date(scheduler)); |
| 84 | + context.setHeaders.set(HttpHeaderNames.SERVER, server); |
| 85 | + } |
| 86 | + context.setHeaders.set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN); |
84 | 87 |
|
85 | | - result = router.match(context); |
| 88 | + result = router.match(context); |
86 | 89 |
|
87 | | - contentLength = contentLength(req); |
88 | | - if (contentLength > 0 || HttpUtil.isTransferEncodingChunked(req)) { |
89 | | - decoder = newDecoder(req, factory); |
90 | | - } else { |
91 | | - result.execute(context); |
92 | | - result = null; |
93 | | - } |
94 | | - } else if (decoder != null && msg instanceof HttpContent) { |
95 | | - HttpContent chunk = (HttpContent) msg; |
96 | | - chunkSize += chunk.content().readableBytes(); |
97 | | - if (chunkSize > maxRequestSize) { |
98 | | - resetDecoderState(true); |
99 | | - chunk.release(); |
100 | | - context.sendError(new StatusCodeException(StatusCode.REQUEST_ENTITY_TOO_LARGE)); |
101 | | - return; |
102 | | - } |
103 | | - |
104 | | - offer(chunk); |
| 90 | + contentLength = contentLength(req); |
| 91 | + if (contentLength > 0 || HttpUtil.isTransferEncodingChunked(req)) { |
| 92 | + decoder = newDecoder(req, factory); |
| 93 | + } else { |
| 94 | + result.execute(context); |
| 95 | + result = null; |
| 96 | + } |
| 97 | + } else if (decoder != null && msg instanceof HttpContent) { |
| 98 | + HttpContent chunk = (HttpContent) msg; |
| 99 | + chunkSize += chunk.content().readableBytes(); |
| 100 | + if (chunkSize > maxRequestSize) { |
| 101 | + resetDecoderState(true); |
| 102 | + context.sendError(new StatusCodeException(StatusCode.REQUEST_ENTITY_TOO_LARGE)); |
| 103 | + return; |
| 104 | + } |
105 | 105 |
|
106 | | - if (contentLength == chunkSize && !(chunk instanceof LastHttpContent)) { |
107 | | - chunk = LastHttpContent.EMPTY_LAST_CONTENT; |
108 | 106 | offer(chunk); |
109 | | - } |
110 | 107 |
|
111 | | - if (chunk instanceof LastHttpContent) { |
112 | | - context.decoder = decoder; |
113 | | - resetDecoderState(false); |
114 | | - result.execute(context); |
115 | | - result = null; |
| 108 | + if (contentLength == chunkSize && !(chunk instanceof LastHttpContent)) { |
| 109 | + chunk = LastHttpContent.EMPTY_LAST_CONTENT; |
| 110 | + offer(chunk); |
| 111 | + } |
| 112 | + |
| 113 | + if (chunk instanceof LastHttpContent) { |
| 114 | + context.decoder = decoder; |
| 115 | + resetDecoderState(false); |
| 116 | + result.execute(context); |
| 117 | + result = null; |
| 118 | + } |
| 119 | + } else if (msg instanceof WebSocketFrame) { |
| 120 | + if (context.webSocket != null) { |
| 121 | + context.webSocket.handleFrame((WebSocketFrame) msg); |
| 122 | + } |
116 | 123 | } |
117 | | - } else if (msg instanceof WebSocketFrame) { |
118 | | - if (context.webSocket != null) { |
119 | | - context.webSocket.handleFrame((WebSocketFrame) msg); |
| 124 | + } finally { |
| 125 | + if (msg instanceof ReferenceCounted) { |
| 126 | + ReferenceCounted ref = (ReferenceCounted) msg; |
| 127 | + if (ref.refCnt() > 0) { |
| 128 | + ref.release(); |
| 129 | + } |
120 | 130 | } |
121 | 131 | } |
122 | 132 | } |
|
0 commit comments