Java Code Examples for com.linecorp.armeria.common.HttpHeaders#builder()
The following examples show how to use
com.linecorp.armeria.common.HttpHeaders#builder() .
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: GrpcTrailersUtil.java From armeria with Apache License 2.0 | 6 votes |
/** * Converts the given gRPC status code, and optionally an error message, to headers. The headers will be * either trailers-only or normal trailers based on {@code headersSent}, whether leading headers have * already been sent to the client. */ public static HttpHeadersBuilder statusToTrailers(int code, @Nullable String message, boolean headersSent) { final HttpHeadersBuilder trailers; if (headersSent) { // Normal trailers. trailers = HttpHeaders.builder(); } else { // Trailers only response trailers = ResponseHeaders.builder() .endOfStream(true) .add(HttpHeaderNames.STATUS, HttpStatus.OK.codeAsText()) .add(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto"); } trailers.add(GrpcHeaderNames.GRPC_STATUS, Integer.toString(code)); if (message != null) { trailers.add(GrpcHeaderNames.GRPC_MESSAGE, StatusMessageEscaper.escape(message)); } return trailers; }
Example 2
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@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"); }
Example 3
Source File: ArmeriaClientCall.java From armeria with Apache License 2.0 | 5 votes |
@Nullable private static HttpHeaders parseGrpcWebTrailers(ByteBuf buf) { final HttpHeadersBuilder trailers = HttpHeaders.builder(); while (buf.readableBytes() > 0) { int start = buf.forEachByte(ByteProcessor.FIND_NON_LINEAR_WHITESPACE); if (start == -1) { return null; } int endExclusive; if (buf.getByte(start) == ':') { // We need to skip the pseudoheader colon when searching for the separator. buf.skipBytes(1); endExclusive = buf.forEachByte(FIND_COLON); buf.readerIndex(start); } else { endExclusive = buf.forEachByte(FIND_COLON); } if (endExclusive == -1) { return null; } final CharSequence name = buf.readCharSequence(endExclusive - start, StandardCharsets.UTF_8); buf.readerIndex(endExclusive + 1); start = buf.forEachByte(ByteProcessor.FIND_NON_LINEAR_WHITESPACE); buf.readerIndex(start); endExclusive = buf.forEachByte(ByteProcessor.FIND_CRLF); final CharSequence value = buf.readCharSequence(endExclusive - start, StandardCharsets.UTF_8); trailers.add(name, value.toString()); start = buf.forEachByte(ByteProcessor.FIND_NON_CRLF); if (start != -1) { buf.readerIndex(start); } else { // Nothing but CRLF remaining, we're done. buf.skipBytes(buf.readableBytes()); } } return trailers.build(); }
Example 4
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@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(); }
Example 5
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@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()); }
Example 6
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@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(); }
Example 7
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@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(); }
Example 8
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@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()); }
Example 9
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@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()); }
Example 10
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@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(); }
Example 11
Source File: ArmeriaHttpUtilTest.java From armeria with Apache License 2.0 | 5 votes |
@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"); }
Example 12
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 5 votes |
/** * Converts the specified Netty HTTP/1 headers into Armeria HTTP/2 headers. */ public static HttpHeaders toArmeria(io.netty.handler.codec.http.HttpHeaders inHeaders) { if (inHeaders.isEmpty()) { return HttpHeaders.of(); } final HttpHeadersBuilder out = HttpHeaders.builder(); out.sizeHint(inHeaders.size()); toArmeria(inHeaders, out); return out.build(); }
Example 13
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 5 votes |
/** * Converts the specified Netty HTTP/2 into Armeria HTTP/2 headers. */ 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(); }
Example 14
Source File: CorsPolicy.java From armeria with Apache License 2.0 | 5 votes |
/** * Generates immutable HTTP response headers that should be added to a CORS preflight response. * * @return {@link HttpHeaders} the HTTP response headers to be added. */ public HttpHeaders generatePreflightResponseHeaders() { final HttpHeadersBuilder headers = HttpHeaders.builder(); preflightResponseHeaders.forEach((key, value) -> { final Object val = getValue(value); if (val instanceof Iterable) { headers.addObject(key, (Iterable<?>) val); } else { headers.addObject(key, val); } }); return headers.build(); }
Example 15
Source File: AnnotatedServiceFactory.java From armeria with Apache License 2.0 | 4 votes |
/** * Returns a list of {@link AnnotatedService} instances. A single {@link AnnotatedService} is * created per each {@link Route} associated with the {@code method}. */ @VisibleForTesting static List<AnnotatedServiceElement> create(String pathPrefix, Object object, Method method, List<RequestConverterFunction> baseRequestConverters, List<ResponseConverterFunction> baseResponseConverters, List<ExceptionHandlerFunction> baseExceptionHandlers) { final Set<Annotation> methodAnnotations = httpMethodAnnotations(method); if (methodAnnotations.isEmpty()) { throw new IllegalArgumentException("HTTP Method specification is missing: " + method.getName()); } final Class<?> clazz = object.getClass(); final Map<HttpMethod, List<String>> httpMethodPatternsMap = getHttpMethodPatternsMap(method, methodAnnotations); final String computedPathPrefix = computePathPrefix(clazz, pathPrefix); final Set<MediaType> consumableMediaTypes = consumableMediaTypes(method, clazz); final Set<MediaType> producibleMediaTypes = producibleMediaTypes(method, clazz); final List<Route> routes = httpMethodPatternsMap.entrySet().stream().flatMap( pattern -> { final HttpMethod httpMethod = pattern.getKey(); final List<String> pathMappings = pattern.getValue(); return pathMappings.stream().map( pathMapping -> Route.builder() .path(computedPathPrefix, pathMapping) .methods(httpMethod) .consumes(consumableMediaTypes) .produces(producibleMediaTypes) .matchesParams( predicates(method, clazz, MatchesParam.class, MatchesParam::value)) .matchesHeaders( predicates(method, clazz, MatchesHeader.class, MatchesHeader::value)) .build()); }).collect(toImmutableList()); final List<RequestConverterFunction> req = getAnnotatedInstances(method, clazz, RequestConverter.class, RequestConverterFunction.class) .addAll(baseRequestConverters).build(); final List<ResponseConverterFunction> res = getAnnotatedInstances(method, clazz, ResponseConverter.class, ResponseConverterFunction.class) .addAll(baseResponseConverters).build(); final List<ExceptionHandlerFunction> eh = getAnnotatedInstances(method, clazz, ExceptionHandler.class, ExceptionHandlerFunction.class) .addAll(baseExceptionHandlers).add(defaultExceptionHandler).build(); final ResponseHeadersBuilder defaultHeaders = ResponseHeaders.builder(defaultResponseStatus(method)); final HttpHeadersBuilder defaultTrailers = HttpHeaders.builder(); final String classAlias = clazz.getName(); final String methodAlias = String.format("%s.%s()", classAlias, method.getName()); setAdditionalHeader(defaultHeaders, clazz, "header", classAlias, "class", AdditionalHeader.class, AdditionalHeader::name, AdditionalHeader::value); setAdditionalHeader(defaultHeaders, method, "header", methodAlias, "method", AdditionalHeader.class, AdditionalHeader::name, AdditionalHeader::value); setAdditionalHeader(defaultTrailers, clazz, "trailer", classAlias, "class", AdditionalTrailer.class, AdditionalTrailer::name, AdditionalTrailer::value); setAdditionalHeader(defaultTrailers, method, "trailer", methodAlias, "method", AdditionalTrailer.class, AdditionalTrailer::name, AdditionalTrailer::value); if (defaultHeaders.status().isContentAlwaysEmpty() && !defaultTrailers.isEmpty()) { logger.warn("A response with HTTP status code '{}' cannot have a content. " + "Trailers defined at '{}' might be ignored if HTTP/1.1 is used.", defaultHeaders.status().code(), methodAlias); } final ResponseHeaders responseHeaders = defaultHeaders.build(); final HttpHeaders responseTrailers = defaultTrailers.build(); final boolean useBlockingTaskExecutor = AnnotationUtil.findFirst(method, Blocking.class) != null; return routes.stream().map(route -> { final List<AnnotatedValueResolver> resolvers = getAnnotatedValueResolvers(req, route, method, clazz); return new AnnotatedServiceElement( route, new AnnotatedService(object, method, resolvers, eh, res, route, responseHeaders, responseTrailers, useBlockingTaskExecutor), decorator(method, clazz)); }).collect(toImmutableList()); }
Example 16
Source File: FileServiceBuilder.java From armeria with Apache License 2.0 | 4 votes |
private HttpHeadersBuilder headersBuilder() { if (headers == null) { headers = HttpHeaders.builder(); } return headers; }
Example 17
Source File: AbstractHttpFileBuilder.java From armeria with Apache License 2.0 | 4 votes |
private HttpHeadersBuilder headersBuilder() { if (headers == null) { headers = HttpHeaders.builder(); } return headers; }