1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
25
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
50
51 msg.headers().remove(HttpHeaderNames.CONTENT_LENGTH);
52
53
54
55 msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
56 } else if (status.code() == HttpResponseStatus.RESET_CONTENT.code()) {
57
58
59 msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
60
61
62
63 msg.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 0);
64 }
65 }
66 }
67
68 @Override
69 protected boolean isContentAlwaysEmpty(HttpResponse msg) {
70
71
72 HttpResponseStatus status = msg.status();
73
74 if (status.codeClass() == HttpStatusClass.INFORMATIONAL) {
75
76 if (status.code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code()) {
77
78
79
80 return msg.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_VERSION);
81 }
82 return true;
83 }
84 return status.code() == HttpResponseStatus.NO_CONTENT.code() ||
85 status.code() == HttpResponseStatus.NOT_MODIFIED.code() ||
86 status.code() == HttpResponseStatus.RESET_CONTENT.code();
87 }
88 }