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.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufUtil;
20  
21  import static io.netty.handler.codec.http.HttpConstants.*;
22  
23  /**
24   * Encodes an {@link HttpResponse} or an {@link HttpContent} into
25   * a {@link ByteBuf}.
26   */
27  public class HttpResponseEncoder extends HttpObjectEncoder<HttpResponse> {
28  
29      @Override
30      public boolean acceptOutboundMessage(Object msg) throws Exception {
31          return super.acceptOutboundMessage(msg) && !(msg instanceof HttpRequest);
32      }
33  
34      @Override
35      protected void encodeInitialLine(ByteBuf buf, HttpResponse response) throws Exception {
36          response.protocolVersion().encode(buf);
37          buf.writeByte(SP);
38          response.status().encode(buf);
39          ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
40      }
41  
42      @Override
43      protected void sanitizeHeadersBeforeEncode(HttpResponse msg, boolean isAlwaysEmpty) {
44          if (isAlwaysEmpty) {
45              HttpResponseStatus status = msg.status();
46              if (status.codeClass() == HttpStatusClass.INFORMATIONAL ||
47                      status.code() == HttpResponseStatus.NO_CONTENT.code()) {
48  
49                  // Stripping Content-Length:
50                  // See https://tools.ietf.org/html/rfc7230#section-3.3.2
51                  msg.headers().remove(HttpHeaderNames.CONTENT_LENGTH);
52  
53                  // Stripping Transfer-Encoding:
54                  // See https://tools.ietf.org/html/rfc7230#section-3.3.1
55                  msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
56              }
57          }
58      }
59  
60      @Override
61      protected boolean isContentAlwaysEmpty(HttpResponse msg) {
62          // Correctly handle special cases as stated in:
63          // https://tools.ietf.org/html/rfc7230#section-3.3.3
64          HttpResponseStatus status = msg.status();
65  
66          if (status.codeClass() == HttpStatusClass.INFORMATIONAL) {
67  
68              if (status.code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code()) {
69                  // We need special handling for WebSockets version 00 as it will include an body.
70                  // Fortunally this version should not really be used in the wild very often.
71                  // See https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00#section-1.2
72                  return msg.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_VERSION);
73              }
74              return true;
75          }
76          return status.code() == HttpResponseStatus.NO_CONTENT.code() ||
77                  status.code() == HttpResponseStatus.NOT_MODIFIED.code();
78      }
79  }