Java Code Examples for io.netty.handler.codec.http2.Http2Headers#contains()

The following examples show how to use io.netty.handler.codec.http2.Http2Headers#contains() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientHttp2ObjectEncoder.java    From armeria with Apache License 2.0 6 votes vote down vote up
private Http2Headers convertHeaders(HttpHeaders inputHeaders) {
    final Http2Headers outputHeaders = ArmeriaHttpUtil.toNettyHttp2ClientHeader(inputHeaders);

    if (!outputHeaders.contains(HttpHeaderNames.USER_AGENT)) {
        outputHeaders.add(HttpHeaderNames.USER_AGENT, HttpHeaderUtil.USER_AGENT.toString());
    }

    if (!outputHeaders.contains(HttpHeaderNames.SCHEME)) {
        outputHeaders.add(HttpHeaderNames.SCHEME, protocol.isTls() ? SessionProtocol.HTTPS.uriText()
                                                                   : SessionProtocol.HTTP.uriText());
    }

    if (!outputHeaders.contains(HttpHeaderNames.AUTHORITY)) {
        final InetSocketAddress remoteAddress = (InetSocketAddress) channel().remoteAddress();
        outputHeaders.add(HttpHeaderNames.AUTHORITY,
                          ArmeriaHttpUtil.authorityHeader(remoteAddress.getHostName(),
                                                          remoteAddress.getPort(), protocol.defaultPort()));
    }
    return outputHeaders;
}
 
Example 2
Source File: ServerHttp2ObjectEncoder.java    From armeria with Apache License 2.0 6 votes vote down vote up
private Http2Headers convertHeaders(HttpHeaders inputHeaders, boolean isTrailersEmpty) {
    final Http2Headers outHeaders = ArmeriaHttpUtil.toNettyHttp2ServerHeaders(inputHeaders);
    if (!isTrailersEmpty && outHeaders.contains(HttpHeaderNames.CONTENT_LENGTH)) {
        // We don't apply chunked encoding when the content-length header is set, which would
        // prevent the trailers from being sent so we go ahead and remove content-length to force
        // chunked encoding.
        outHeaders.remove(HttpHeaderNames.CONTENT_LENGTH);
    }

    if (enableServerHeader && !outHeaders.contains(HttpHeaderNames.SERVER)) {
        outHeaders.add(HttpHeaderNames.SERVER, ArmeriaHttpUtil.SERVER_HEADER);
    }

    if (enableDateHeader && !outHeaders.contains(HttpHeaderNames.DATE)) {
        outHeaders.add(HttpHeaderNames.DATE, HttpTimestampSupplier.currentTime());
    }
    return outHeaders;
}
 
Example 3
Source File: H2ToStH1ServerDuplexHandler.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private NettyH2HeadersToHttpHeaders h2HeadersToH1HeadersServer(Http2Headers h2Headers,
                                                               @Nullable HttpRequestMethod httpMethod) {
    CharSequence value = h2Headers.getAndRemove(AUTHORITY.value());
    if (value != null) {
        h2Headers.set(HOST, value);
    }
    h2Headers.remove(Http2Headers.PseudoHeaderName.SCHEME.value());
    h2HeadersSanitizeForH1(h2Headers);
    if (httpMethod != null && !h2Headers.contains(HttpHeaderNames.CONTENT_LENGTH) &&
            clientMaySendPayloadBodyFor(httpMethod)) {
        h2Headers.add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    }
    return new NettyH2HeadersToHttpHeaders(h2Headers, headersFactory.validateCookies());
}
 
Example 4
Source File: ArmeriaHttpUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the specified Netty HTTP/2 into Armeria HTTP/2 headers.
 */
public static HttpHeaders toArmeria(Http2Headers headers, boolean request, boolean endOfStream) {
    final HttpHeadersBuilder builder;
    if (request) {
        builder = headers.contains(HttpHeaderNames.METHOD) ? RequestHeaders.builder()
                                                           : HttpHeaders.builder();
    } else {
        builder = headers.contains(HttpHeaderNames.STATUS) ? ResponseHeaders.builder()
                                                           : HttpHeaders.builder();
    }

    toArmeria(builder, headers, endOfStream);
    return builder.build();
}
 
