Search in sources :

Example 26 with DefaultHttp2Headers

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

the class Http2ServerConnection method sendPush.

synchronized void sendPush(int streamId, String host, HttpMethod method, MultiMap headers, String path, Handler<AsyncResult<HttpServerResponse>> completionHandler) {
    Http2Headers headers_ = new DefaultHttp2Headers();
    if (method == HttpMethod.OTHER) {
        throw new IllegalArgumentException("Cannot push HttpMethod.OTHER");
    } else {
        headers_.method(method.name());
    }
    headers_.path(path);
    headers_.scheme(isSsl() ? "https" : "http");
    if (host != null) {
        headers_.authority(host);
    }
    if (headers != null) {
        headers.forEach(header -> headers_.add(header.getKey(), header.getValue()));
    }
    handler.writePushPromise(streamId, headers_, new Handler<AsyncResult<Integer>>() {

        @Override
        public void handle(AsyncResult<Integer> ar) {
            if (ar.succeeded()) {
                synchronized (Http2ServerConnection.this) {
                    int promisedStreamId = ar.result();
                    String contentEncoding = HttpUtils.determineContentEncoding(headers_);
                    Http2Stream promisedStream = handler.connection().stream(promisedStreamId);
                    boolean writable = handler.encoder().flowController().isWritable(promisedStream);
                    Push push = new Push(promisedStream, contentEncoding, method, path, writable, completionHandler);
                    streams.put(promisedStreamId, push);
                    if (maxConcurrentStreams == null || concurrentStreams < maxConcurrentStreams) {
                        concurrentStreams++;
                        context.executeFromIO(push::complete);
                    } else {
                        pendingPushes.add(push);
                    }
                }
            } else {
                context.executeFromIO(() -> {
                    completionHandler.handle(Future.failedFuture(ar.cause()));
                });
            }
        }
    });
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Http2Stream(io.netty.handler.codec.http2.Http2Stream) AsyncResult(io.vertx.core.AsyncResult)

Example 27 with DefaultHttp2Headers

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

the class Http2ServerTest method testURI.

@Test
public void testURI() throws Exception {
    server.requestHandler(req -> {
        assertEquals("/some/path", req.path());
        assertEquals("foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.query());
        assertEquals("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.uri());
        assertEquals("http://whatever.com/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.absoluteURI());
        assertEquals("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.getHeader(":path"));
        assertEquals("whatever.com", req.host());
        MultiMap params = req.params();
        Set<String> names = params.names();
        assertEquals(2, names.size());
        assertTrue(names.contains("foo"));
        assertTrue(names.contains("bar"));
        assertEquals("foo_value", params.get("foo"));
        assertEquals(Collections.singletonList("foo_value"), params.getAll("foo"));
        assertEquals("bar_value_2", params.get("bar"));
        assertEquals(Arrays.asList("bar_value_1", "bar_value_2"), params.getAll("bar"));
        testComplete();
    });
    startServer();
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        int id = request.nextStreamId();
        Http2Headers headers = new DefaultHttp2Headers().method("GET").scheme("http").authority("whatever.com").path("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2");
        request.encoder.writeHeaders(request.context, id, headers, 0, true, request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    await();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) MultiMap(io.vertx.core.MultiMap) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Test(org.junit.Test)

Example 28 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.

the class GrpcHttp2HeadersDecoderTest method decode_responseHeaders.

@Test
public void decode_responseHeaders() throws Http2Exception {
    Http2HeadersDecoder decoder = new GrpcHttp2ClientHeadersDecoder(DEFAULT_MAX_HEADER_LIST_SIZE);
    Http2HeadersEncoder encoder = new DefaultHttp2HeadersEncoder(NEVER_SENSITIVE);
    Http2Headers headers = new DefaultHttp2Headers(false);
    headers.add(of(":status"), of("200")).add(of("custom"), of("header"));
    encodedHeaders = Unpooled.buffer();
    encoder.encodeHeaders(1, /* randomly chosen */
    headers, encodedHeaders);
    Http2Headers decodedHeaders = decoder.decodeHeaders(3, /* randomly chosen */
    encodedHeaders);
    assertEquals(headers.get(of(":status")), decodedHeaders.get(of(":status")));
    assertEquals(headers.get(of("custom")), decodedHeaders.get(of("custom")));
    assertEquals(headers.size(), decodedHeaders.size());
    String toString = decodedHeaders.toString();
    assertContainsKeyAndValue(toString, ":status", decodedHeaders.get(of(":status")));
    assertContainsKeyAndValue(toString, "custom", decodedHeaders.get(of("custom")));
}
Also used : DefaultHttp2HeadersEncoder(io.netty.handler.codec.http2.DefaultHttp2HeadersEncoder) Http2HeadersEncoder(io.netty.handler.codec.http2.Http2HeadersEncoder) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) GrpcHttp2ClientHeadersDecoder(io.grpc.netty.GrpcHttp2HeadersDecoder.GrpcHttp2ClientHeadersDecoder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Http2HeadersDecoder(io.netty.handler.codec.http2.Http2HeadersDecoder) DefaultHttp2HeadersEncoder(io.netty.handler.codec.http2.DefaultHttp2HeadersEncoder) Test(org.junit.Test)

Example 29 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.

the class NettyServerStreamTest method closeWithErrorBeforeClientHalfCloseShouldSucceed.

@Test
public void closeWithErrorBeforeClientHalfCloseShouldSucceed() throws Exception {
    ListMultimap<CharSequence, CharSequence> expectedHeaders = ImmutableListMultimap.copyOf(new DefaultHttp2Headers().status(new AsciiString("200")).set(new AsciiString("content-type"), new AsciiString("application/grpc")).set(new AsciiString("grpc-status"), new AsciiString("1")));
    // Error is sent on wire and ends the stream
    stream().close(Status.CANCELLED, trailers);
    ArgumentCaptor<SendResponseHeadersCommand> sendHeadersCap = ArgumentCaptor.forClass(SendResponseHeadersCommand.class);
    verify(writeQueue).enqueue(sendHeadersCap.capture(), eq(true));
    SendResponseHeadersCommand sendHeaders = sendHeadersCap.getValue();
    assertThat(sendHeaders.stream()).isSameAs(stream.transportState());
    assertThat(ImmutableListMultimap.copyOf(sendHeaders.headers())).containsExactlyEntriesIn(expectedHeaders);
    assertThat(sendHeaders.endOfStream()).isTrue();
    verifyZeroInteractions(serverListener);
    // Sending complete. Listener gets closed()
    stream().transportState().complete();
    verify(serverListener).closed(Status.OK);
    verifyZeroInteractions(serverListener);
}
Also used : DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 30 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.

the class NettyServerStreamTest method writeMessageShouldSendResponse.

@Test
public void writeMessageShouldSendResponse() throws Exception {
    ListMultimap<CharSequence, CharSequence> expectedHeaders = ImmutableListMultimap.copyOf(new DefaultHttp2Headers().status(Utils.STATUS_OK).set(Utils.CONTENT_TYPE_HEADER, Utils.CONTENT_TYPE_GRPC));
    stream.writeHeaders(new Metadata());
    ArgumentCaptor<SendResponseHeadersCommand> sendHeadersCap = ArgumentCaptor.forClass(SendResponseHeadersCommand.class);
    verify(writeQueue).enqueue(sendHeadersCap.capture(), eq(true));
    SendResponseHeadersCommand sendHeaders = sendHeadersCap.getValue();
    assertThat(sendHeaders.stream()).isSameAs(stream.transportState());
    assertThat(ImmutableListMultimap.copyOf(sendHeaders.headers())).containsExactlyEntriesIn(expectedHeaders);
    assertThat(sendHeaders.endOfStream()).isFalse();
    byte[] msg = smallMessage();
    stream.writeMessage(new ByteArrayInputStream(msg));
    stream.flush();
    verify(writeQueue).enqueue(eq(new SendGrpcFrameCommand(stream.transportState(), messageFrame(MESSAGE), false)), isA(ChannelPromise.class), eq(true));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Metadata(io.grpc.Metadata) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)36 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)32 AsciiString (io.netty.util.AsciiString)32 ByteBuf (io.netty.buffer.ByteBuf)23 Http2Headers (io.netty.handler.codec.http2.Http2Headers)23 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)17 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)12 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)11 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)9 Metadata (io.grpc.Metadata)7 ChannelFuture (io.netty.channel.ChannelFuture)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)5 Channel (io.netty.channel.Channel)5 Http2Settings (io.netty.handler.codec.http2.Http2Settings)5 ChannelInitializer (io.netty.channel.ChannelInitializer)4 ChannelPipeline (io.netty.channel.ChannelPipeline)4 EventLoopGroup (io.netty.channel.EventLoopGroup)4