Search in sources :

Example 1 with RedirectingHttpRequesterFilter

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);
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) HttpServerContext(io.servicetalk.http.api.HttpServerContext) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with RedirectingHttpRequesterFilter

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;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter)

Example 3 with RedirectingHttpRequesterFilter

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();
    }
}
Also used : NON_SECURE_SERVER_PORT(io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT) HttpSerializers.textSerializerAscii(io.servicetalk.http.api.HttpSerializers.textSerializerAscii) CUSTOM_HEADER(io.servicetalk.examples.http.redirects.RedirectingServer.CUSTOM_HEADER) HttpClient(io.servicetalk.http.api.HttpClient) POST(io.servicetalk.http.api.HttpRequestMethod.POST) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) HttpClients(io.servicetalk.http.netty.HttpClients) GET(io.servicetalk.http.api.HttpRequestMethod.GET) HttpClient(io.servicetalk.http.api.HttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter)

Aggregations

RedirectingHttpRequesterFilter (io.servicetalk.http.utils.RedirectingHttpRequesterFilter)3 RedirectConfigBuilder (io.servicetalk.http.api.RedirectConfigBuilder)2 Buffer (io.servicetalk.buffer.api.Buffer)1 BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)1 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)1 CompositeCloseable (io.servicetalk.concurrent.api.CompositeCloseable)1 CUSTOM_HEADER (io.servicetalk.examples.http.redirects.RedirectingServer.CUSTOM_HEADER)1 NON_SECURE_SERVER_PORT (io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT)1 FilterableStreamingHttpClient (io.servicetalk.http.api.FilterableStreamingHttpClient)1 HttpClient (io.servicetalk.http.api.HttpClient)1 HttpExecutionContext (io.servicetalk.http.api.HttpExecutionContext)1 HttpExecutionStrategy (io.servicetalk.http.api.HttpExecutionStrategy)1 GET (io.servicetalk.http.api.HttpRequestMethod.GET)1 POST (io.servicetalk.http.api.HttpRequestMethod.POST)1 HttpSerializers.textSerializerAscii (io.servicetalk.http.api.HttpSerializers.textSerializerAscii)1 HttpServerContext (io.servicetalk.http.api.HttpServerContext)1 SingleAddressHttpClientBuilder (io.servicetalk.http.api.SingleAddressHttpClientBuilder)1 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)1 StreamingHttpConnection (io.servicetalk.http.api.StreamingHttpConnection)1 HttpClients (io.servicetalk.http.netty.HttpClients)1