Home / Class/ Decoder Class — netty Architecture

Decoder Class — netty Architecture

Architecture documentation for the Decoder class in HttpClientCodec.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  71405c1a_689d_5893_0dd0_554c0d86697f["Decoder"]
  deb34d0e_cce7_d0ba_857f_8bfb8dcec181["HttpClientCodec.java"]
  71405c1a_689d_5893_0dd0_554c0d86697f -->|defined in| deb34d0e_cce7_d0ba_857f_8bfb8dcec181
  1c7ae380_cd85_ed6a_3c22_fc7645e3db9f["Decoder()"]
  71405c1a_689d_5893_0dd0_554c0d86697f -->|method| 1c7ae380_cd85_ed6a_3c22_fc7645e3db9f
  e573c3d9_a2d1_8d22_f8de_89836de09966["decode()"]
  71405c1a_689d_5893_0dd0_554c0d86697f -->|method| e573c3d9_a2d1_8d22_f8de_89836de09966
  83499358_8d03_a558_1357_5013330d43f6["decrement()"]
  71405c1a_689d_5893_0dd0_554c0d86697f -->|method| 83499358_8d03_a558_1357_5013330d43f6
  51f6d3de_fe54_872f_014f_2ef92399fa0e["isContentAlwaysEmpty()"]
  71405c1a_689d_5893_0dd0_554c0d86697f -->|method| 51f6d3de_fe54_872f_014f_2ef92399fa0e
  fd1300b0_2e73_299f_7583_1e44118f7a58["channelInactive()"]
  71405c1a_689d_5893_0dd0_554c0d86697f -->|method| fd1300b0_2e73_299f_7583_1e44118f7a58

Relationship Graph

Source Code

codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java lines 302–421

    private final class Decoder extends HttpResponseDecoder {
        Decoder(HttpDecoderConfig config) {
            super(config);
        }

        @Override
        protected void decode(
                ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
            if (done) {
                int readable = actualReadableBytes();
                if (readable == 0) {
                    // if non is readable just return null
                    // https://github.com/netty/netty/issues/1159
                    return;
                }
                out.add(buffer.readBytes(readable));
            } else {
                int oldSize = out.size();
                super.decode(ctx, buffer, out);
                if (failOnMissingResponse) {
                    int size = out.size();
                    for (int i = oldSize; i < size; i++) {
                        decrement(out.get(i));
                    }
                }
            }
        }

        private void decrement(Object msg) {
            if (msg == null) {
                return;
            }

            // check if it's an Header and its transfer encoding is not chunked.
            if (msg instanceof LastHttpContent) {
                requestResponseCounter.decrementAndGet();
            }
        }

        @Override
        protected boolean isContentAlwaysEmpty(HttpMessage msg) {
            // Get the method of the HTTP request that corresponds to the
            // current response.
            //
            // Even if we do not use the method to compare we still need to poll it to ensure we keep
            // request / response pairs in sync.
            HttpMethod method = queue.poll();

            final HttpResponseStatus status = ((HttpResponse) msg).status();
            final HttpStatusClass statusClass = status.codeClass();
            final int statusCode = status.code();
            if (statusClass == HttpStatusClass.INFORMATIONAL) {
                // An informational response should be excluded from paired comparison.
                // Just delegate to super method which has all the needed handling.
                return super.isContentAlwaysEmpty(msg);
            }

            // If the remote peer did for example send multiple responses for one request (which is not allowed per
            // spec but may still be possible) method will be null so guard against it.
            if (method != null) {
                char firstChar = method.name().charAt(0);
                switch (firstChar) {
                    case 'H':
                        // According to 4.3, RFC2616:
                        // All responses to the HEAD request method MUST NOT include a
                        // message-body, even though the presence of entity-header fields
                        // might lead one to believe they do.
                        if (HttpMethod.HEAD.equals(method)) {
                            return true;

                            // The following code was inserted to work around the servers
                            // that behave incorrectly.  It has been commented out
                            // because it does not work with well behaving servers.
                            // Please note, even if the 'Transfer-Encoding: chunked'
                            // header exists in the HEAD response, the response should
                            // have absolutely no content.
                            //
                            //// Interesting edge case:
                            //// Some poorly implemented servers will send a zero-byte
                            //// chunk if Transfer-Encoding of the response is 'chunked'.
                            ////

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free