Search in sources :

Example 26 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class ForwardedHeaderTransformerTests method xForwardedPrefixTrailingSlash.

@Test
void xForwardedPrefixTrailingSlash() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.add("X-Forwarded-Prefix", "/prefix////");
    ServerHttpRequest request = this.requestMutator.apply(getRequest(headers));
    assertThat(request.getURI()).isEqualTo(new URI("https://example.com/prefix/path"));
    assertThat(request.getPath().value()).isEqualTo("/prefix/path");
    assertForwardedHeadersRemoved(request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 27 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class HttpHandlerConnector method doConnect.

private Mono<ClientHttpResponse> doConnect(HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
    // unsafe(): we're intercepting, already serialized Publisher signals
    Sinks.Empty<Void> requestWriteSink = Sinks.unsafe().empty();
    Sinks.Empty<Void> handlerSink = Sinks.unsafe().empty();
    ClientHttpResponse[] savedResponse = new ClientHttpResponse[1];
    MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
    MockServerHttpResponse mockServerResponse = new MockServerHttpResponse();
    mockClientRequest.setWriteHandler(requestBody -> {
        log("Invoking HttpHandler for ", httpMethod, uri);
        ServerHttpRequest mockServerRequest = adaptRequest(mockClientRequest, requestBody);
        ServerHttpResponse responseToUse = prepareResponse(mockServerResponse, mockServerRequest);
        this.handler.handle(mockServerRequest, responseToUse).subscribe(aVoid -> {
        }, // Ignore result: signals cannot compete
        handlerSink::tryEmitError, handlerSink::tryEmitEmpty);
        return Mono.empty();
    });
    mockServerResponse.setWriteHandler(responseBody -> Mono.fromRunnable(() -> {
        log("Creating client response for ", httpMethod, uri);
        savedResponse[0] = adaptResponse(mockServerResponse, responseBody);
    }));
    log("Writing client request for ", httpMethod, uri);
    requestCallback.apply(mockClientRequest).subscribe(aVoid -> {
    }, // Ignore result: signals cannot compete
    requestWriteSink::tryEmitError, requestWriteSink::tryEmitEmpty);
    return Mono.when(requestWriteSink.asMono(), handlerSink.asMono()).onErrorMap(ex -> {
        ClientHttpResponse response = savedResponse[0];
        return response != null ? new FailureAfterResponseCompletedException(response, ex) : ex;
    }).then(Mono.fromCallable(() -> savedResponse[0] != null ? savedResponse[0] : adaptResponse(mockServerResponse, Flux.empty())));
}
Also used : Sinks(reactor.core.publisher.Sinks) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Function(java.util.function.Function) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) HttpCookie(org.springframework.http.HttpCookie) Schedulers(reactor.core.scheduler.Schedulers) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) HttpMethod(org.springframework.http.HttpMethod) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) HttpHeadResponseDecorator(org.springframework.http.server.reactive.HttpHeadResponseDecorator) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) Assert(org.springframework.util.Assert) Sinks(reactor.core.publisher.Sinks) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse)

Example 28 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class DefaultCorsProcessor method processRequest.

@Override
public boolean processRequest(CorsConfiguration config, ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    if (!CorsUtils.isCorsRequest(request)) {
        return true;
    }
    if (responseHasCors(response)) {
        logger.debug("Skip CORS: response already contains \"Access-Control-Allow-Origin\" header");
        return true;
    }
    if (CorsUtils.isSameOrigin(request)) {
        logger.debug("Skip CORS: request is from same origin");
        return true;
    }
    boolean preFlightRequest = CorsUtils.isPreFlightRequest(request);
    if (config == null) {
        if (preFlightRequest) {
            rejectRequest(response);
            return false;
        } else {
            return true;
        }
    }
    return handleInternal(exchange, config, preFlightRequest);
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Example 29 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class HttpHandlerConnector method connect.

@Override
public Mono<ClientHttpResponse> connect(HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
    MonoProcessor<ClientHttpResponse> result = MonoProcessor.create();
    MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
    MockServerHttpResponse mockServerResponse = new MockServerHttpResponse();
    mockClientRequest.setWriteHandler(requestBody -> {
        log("Invoking HttpHandler for ", httpMethod, uri);
        ServerHttpRequest mockServerRequest = adaptRequest(mockClientRequest, requestBody);
        this.handler.handle(mockServerRequest, mockServerResponse).subscribe(aVoid -> {
        }, result::onError);
        return Mono.empty();
    });
    mockServerResponse.setWriteHandler(responseBody -> {
        log("Creating client response for ", httpMethod, uri);
        result.onNext(adaptResponse(mockServerResponse, responseBody));
        return Mono.empty();
    });
    log("Writing client request for ", httpMethod, uri);
    requestCallback.apply(mockClientRequest).subscribe(aVoid -> {
    }, result::onError);
    return result;
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse)

Example 30 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class TomcatRequestUpgradeStrategy method getHandshakeInfo.

private HandshakeInfo getHandshakeInfo(ServerWebExchange exchange, Optional<String> protocol) {
    ServerHttpRequest request = exchange.getRequest();
    Mono<Principal> principal = exchange.getPrincipal();
    return new HandshakeInfo(request.getURI(), request.getHeaders(), principal, protocol);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Principal(java.security.Principal) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Aggregations

ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)71 HttpHeaders (org.springframework.http.HttpHeaders)26 Test (org.junit.jupiter.api.Test)25 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)20 ServerWebExchange (org.springframework.web.server.ServerWebExchange)19 URI (java.net.URI)17 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)17 Mono (reactor.core.publisher.Mono)13 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)10 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)9 HttpMethod (org.springframework.http.HttpMethod)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 Map (java.util.Map)6 HttpStatus (org.springframework.http.HttpStatus)6 MediaType (org.springframework.http.MediaType)6 Flux (reactor.core.publisher.Flux)6 Principal (java.security.Principal)5 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5