use of io.servicetalk.http.api.HttpHeaderValues.ZERO in project servicetalk by apple.
the class DefaultContainerResponseWriter method sendResponse.
private void sendResponse(final long contentLength, @Nullable final Publisher<Buffer> content, final ContainerResponse containerResponse) {
final HttpResponseStatus status = getStatus(containerResponse);
final StreamingHttpResponse response;
if (content != null && !isHeadRequest()) {
final HttpExecutionStrategy executionStrategy = getResponseExecutionStrategy(request);
// TODO(scott): use request factory methods that accept a payload body to avoid overhead of payloadBody.
final Publisher<Buffer> payloadBody = (executionStrategy != null && executionStrategy.isSendOffloaded() ? content.subscribeOn(serviceCtx.executionContext().executor(), IoThreadFactory.IoThread::currentThreadIsIoThread) : content).beforeCancel(// Cleanup internal state if server cancels response body
this::cancelResponse);
response = responseFactory.newResponse(status).version(protocolVersion).payloadBody(payloadBody);
} else {
response = responseFactory.newResponse(status).version(protocolVersion);
}
final HttpHeaders headers = response.headers();
// If we use HTTP/2 protocol all headers MUST be in lower case
final boolean isH2 = response.version().major() == 2;
containerResponse.getHeaders().forEach((k, vs) -> vs.forEach(v -> {
headers.add(isH2 ? k.toLowerCase() : k, v == null ? emptyAsciiString() : asCharSequence(v));
}));
if (!headers.contains(CONTENT_LENGTH)) {
if (contentLength == UNKNOWN_RESPONSE_LENGTH) {
// We can omit Transfer-Encoding for HEAD per https://tools.ietf.org/html/rfc7231#section-4.3.2
if (!isHeadRequest() && !HTTP_1_0.equals(protocolVersion)) {
headers.set(TRANSFER_ENCODING, CHUNKED);
}
} else {
headers.set(CONTENT_LENGTH, contentLength == 0 ? ZERO : Long.toString(contentLength));
headers.removeIgnoreCase(TRANSFER_ENCODING, CHUNKED);
}
}
responseSubscriber.onSuccess(response);
}
use of io.servicetalk.http.api.HttpHeaderValues.ZERO in project servicetalk by apple.
the class SslAndNonSslConnectionsTest method beforeClass.
@BeforeAll
static void beforeClass() throws Exception {
final HttpHeaders httpHeaders = DefaultHttpHeadersFactory.INSTANCE.newHeaders().set(CONTENT_LENGTH, ZERO);
// Configure HTTP server
when(STREAMING_HTTP_SERVICE.handle(any(), any(), any())).thenAnswer((Answer<Single<StreamingHttpResponse>>) invocation -> {
StreamingHttpResponseFactory factory = invocation.getArgument(2);
StreamingHttpResponse resp = factory.ok();
resp.headers().set(httpHeaders);
return succeeded(resp);
});
when(STREAMING_HTTP_SERVICE.closeAsync()).thenReturn(completed());
when(STREAMING_HTTP_SERVICE.closeAsyncGracefully()).thenReturn(completed());
serverCtx = HttpServers.forAddress(localAddress(0)).executionStrategy(offloadNever()).listenStreamingAndAwait(STREAMING_HTTP_SERVICE);
final String serverHostHeader = hostHeader(serverHostAndPort(serverCtx));
requestTarget = "http://" + serverHostHeader + "/";
// Configure HTTPS server
when(SECURE_STREAMING_HTTP_SERVICE.handle(any(), any(), any())).thenAnswer(invocation -> {
StreamingHttpResponseFactory factory = invocation.getArgument(2);
StreamingHttpResponse resp = factory.ok();
resp.headers().set(httpHeaders);
return succeeded(resp);
});
when(SECURE_STREAMING_HTTP_SERVICE.closeAsync()).thenReturn(completed());
when(SECURE_STREAMING_HTTP_SERVICE.closeAsyncGracefully()).thenReturn(completed());
secureServerCtx = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build()).executionStrategy(offloadNever()).listenStreamingAndAwait(SECURE_STREAMING_HTTP_SERVICE);
final String secureServerHostHeader = hostHeader(serverHostAndPort(secureServerCtx));
secureRequestTarget = "https://" + secureServerHostHeader + "/";
}
use of io.servicetalk.http.api.HttpHeaderValues.ZERO in project servicetalk by apple.
the class MultiAddressUrlHttpClientTest method beforeClass.
@BeforeAll
static void beforeClass() throws Exception {
afterClassCloseables = newCompositeCloseable();
client = afterClassCloseables.append(HttpClients.forMultiAddressUrl().followRedirects(new RedirectConfigBuilder().allowNonRelativeRedirects(true).build()).initializer((scheme, address, builder) -> builder.serviceDiscoverer(sdThatSupportsInvalidHostname())).buildStreaming());
httpService = (ctx, request, factory) -> {
if (HTTP_1_1.equals(request.version()) && !request.headers().contains(HOST)) {
return succeeded(factory.badRequest().setHeader(CONTENT_LENGTH, ZERO));
}
if (OPTIONS.equals(request.method()) || CONNECT.equals(request.method())) {
return succeeded(factory.ok().setHeader(CONTENT_LENGTH, ZERO));
}
StreamingHttpResponse response;
try {
HttpResponseStatus status = HttpResponseStatus.of(parseInt(request.path().substring(1)), "");
response = factory.newResponse(status);
final CharSequence locationHeader = request.headers().get(X_REQUESTED_LOCATION);
if (locationHeader != null) {
response.headers().set(LOCATION, locationHeader);
}
} catch (Exception e) {
response = factory.badRequest();
}
return succeeded(response.setHeader(CONTENT_LENGTH, ZERO).setHeader(X_RECEIVED_REQUEST_TARGET, request.requestTarget()));
};
final ServerContext serverCtx = startNewLocalServer(httpService, afterClassCloseables);
final HostAndPort serverHostAndPort = serverHostAndPort(serverCtx);
serverHost = serverHostAndPort.hostName();
serverPort = serverHostAndPort.port();
hostHeader = hostHeader(serverHostAndPort);
}
Aggregations