Home / Class/ HttpContentEncoder Class — netty Architecture

HttpContentEncoder Class — netty Architecture

Architecture documentation for the HttpContentEncoder class in HttpContentEncoder.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  77d3b233_ef0d_c6ed_64de_3e425b740522["HttpContentEncoder"]
  1feebdb6_ff86_5cce_50d1_aa7e0e12711c["HttpContentEncoder.java"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|defined in| 1feebdb6_ff86_5cce_50d1_aa7e0e12711c
  49c09bef_3d1a_b85a_a723_bd47e543ba84["HttpContentEncoder()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| 49c09bef_3d1a_b85a_a723_bd47e543ba84
  4530c734_912d_c7d4_ae6c_61186d4d3ce1["acceptOutboundMessage()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| 4530c734_912d_c7d4_ae6c_61186d4d3ce1
  b5497765_76d0_d648_44c2_32a8f1535d9f["decode()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| b5497765_76d0_d648_44c2_32a8f1535d9f
  5f451408_0805_44d2_06ca_843854c94345["encode()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| 5f451408_0805_44d2_06ca_843854c94345
  f8b06062_b563_2e15_a80d_cec486725f70["encodeFullResponse()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| f8b06062_b563_2e15_a80d_cec486725f70
  1f658e60_1506_b115_e30f_7101ef756c81["isPassthru()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| 1f658e60_1506_b115_e30f_7101ef756c81
  c6773d44_960e_8c19_4b9b_555876ac1fd1["ensureHeaders()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| c6773d44_960e_8c19_4b9b_555876ac1fd1
  3a215796_4cd6_7df2_7e23_3daf458a843b["ensureContent()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| 3a215796_4cd6_7df2_7e23_3daf458a843b
  28841692_90a0_d414_d295_b29438c4c6d4["encodeContent()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| 28841692_90a0_d414_d295_b29438c4c6d4
  e666ac6d_2728_929f_3299_1fd955e11ca6["Result()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| e666ac6d_2728_929f_3299_1fd955e11ca6
  d38dae55_28ac_ee80_966b_d3c9e1696ed7["handlerRemoved()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| d38dae55_28ac_ee80_966b_d3c9e1696ed7
  8629595d_fbce_8577_96ab_7e8843366fc3["channelInactive()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| 8629595d_fbce_8577_96ab_7e8843366fc3
  de511860_2350_ce16_d9dc_d811d15774e2["cleanup()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522 -->|method| de511860_2350_ce16_d9dc_d811d15774e2

Relationship Graph

Source Code

codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java lines 57–383

public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpRequest, HttpObject> {

    private enum State {
        PASS_THROUGH,
        AWAIT_HEADERS,
        AWAIT_CONTENT
    }

    private static final CharSequence ZERO_LENGTH_HEAD = "HEAD";
    private static final CharSequence ZERO_LENGTH_CONNECT = "CONNECT";

    private final Queue<CharSequence> acceptEncodingQueue = new ArrayDeque<CharSequence>();
    private EmbeddedChannel encoder;
    private State state = State.AWAIT_HEADERS;

    public HttpContentEncoder() {
        super(HttpRequest.class, HttpObject.class);
    }

    @Override
    public boolean acceptOutboundMessage(Object msg) throws Exception {
        return msg instanceof HttpContent || msg instanceof HttpResponse;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) throws Exception {
        CharSequence acceptEncoding;
        List<String> acceptEncodingHeaders = msg.headers().getAll(ACCEPT_ENCODING);
        switch (acceptEncodingHeaders.size()) {
        case 0:
            acceptEncoding = HttpContentDecoder.IDENTITY;
            break;
        case 1:
            acceptEncoding = acceptEncodingHeaders.get(0);
            break;
        default:
            // Multiple message-header fields https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
            acceptEncoding = StringUtil.join(",", acceptEncodingHeaders);
            break;
        }

        HttpMethod method = msg.method();
        if (HttpMethod.HEAD.equals(method)) {
            acceptEncoding = ZERO_LENGTH_HEAD;
        } else if (HttpMethod.CONNECT.equals(method)) {
            acceptEncoding = ZERO_LENGTH_CONNECT;
        }

        acceptEncodingQueue.add(acceptEncoding);
        out.add(ReferenceCountUtil.retain(msg));
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception {
        final boolean isFull = msg instanceof HttpResponse && msg instanceof LastHttpContent;
        switch (state) {
            case AWAIT_HEADERS: {
                ensureHeaders(msg);
                assert encoder == null;

                final HttpResponse res = (HttpResponse) msg;
                final int code = res.status().code();
                final HttpStatusClass codeClass = res.status().codeClass();
                final CharSequence acceptEncoding;
                if (codeClass == HttpStatusClass.INFORMATIONAL) {
                    // We need to not poll the encoding when response with 1xx codes as another response will follow
                    // for the issued request.
                    // See https://github.com/netty/netty/issues/12904 and https://github.com/netty/netty/issues/4079
                    acceptEncoding = null;
                } else {
                    // Get the list of encodings accepted by the peer.
                    acceptEncoding = acceptEncodingQueue.poll();
                    if (acceptEncoding == null) {
                        throw new IllegalStateException("cannot send more responses than requests");
                    }
                }

                /*
                 * per rfc2616 4.3 Message Body
                 * All 1xx (informational), 204 (no content), and 304 (not modified) responses MUST NOT include a
                 * message-body. All other responses do include a message-body, although it MAY be of zero length.

Frequently Asked Questions

What is the HttpContentEncoder class?
HttpContentEncoder is a class in the netty codebase, defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java.
Where is HttpContentEncoder defined?
HttpContentEncoder is defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java at line 57.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free