Search in sources :

Example 91 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project vert.x by eclipse.

the class Http2ServerConnection method createRequest.

private Http2ServerRequest createRequest(int streamId, Http2Headers headers, boolean streamEnded) {
    Http2Stream stream = handler.connection().stream(streamId);
    String contentEncoding = options.isCompressionSupported() ? HttpUtils.determineContentEncoding(headers) : null;
    Http2ServerRequest request = new Http2ServerRequest(this, options.getTracingPolicy(), streamContextSupplier.get(), serverOrigin, headers, contentEncoding, streamEnded);
    request.isConnect = request.method() == HttpMethod.CONNECT;
    request.init(stream);
    return request;
}
Also used : Http2Stream(io.netty.handler.codec.http2.Http2Stream)

Example 92 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project rest.li by linkedin.

the class NettyRequestAdapter method toHttp2Headers.

/**
 * Extracts fields from a {@link Request} and construct a {@link Http2Headers} instance.
 *
 * @param request StreamRequest to extract fields from
 * @return a new instance of Http2Headers
 * @throws {@link Exception}
 */
public static <R extends Request> Http2Headers toHttp2Headers(R request) throws Exception {
    URI uri = request.getURI();
    URL url = new URL(uri.toString());
    String method = request.getMethod();
    String authority = url.getAuthority();
    String path = url.getFile();
    String scheme = uri.getScheme();
    // RFC 2616, section 5.1.2:
    // Note that the absolute path cannot be empty; if none is present in the original URI,
    // it MUST be given as "/" (the server root).
    path = path.isEmpty() ? "/" : path;
    final Http2Headers headers = new DefaultHttp2Headers().method(method).authority(authority).path(path).scheme(scheme);
    for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
        // Ignores HTTP/2 blacklisted headers
        if (HEADER_BLACKLIST.contains(entry.getKey().toLowerCase())) {
            continue;
        }
        // RFC 7540, section 8.1.2:
        // ... header field names MUST be converted to lowercase prior to their
        // encoding in HTTP/2.  A request or response containing uppercase
        // header field names MUST be treated as malformed (Section 8.1.2.6).
        String name = entry.getKey().toLowerCase();
        String value = entry.getValue();
        headers.set(name, value);
    }
    // Split up cookies to allow for better header compression
    headers.set(HttpHeaderNames.COOKIE, request.getCookies());
    return headers;
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL)

Example 93 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project rest.li by linkedin.

the class TestNettyRequestAdapter method testStreamToHttp2HeadersCookies.

@Test
public void testStreamToHttp2HeadersCookies() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    IntStream.range(0, 10).forEach(i -> streamRequestBuilder.addCookie(ANY_COOKIE));
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    List<CharSequence> cookies = headers.getAll(HttpHeaderNames.COOKIE);
    Assert.assertNotNull(cookies);
    Assert.assertEquals(cookies.size(), 10);
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 94 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project rest.li by linkedin.

the class TestNettyRequestAdapter method testStreamToHttp2HeadersBlacklist.

@Test
public void testStreamToHttp2HeadersBlacklist() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    HEADER_BLACKLIST.forEach(header -> streamRequestBuilder.addHeaderValue(header, ANY_HEADER));
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    HEADER_BLACKLIST.forEach(header -> Assert.assertFalse(headers.contains(header), header));
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 95 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project rest.li by linkedin.

the class TestNettyRequestAdapter method testStreamToHttp2HeadersPseudoHeaders.

@Test
public void testStreamToHttp2HeadersPseudoHeaders() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    Assert.assertEquals(headers.authority(), "localhost:8080");
    Assert.assertEquals(headers.method(), "GET");
    Assert.assertEquals(headers.path(), "/foo/bar?q=baz");
    Assert.assertEquals(headers.scheme(), "http");
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Aggregations

Http2Headers (io.netty.handler.codec.http2.Http2Headers)126 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)111 ByteBuf (io.netty.buffer.ByteBuf)103 Test (org.junit.Test)97 ChannelFuture (io.netty.channel.ChannelFuture)76 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)76 AsciiString (io.netty.util.AsciiString)58 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)58 AtomicReference (java.util.concurrent.atomic.AtomicReference)56 ByteArrayOutputStream (java.io.ByteArrayOutputStream)54 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)54 Channel (io.netty.channel.Channel)53 Collections (java.util.Collections)53 ChannelInitializer (io.netty.channel.ChannelInitializer)52 ChannelPipeline (io.netty.channel.ChannelPipeline)52 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)52 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)52 ApplicationProtocolNames (io.netty.handler.ssl.ApplicationProtocolNames)52 ApplicationProtocolNegotiationHandler (io.netty.handler.ssl.ApplicationProtocolNegotiationHandler)52 SslHandler (io.netty.handler.ssl.SslHandler)52