io.netty.handler.codec.http2.Http2Headers Java Examples

The following examples show how to use io.netty.handler.codec.http2.Http2Headers. 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: NettyServerHandlerTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void keepAliveEnforcer_sendingDataResetsCounters() throws Exception {
  permitKeepAliveWithoutCalls = false;
  permitKeepAliveTimeInNanos = TimeUnit.HOURS.toNanos(1);
  manualSetUp();

  createStream();
  Http2Headers headers = Utils.convertServerHeaders(new Metadata());
  ChannelFuture future = enqueue(
      SendResponseHeadersCommand.createHeaders(stream.transportState(), headers));
  future.get();
  for (int i = 0; i < 10; i++) {
    future = enqueue(
        new SendGrpcFrameCommand(stream.transportState(), content().retainedSlice(), false));
    future.get();
    channel().releaseOutbound();
    channelRead(pingFrame(false /* isAck */, 1L));
  }
  verifyWrite(never()).writeGoAway(eq(ctx()), eq(STREAM_ID),
      eq(Http2Error.ENHANCE_YOUR_CALM.code()), any(ByteBuf.class), any(ChannelPromise.class));
}
 
Example #2
Source File: Http2ServerCodecUnitTest.java    From xio with Apache License 2.0 6 votes vote down vote up
@Test
public void testFullRequest() throws Exception {
  outputReceived = new CountDownLatch(1);

  Http2Headers headers = new DefaultHttp2Headers().method("GET").path("/");
  Http2Request requestIn = Http2Request.build(1, headers, true);

  channel.writeInbound(requestIn);

  channel.runPendingTasks(); // blocks

  Uninterruptibles.awaitUninterruptibly(outputReceived);

  Request requestOut = requests.remove(0);

  assertNotNull(requestOut);
  assertTrue(requestOut instanceof FullRequest);
  assertEquals("h2", requestOut.version());
  assertEquals(HttpMethod.GET, requestOut.method());
  assertEquals("/", requestOut.path());
  assertFalse(requestOut.hasBody());
  assertNotNull(requestOut.body());
  assertEquals(0, requestOut.body().readableBytes());
  assertEquals(1, requestOut.streamId());
}
 
Example #3
Source File: NettyServerHandler.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private void respondWithHttpError(
    ChannelHandlerContext ctx, int streamId, int code, Status.Code statusCode, String msg) {
  Metadata metadata = new Metadata();
  metadata.put(InternalStatus.CODE_KEY, statusCode.toStatus());
  metadata.put(InternalStatus.MESSAGE_KEY, msg);
  byte[][] serialized = InternalMetadata.serialize(metadata);

  Http2Headers headers = new DefaultHttp2Headers(true, serialized.length / 2)
      .status("" + code)
      .set(CONTENT_TYPE_HEADER, "text/plain; encoding=utf-8");
  for (int i = 0; i < serialized.length; i += 2) {
    headers.add(new AsciiString(serialized[i], false), new AsciiString(serialized[i + 1], false));
  }
  encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise());
  ByteBuf msgBuf = ByteBufUtil.writeUtf8(ctx.alloc(), msg);
  encoder().writeData(ctx, streamId, msgBuf, 0, true, ctx.newPromise());
}
 
Example #4
Source File: H2PriorKnowledgeFeatureParityTest.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
private void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) {
    if (headers.isEndStream()) {
        ctx.write(new DefaultHttp2HeadersFrame(headers.headers(), true));
    } else {
        Http2Headers outHeaders = new DefaultHttp2Headers();
        if (headers.headers().contains(EXPECT, CONTINUE)) {
            if (headers.headers().contains(EXPECT_FAIL_HEADER)) {
                outHeaders.status(
                        io.netty.handler.codec.http.HttpResponseStatus.EXPECTATION_FAILED.codeAsText());
                ctx.write(new DefaultHttp2HeadersFrame(outHeaders, true));
                return;
            } else {
                outHeaders.status(io.netty.handler.codec.http.HttpResponseStatus.CONTINUE.codeAsText());
            }
        } else {
            outHeaders.status(io.netty.handler.codec.http.HttpResponseStatus.OK.codeAsText());
        }

        CharSequence contentType = headers.headers().get(CONTENT_TYPE);
        if (contentType != null) {
            outHeaders.add(CONTENT_TYPE, contentType);
        }
        outHeaders.add(HttpHeaderNames.COOKIE, headers.headers().getAll(HttpHeaderNames.COOKIE));
        ctx.write(new DefaultHttp2HeadersFrame(outHeaders));
    }
}
 
