Java Code Examples for io.netty.handler.codec.http2.Http2Headers#add()
The following examples show how to use
io.netty.handler.codec.http2.Http2Headers#add() .
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 |
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: GrpcHttp2InboundHeadersTest.java From grpc-java with Apache License 2.0 | 6 votes |
@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 3
Source File: NettyServerHandler.java From grpc-java with Apache License 2.0 | 6 votes |
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: GrpcHttp2InboundHeadersTest.java From grpc-java with Apache License 2.0 | 6 votes |
@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 5
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 6 votes |
/** * 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 toNettyHttp2ServerTrailer(HttpHeaders inputHeaders) { final Http2Headers outputHeaders = new DefaultHttp2Headers(false, inputHeaders.size()); 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 (ADDITIONAL_RESPONSE_HEADER_BLACKLIST.contains(name)) { continue; } if (isTrailerBlacklisted(name)) { continue; } outputHeaders.add(name, value); } return outputHeaders; }
Example 6
Source File: ServerHttp2ObjectEncoder.java From armeria with Apache License 2.0 | 6 votes |
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 7
Source File: NettyServerHandler.java From grpc-nebula-java with Apache License 2.0 | 6 votes |
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 8
Source File: H2PriorKnowledgeFeatureParityTest.java From servicetalk with Apache License 2.0 | 6 votes |
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 9
Source File: GrpcHttp2InboundHeadersTest.java From grpc-nebula-java with Apache License 2.0 | 6 votes |
@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 10
Source File: GrpcHttp2InboundHeadersTest.java From grpc-nebula-java with Apache License 2.0 | 6 votes |
@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 11
Source File: GrpcHttp2HeadersUtilsTest.java From grpc-nebula-java with Apache License 2.0 | 6 votes |
@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 12
Source File: H2ToStH1ServerDuplexHandler.java From servicetalk with Apache License 2.0 | 5 votes |
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 13
Source File: Http2CorsHandler.java From xrpc with Apache License 2.0 | 5 votes |
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 14
Source File: ReadOnlyHttp2HeadersBenchmark.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@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 15
Source File: ReadOnlyHttp2HeadersBenchmark.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Benchmark @BenchmarkMode(Mode.AverageTime) public int defaultClientHeaders() { Http2Headers headers = new DefaultHttp2Headers(false); for (int i = 0; i < headerCount; ++i) { headers.add(headerNames[i], headerValues[i]); } headers.method(HttpMethod.POST.asciiName()); headers.scheme(HttpScheme.HTTPS.name()); headers.path(path); headers.authority(authority); return iterate(headers); }
Example 16
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 5 votes |
/** * 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 17
Source File: ReadOnlyHttp2HeadersBenchmark.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@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 18
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 5 votes |
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 19
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void inboundCookiesMustBeMergedForHttp2() { final Http2Headers in = new DefaultHttp2Headers(); in.add(HttpHeaderNames.COOKIE, "a=b; c=d"); in.add(HttpHeaderNames.COOKIE, "e=f;g=h"); in.addObject(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8); in.add(HttpHeaderNames.COOKIE, "i=j"); in.add(HttpHeaderNames.COOKIE, "k=l;"); final HttpHeaders out = toArmeria(in, true, false); assertThat(out.getAll(HttpHeaderNames.COOKIE)) .containsExactly("a=b; c=d; e=f; g=h; i=j; k=l"); }
Example 20
Source File: Http2HeadersWrapperUnitTest.java From xio with Apache License 2.0 | 5 votes |
@Test public void testAddHeaders() { Http2Headers other = new DefaultHttp2Headers(); other.add("header", "value"); headers.add(other); assertEquals(other, headers.delegate()); assertEquals("value", headers.delegate().get("header")); }