ArmeriaHttpUtil { 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(); } private  ArmeriaHttpUtil(); static String concatPaths(@Nullable String path1, @Nullable String path2); static String decodePath(String path); static boolean isAbsoluteUri(@Nullable String maybeUri); static boolean isInformational(@Nullable String statusText); static boolean isContentAlwaysEmptyWithValidation(HttpStatus status, HttpData content); static boolean isCorsPreflightRequest(com.linecorp.armeria.common.HttpRequest request); static void parseDirectives(String directives, BiConsumer<String, String> callback); static long parseDirectiveValueAsSeconds(@Nullable String value); static RequestHeaders toArmeriaRequestHeaders(ChannelHandlerContext ctx, Http2Headers headers,
                                                         boolean endOfStream, String scheme,
                                                         ServerConfig cfg); static HttpHeaders toArmeria(Http2Headers headers, boolean request, boolean endOfStream); static RequestHeaders toArmeria(ChannelHandlerContext ctx, HttpRequest in,
                                           ServerConfig cfg); static ResponseHeaders toArmeria(HttpResponse in); static HttpHeaders toArmeria(io.netty.handler.codec.http.HttpHeaders inHeaders); static void toArmeria(io.netty.handler.codec.http.HttpHeaders inHeaders, HttpHeadersBuilder out); static Http2Headers toNettyHttp2ServerHeaders(HttpHeaders inputHeaders); static Http2Headers toNettyHttp2ServerTrailer(HttpHeaders inputHeaders); static Http2Headers toNettyHttp2ClientHeader(HttpHeaders inputHeaders); static Http2Headers toNettyHttp2ClientTrailer(HttpHeaders inputHeaders); static void toNettyHttp1ServerHeader(
            HttpHeaders inputHeaders, io.netty.handler.codec.http.HttpHeaders outputHeaders); static void toNettyHttp1ServerTrailer(
            HttpHeaders inputHeaders, io.netty.handler.codec.http.HttpHeaders outputHeaders); static void toNettyHttp1ClientHeader(
            HttpHeaders inputHeaders, io.netty.handler.codec.http.HttpHeaders outputHeaders); static void toNettyHttp1ClientTrailer(
            HttpHeaders inputHeaders, io.netty.handler.codec.http.HttpHeaders outputHeaders); static ResponseHeaders setOrRemoveContentLength(ResponseHeaders headers, HttpData content,
                                                           HttpHeaders trailers); static boolean isTrailerBlacklisted(AsciiString name); static String authorityHeader(String host, int port, int defaultPort); static final Charset HTTP_DEFAULT_CONTENT_CHARSET; static final AsciiString HEADER_NAME_KEEP_ALIVE; static final AsciiString HEADER_NAME_PROXY_CONNECTION; static final String SERVER_HEADER; }