Example #5
Source File: ArmeriaHttpUtilTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void toArmeriaRequestHeaders() {
    final Http2Headers in = new DefaultHttp2Headers().set("a", "b");

    final InetSocketAddress socketAddress = new InetSocketAddress(36462);
    final Channel channel = mock(Channel.class);
    when(channel.localAddress()).thenReturn(socketAddress);

    final ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(channel);

    in.set(HttpHeaderNames.METHOD, "GET")
      .set(HttpHeaderNames.PATH, "/");
    // Request headers without pseudo headers.
    final RequestHeaders headers =
            ArmeriaHttpUtil.toArmeriaRequestHeaders(ctx, in, false, "https", serverConfig());
    assertThat(headers.scheme()).isEqualTo("https");
    assertThat(headers.authority()).isEqualTo("foo:36462");
}
 
Example #6
Source File: Http2Handler.java    From xrpc with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the given response body as "text/plain" to the given stream. Marks the response status
 * metric. Closes the stream after writing the response.
 */
private void writeResponse(
    ChannelHandlerContext ctx, int streamId, HttpResponseStatus status, ByteBuf body) {

  Preconditions.checkArgument(body != null, "body must not be null");

  markResponseStatus(ctx, status);

  Http2Headers headers = new DefaultHttp2Headers(true);
  // TODO(jkinkead): This should honor accept headers; we shouldn't send text/plain if the client
  // doesn't want it.
  headers.set(CONTENT_TYPE, "text/plain");
  headers.setInt(CONTENT_LENGTH, body.readableBytes());
  headers.status(status.codeAsText());

  writeResponse(ctx, streamId, headers, Optional.of(body));
}
 
Example #7
Source File: NettyServerHandlerTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void headersWithInvalidContentTypeShouldFail() throws Exception {
  manualSetUp();
  Http2Headers headers = new DefaultHttp2Headers()
      .method(HTTP_METHOD)
      .set(CONTENT_TYPE_HEADER, new AsciiString("application/bad", UTF_8))
      .set(TE_HEADER, TE_TRAILERS)
      .path(new AsciiString("/foo/bar"));
  ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
  channelRead(headersFrame);
  Http2Headers responseHeaders = new DefaultHttp2Headers()
      .set(InternalStatus.CODE_KEY.name(), String.valueOf(Code.INTERNAL.value()))
      .set(InternalStatus.MESSAGE_KEY.name(), "Content-Type 'application/bad' is not supported")
      .status("" + 415)
      .set(CONTENT_TYPE_HEADER, "text/plain; encoding=utf-8");

  verifyWrite()
      .writeHeaders(
          eq(ctx()),
          eq(STREAM_ID),
          eq(responseHeaders),
          eq(0),
          eq(false),
          any(ChannelPromise.class));
}
 
Example #8
Source File: Utils.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
public static Http2Headers convertClientHeaders(Metadata headers,
    AsciiString scheme,
    AsciiString defaultPath,
    AsciiString authority,
    AsciiString method,
    AsciiString userAgent) {
  Preconditions.checkNotNull(defaultPath, "defaultPath");
  Preconditions.checkNotNull(authority, "authority");
  Preconditions.checkNotNull(method, "method");

  // Discard any application supplied duplicates of the reserved headers
  headers.discardAll(CONTENT_TYPE_KEY);
  headers.discardAll(GrpcUtil.TE_HEADER);
  headers.discardAll(GrpcUtil.USER_AGENT_KEY);

  return GrpcHttp2OutboundHeaders.clientRequestHeaders(
      toHttp2Headers(headers),
      authority,
      defaultPath,
      method,
      scheme,
      userAgent);
}
 
Example #9
Source File: GrpcHttp2InboundHeadersTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void basicCorrectness() {
  Http2Headers headers = new GrpcHttp2RequestHeaders(1);
  headers.add(of(":method"), of("POST"));
  headers.add(of("content-type"), of("application/grpc+proto"));
  headers.add(of(":path"), of("/google.pubsub.v2.PublisherService/CreateTopic"));
  headers.add(of(":scheme"), of("https"));
  headers.add(of("te"), of("trailers"));
  headers.add(of(":authority"), of("pubsub.googleapis.com"));
  headers.add(of("foo"), of("bar"));

  assertEquals(7, headers.size());
  // Number of headers without the pseudo headers and 'te' header.
  assertEquals(2, ((GrpcHttp2InboundHeaders)headers).numHeaders());

  assertEquals(of("application/grpc+proto"), headers.get(of("content-type")));
  assertEquals(of("/google.pubsub.v2.PublisherService/CreateTopic"), headers.path());
  assertEquals(of("https"), headers.scheme());
  assertEquals(of("POST"), headers.method());
  assertEquals(of("pubsub.googleapis.com"), headers.authority());
  assertEquals(of("trailers"), headers.get(of("te")));
  assertEquals(of("bar"), headers.get(of("foo")));
}
 
