use of io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH in project servicetalk by apple.
the class ContentLengthAndTrailersTest method setUp.
private void setUp(HttpProtocol protocol, String content) {
this.protocol = protocol;
this.content = content;
protocol(protocol.config);
serviceFilterFactory(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
// Use transform to simulate access to request trailers
return delegate().handle(ctx, request.transform(new StatelessTrailersTransformer<>()), responseFactory).map(response -> {
final HttpHeaders headers = request.headers();
if (headers.contains(CONTENT_LENGTH)) {
response.setHeader(CLIENT_CONTENT_LENGTH, mergeValues(headers.values(CONTENT_LENGTH)));
}
if (headers.contains(TRANSFER_ENCODING)) {
response.setHeader(CLIENT_TRANSFER_ENCODING, mergeValues(headers.values(TRANSFER_ENCODING)));
}
return response;
});
}
});
setUp(CACHED, CACHED_SERVER);
}
use of io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH 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);
}
use of io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH in project servicetalk by apple.
the class ServerGracefulConnectionClosureHandlingTest method setUp.
@BeforeEach
void setUp() throws Exception {
AtomicReference<Runnable> serverClose = new AtomicReference<>();
serverContext = forAddress(localAddress(0)).ioExecutor(SERVER_CTX.ioExecutor()).executor(SERVER_CTX.executor()).executionStrategy(offloadNone()).appendConnectionAcceptorFilter(original -> new DelegatingConnectionAcceptor(original) {
@Override
public Completable accept(final ConnectionContext context) {
((NettyHttpServerConnection) context).onClosing().whenFinally(serverConnectionClosing::countDown).subscribe();
context.onClose().whenFinally(serverConnectionClosed::countDown).subscribe();
return completed();
}
}).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok().addHeader(CONTENT_LENGTH, valueOf(RESPONSE_CONTENT.length())).payloadBody(request.payloadBody().ignoreElements().concat(from(RESPONSE_CONTENT)), RAW_STRING_SERIALIZER).transformMessageBody(payload -> payload.whenFinally(serverClose.get()))));
serverContext.onClose().whenFinally(serverContextClosed::countDown).subscribe();
serverClose.set(() -> serverContext.closeAsyncGracefully().subscribe());
serverAddress = (InetSocketAddress) serverContext.listenAddress();
}
use of io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH 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());
when(STREAMING_HTTP_SERVICE.requiredOffloads()).thenCallRealMethod();
serverCtx = HttpServers.forAddress(localAddress(0)).executionStrategy(offloadNone()).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());
when(SECURE_STREAMING_HTTP_SERVICE.requiredOffloads()).thenCallRealMethod();
secureServerCtx = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build()).executionStrategy(offloadNone()).listenStreamingAndAwait(SECURE_STREAMING_HTTP_SERVICE);
final String secureServerHostHeader = hostHeader(serverHostAndPort(secureServerCtx));
secureRequestTarget = "https://" + secureServerHostHeader + "/";
}
Aggregations