use of io.servicetalk.http.utils.RedirectingHttpRequesterFilter in project servicetalk by apple.
the class ExpectContinueTest method serverRespondsWithRedirect.
@ParameterizedTest(name = "protocol={0} withCL={1}")
@MethodSource("arguments")
void serverRespondsWithRedirect(HttpProtocol protocol, boolean withCL) throws Exception {
try (HttpServerContext serverContext = startServer(protocol, (ctx, request, response) -> {
if ("/redirect".equals(request.requestTarget())) {
response.status(PERMANENT_REDIRECT);
response.setHeader(LOCATION, "/");
response.sendMetaData().close();
return;
}
requestReceived.countDown();
sendContinue.await();
StringBuilder sb = new StringBuilder();
request.payloadBody().forEach(chunk -> sb.append(chunk.toString(US_ASCII)));
returnResponse.await();
try (HttpPayloadWriter<Buffer> writer = response.sendMetaData()) {
writer.write(ctx.executionContext().bufferAllocator().fromAscii(sb));
}
});
StreamingHttpClient client = createClient(serverContext, protocol, new RedirectingHttpRequesterFilter(new RedirectConfigBuilder().allowedMethods(POST).headersToRedirect(CONTENT_LENGTH, TRANSFER_ENCODING, EXPECT).redirectPayloadBody(true).build()));
StreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get()) {
BufferAllocator allocator = connection.executionContext().bufferAllocator();
TestPublisher<Buffer> payload = new TestPublisher.Builder<Buffer>().singleSubscriber().build();
connection.request(newRequest(connection, withCL, false, payload).requestTarget("/redirect")).subscribe(responses::add);
requestReceived.await();
assertThat("Unexpected subscribe to payload body before 100 (Continue)", payload.isSubscribed(), is(false));
sendContinue.countDown();
sendRequestPayload(payload, allocator);
returnResponse.countDown();
assertResponse(OK, PAYLOAD + PAYLOAD);
sendFollowUpRequest(connection, withCL, allocator, OK);
}
}
use of io.servicetalk.http.utils.RedirectingHttpRequesterFilter in project servicetalk by apple.
the class DefaultMultiAddressUrlHttpClientBuilder method buildStreaming.
@Override
public StreamingHttpClient buildStreaming() {
final CompositeCloseable closeables = newCompositeCloseable();
try {
final HttpClientBuildContext<HostAndPort, InetSocketAddress> buildContext = builderTemplate.copyBuildCtx();
final ClientFactory clientFactory = new ClientFactory(buildContext.builder, singleAddressInitializer);
HttpExecutionContext executionContext = buildContext.builder.executionContextBuilder.build();
final CachingKeyFactory keyFactory = closeables.prepend(new CachingKeyFactory());
FilterableStreamingHttpClient urlClient = closeables.prepend(new StreamingUrlHttpClient(executionContext, clientFactory, keyFactory, defaultReqRespFactory(buildContext.httpConfig().asReadOnly(), executionContext.bufferAllocator())));
// Need to wrap the top level client (group) in order for non-relative redirects to work
urlClient = redirectConfig == null ? urlClient : new RedirectingHttpRequesterFilter(redirectConfig).create(urlClient);
HttpExecutionStrategy computedStrategy = buildContext.builder.computeChainStrategy(executionContext.executionStrategy());
LOGGER.debug("Client created with base strategy {} → computed strategy {}", executionContext.executionStrategy(), computedStrategy);
return new FilterableClientToClient(urlClient, computedStrategy);
} catch (final Throwable t) {
closeables.closeAsync().subscribe();
throw t;
}
}
use of io.servicetalk.http.utils.RedirectingHttpRequesterFilter in project servicetalk by apple.
the class SingleAddressRedirectClient method main.
public static void main(String... args) throws Exception {
try (HttpClient client = HttpClients.forSingleAddress("localhost", NON_SECURE_SERVER_PORT).appendClientFilter(new RedirectingHttpRequesterFilter(new RedirectConfigBuilder().allowedMethods(GET, // by default, POST requests don't follow redirects:
POST).build())).build()) {
System.out.println("- Simple GET request:");
client.request(client.get("/relative")).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(textSerializerAscii()));
System.out.println();
}).toFuture().get();
System.out.println("- POST request with headers and payload body:");
client.request(client.post("/relative").addHeader(CUSTOM_HEADER, "value").payloadBody(client.executionContext().bufferAllocator().fromAscii("some_content"))).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(textSerializerAscii()));
System.out.println();
}).toFuture().get();
}
}
Aggregations