Example #10
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 #11
Source File: Http2ServerCodecUnitTest.java    From xio with Apache License 2.0 6 votes vote down vote up
@Test
public void testFullResponse() throws Exception {
  outputReceived = new CountDownLatch(2);
  Http2Headers headersIn = new DefaultHttp2Headers().method("GET").path("/");
  Http2Request requestIn = Http2Request.build(1, headersIn, true);
  FullResponse responseIn =
      ResponseBuilders.newOk().streamId(1).body(Unpooled.EMPTY_BUFFER).build();

  channel.writeInbound(requestIn);
  channel.runPendingTasks(); // blocks
  channel.writeOutbound(responseIn);

  channel.runPendingTasks(); // blocks

  Uninterruptibles.awaitUninterruptibly(outputReceived);

  Http2Response responseOut = responses.remove(0);

  assertNotNull(responseOut);
  assertTrue(responseOut.payload instanceof Http2Headers);
  assertEquals("200", ((Http2Headers) responseOut.payload).status().toString());
  assertTrue(responseOut.eos);
  assertEquals(1, responseOut.streamId);
}
 
Example #12
Source File: Utils.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
public static Http2Headers convertClientHeaders(Metadata headers,
    AsciiString scheme,
    AsciiString defaultPath,
    AsciiString authority,
    AsciiString method,
    AsciiString userAgent) {
  Preconditions.checkNotNull(defaultPath, "defaultPath");
  Preconditions.checkNotNull(authority, "authority");
  Preconditions.checkNotNull(method, "method");

  // Discard any application supplied duplicates of the reserved headers
  headers.discardAll(CONTENT_TYPE_KEY);
  headers.discardAll(GrpcUtil.TE_HEADER);
  headers.discardAll(GrpcUtil.USER_AGENT_KEY);

  return GrpcHttp2OutboundHeaders.clientRequestHeaders(
      toHttp2Headers(headers),
      authority,
      defaultPath,
      method,
      scheme,
      userAgent);
}
 
Example #13
Source File: GrpcHttp2InboundHeadersTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void binaryHeadersShouldBeBase64Decoded() {
  Http2Headers headers = new GrpcHttp2RequestHeaders(1);

  byte[] data = new byte[100];
  new Random().nextBytes(data);
  headers.add(of("foo-bin"), of(BASE64_ENCODING_OMIT_PADDING.encode(data)));

  assertEquals(1, headers.size());

  byte[][] namesAndValues = ((GrpcHttp2InboundHeaders)headers).namesAndValues();

  assertEquals(of("foo-bin"), new AsciiString(namesAndValues[0]));
  assertNotSame(data, namesAndValues[1]);
  assertArrayEquals(data, namesAndValues[1]);
}
 
Example #14
Source File: GrpcHttp2HeadersUtilsTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@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")));
}
 
Example #15
Source File: Http2HandlerTest.java    From xrpc with Apache License 2.0 6 votes vote down vote up
/** Test that several data frames will be aggregated into a response. */
@Test
void testOnDataRead_dataAggregated() {
  testHandler = new Http2Handler(mockEncoder, MAX_PAYLOAD, NO_CORS);

  // Create a fake request to aggregate data into.
  XrpcRequest fakeRequest = new XrpcRequest((Http2Headers) null, null, null, channel);
  testHandler.requests.put(STREAM_ID, fakeRequest);

  // Append several data frames.
  testHandler.onDataRead(
      mockContext, STREAM_ID, Unpooled.wrappedBuffer(new byte[] {1}), 0, false);
  testHandler.onDataRead(
      mockContext, STREAM_ID, Unpooled.wrappedBuffer(new byte[] {2}), 0, false);
  testHandler.onDataRead(
      mockContext, STREAM_ID, Unpooled.wrappedBuffer(new byte[] {3}), 0, false);

  // Assert that the request has all the data needed.
  assertEquals(Unpooled.wrappedBuffer(new byte[] {1, 2, 3}), fakeRequest.body());
}
 
