-
Notifications
You must be signed in to change notification settings - Fork 558
/
BareResponseImpl.java
361 lines (312 loc) · 13.7 KB
/
BareResponseImpl.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
/*
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
*
* Licensed 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
*
* http://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 io.helidon.webserver;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BooleanSupplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.helidon.common.http.DataChunk;
import io.helidon.common.http.Http;
import io.helidon.common.reactive.Flow;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultHttpContent;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.DefaultLastHttpContent;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import static io.netty.handler.codec.http.HttpResponseStatus.valueOf;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
/**
* The BareResponseImpl.
*/
class BareResponseImpl implements BareResponse {
private static final Logger LOGGER = Logger.getLogger(BareResponseImpl.class.getName());
// See HttpConversionUtil.ExtensionHeaderNames
private static final String HTTP_2_HEADER_PREFIX = "x-http2";
private static final SocketClosedException CLOSED = new SocketClosedException("Response channel is closed!");
private static final LastHttpContent LAST_HTTP_CONTENT = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER);
private final boolean keepAlive;
private final ChannelHandlerContext ctx;
private final AtomicBoolean statusHeadersSent = new AtomicBoolean(false);
private final AtomicBoolean internallyClosed = new AtomicBoolean(false);
private final CompletableFuture<BareResponse> responseFuture;
private final CompletableFuture<BareResponse> headersFuture;
private final BooleanSupplier requestContentConsumed;
private final Thread thread;
private final long requestId;
private final HttpHeaders requestHeaders;
private final ChannelFuture channelClosedFuture;
private final GenericFutureListener<? extends Future<? super Void>> channelClosedListener;
private volatile Flow.Subscription subscription;
private volatile DataChunk firstChunk;
private volatile DefaultHttpResponse response;
private volatile boolean lengthOptimization;
/**
* @param ctx the channel handler context
* @param request the request
* @param requestContentConsumed whether the request content is consumed
* @param thread the outbound event loop thread which will be used to write the response
* @param requestId the correlation ID that is added to the log statements
*/
BareResponseImpl(ChannelHandlerContext ctx,
HttpRequest request,
BooleanSupplier requestContentConsumed,
Thread thread,
long requestId) {
this.requestContentConsumed = requestContentConsumed;
this.thread = thread;
this.responseFuture = new CompletableFuture<>();
this.headersFuture = new CompletableFuture<>();
this.ctx = ctx;
this.requestId = requestId;
this.keepAlive = HttpUtil.isKeepAlive(request);
this.requestHeaders = request.headers();
// We need to keep this listener so we can remove it when this response completes. If we don't, we leak
// while the channel remains open since each response adds a new listener that references 'this'.
// Use fields to avoid capturing lambdas.
this.channelClosedListener = this::channelClosed;
this.channelClosedFuture = ctx.channel().closeFuture();
// to make this work, when programmatically closing the channel the responseFuture must be closed first!
channelClosedFuture.addListener(channelClosedListener);
responseFuture.whenComplete(this::responseComplete);
}
private void responseComplete(BareResponse self, Throwable throwable) {
if (throwable == null) {
headersFuture.complete(this);
} else {
headersFuture.completeExceptionally(throwable);
}
channelClosedFuture.removeListener(channelClosedListener);
}
private void channelClosed(Future<? super Void> future) {
responseFuture.completeExceptionally(CLOSED);
}
@Override
public void writeStatusAndHeaders(Http.ResponseStatus status, Map<String, List<String>> headers) {
Objects.requireNonNull(status, "Parameter 'statusCode' was null!");
if (!statusHeadersSent.compareAndSet(false, true)) {
throw new IllegalStateException("Status and headers were already sent");
}
response = new DefaultHttpResponse(HTTP_1_1, valueOf(status.code()));
for (Map.Entry<String, List<String>> headerEntry : headers.entrySet()) {
response.headers().add(headerEntry.getKey(), headerEntry.getValue());
}
// Copy HTTP/2 headers to response for correlation (streamId)
requestHeaders.names().stream()
.filter(header -> header.startsWith(HTTP_2_HEADER_PREFIX))
.forEach(header -> response.headers().add(header, requestHeaders.get(header)));
// Set chunked if length not set, may switch to length later
boolean lengthSet = HttpUtil.isContentLengthSet(response);
if (!lengthSet) {
lengthOptimization = status.code() == Http.Status.OK_200.code()
&& !HttpUtil.isTransferEncodingChunked(response);
HttpUtil.setTransferEncodingChunked(response, true);
}
// Add keep alive header as per:
// http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
if (keepAlive) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
// Content length optimization attempt
if (!lengthOptimization) {
LOGGER.finest(() -> log("Writing headers: " + status));
initWriteResponse();
}
}
/**
* Completes {@code responseFuture} instance to signal that this response is done.
* <b>Prefer to use {@link #completeInternal(Throwable)} to cover whole completion process.</b>
*
* @param throwable if {@code not-null} then this response is completed exceptionally.
*/
private void completeResponseFuture(Throwable throwable) {
if (throwable == null) {
responseFuture.complete(this);
} else {
LOGGER.log(Level.FINER, throwable, () -> log("Response completion failed!"));
responseFuture.completeExceptionally(throwable);
}
}
/**
* Completes this response. No other data are send to the client when response is completed. All caches are flushed.
*
* @param throwable if {@code not-null} then this response is completed exceptionally.
*/
private void completeInternal(Throwable throwable) {
if (!internallyClosed.compareAndSet(false, true)) {
// if already closed, as the contract specifies, don't fail
completeResponseFuture(throwable);
return;
}
if (keepAlive) {
LOGGER.finest(() -> log("Writing an empty last http content; keep-alive: true"));
writeLastContent(throwable, ChannelFutureListener.CLOSE_ON_FAILURE);
if (!requestContentConsumed.getAsBoolean()) {
// the request content wasn't read, close the connection once the content is fully written.
LOGGER.finer(() -> log("Request content not fully read; trying to keep the connection; keep-alive: true"));
// if content is not consumed, we need to trigger next chunk read in order to not get stuck forever; the
// connection will be closed in the ForwardingHandler in case there is more than just small amount of data
ctx.channel().read();
}
} else {
LOGGER.finest(() -> log("Closing with an empty buffer; keep-alive: " + keepAlive));
writeLastContent(throwable, ChannelFutureListener.CLOSE);
}
}
/**
* Write last HTTP content. If length optimization is active and a first chunk is cached,
* switch content encoding and write response.
*
* @param throwable A throwable.
* @param closeAction Close action listener.
*/
private void writeLastContent(final Throwable throwable, final ChannelFutureListener closeAction) {
if (lengthOptimization) {
if (firstChunk != null) {
HttpUtil.setTransferEncodingChunked(response, false);
HttpUtil.setContentLength(response, firstChunk.data().remaining());
}
initWriteResponse();
}
ctx.writeAndFlush(LAST_HTTP_CONTENT)
.addListener(completeOnFailureListener("An exception occurred when writing last http content."))
.addListener(completeOnSuccessListener(throwable))
.addListener(closeAction);
}
private GenericFutureListener<Future<? super Void>> completeOnFailureListener(String message) {
return future -> {
if (!future.isSuccess()) {
completeResponseFuture(new IllegalStateException(message, future.cause()));
}
};
}
private GenericFutureListener<Future<? super Void>> completeOnSuccessListener(Throwable throwable) {
return future -> {
if (future.isSuccess()) {
completeResponseFuture(throwable);
LOGGER.finest(() -> log("Last http message flushed."));
}
};
}
@Override
public void onSubscribe(Flow.Subscription subscription) {
this.subscription = subscription;
subscription.request(Long.MAX_VALUE);
}
@Override
public void onNext(DataChunk data) {
if (internallyClosed.get()) {
throw new IllegalStateException("Response is already closed!");
}
if (data != null) {
if (data.isFlushChunk()) {
ctx.flush();
return;
}
if (lengthOptimization) {
if (firstChunk == null) {
firstChunk = data.isReadOnly() ? data : data.duplicate(); // cache first chunk
return;
}
initWriteResponse();
}
sendData(data);
}
}
/**
* Initiates write of response and sends first chunk if available.
*
* @return Future of response or first chunk.
*/
private ChannelFuture initWriteResponse() {
ChannelFuture cf = ctx.write(response)
.addListener(future -> {
if (future.isSuccess()) {
headersFuture.complete(this);
}
})
.addListener(completeOnFailureListener("An exception occurred when writing headers."))
.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
response = null;
if (firstChunk != null) {
cf = sendData(firstChunk);
firstChunk = null;
}
lengthOptimization = false;
return cf;
}
private ChannelFuture sendData(DataChunk data) {
LOGGER.finest(() -> log("Sending data chunk"));
DefaultHttpContent httpContent = new DefaultHttpContent(Unpooled.wrappedBuffer(data.data()));
LOGGER.finest(() -> log("Sending data chunk on event loop thread."));
ChannelFuture channelFuture;
if (data.flush()) {
channelFuture = ctx.writeAndFlush(httpContent);
} else {
channelFuture = ctx.write(httpContent);
}
return channelFuture
.addListener(future -> {
data.release();
LOGGER.finest(() -> log("Data chunk sent with result: " + future.isSuccess()));
})
.addListener(completeOnFailureListener("Failure when sending a content!"))
.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
}
private String log(String s) {
return "(reqID: " + requestId + ") " + s;
}
@Override
public void onError(Throwable thr) {
completeInternal(thr);
if (subscription != null) {
subscription.cancel();
}
}
@Override
public void onComplete() {
completeInternal(null);
if (subscription != null) {
subscription.cancel();
}
}
@Override
public CompletionStage<BareResponse> whenCompleted() {
return responseFuture;
}
@Override
public CompletionStage<BareResponse> whenHeadersCompleted() {
return headersFuture;
}
@Override
public long requestId() {
return requestId;
}
}