1 /*
2 * Copyright 2017 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.handler.codec.http;
17
18 import io.netty.buffer.Unpooled;
19 import io.netty.channel.ChannelFutureListener;
20 import io.netty.channel.ChannelHandlerContext;
21 import io.netty.channel.ChannelInboundHandlerAdapter;
22 import io.netty.util.ReferenceCountUtil;
23
24 import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
25 import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
26 import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
27
28 /**
29 * Sends a <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3">100 CONTINUE</a>
30 * {@link HttpResponse} to {@link HttpRequest}s which contain a 'expect: 100-continue' header. It
31 * should only be used for applications which do <b>not</b> install the {@link HttpObjectAggregator}.
32 * <p>
33 * By default it accepts all expectations.
34 * <p>
35 * Since {@link HttpServerExpectContinueHandler} expects {@link HttpRequest}s it should be added after {@link
36 * HttpServerCodec} but before any other handlers that might send a {@link HttpResponse}. <blockquote>
37 * <pre>
38 * {@link io.netty.channel.ChannelPipeline} p = ...;
39 * ...
40 * p.addLast("serverCodec", new {@link HttpServerCodec}());
41 * p.addLast("respondExpectContinue", <b>new {@link HttpServerExpectContinueHandler}()</b>);
42 * ...
43 * p.addLast("handler", new HttpRequestHandler());
44 * </pre>
45 * </blockquote>
46 */
47 public class HttpServerExpectContinueHandler extends ChannelInboundHandlerAdapter {
48
49 private static final FullHttpResponse EXPECTATION_FAILED = new DefaultFullHttpResponse(
50 HTTP_1_1, HttpResponseStatus.EXPECTATION_FAILED, Unpooled.EMPTY_BUFFER);
51
52 private static final FullHttpResponse ACCEPT = new DefaultFullHttpResponse(
53 HTTP_1_1, CONTINUE, Unpooled.EMPTY_BUFFER);
54
55 static {
56 EXPECTATION_FAILED.headers().set(CONTENT_LENGTH, 0);
57 ACCEPT.headers().set(CONTENT_LENGTH, 0);
58 }
59
60 /**
61 * Produces a {@link HttpResponse} for {@link HttpRequest}s which define an expectation. Returns {@code null} if the
62 * request should be rejected. See {@link #rejectResponse(HttpRequest)}.
63 */
64 protected HttpResponse acceptMessage(@SuppressWarnings("unused") HttpRequest request) {
65 return ACCEPT.retainedDuplicate();
66 }
67
68 /**
69 * Returns the appropriate 4XX {@link HttpResponse} for the given {@link HttpRequest}.
70 */
71 protected HttpResponse rejectResponse(@SuppressWarnings("unused") HttpRequest request) {
72 return EXPECTATION_FAILED.retainedDuplicate();
73 }
74
75 @Override
76 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
77 if (msg instanceof HttpRequest) {
78 HttpRequest req = (HttpRequest) msg;
79
80 if (HttpUtil.is100ContinueExpected(req)) {
81 HttpResponse accept = acceptMessage(req);
82
83 if (accept == null) {
84 // the expectation failed so we refuse the request.
85 HttpResponse rejection = rejectResponse(req);
86 ReferenceCountUtil.release(msg);
87 ctx.writeAndFlush(rejection).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
88 return;
89 }
90
91 ctx.writeAndFlush(accept).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
92 req.headers().remove(HttpHeaderNames.EXPECT);
93 }
94 }
95 super.channelRead(ctx, msg);
96 }
97 }