Home / Function/ encode() — netty Function Reference

encode() — netty Function Reference

Architecture documentation for the encode() function in HttpContentEncoder.java from the netty codebase.

Function java ProtocolCodecs HTTP calls 6 called by 1

Entity Profile

Dependency Diagram

graph TD
  5f451408_0805_44d2_06ca_843854c94345["encode()"]
  77d3b233_ef0d_c6ed_64de_3e425b740522["HttpContentEncoder"]
  5f451408_0805_44d2_06ca_843854c94345 -->|defined in| 77d3b233_ef0d_c6ed_64de_3e425b740522
  28841692_90a0_d414_d295_b29438c4c6d4["encodeContent()"]
  28841692_90a0_d414_d295_b29438c4c6d4 -->|calls| 5f451408_0805_44d2_06ca_843854c94345
  c6773d44_960e_8c19_4b9b_555876ac1fd1["ensureHeaders()"]
  5f451408_0805_44d2_06ca_843854c94345 -->|calls| c6773d44_960e_8c19_4b9b_555876ac1fd1
  1f658e60_1506_b115_e30f_7101ef756c81["isPassthru()"]
  5f451408_0805_44d2_06ca_843854c94345 -->|calls| 1f658e60_1506_b115_e30f_7101ef756c81
  3a215796_4cd6_7df2_7e23_3daf458a843b["ensureContent()"]
  5f451408_0805_44d2_06ca_843854c94345 -->|calls| 3a215796_4cd6_7df2_7e23_3daf458a843b
  f8b06062_b563_2e15_a80d_cec486725f70["encodeFullResponse()"]
  5f451408_0805_44d2_06ca_843854c94345 -->|calls| f8b06062_b563_2e15_a80d_cec486725f70
  28841692_90a0_d414_d295_b29438c4c6d4["encodeContent()"]
  5f451408_0805_44d2_06ca_843854c94345 -->|calls| 28841692_90a0_d414_d295_b29438c4c6d4
  6a29fc65_1383_f620_e18e_44a878265daa["fetchEncoderOutput()"]
  5f451408_0805_44d2_06ca_843854c94345 -->|calls| 6a29fc65_1383_f620_e18e_44a878265daa
  style 5f451408_0805_44d2_06ca_843854c94345 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java lines 109–228

    @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.
                 *
                 * 9.4 HEAD
                 * The HEAD method is identical to GET except that the server MUST NOT return a message-body
                 * in the response.
                 *
                 * Also we should pass through HTTP/1.0 as transfer-encoding: chunked is not supported.
                 *
                 * See https://github.com/netty/netty/issues/5382
                 */
                if (isPassthru(res.protocolVersion(), code, acceptEncoding)) {
                    out.add(ReferenceCountUtil.retain(res));
                    if (!isFull) {
                        // Pass through all following contents.
                        state = State.PASS_THROUGH;
                    }
                    break;
                }

                if (isFull) {
                    // Pass through the full response with empty content and continue waiting for the next resp.
                    if (!((ByteBufHolder) res).content().isReadable()) {
                        out.add(ReferenceCountUtil.retain(res));
                        break;
                    }
                }

                // Prepare to encode the content.
                final Result result = beginEncode(res, acceptEncoding.toString());

                // If unable to encode, pass through.
                if (result == null) {
                    out.add(ReferenceCountUtil.retain(res));
                    if (!isFull) {
                        // Pass through all following contents.
                        state = State.PASS_THROUGH;
                    }
                    break;
                }

                encoder = result.contentEncoder();

                // Encode the content and remove or replace the existing headers
                // so that the message looks like a decoded message.
                res.headers().set(HttpHeaderNames.CONTENT_ENCODING, result.targetContentEncoding());

                // Output the rewritten response.
                if (isFull) {
                    // Convert full message into unfull one.
                    HttpResponse newRes = new DefaultHttpResponse(res.protocolVersion(), res.status());
                    newRes.headers().set(res.headers());
                    out.add(newRes);

Subdomains

Called By

Frequently Asked Questions

What does encode() do?
encode() is a function in the netty codebase, defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java.
Where is encode() defined?
encode() is defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java at line 109.
What does encode() call?
encode() calls 6 function(s): encodeContent, encodeFullResponse, ensureContent, ensureHeaders, fetchEncoderOutput, isPassthru.
What calls encode()?
encode() is called by 1 function(s): encodeContent.

Analyze Your Own Codebase

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

Try Supermodel Free