1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http2;
17
18 import static org.jboss.netty.channel.Channels.*;
19 import static org.jboss.netty.handler.codec.http2.HttpHeaders.*;
20
21 import java.util.List;
22 import java.util.Map.Entry;
23
24 import org.jboss.netty.buffer.ChannelBuffer;
25 import org.jboss.netty.buffer.ChannelBuffers;
26 import org.jboss.netty.channel.ChannelHandler;
27 import org.jboss.netty.channel.ChannelHandlerContext;
28 import org.jboss.netty.channel.ChannelPipeline;
29 import org.jboss.netty.channel.Channels;
30 import org.jboss.netty.channel.MessageEvent;
31 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
32 import org.jboss.netty.handler.codec.frame.TooLongFrameException;
33 import org.jboss.netty.util.CharsetUtil;
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 public class HttpChunkAggregator extends SimpleChannelUpstreamHandler {
59
60 private static final ChannelBuffer CONTINUE = ChannelBuffers.copiedBuffer(
61 "HTTP/1.1 100 Continue\r\n\r\n", CharsetUtil.US_ASCII);
62
63 private final int maxContentLength;
64 private HttpMessage currentMessage;
65
66
67
68
69
70
71
72
73
74 public HttpChunkAggregator(int maxContentLength) {
75 if (maxContentLength <= 0) {
76 throw new IllegalArgumentException(
77 "maxContentLength must be a positive integer: " +
78 maxContentLength);
79 }
80 this.maxContentLength = maxContentLength;
81 }
82
83 @Override
84 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
85 throws Exception {
86
87 Object msg = e.getMessage();
88 HttpMessage currentMessage = this.currentMessage;
89
90 if (msg instanceof HttpMessage) {
91 HttpMessage m = (HttpMessage) msg;
92
93
94
95
96
97
98 if (is100ContinueExpected(m)) {
99 write(ctx, succeededFuture(ctx.getChannel()),
100 CONTINUE.duplicate());
101 }
102
103 if (m.isChunked()) {
104
105
106 List<String> encodings = m
107 .getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
108 encodings.remove(HttpHeaders.Values.CHUNKED);
109 if (encodings.isEmpty()) {
110 m.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
111 }
112 m.setChunked(false);
113 m.setContent(ChannelBuffers.dynamicBuffer(e.getChannel()
114 .getConfig().getBufferFactory()));
115 this.currentMessage = m;
116 } else {
117
118 this.currentMessage = null;
119 ctx.sendUpstream(e);
120 }
121 } else if (msg instanceof HttpChunk) {
122
123 if (currentMessage == null) {
124 throw new IllegalStateException("received " +
125 HttpChunk.class.getSimpleName() + " without " +
126 HttpMessage.class.getSimpleName());
127 }
128
129
130 HttpChunk chunk = (HttpChunk) msg;
131 ChannelBuffer content = currentMessage.getContent();
132
133 if (content.readableBytes() > maxContentLength -
134 chunk.getContent().readableBytes()) {
135
136
137
138
139 throw new TooLongFrameException(
140 "HTTP content length exceeded " + maxContentLength +
141 " bytes.");
142 }
143
144 content.writeBytes(chunk.getContent());
145 if (chunk.isLast()) {
146 this.currentMessage = null;
147
148
149 if (chunk instanceof HttpChunkTrailer) {
150 HttpChunkTrailer trailer = (HttpChunkTrailer) chunk;
151 for (Entry<String, String> header: trailer.getHeaders()) {
152 currentMessage.setHeader(header.getKey(),
153 header.getValue());
154 }
155 }
156
157
158 currentMessage.setHeader(HttpHeaders.Names.CONTENT_LENGTH,
159 String.valueOf(content.readableBytes()));
160
161
162 Channels.fireMessageReceived(ctx, currentMessage,
163 e.getRemoteAddress());
164 }
165 } else {
166
167 ctx.sendUpstream(e);
168 }
169 }
170
171 }