use of io.servicetalk.http.api.HttpHeaders in project servicetalk by apple.
the class HttpResponseDecoderBenchmark method initialLine.
@Benchmark
public int initialLine() {
channel.writeInbound(responseByteBuf.duplicate());
final HttpResponseMetaData response = channel.readInbound();
final HttpHeaders trailers = channel.readInbound();
if (response.status().code() != statusCode) {
throw new IllegalStateException("Unexpected statusCode: " + response.status().code());
}
return response.headers().size() + trailers.size();
}
use of io.servicetalk.http.api.HttpHeaders in project servicetalk by apple.
the class GrpcUtils method validateResponseAndGetPayload.
static <Resp> Resp validateResponseAndGetPayload(final HttpResponse response, final CharSequence expectedContentType, final BufferAllocator allocator, final GrpcDeserializer<Resp> deserializer) {
// In case of an empty response, gRPC-server may return only one HEADER frame with endStream=true. Our
// HTTP1-based implementation translates them into response headers so we need to look for a grpc-status in both
// headers and trailers.
final HttpHeaders headers = response.headers();
final HttpHeaders trailers = response.trailers();
validateContentType(headers, expectedContentType);
// We will try the trailers first as this is the most likely place to find the gRPC-related headers.
final GrpcStatusCode grpcStatusCode = extractGrpcStatusCodeFromHeaders(trailers);
if (grpcStatusCode != null) {
final GrpcStatusException grpcStatusException = convertToGrpcStatusException(grpcStatusCode, trailers);
if (grpcStatusException != null) {
throw grpcStatusException;
}
return deserializer.deserialize(response.payloadBody(), allocator);
}
// There was no grpc-status in the trailers, so it must be in headers.
ensureGrpcStatusReceived(headers);
return deserializer.deserialize(response.payloadBody(), allocator);
}
use of io.servicetalk.http.api.HttpHeaders in project servicetalk by apple.
the class GrpcUtils method initRequest.
static void initRequest(final HttpRequestMetaData request, final CharSequence contentType, @Nullable final CharSequence encoding, @Nullable final CharSequence acceptedEncoding, @Nullable final Duration timeout) {
assert POST.equals(request.method());
final HttpHeaders headers = request.headers();
final CharSequence timeoutValue = makeTimeoutHeader(timeout);
if (null != timeoutValue) {
headers.set(GRPC_TIMEOUT_HEADER_KEY, timeoutValue);
}
headers.set(USER_AGENT, SERVICETALK_USER_AGENT);
headers.set(TE, TRAILERS);
headers.set(CONTENT_TYPE, contentType);
if (encoding != null) {
headers.set(GRPC_MESSAGE_ENCODING, encoding);
}
if (acceptedEncoding != null) {
headers.set(GRPC_MESSAGE_ACCEPT_ENCODING, acceptedEncoding);
}
}
use of io.servicetalk.http.api.HttpHeaders in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientAllowDropTrailers.
private void clientAllowDropTrailers(boolean allowDrop, boolean serverAddTrailerHeader) throws Exception {
String trailerName = "t1";
String trailerValue = "v1";
InetSocketAddress serverAddress = serverAddTrailerHeader ? bindHttpEchoServerWithTrailer(trailerName) : bindHttpEchoServer();
try (StreamingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).allowDropResponseTrailers(allowDrop).executionStrategy(clientExecutionStrategy).buildStreaming()) {
StreamingHttpResponse response = client.request(client.get("/").transform(new StatelessTrailersTransformer<Buffer>() {
@Override
protected HttpHeaders payloadComplete(final HttpHeaders trailers) {
trailers.add(trailerName, trailerValue);
return trailers;
}
})).toFuture().get();
SingleSource.Processor<HttpHeaders, HttpHeaders> trailersProcessor = Processors.newSingleProcessor();
// intermediate Buffer transform may drop trailers
response.transformPayloadBody(buf -> buf).transform(new StatelessTrailersTransformer<Buffer>() {
@Override
protected HttpHeaders payloadComplete(final HttpHeaders trailers) {
trailersProcessor.onSuccess(trailers);
return trailers;
}
}).messageBody().ignoreElements().toFuture().get();
HttpHeaders responseTrailers = fromSource(trailersProcessor).toFuture().get();
if (allowDrop && !serverAddTrailerHeader) {
assertFalse(responseTrailers.contains(trailerName));
} else {
assertHeaderValue(responseTrailers, trailerName, trailerValue);
}
}
}
use of io.servicetalk.http.api.HttpHeaders in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method serverAllowDropTrailers.
private void serverAllowDropTrailers(boolean allowDrop, boolean clientAddTrailerHeader) throws Exception {
String trailerName = "t1";
String trailerValue = "v1";
SingleSource.Processor<HttpHeaders, HttpHeaders> trailersProcessor = Processors.newSingleProcessor();
h1ServerContext = HttpServers.forAddress(localAddress(0)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).allowDropRequestTrailers(allowDrop).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok().payloadBody(// intermediate Buffer transform may drop trailers
request.transformPayloadBody(buf -> buf).transform(new StatelessTrailersTransformer<Buffer>() {
@Override
protected HttpHeaders payloadComplete(final HttpHeaders trailers) {
trailersProcessor.onSuccess(trailers);
return trailers;
}
}).payloadBody())));
InetSocketAddress serverAddress = (InetSocketAddress) h1ServerContext.listenAddress();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).allowDropResponseTrailers(allowDrop).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpRequest request = client.get("/");
if (clientAddTrailerHeader) {
request.headers().add(TRAILER, trailerName);
}
request.trailers().add(trailerName, trailerValue);
client.request(request);
HttpHeaders requestTrailers = fromSource(trailersProcessor).toFuture().get();
if (allowDrop && !clientAddTrailerHeader) {
assertFalse(requestTrailers.contains(trailerName));
} else {
assertHeaderValue(requestTrailers, trailerName, trailerValue);
}
}
}
Aggregations