@Test void inboundCookiesMustBeMergedForHttp1() { final io.netty.handler.codec.http.HttpHeaders in = new DefaultHttpHeaders(); in.add(HttpHeaderNames.COOKIE, "a=b; c=d"); in.add(HttpHeaderNames.COOKIE, "e=f;g=h"); in.add(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8); in.add(HttpHeaderNames.COOKIE, "i=j"); in.add(HttpHeaderNames.COOKIE, "k=l;"); final HttpHeadersBuilder out = HttpHeaders.builder(); toArmeria(in, out); assertThat(out.getAll(HttpHeaderNames.COOKIE)) .containsExactly("a=b; c=d; e=f; g=h; i=j; k=l"); }
@Test void endOfStreamSet() { final Http2Headers in = new DefaultHttp2Headers(); in.setInt(HttpHeaderNames.CONTENT_LENGTH, 0); final HttpHeaders out = toArmeria(in, true, true); assertThat(out.isEndOfStream()).isTrue(); final HttpHeaders out2 = toArmeria(in, true, false); assertThat(out2.isEndOfStream()).isFalse(); }
@Test void endOfStreamSetEmpty() { final Http2Headers in = new DefaultHttp2Headers(); final HttpHeaders out = toArmeria(in, true, true); assertThat(out.isEndOfStream()).isTrue(); final HttpHeaders out2 = toArmeria(in, true, false); assertThat(out2.isEndOfStream()).isFalse(); }
@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"); }
@Test void addHostHeaderIfMissing() throws URISyntaxException { final io.netty.handler.codec.http.HttpHeaders headers = new DefaultHttpHeaders(); headers.add(HttpHeaderNames.HOST, "bar"); final HttpRequest originReq = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/hello", headers); 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); RequestHeaders armeriaHeaders = toArmeria(ctx, originReq, serverConfig()); assertThat(armeriaHeaders.get(HttpHeaderNames.HOST)).isEqualTo("bar"); assertThat(armeriaHeaders.authority()).isEqualTo("bar"); final HttpRequest absoluteUriReq = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: armeriaHeaders = toArmeria(ctx, absoluteUriReq, serverConfig()); assertThat(armeriaHeaders.get(HttpHeaderNames.HOST)).isEqualTo("bar"); assertThat(armeriaHeaders.authority()).isEqualTo("bar"); headers.remove(HttpHeaderNames.HOST); armeriaHeaders = toArmeria(ctx, originReq, serverConfig()); assertThat(armeriaHeaders.get(HttpHeaderNames.HOST)).isEqualTo("foo:36462"); assertThat(armeriaHeaders.authority()).isEqualTo("foo:36462"); armeriaHeaders = toArmeria(ctx, absoluteUriReq, serverConfig()); assertThat(armeriaHeaders.get(HttpHeaderNames.HOST)).isEqualTo("example.com"); assertThat(armeriaHeaders.authority()).isEqualTo("example.com"); }
@Test void stripTEHeaders() { final io.netty.handler.codec.http.HttpHeaders in = new DefaultHttpHeaders(); in.add(HttpHeaderNames.TE, HttpHeaderValues.GZIP); final HttpHeadersBuilder out = HttpHeaders.builder(); toArmeria(in, out); assertThat(out).isEmpty(); }
@Test void stripTEHeadersExcludingTrailers() { final io.netty.handler.codec.http.HttpHeaders in = new DefaultHttpHeaders(); in.add(HttpHeaderNames.TE, HttpHeaderValues.GZIP); in.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS); final HttpHeadersBuilder out = HttpHeaders.builder(); toArmeria(in, out); assertThat(out.get(HttpHeaderNames.TE)).isEqualTo(HttpHeaderValues.TRAILERS.toString()); }
@Test void stripTEHeadersCsvSeparatedExcludingTrailers() { final io.netty.handler.codec.http.HttpHeaders in = new DefaultHttpHeaders(); in.add(HttpHeaderNames.TE, HttpHeaderValues.GZIP + "," + HttpHeaderValues.TRAILERS); final HttpHeadersBuilder out = HttpHeaders.builder(); toArmeria(in, out); assertThat(out.get(HttpHeaderNames.TE)).isEqualTo(HttpHeaderValues.TRAILERS.toString()); }
@Test void stripTEHeadersCsvSeparatedAccountsForValueSimilarToTrailers() { final io.netty.handler.codec.http.HttpHeaders in = new DefaultHttpHeaders(); in.add(HttpHeaderNames.TE, HttpHeaderValues.GZIP + "," + HttpHeaderValues.TRAILERS + "foo"); final HttpHeadersBuilder out = HttpHeaders.builder(); toArmeria(in, out); assertThat(out.contains(HttpHeaderNames.TE)).isFalse(); }
@Test void stripTEHeadersAccountsForValueSimilarToTrailers() { final io.netty.handler.codec.http.HttpHeaders in = new DefaultHttpHeaders(); in.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS + "foo"); final HttpHeadersBuilder out = HttpHeaders.builder(); toArmeria(in, out); assertThat(out.contains(HttpHeaderNames.TE)).isFalse(); }
@Test void stripTEHeadersAccountsForOWS() { final io.netty.handler.codec.http.HttpHeaders in = new DefaultHttpHeaders(); in.add(HttpHeaderNames.TE, " " + HttpHeaderValues.TRAILERS + ' '); final HttpHeadersBuilder out = HttpHeaders.builder(); toArmeria(in, out); assertThat(out.get(HttpHeaderNames.TE)).isEqualTo(HttpHeaderValues.TRAILERS.toString()); }
@Test void stripConnectionHeadersAndNominees() { final io.netty.handler.codec.http.HttpHeaders in = new DefaultHttpHeaders(); in.add(HttpHeaderNames.CONNECTION, "foo"); in.add("foo", "bar"); final HttpHeadersBuilder out = HttpHeaders.builder(); toArmeria(in, out); assertThat(out).isEmpty(); }
@Test void stripConnectionNomineesWithCsv() { final io.netty.handler.codec.http.HttpHeaders in = new DefaultHttpHeaders(); in.add(HttpHeaderNames.CONNECTION, "foo, bar"); in.add("foo", "baz"); in.add("bar", "qux"); in.add("hello", "world"); final HttpHeadersBuilder out = HttpHeaders.builder(); toArmeria(in, out); assertThat(out).hasSize(1); assertThat(out.get(HttpHeaderNames.of("hello"))).isEqualTo("world"); }
@Test void convertedHeaderTypes() { final Http2Headers in = new DefaultHttp2Headers().set("a", "b"); assertThat(toArmeria(in, true, false)).isInstanceOf(HttpHeaders.class) .isNotInstanceOf(RequestHeaders.class) .isNotInstanceOf(ResponseHeaders.class); assertThat(toArmeria(in, false, false)).isInstanceOf(HttpHeaders.class) .isNotInstanceOf(RequestHeaders.class) .isNotInstanceOf(ResponseHeaders.class); in.clear() .set(HttpHeaderNames.METHOD, "GET") .set(HttpHeaderNames.PATH, "/"); assertThat(toArmeria(in, true, false)).isInstanceOf(RequestHeaders.class) .isNotInstanceOf(ResponseHeaders.class); in.clear() .set(HttpHeaderNames.STATUS, "200"); assertThat(toArmeria(in, false, false)).isInstanceOf(ResponseHeaders.class) .isNotInstanceOf(RequestHeaders.class); in.clear() .set(HttpHeaderNames.METHOD, "GET") .set(HttpHeaderNames.PATH, "/") .set(HttpHeaderNames.STATUS, "200"); assertThat(toArmeria(in, true, false)).isInstanceOf(RequestHeaders.class) .isNotInstanceOf(ResponseHeaders.class); in.clear() .set(HttpHeaderNames.STATUS, "200") .set(HttpHeaderNames.METHOD, "GET"); assertThat(toArmeria(in, false, false)).isInstanceOf(ResponseHeaders.class) .isNotInstanceOf(RequestHeaders.class); }