Example 5
Source File: ArmeriaHttpUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void toNettyHttp2Client(HttpHeaders inputHeaders, Http2Headers outputHeaders,
                                       boolean isTrailer) {
    for (Entry<AsciiString, String> entry : inputHeaders) {
        final AsciiString name = entry.getKey();
        final String value = entry.getValue();
        if (HTTP_TO_HTTP2_HEADER_BLACKLIST.contains(name)) {
            continue;
        }

        if (isTrailer && isTrailerBlacklisted(name)) {
            continue;
        }

        outputHeaders.add(name, value);
    }

    if (!outputHeaders.contains(HttpHeaderNames.COOKIE)) {
        return;
    }

    // Split up cookies to allow for better compression.
    // https://tools.ietf.org/html/rfc7540#section-8.1.2.5
    final List<CharSequence> cookies = outputHeaders.getAllAndRemove(HttpHeaderNames.COOKIE);
    for (CharSequence c : cookies) {
        outputHeaders.add(HttpHeaderNames.COOKIE, COOKIE_SPLITTER.split(c));
    }
}
 
Example 6
Source File: Http2CorsHandler.java    From xrpc with Apache License 2.0 4 votes vote down vote up
private boolean isPreflight(final Http2Headers headers) {
  return headers.method().toString().equals(HttpMethod.OPTIONS.toString())
      && headers.contains(HttpHeaderNames.ORIGIN)
      && headers.contains(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD);
}
 
Example 7
Source File: Http2RequestDecoder.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding,
                          boolean endOfStream) throws Http2Exception {
    keepAliveChannelRead();
    DecodedHttpRequest req = requests.get(streamId);
    if (req == null) {
        // Validate the method.
        final CharSequence method = headers.method();
        if (method == null) {
            writeErrorResponse(ctx, streamId, HttpResponseStatus.BAD_REQUEST, DATA_MISSING_METHOD);
            return;
        }
        if (!HttpMethod.isSupported(method.toString())) {
            writeErrorResponse(ctx, streamId, HttpResponseStatus.METHOD_NOT_ALLOWED,
                               DATA_UNSUPPORTED_METHOD);
            return;
        }

        // Validate the 'content-length' header if exists.
        final boolean contentEmpty;
        if (headers.contains(HttpHeaderNames.CONTENT_LENGTH)) {
            final long contentLength = headers.getLong(HttpHeaderNames.CONTENT_LENGTH, -1L);
            if (contentLength < 0) {
                writeErrorResponse(ctx, streamId, HttpResponseStatus.BAD_REQUEST,
                                   DATA_INVALID_CONTENT_LENGTH);
                return;
            }
            contentEmpty = contentLength == 0;
        } else {
            contentEmpty = true;
        }

        if (!handle100Continue(ctx, streamId, headers)) {
            writeErrorResponse(ctx, streamId, HttpResponseStatus.EXPECTATION_FAILED, null);
            return;
        }

        req = new DecodedHttpRequest(ctx.channel().eventLoop(), ++nextId, streamId,
                                     ArmeriaHttpUtil.toArmeriaRequestHeaders(ctx, headers, endOfStream,
                                                                             scheme, cfg),
                                     true, inboundTrafficController,
                                     // FIXME(trustin): Use a different maxRequestLength
                                     //                 for a different host.
                                     cfg.defaultVirtualHost().maxRequestLength());

        // Close the request early when it is sure that there will be
        // neither content nor trailers.
        if (contentEmpty && endOfStream) {
            req.close();
        }

        requests.put(streamId, req);
        ctx.fireChannelRead(req);
    } else {
        try {
            req.write(ArmeriaHttpUtil.toArmeria(headers, true, endOfStream));
        } catch (Throwable t) {
            req.close(t);
            throw connectionError(INTERNAL_ERROR, t, "failed to consume a HEADERS frame");
        }
    }

    if (endOfStream) {
        req.close();
    }
}