1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.channel.embedded.EmbeddedChannel;
21 import io.netty.handler.codec.CodecException;
22 import io.netty.handler.codec.MessageToMessageDecoder;
23 import io.netty.util.ReferenceCountUtil;
24
25 import java.util.List;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public abstract class HttpContentDecoder extends MessageToMessageDecoder<HttpObject> {
47
48 protected ChannelHandlerContext ctx;
49 private EmbeddedChannel decoder;
50 private boolean continueResponse;
51
52 @Override
53 protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception {
54 if (msg instanceof HttpResponse && ((HttpResponse) msg).getStatus().code() == 100) {
55
56 if (!(msg instanceof LastHttpContent)) {
57 continueResponse = true;
58 }
59
60 out.add(ReferenceCountUtil.retain(msg));
61 return;
62 }
63
64 if (continueResponse) {
65 if (msg instanceof LastHttpContent) {
66 continueResponse = false;
67 }
68
69 out.add(ReferenceCountUtil.retain(msg));
70 return;
71 }
72
73 if (msg instanceof HttpMessage) {
74 cleanup();
75 final HttpMessage message = (HttpMessage) msg;
76 final HttpHeaders headers = message.headers();
77
78
79 String contentEncoding = headers.get(HttpHeaders.Names.CONTENT_ENCODING);
80 if (contentEncoding != null) {
81 contentEncoding = contentEncoding.trim();
82 } else {
83 contentEncoding = HttpHeaders.Values.IDENTITY;
84 }
85 decoder = newContentDecoder(contentEncoding);
86
87 if (decoder == null) {
88 if (message instanceof HttpContent) {
89 ((HttpContent) message).retain();
90 }
91 out.add(message);
92 return;
93 }
94
95
96
97
98
99 if (headers.contains(HttpHeaders.Names.CONTENT_LENGTH)) {
100 headers.remove(HttpHeaders.Names.CONTENT_LENGTH);
101 headers.set(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
102 }
103
104
105
106
107 CharSequence targetContentEncoding = getTargetContentEncoding(contentEncoding);
108 if (HttpHeaders.Values.IDENTITY.equals(targetContentEncoding)) {
109
110
111 headers.remove(HttpHeaders.Names.CONTENT_ENCODING);
112 } else {
113 headers.set(HttpHeaders.Names.CONTENT_ENCODING, targetContentEncoding);
114 }
115
116 if (message instanceof HttpContent) {
117
118
119
120
121 HttpMessage copy;
122 if (message instanceof HttpRequest) {
123 HttpRequest r = (HttpRequest) message;
124 copy = new DefaultHttpRequest(r.getProtocolVersion(), r.getMethod(), r.getUri());
125 } else if (message instanceof HttpResponse) {
126 HttpResponse r = (HttpResponse) message;
127 copy = new DefaultHttpResponse(r.getProtocolVersion(), r.getStatus());
128 } else {
129 throw new CodecException("Object of class " + message.getClass().getName() +
130 " is not a HttpRequest or HttpResponse");
131 }
132 copy.headers().set(message.headers());
133 copy.setDecoderResult(message.getDecoderResult());
134 out.add(copy);
135 } else {
136 out.add(message);
137 }
138 }
139
140 if (msg instanceof HttpContent) {
141 final HttpContent c = (HttpContent) msg;
142 if (decoder == null) {
143 out.add(c.retain());
144 } else {
145 decodeContent(c, out);
146 }
147 }
148 }
149
150 private void decodeContent(HttpContent c, List<Object> out) {
151 ByteBuf content = c.content();
152
153 decode(content, out);
154
155 if (c instanceof LastHttpContent) {
156 finishDecode(out);
157
158 LastHttpContent last = (LastHttpContent) c;
159
160
161 HttpHeaders headers = last.trailingHeaders();
162 if (headers.isEmpty()) {
163 out.add(LastHttpContent.EMPTY_LAST_CONTENT);
164 } else {
165 out.add(new ComposedLastHttpContent(headers));
166 }
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179 protected abstract EmbeddedChannel newContentDecoder(String contentEncoding) throws Exception;
180
181
182
183
184
185
186
187
188
189 protected String getTargetContentEncoding(
190 @SuppressWarnings("UnusedParameters") String contentEncoding) throws Exception {
191 return HttpHeaders.Values.IDENTITY;
192 }
193
194 @Override
195 public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
196 cleanupSafely(ctx);
197 super.handlerRemoved(ctx);
198 }
199
200 @Override
201 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
202 cleanupSafely(ctx);
203 super.channelInactive(ctx);
204 }
205
206 @Override
207 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
208 this.ctx = ctx;
209 super.handlerAdded(ctx);
210 }
211
212 private void cleanup() {
213 if (decoder != null) {
214
215 decoder.finishAndReleaseAll();
216 decoder = null;
217 }
218 }
219
220 private void cleanupSafely(ChannelHandlerContext ctx) {
221 try {
222 cleanup();
223 } catch (Throwable cause) {
224
225
226 ctx.fireExceptionCaught(cause);
227 }
228 }
229
230 private void decode(ByteBuf in, List<Object> out) {
231
232 decoder.writeInbound(in.retain());
233 fetchDecoderOutput(out);
234 }
235
236 private void finishDecode(List<Object> out) {
237 if (decoder.finish()) {
238 fetchDecoderOutput(out);
239 }
240 decoder = null;
241 }
242
243 private void fetchDecoderOutput(List<Object> out) {
244 for (;;) {
245 ByteBuf buf = (ByteBuf) decoder.readInbound();
246 if (buf == null) {
247 break;
248 }
249 if (!buf.isReadable()) {
250 buf.release();
251 continue;
252 }
253 out.add(new DefaultHttpContent(buf));
254 }
255 }
256 }