View Javadoc
1   /*
2    * Copyright 2012 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    *   http://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.channel.ChannelHandlerAppender;
19  import io.netty.channel.ChannelHandlerContext;
20  
21  
22  /**
23   * A combination of {@link HttpRequestDecoder} and {@link HttpResponseEncoder}
24   * which enables easier server side HTTP implementation. Also supports use with
25   * a {@link HttpServerUpgradeHandler} to support upgrading to another protocol
26   * from HTTP.
27   *
28   * @see HttpClientCodec
29   * @see HttpServerUpgradeHandler
30   */
31  public final class HttpServerCodec extends ChannelHandlerAppender implements
32          HttpServerUpgradeHandler.SourceCodec {
33  
34      /**
35       * Creates a new instance with the default decoder options
36       * ({@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and
37       * {@code maxChunkSize (8192)}).
38       */
39      public HttpServerCodec() {
40          this(4096, 8192, 8192);
41      }
42  
43      /**
44       * Creates a new instance with the specified decoder options.
45       */
46      public HttpServerCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) {
47          super(new HttpRequestDecoder(maxInitialLineLength, maxHeaderSize, maxChunkSize), new HttpResponseEncoder());
48      }
49  
50      /**
51       * Creates a new instance with the specified decoder options.
52       */
53      public HttpServerCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders) {
54          super(new HttpRequestDecoder(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders),
55                  new HttpResponseEncoder());
56      }
57  
58      /**
59       * Upgrades to another protocol from HTTP. Removes the {@link HttpRequestDecoder} and
60       * {@link HttpResponseEncoder} from the pipeline.
61       */
62      @Override
63      public void upgradeFrom(ChannelHandlerContext ctx) {
64          ctx.pipeline().remove(HttpRequestDecoder.class);
65          ctx.pipeline().remove(HttpResponseEncoder.class);
66      }
67  
68      /**
69       * Returns the encoder of this codec.
70       */
71      public HttpResponseEncoder encoder() {
72          return handlerAt(1);
73      }
74  
75      /**
76       * Returns the decoder of this codec.
77       */
78      public HttpRequestDecoder decoder() {
79          return handlerAt(0);
80      }
81  }