Example #16
Source File: GrpcHttp2HeadersUtilsTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void dupBinHeadersWithComma() {
  Key<byte[]> key = Key.of("bytes-bin", BINARY_BYTE_MARSHALLER);
  Http2Headers http2Headers = new GrpcHttp2RequestHeaders(2);
  http2Headers.add(AsciiString.of("bytes-bin"), AsciiString.of("BaS,e6,,4+,padding=="));
  http2Headers.add(AsciiString.of("bytes-bin"), AsciiString.of("more"));
  http2Headers.add(AsciiString.of("bytes-bin"), AsciiString.of(""));
  Metadata recoveredHeaders = Utils.convertHeaders(http2Headers);
  byte[][] values = Iterables.toArray(recoveredHeaders.getAll(key), byte[].class);

  assertTrue(Arrays.deepEquals(
      new byte[][] {
          BaseEncoding.base64().decode("BaS"),
          BaseEncoding.base64().decode("e6"),
          BaseEncoding.base64().decode(""),
          BaseEncoding.base64().decode("4+"),
          BaseEncoding.base64().decode("padding"),
          BaseEncoding.base64().decode("more"),
          BaseEncoding.base64().decode("")},
      values));
}
 
Example #17
Source File: NettyServerHandlerTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void headersWithInvalidMethodShouldFail() throws Exception {
  manualSetUp();
  Http2Headers headers = new DefaultHttp2Headers()
      .method(HTTP_FAKE_METHOD)
      .set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC)
      .path(new AsciiString("/foo/bar"));
  ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
  channelRead(headersFrame);
  Http2Headers responseHeaders = new DefaultHttp2Headers()
      .set(InternalStatus.CODE_KEY.name(), String.valueOf(Code.INTERNAL.value()))
      .set(InternalStatus.MESSAGE_KEY.name(), "Method 'FAKE' is not supported")
      .status("" + 405)
      .set(CONTENT_TYPE_HEADER, "text/plain; encoding=utf-8");

  verifyWrite().writeHeaders(eq(ctx()), eq(STREAM_ID), eq(responseHeaders), eq(0),
      eq(DEFAULT_PRIORITY_WEIGHT), eq(false), eq(0), eq(false), any(ChannelPromise.class));
}
 
Example #18
Source File: Http2ServerHandler.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
        int padding, boolean endOfStream) {
    if (endOfStream) {
        ByteBuf content = ctx.alloc().buffer();
        content.writeBytes(CONTENT);
        sendResponse(ctx, streamId, content);
    }
}
 
Example #19
Source File: Utils.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
public static Http2Headers convertServerHeaders(Metadata headers) {
  // Discard any application supplied duplicates of the reserved headers
  headers.discardAll(CONTENT_TYPE_KEY);
  headers.discardAll(GrpcUtil.TE_HEADER);
  headers.discardAll(GrpcUtil.USER_AGENT_KEY);

  return GrpcHttp2OutboundHeaders.serverResponseHeaders(toHttp2Headers(headers));
}
 
Example #20
Source File: Http2CorsHandler.java    From xrpc with Apache License 2.0 5 votes vote down vote up
private Http2Headers preflightHeaders(HttpMethod requestMethod) {
  final Http2Headers responseHeaders = new DefaultHttp2Headers(true);

  responseHeaders.set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO);

  if (!setAccessAllowOriginHeader(responseHeaders)) {
    return responseHeaders;
  }

  if (config.allowedRequestMethods().contains(requestMethod)) {
    responseHeaders.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.toString());
  }

  if (config.isCredentialsAllowed()
      && !responseHeaders.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN).equals(ANY_ORIGIN)) {
    responseHeaders.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
  }

  if (!config.exposedHeaders().isEmpty()) {
    responseHeaders.set(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS, config.exposedHeaders());
  }

  responseHeaders.set(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE, String.valueOf(config.maxAge()));
  responseHeaders.set(
      HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, config.allowedRequestHeaders());

  return responseHeaders;
}
 
Example #21
Source File: HelloWorldHttp2Handler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a "Hello World" DATA frame to the client.
 */
private void sendResponse(ChannelHandlerContext ctx, int streamId, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise());
    encoder().writeData(ctx, streamId, payload, 0, true, ctx.newPromise());

    // no need to call flush as channelReadComplete(...) will take care of it.
}
 
Example #22
Source File: GrpcHttp2HeadersUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void decode_emptyHeaders() throws Http2Exception {
  Http2HeadersDecoder decoder = new GrpcHttp2ClientHeadersDecoder(8192);
  Http2HeadersEncoder encoder =
      new DefaultHttp2HeadersEncoder(NEVER_SENSITIVE);

  ByteBuf encodedHeaders = Unpooled.buffer();
  encoder.encodeHeaders(1 /* randomly chosen */, new DefaultHttp2Headers(false), encodedHeaders);

  Http2Headers decodedHeaders = decoder.decodeHeaders(3 /* randomly chosen */, encodedHeaders);
  assertEquals(0, decodedHeaders.size());
  assertThat(decodedHeaders.toString(), containsString("[]"));
}
 
