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