HttpResponseEncoder Class — netty Architecture
Architecture documentation for the HttpResponseEncoder class in HttpResponseEncoder.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD bcdc6576_85d8_9fc4_6f59_ed2fc79de374["HttpResponseEncoder"] 45d25d3c_66ff_e449_9403_444ff2a8931d["HttpResponseEncoder.java"] bcdc6576_85d8_9fc4_6f59_ed2fc79de374 -->|defined in| 45d25d3c_66ff_e449_9403_444ff2a8931d c71d1c96_0144_1b0f_6ae0_cf23e738fe33["acceptOutboundMessage()"] bcdc6576_85d8_9fc4_6f59_ed2fc79de374 -->|method| c71d1c96_0144_1b0f_6ae0_cf23e738fe33 0c4d3a6c_5378_a86f_848f_60b1f96b473a["encodeInitialLine()"] bcdc6576_85d8_9fc4_6f59_ed2fc79de374 -->|method| 0c4d3a6c_5378_a86f_848f_60b1f96b473a f93dc19a_4424_c8c5_bed9_e10fd1a511b0["sanitizeHeadersBeforeEncode()"] bcdc6576_85d8_9fc4_6f59_ed2fc79de374 -->|method| f93dc19a_4424_c8c5_bed9_e10fd1a511b0 763dd229_87d5_3979_b18c_c169d13592c4["isContentAlwaysEmpty()"] bcdc6576_85d8_9fc4_6f59_ed2fc79de374 -->|method| 763dd229_87d5_3979_b18c_c169d13592c4
Relationship Graph
Source Code
codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseEncoder.java lines 27–98
public class HttpResponseEncoder extends HttpObjectEncoder<HttpResponse> {
@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
// JDK type checks vs non-implemented interfaces costs O(N), where
// N is the number of interfaces already implemented by the concrete type that's being tested.
// !(msg instanceof HttpRequest) is supposed to always be true (and meaning that msg isn't a HttpRequest),
// but sadly was part of the original behaviour of this method and cannot be removed.
// We place here exact checks vs DefaultHttpResponse and DefaultFullHttpResponse because bad users can
// extends such types and make them to implement HttpRequest (non-sense, but still possible).
final Class<?> msgClass = msg.getClass();
if (msgClass == DefaultFullHttpResponse.class || msgClass == DefaultHttpResponse.class) {
return true;
}
return super.acceptOutboundMessage(msg) && !(msg instanceof HttpRequest);
}
@Override
protected void encodeInitialLine(ByteBuf buf, HttpResponse response) throws Exception {
response.protocolVersion().encode(buf);
buf.writeByte(SP);
response.status().encode(buf);
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
}
@Override
protected void sanitizeHeadersBeforeEncode(HttpResponse msg, boolean isAlwaysEmpty) {
if (isAlwaysEmpty) {
HttpResponseStatus status = msg.status();
if (status.codeClass() == HttpStatusClass.INFORMATIONAL ||
status.code() == HttpResponseStatus.NO_CONTENT.code()) {
// Stripping Content-Length:
// See https://tools.ietf.org/html/rfc7230#section-3.3.2
msg.headers().remove(HttpHeaderNames.CONTENT_LENGTH);
// Stripping Transfer-Encoding:
// See https://tools.ietf.org/html/rfc7230#section-3.3.1
msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
} else if (status.code() == HttpResponseStatus.RESET_CONTENT.code()) {
// Stripping Transfer-Encoding:
msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
// Set Content-Length: 0
// https://httpstatuses.com/205
msg.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 0);
}
}
}
@Override
protected boolean isContentAlwaysEmpty(HttpResponse msg) {
// Correctly handle special cases as stated in:
// https://tools.ietf.org/html/rfc7230#section-3.3.3
HttpResponseStatus status = msg.status();
if (status.codeClass() == HttpStatusClass.INFORMATIONAL) {
if (status.code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code()) {
// We need special handling for WebSockets version 00 as it will include an body.
// Fortunally this version should not really be used in the wild very often.
// See https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00#section-1.2
return msg.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_VERSION);
}
return true;
}
return status.code() == HttpResponseStatus.NO_CONTENT.code() ||
status.code() == HttpResponseStatus.NOT_MODIFIED.code() ||
status.code() == HttpResponseStatus.RESET_CONTENT.code();
}
}
Source
Frequently Asked Questions
What is the HttpResponseEncoder class?
HttpResponseEncoder is a class in the netty codebase, defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseEncoder.java.
Where is HttpResponseEncoder defined?
HttpResponseEncoder is defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseEncoder.java at line 27.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free