Example usage for io.netty.handler.codec.http HttpUtil setKeepAlive

List of usage examples for io.netty.handler.codec.http HttpUtil setKeepAlive

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpUtil setKeepAlive.

Prototype

public static void setKeepAlive(HttpHeaders h, HttpVersion httpVersion, boolean keepAlive) 

Source Link

Document

Sets the value of the "Connection" header depending on the protocol version of the specified message.

Usage

From source file:com.linecorp.armeria.internal.ArmeriaHttpUtil.java

License:Apache License

/**
 * Translate and add HTTP/2 headers to HTTP/1.x headers.
 *
 * @param streamId The stream associated with {@code sourceHeaders}.
 * @param inputHeaders The HTTP/2 headers to convert.
 * @param outputHeaders The object which will contain the resulting HTTP/1.x headers..
 * @param httpVersion What HTTP/1.x version {@code outputHeaders} should be treated as
 *                    when doing the conversion.
 * @param isTrailer {@code true} if {@code outputHeaders} should be treated as trailing headers.
 *                  {@code false} otherwise.
 * @param isRequest {@code true} if the {@code outputHeaders} will be used in a request message.
 *                  {@code false} for response message.
 *
 * @throws Http2Exception If not all HTTP/2 headers can be translated to HTTP/1.x.
 *///  w w  w.  j av  a  2  s.c  o m
public static void toNettyHttp1(int streamId, HttpHeaders inputHeaders,
        io.netty.handler.codec.http.HttpHeaders outputHeaders, HttpVersion httpVersion, boolean isTrailer,
        boolean isRequest) throws Http2Exception {

    final CharSequenceMap translations = isRequest ? REQUEST_HEADER_TRANSLATIONS : RESPONSE_HEADER_TRANSLATIONS;
    StringJoiner cookieJoiner = null;
    try {
        for (Entry<AsciiString, String> entry : inputHeaders) {
            final AsciiString name = entry.getKey();
            final String value = entry.getValue();
            final AsciiString translatedName = translations.get(name);
            if (translatedName != null) {
                outputHeaders.add(translatedName, value);
                continue;
            }

            // https://tools.ietf.org/html/rfc7540#section-8.1.2.3
            if (name.isEmpty() || HTTP2_TO_HTTP_HEADER_BLACKLIST.contains(name)) {
                continue;
            }

            if (HttpHeaderNames.COOKIE.equals(name)) {
                // combine the cookie values into 1 header entry.
                // https://tools.ietf.org/html/rfc7540#section-8.1.2.5
                if (cookieJoiner == null) {
                    cookieJoiner = new StringJoiner(COOKIE_SEPARATOR);
                }
                COOKIE_SPLITTER.split(value).forEach(cookieJoiner::add);
            } else {
                outputHeaders.add(name, value);
            }
        }

        if (cookieJoiner != null && cookieJoiner.length() != 0) {
            outputHeaders.add(HttpHeaderNames.COOKIE, cookieJoiner.toString());
        }
    } catch (Throwable t) {
        throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
    }

    if (!isTrailer) {
        HttpUtil.setKeepAlive(outputHeaders, httpVersion, true);
    }
}

From source file:com.linecorp.armeria.internal.http.ArmeriaHttpUtil.java

License:Apache License

/**
 * Translate and add HTTP/2 headers to HTTP/1.x headers.
 *
 * @param streamId The stream associated with {@code sourceHeaders}.
 * @param inputHeaders The HTTP/2 headers to convert.
 * @param outputHeaders The object which will contain the resulting HTTP/1.x headers..
 * @param httpVersion What HTTP/1.x version {@code outputHeaders} should be treated as when doing the conversion.
 * @param isTrailer {@code true} if {@code outputHeaders} should be treated as trailing headers.
 *                  {@code false} otherwise.
 * @param isRequest {@code true} if the {@code outputHeaders} will be used in a request message.
 *                  {@code false} for response message.
 *
 * @throws Http2Exception If not all HTTP/2 headers can be translated to HTTP/1.x.
 *///from  ww  w . j  a  v  a  2s  .c  om
public static void toNettyHttp1(int streamId, HttpHeaders inputHeaders,
        io.netty.handler.codec.http.HttpHeaders outputHeaders, HttpVersion httpVersion, boolean isTrailer,
        boolean isRequest) throws Http2Exception {

    final Http2ToHttpHeaderTranslator translator = new Http2ToHttpHeaderTranslator(outputHeaders, isRequest);
    try {
        for (Entry<AsciiString, String> entry : inputHeaders) {
            translator.translate(entry);
        }
    } catch (Throwable t) {
        throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
    }

    outputHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
    outputHeaders.remove(HttpHeaderNames.TRAILER);
    if (!isTrailer) {
        HttpUtil.setKeepAlive(outputHeaders, httpVersion, true);
    }
}