Example #23
Source File: ReadOnlyHttp2HeadersBenchmark.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int defaultServerHeaders() {
    Http2Headers headers = new DefaultHttp2Headers(false);
    for (int i = 0; i < headerCount; ++i) {
        headers.add(headerNames[i], headerValues[i]);
    }
    headers.status(HttpResponseStatus.OK.codeAsText());
    return iterate(headers);
}
 
Example #24
Source File: ReadOnlyHttp2HeadersBenchmark.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int defaultTrailers() {
    Http2Headers headers = new DefaultHttp2Headers(false);
    for (int i = 0; i < headerCount; ++i) {
        headers.add(headerNames[i], headerValues[i]);
    }
    return iterate(headers);
}
 
Example #25
Source File: ArmeriaHttpUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the specified Armeria HTTP/2 response headers into Netty HTTP/2 headers.
 *
 * @param inputHeaders the HTTP/2 response headers to convert.
 */
public static Http2Headers toNettyHttp2ServerHeaders(HttpHeaders inputHeaders) {
    final int headerSizeHint = inputHeaders.size() + 2; // server and data headers
    final Http2Headers outputHeaders = new DefaultHttp2Headers(false, headerSizeHint);
    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;
        }
        outputHeaders.add(name, value);
    }
    return outputHeaders;
}
 
Example #26
Source File: Http2ResponseDecoder.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding,
                          boolean endOfStream) throws Http2Exception {
    keepAliveChannelRead();
    final HttpResponseWrapper res = getResponse(streamIdToId(streamId), endOfStream);
    if (res == null) {
        if (conn.streamMayHaveExisted(streamId)) {
            if (logger.isDebugEnabled()) {
                logger.debug("{} Received a late HEADERS frame for a closed stream: {}",
                             ctx.channel(), streamId);
            }
            return;
        }

        throw connectionError(PROTOCOL_ERROR, "received a HEADERS frame for an unknown stream: %d",
                              streamId);
    }

    res.logResponseFirstBytesTransferred();

    final HttpHeaders converted = ArmeriaHttpUtil.toArmeria(headers, false, endOfStream);
    try {
        res.initTimeout();
        res.write(converted);
    } catch (Throwable t) {
        res.close(t);
        throw connectionError(INTERNAL_ERROR, t, "failed to consume a HEADERS frame");
    }

    if (endOfStream) {
        res.close();
    }
}
 
Example #27
Source File: NettyServerHandler.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
    int streamDependency, short weight, boolean exclusive, int padding, boolean endStream,
    ChannelPromise promise) {
  keepAliveEnforcer.resetCounters();
  return super.writeHeaders(ctx, streamId, headers, streamDependency, weight, exclusive,
      padding, endStream, promise);
}
 
Example #28
Source File: HelloWorldHttp2Handler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId,
                          Http2Headers headers, int padding, boolean endOfStream) {
    if (endOfStream) {
        ByteBuf content = ctx.alloc().buffer();
        content.writeBytes(RESPONSE_BYTES.duplicate());
        ByteBufUtil.writeAscii(content, " - via HTTP/2");
        sendResponse(ctx, streamId, content);
    }
}
 
Example #29
Source File: Http2PriorityHeadersEvent.java    From cute-proxy with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Http2PriorityHeadersEvent(int streamId, Http2Headers headers, int padding, boolean endOfStream,
                                 int streamDependency, short weight, boolean exclusive) {
    super(Http2FrameTypes.HEADERS, streamId);
    this.headers = headers;
    this.padding = padding;
    this.endOfStream = endOfStream;

    this.streamDependency = streamDependency;
    this.weight = weight;
    this.exclusive = exclusive;
}
 
Example #30
Source File: GrpcHttp2OutboundHeaders.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
static GrpcHttp2OutboundHeaders serverResponseHeaders(byte[][] serializedMetadata) {
  AsciiString[] preHeaders = new AsciiString[] {
      Http2Headers.PseudoHeaderName.STATUS.value(), Utils.STATUS_OK,
      Utils.CONTENT_TYPE_HEADER, Utils.CONTENT_TYPE_GRPC,
  };
  return new GrpcHttp2OutboundHeaders(preHeaders, serializedMetadata);
}