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    *   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.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          // JDK type checks vs non-implemented interfaces costs O(N), where
32          // N is the number of interfaces already implemented by the concrete type that's being tested.
33          // !(msg instanceof HttpRequest) is supposed to always be true (and meaning that msg isn't a HttpRequest),
34          // but sadly was part of the original behaviour of this method and cannot be removed.
35          // We place here exact checks vs DefaultHttpResponse and DefaultFullHttpResponse because bad users can
36          // extends such types and make them to implement HttpRequest (non-sense, but still possible).
37          final Class<?> msgClass = msg.getClass();
38          if (msgClass == DefaultFullHttpResponse.class || msgClass == DefaultHttpResponse.class) {
39              return true;
40          }
41          return super.acceptOutboundMessage(msg) && !(msg instanceof HttpRequest);
42      }
43  
44      @Override
45      protected void encodeInitialLine(ByteBuf buf, HttpResponse response) throws Exception {
46          response.protocolVersion().encode(buf);
47          buf.writeByte(SP);
48          response.status().encode(buf);
49          ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
50      }
51  
52      @Override
53      protected void sanitizeHeadersBeforeEncode(HttpResponse msg, boolean isAlwaysEmpty) {
54          if (isAlwaysEmpty) {
55              HttpResponseStatus status = msg.status();
56              if (status.codeClass() == HttpStatusClass.INFORMATIONAL ||
57                      status.code() == HttpResponseStatus.NO_CONTENT.code()) {
58  
59                  // Stripping Content-Length:
60                  // See https://tools.ietf.org/html/rfc7230#section-3.3.2
61                  msg.headers().remove(HttpHeaderNames.CONTENT_LENGTH);
62  
63                  // Stripping Transfer-Encoding:
64                  // See https://tools.ietf.org/html/rfc7230#section-3.3.1
65                  msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
66              } else if (status.code() == HttpResponseStatus.RESET_CONTENT.code()) {
67  
68                  // Stripping Transfer-Encoding:
69                  msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
70  
71                  // Set Content-Length: 0
72                  // https://httpstatuses.com/205
73                  msg.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 0);
74              }
75          }
76      }
77  
78      @Override
79      protected boolean isContentAlwaysEmpty(HttpResponse msg) {
80          // Correctly handle special cases as stated in:
81          // https://tools.ietf.org/html/rfc7230#section-3.3.3
82          HttpResponseStatus status = msg.status();
83  
84          if (status.codeClass() == HttpStatusClass.INFORMATIONAL) {
85  
86              if (status.code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code()) {
87                  // We need special handling for WebSockets version 00 as it will include an body.
88                  // Fortunally this version should not really be used in the wild very often.
89                  // See https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00#section-1.2
90                  return msg.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_VERSION);
91              }
92              return true;
93          }
94          return status.code() == HttpResponseStatus.NO_CONTENT.code() ||
95                  status.code() == HttpResponseStatus.NOT_MODIFIED.code() ||
96                  status.code() == HttpResponseStatus.RESET_CONTENT.code();
97      }
98  }