View Javadoc
1   /*
2    * Copyright 2015 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  
17  package io.netty5.example.http2.tiles;
18  
19  import io.netty5.channel.ChannelFutureListeners;
20  import io.netty5.channel.ChannelHandlerContext;
21  import io.netty5.handler.codec.http.DefaultFullHttpResponse;
22  import io.netty5.handler.codec.http.FullHttpRequest;
23  import io.netty5.handler.codec.http.FullHttpResponse;
24  import io.netty5.handler.codec.http.HttpUtil;
25  
26  import java.util.concurrent.TimeUnit;
27  
28  import static io.netty5.handler.codec.http.HttpHeaderNames.CONNECTION;
29  import static io.netty5.handler.codec.http.HttpHeaderValues.CLOSE;
30  import static io.netty5.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
31  import static io.netty5.handler.codec.http.HttpResponseStatus.CONTINUE;
32  import static io.netty5.handler.codec.http.HttpUtil.isKeepAlive;
33  import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_0;
34  import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_1;
35  
36  /**
37   * Handles the requests for the tiled image using HTTP 1.x as a protocol.
38   */
39  public final class Http1RequestHandler extends Http2RequestHandler {
40  
41      @Override
42      protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
43          if (HttpUtil.is100ContinueExpected(request)) {
44              ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE, ctx.bufferAllocator().allocate(0)));
45          }
46          super.messageReceived(ctx, request);
47      }
48  
49      @Override
50      protected void sendResponse(final ChannelHandlerContext ctx, String streamId, int latency,
51              final FullHttpResponse response, final FullHttpRequest request) {
52          HttpUtil.setContentLength(response, response.payload().readableBytes());
53          ctx.executor().schedule(() -> {
54              if (isKeepAlive(request)) {
55                  if (request.protocolVersion().equals(HTTP_1_0)) {
56                      response.headers().set(CONNECTION, KEEP_ALIVE);
57                  }
58                  ctx.writeAndFlush(response);
59              } else {
60                  // Tell the client we're going to close the connection.
61                  response.headers().set(CONNECTION, CLOSE);
62                  ctx.writeAndFlush(response).addListener(ctx, ChannelFutureListeners.CLOSE);
63              }
64          }, latency, TimeUnit.MILLISECONDS);
65      }
66  }