-
Notifications
You must be signed in to change notification settings - Fork 897
/
Http2ResponseDecoder.java
370 lines (309 loc) · 13.8 KB
/
Http2ResponseDecoder.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
/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.armeria.client;
import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.math.LongMath;
import com.linecorp.armeria.common.ContentTooLargeException;
import com.linecorp.armeria.common.HttpData;
import com.linecorp.armeria.common.HttpHeaders;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.stream.ClosedStreamException;
import com.linecorp.armeria.internal.common.ArmeriaHttpUtil;
import com.linecorp.armeria.internal.common.Http2GoAwayHandler;
import com.linecorp.armeria.internal.common.InboundTrafficController;
import com.linecorp.armeria.internal.common.KeepAliveHandler;
import com.linecorp.armeria.internal.common.NoopKeepAliveHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoop;
import io.netty.handler.codec.http2.Http2Connection;
import io.netty.handler.codec.http2.Http2ConnectionEncoder;
import io.netty.handler.codec.http2.Http2Error;
import io.netty.handler.codec.http2.Http2Exception;
import io.netty.handler.codec.http2.Http2Flags;
import io.netty.handler.codec.http2.Http2FrameListener;
import io.netty.handler.codec.http2.Http2Headers;
import io.netty.handler.codec.http2.Http2Settings;
import io.netty.handler.codec.http2.Http2Stream;
final class Http2ResponseDecoder extends HttpResponseDecoder implements Http2Connection.Listener,
Http2FrameListener {
private static final Logger logger = LoggerFactory.getLogger(Http2ResponseDecoder.class);
private final Http2Connection conn;
private final Http2ConnectionEncoder encoder;
private final Http2GoAwayHandler goAwayHandler;
private final KeepAliveHandler keepAliveHandler;
Http2ResponseDecoder(Channel channel, Http2ConnectionEncoder encoder, HttpClientFactory clientFactory,
KeepAliveHandler keepAliveHandler) {
super(channel,
InboundTrafficController.ofHttp2(channel, clientFactory.http2InitialConnectionWindowSize()));
conn = encoder.connection();
this.encoder = encoder;
assert keepAliveHandler instanceof Http2ClientKeepAliveHandler ||
keepAliveHandler instanceof NoopKeepAliveHandler;
this.keepAliveHandler = keepAliveHandler;
goAwayHandler = new Http2GoAwayHandler();
}
@Override
HttpResponseWrapper addResponse(
int id, DecodedHttpResponse res, @Nullable ClientRequestContext ctx,
EventLoop eventLoop, long responseTimeoutMillis, long maxContentLength) {
final HttpResponseWrapper resWrapper =
super.addResponse(id, res, ctx, eventLoop, responseTimeoutMillis, maxContentLength);
resWrapper.whenComplete().handle((unused, cause) -> {
if (eventLoop.inEventLoop()) {
onWrapperCompleted(resWrapper, id, cause);
} else {
eventLoop.execute(() -> onWrapperCompleted(resWrapper, id, cause));
}
return null;
});
return resWrapper;
}
private void onWrapperCompleted(HttpResponseWrapper resWrapper, int id, @Nullable Throwable cause) {
// Cancel timeout future and abort the request if it exists.
resWrapper.onSubscriptionCancelled(cause);
if (cause != null) {
// We are not closing the connection but just send a RST_STREAM,
// so we have to remove the response manually.
removeResponse(id);
// Reset the stream.
final int streamId = idToStreamId(id);
final int lastStreamId = conn.local().lastStreamKnownByPeer();
if (lastStreamId < 0 || // Did not receive a GOAWAY yet or
streamId <= lastStreamId) { // received a GOAWAY and the request's streamId <= lastStreamId
final ChannelHandlerContext ctx = channel().pipeline().lastContext();
if (ctx != null) {
encoder.writeRstStream(ctx, streamId, Http2Error.CANCEL.code(), ctx.newPromise());
ctx.flush();
} else {
// The pipeline has been cleaned up due to disconnection.
}
}
}
}
Http2GoAwayHandler goAwayHandler() {
return goAwayHandler;
}
@Override
public void onStreamAdded(Http2Stream stream) {}
@Override
public void onStreamActive(Http2Stream stream) {}
@Override
public void onStreamHalfClosed(Http2Stream stream) {}
@Override
public void onStreamClosed(Http2Stream stream) {
goAwayHandler.onStreamClosed(channel(), stream);
final HttpResponseWrapper res = getResponse(streamIdToId(stream.id()), true);
if (res == null) {
return;
}
if (!goAwayHandler.receivedGoAway()) {
res.close(ClosedStreamException.get());
return;
}
final int lastStreamId = conn.local().lastStreamKnownByPeer();
if (stream.id() > lastStreamId) {
res.close(UnprocessedRequestException.of(GoAwayReceivedException.get()));
} else {
res.close(ClosedStreamException.get());
}
if (shouldSendGoAway()) {
channel().close();
}
}
@Override
public void onStreamRemoved(Http2Stream stream) {}
@Override
public void onGoAwaySent(int lastStreamId, long errorCode, ByteBuf debugData) {
disconnectWhenFinished();
goAwayHandler.onGoAwaySent(channel(), lastStreamId, errorCode, debugData);
}
@Override
public void onGoAwayReceived(int lastStreamId, long errorCode, ByteBuf debugData) {
// Should not reuse a connection that received a GOAWAY frame.
HttpSession.get(channel()).deactivate();
disconnectWhenFinished();
goAwayHandler.onGoAwayReceived(channel(), lastStreamId, errorCode, debugData);
}
@Override
public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings) {
ctx.fireChannelRead(settings);
}
@Override
public void onSettingsAckRead(ChannelHandlerContext ctx) {}
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding,
boolean endOfStream) throws Http2Exception {
keepAliveChannelRead();
final HttpResponseWrapper res = getResponse(streamIdToId(streamId), endOfStream);
if (res == null) {
if (conn.streamMayHaveExisted(streamId)) {
if (logger.isDebugEnabled()) {
logger.debug("{} Received a late HEADERS frame for a closed stream: {}",
ctx.channel(), streamId);
}
return;
}
throw connectionError(PROTOCOL_ERROR, "received a HEADERS frame for an unknown stream: %d",
streamId);
}
res.logResponseFirstBytesTransferred();
final HttpHeaders converted = ArmeriaHttpUtil.toArmeria(headers, false, endOfStream);
try {
res.initTimeout();
res.write(converted);
} catch (Throwable t) {
res.close(t);
throw connectionError(INTERNAL_ERROR, t, "failed to consume a HEADERS frame");
}
if (endOfStream) {
res.close();
if (shouldSendGoAway()) {
channel().close();
}
}
}
@Override
public void onHeadersRead(
ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency,
short weight, boolean exclusive, int padding, boolean endOfStream) throws Http2Exception {
onHeadersRead(ctx, streamId, headers, padding, endOfStream);
}
@Override
public int onDataRead(
ChannelHandlerContext ctx, int streamId, ByteBuf data,
int padding, boolean endOfStream) throws Http2Exception {
keepAliveChannelRead();
final int dataLength = data.readableBytes();
final HttpResponseWrapper res = getResponse(streamIdToId(streamId), endOfStream);
if (res == null) {
if (conn.streamMayHaveExisted(streamId)) {
if (logger.isDebugEnabled()) {
logger.debug("{} Received a late DATA frame for a closed stream: {}",
ctx.channel(), streamId);
}
return dataLength + padding;
}
throw connectionError(PROTOCOL_ERROR, "received a DATA frame for an unknown stream: %d",
streamId);
}
final long maxContentLength = res.maxContentLength();
final long writtenBytes = res.writtenBytes();
if (maxContentLength > 0 && writtenBytes > maxContentLength - dataLength) {
final long transferred = LongMath.saturatedAdd(writtenBytes, dataLength);
res.close(ContentTooLargeException.builder()
.maxContentLength(maxContentLength)
.contentLength(res.headers())
.transferred(transferred)
.build());
throw connectionError(
INTERNAL_ERROR,
"content too large: transferred(%d + %d) > limit(%d) (stream: %d)",
writtenBytes, dataLength, maxContentLength, streamId);
}
try {
res.write(HttpData.wrap(data.retain()).withEndOfStream(endOfStream));
} catch (Throwable t) {
res.close(t);
throw connectionError(INTERNAL_ERROR, t, "failed to consume a DATA frame");
}
if (endOfStream) {
res.close();
if (shouldSendGoAway()) {
// The connection has reached its lifespan.
// Should send a GOAWAY frame if it did not receive or send a GOAWAY frame.
channel().close();
}
}
// All bytes have been processed.
return dataLength + padding;
}
/**
* Returns {@code true} if a connection has reached its lifespan
* and the connection did not receive or send a GOAWAY frame.
*/
private boolean shouldSendGoAway() {
return needsToDisconnectNow() && !goAwayHandler.sentGoAway() && !goAwayHandler.receivedGoAway();
}
@Override
public void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long errorCode) throws Http2Exception {
keepAliveChannelRead();
final HttpResponseWrapper res = removeResponse(streamIdToId(streamId));
if (res == null) {
if (conn.streamMayHaveExisted(streamId)) {
if (logger.isDebugEnabled()) {
logger.debug("{} Received a late RST_STREAM frame for a closed stream: {}",
ctx.channel(), streamId);
}
} else {
throw connectionError(PROTOCOL_ERROR,
"received a RST_STREAM frame for an unknown stream: %d", streamId);
}
return;
}
final Http2Error http2Error = Http2Error.valueOf(errorCode);
final ClosedStreamException cause =
new ClosedStreamException("received a RST_STREAM frame: " + http2Error);
if (http2Error == Http2Error.REFUSED_STREAM) {
res.close(UnprocessedRequestException.of(cause));
} else {
res.close(cause);
}
}
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId,
Http2Headers headers, int padding) {}
@Override
public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight,
boolean exclusive) {}
@Override
public void onPingRead(ChannelHandlerContext ctx, long data) {
keepAliveHandler.onPing();
}
@Override
public void onPingAckRead(ChannelHandlerContext ctx, long data) {
if (keepAliveHandler.isHttp2()) {
keepAliveHandler.onPingAck(data);
}
}
@Override
public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData) {}
@Override
public void onWindowUpdateRead(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement) {}
@Override
public void onUnknownFrame(ChannelHandlerContext ctx, byte frameType, int streamId, Http2Flags flags,
ByteBuf payload) {}
@Override
KeepAliveHandler keepAliveHandler() {
return keepAliveHandler;
}
private void keepAliveChannelRead() {
if (keepAliveHandler != null) {
keepAliveHandler.onReadOrWrite();
}
}
private static int streamIdToId(int streamId) {
return streamId - 1 >>> 1;
}
private static int idToStreamId(int id) {
return (id << 1) + 1;
}
}