Home / Class/ Http2ToHttpHeaderTranslator Class — netty Architecture

Http2ToHttpHeaderTranslator Class — netty Architecture

Architecture documentation for the Http2ToHttpHeaderTranslator class in HttpConversionUtil.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  32b1613b_3d60_d813_0e0f_c8db7d1be33b["Http2ToHttpHeaderTranslator"]
  c9afade8_db3e_8af8_f9ef_81beb249ccd9["HttpConversionUtil.java"]
  32b1613b_3d60_d813_0e0f_c8db7d1be33b -->|defined in| c9afade8_db3e_8af8_f9ef_81beb249ccd9
  f54b89d5_a79a_ff0b_818d_bab01aa6b6f3["Http2ToHttpHeaderTranslator()"]
  32b1613b_3d60_d813_0e0f_c8db7d1be33b -->|method| f54b89d5_a79a_ff0b_818d_bab01aa6b6f3
  9719360f_4072_b46c_34c6_ba4c95f8f01d["translateHeaders()"]
  32b1613b_3d60_d813_0e0f_c8db7d1be33b -->|method| 9719360f_4072_b46c_34c6_ba4c95f8f01d

Relationship Graph

Source Code

codec-http2/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java lines 664–734

    private static final class Http2ToHttpHeaderTranslator {
        /**
         * Translations from HTTP/2 header name to the HTTP/1.x equivalent.
         */
        private static final CharSequenceMap<AsciiString>
            REQUEST_HEADER_TRANSLATIONS = new CharSequenceMap<AsciiString>();
        private static final CharSequenceMap<AsciiString>
            RESPONSE_HEADER_TRANSLATIONS = new CharSequenceMap<AsciiString>();
        static {
            RESPONSE_HEADER_TRANSLATIONS.add(Http2Headers.PseudoHeaderName.AUTHORITY.value(),
                            HttpHeaderNames.HOST);
            RESPONSE_HEADER_TRANSLATIONS.add(Http2Headers.PseudoHeaderName.SCHEME.value(),
                            ExtensionHeaderNames.SCHEME.text());
            REQUEST_HEADER_TRANSLATIONS.add(RESPONSE_HEADER_TRANSLATIONS);
            RESPONSE_HEADER_TRANSLATIONS.add(Http2Headers.PseudoHeaderName.PATH.value(),
                            ExtensionHeaderNames.PATH.text());
        }

        private final int streamId;
        private final HttpHeaders output;
        private final CharSequenceMap<AsciiString> translations;

        /**
         * Create a new instance
         *
         * @param output The HTTP/1.x headers object to store the results of the translation
         * @param request if {@code true}, translates headers using the request translation map. Otherwise uses the
         *        response translation map.
         */
        Http2ToHttpHeaderTranslator(int streamId, HttpHeaders output, boolean request) {
            this.streamId = streamId;
            this.output = output;
            translations = request ? REQUEST_HEADER_TRANSLATIONS : RESPONSE_HEADER_TRANSLATIONS;
        }

        void translateHeaders(Iterable<Entry<CharSequence, CharSequence>> inputHeaders) throws Http2Exception {
            // lazily created as needed
            StringBuilder cookies = null;

            for (Entry<CharSequence, CharSequence> entry : inputHeaders) {
                final CharSequence name = entry.getKey();
                final CharSequence value = entry.getValue();
                AsciiString translatedName = translations.get(name);
                if (translatedName != null) {
                    output.add(translatedName, AsciiString.of(value));
                } else if (!Http2Headers.PseudoHeaderName.isPseudoHeader(name)) {
                    // https://tools.ietf.org/html/rfc7540#section-8.1.2.3
                    // All headers that start with ':' are only valid in HTTP/2 context
                    if (name.length() == 0 || name.charAt(0) == ':') {
                        throw streamError(streamId, PROTOCOL_ERROR,
                                "Invalid HTTP/2 header '%s' encountered in translation to HTTP/1.x", name);
                    }
                    if (COOKIE.equals(name)) {
                        // combine the cookie values into 1 header entry.
                        // https://tools.ietf.org/html/rfc7540#section-8.1.2.5
                        if (cookies == null) {
                            cookies = InternalThreadLocalMap.get().stringBuilder();
                        } else if (cookies.length() > 0) {
                            cookies.append("; ");
                        }
                        cookies.append(value);
                    } else {
                        output.add(name, value);
                    }
                }
            }
            if (cookies != null) {
                output.add(COOKIE, cookies.toString());
            }
        }
    }

Frequently Asked Questions

What is the Http2ToHttpHeaderTranslator class?
Http2ToHttpHeaderTranslator is a class in the netty codebase, defined in codec-http2/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java.
Where is Http2ToHttpHeaderTranslator defined?
Http2ToHttpHeaderTranslator is defined in codec-http2/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java at line 664.

Analyze Your Own Codebase

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

Try Supermodel Free