Java Code Examples for com.linecorp.armeria.common.HttpHeadersBuilder#add()

The following examples show how to use com.linecorp.armeria.common.HttpHeadersBuilder#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: ArmeriaServerCall.java    From armeria with Apache License 2.0 6 votes vote down vote up
static HttpHeaders statusToTrailers(
        ServiceRequestContext ctx, Status status, Metadata metadata, boolean headersSent) {
    final HttpHeadersBuilder trailers = GrpcTrailersUtil.statusToTrailers(
            status.getCode().value(), status.getDescription(), headersSent);

    MetadataUtil.fillHeaders(metadata, trailers);

    if (ctx.config().verboseResponses() && status.getCause() != null) {
        final ThrowableProto proto = GrpcStatus.serializeThrowable(status.getCause());
        trailers.add(GrpcHeaderNames.ARMERIA_GRPC_THROWABLEPROTO_BIN,
                     Base64.getEncoder().encodeToString(proto.toByteArray()));
    }

    final HttpHeaders additionalTrailers = ctx.additionalResponseTrailers();
    ctx.mutateAdditionalResponseTrailers(HttpHeadersBuilder::clear);
    trailers.add(additionalTrailers);
    return trailers.build();
}
 
Example 2
Source File: GrpcTrailersUtil.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * 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 3
Source File: ArmeriaHttpUtil.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Filter the {@link HttpHeaderNames#TE} header according to the
 * <a href="https://tools.ietf.org/html/rfc7540#section-8.1.2.2">special rules in the HTTP/2 RFC</a>.
 *
 * @param entry the entry whose name is {@link HttpHeaderNames#TE}.
 * @param out the resulting HTTP/2 headers.
 */
private static void toHttp2HeadersFilterTE(Entry<CharSequence, CharSequence> entry,
                                           HttpHeadersBuilder out) {
    if (AsciiString.indexOf(entry.getValue(), ',', 0) == -1) {
        if (AsciiString.contentEqualsIgnoreCase(AsciiString.trim(entry.getValue()),
                                                HttpHeaderValues.TRAILERS)) {
            out.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS.toString());
        }
    } else {
        final List<CharSequence> teValues = StringUtil.unescapeCsvFields(entry.getValue());
        for (CharSequence teValue : teValues) {
            if (AsciiString.contentEqualsIgnoreCase(AsciiString.trim(teValue),
                                                    HttpHeaderValues.TRAILERS)) {
                out.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS.toString());
                break;
            }
        }
    }
}
 
Example 4
Source File: ArmeriaClientCall.java    From armeria with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: ArmeriaClientHttpRequest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static void setDefaultRequestHeaders(HttpHeadersBuilder headers) {
    if (!headers.contains(HttpHeaderNames.ACCEPT)) {
        headers.add(HttpHeaderNames.ACCEPT, "*/*");
    }
}