Search in sources :

Example 21 with ServerHttpRequest

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

the class TomcatRequestUpgradeStrategy method upgrade.

// for old doUpgrade variant in Tomcat 9.0.55
@SuppressWarnings("deprecation")
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = ServerHttpRequestDecorator.getNativeRequest(request);
    HttpServletResponse servletResponse = ServerHttpResponseDecorator.getNativeResponse(response);
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    DataBufferFactory bufferFactory = response.bufferFactory();
    // Trigger WebFlux preCommit actions and upgrade
    return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
        Endpoint endpoint = new StandardWebSocketHandlerAdapter(ContextWebSocketHandler.decorate(handler, contextView), session -> new TomcatWebSocketSession(session, handshakeInfo, bufferFactory));
        String requestURI = servletRequest.getRequestURI();
        DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
        config.setSubprotocols(subProtocol != null ? Collections.singletonList(subProtocol) : Collections.emptyList());
        WsServerContainer container = getContainer(servletRequest);
        try {
            container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
        } catch (Exception ex) {
            return Mono.error(ex);
        }
        return Mono.empty();
    }));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) RequestUpgradeStrategy(org.springframework.web.reactive.socket.server.RequestUpgradeStrategy) Mono(reactor.core.publisher.Mono) ServerContainer(jakarta.websocket.server.ServerContainer) Supplier(java.util.function.Supplier) TomcatWebSocketSession(org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Endpoint(jakarta.websocket.Endpoint) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) ServerHttpRequestDecorator(org.springframework.http.server.reactive.ServerHttpRequestDecorator) ServerHttpResponseDecorator(org.springframework.http.server.reactive.ServerHttpResponseDecorator) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) ContextWebSocketHandler(org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Assert(org.springframework.util.Assert) WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) TomcatWebSocketSession(org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Endpoint(jakarta.websocket.Endpoint) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 22 with ServerHttpRequest

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

the class WebHttpHandlerBuilderTests method httpHandlerDecorator.

@Test
void httpHandlerDecorator() {
    BiFunction<ServerHttpRequest, String, ServerHttpRequest> mutator = (req, value) -> req.mutate().headers(headers -> headers.add("My-Header", value)).build();
    AtomicBoolean success = new AtomicBoolean();
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(exchange -> {
        HttpHeaders headers = exchange.getRequest().getHeaders();
        assertThat(headers.get("My-Header")).containsExactlyInAnyOrder("1", "2", "3");
        success.set(true);
        return Mono.empty();
    }).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "1"), res)).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "2"), res)).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "3"), res)).build();
    httpHandler.handle(MockServerHttpRequest.get("/").build(), new MockServerHttpResponse()).block();
    assertThat(success.get()).isTrue();
}
Also used : HttpHandlerDecoratorFactory(org.springframework.http.server.reactive.HttpHandlerDecoratorFactory) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebHandler(org.springframework.web.server.WebHandler) Function(java.util.function.Function) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilter(org.springframework.web.server.WebFilter) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Order(org.springframework.core.annotation.Order) HttpHeaders(org.springframework.http.HttpHeaders) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) Flux(reactor.core.publisher.Flux) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Duration.ofMillis(java.time.Duration.ofMillis) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpHeaders(org.springframework.http.HttpHeaders) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 23 with ServerHttpRequest

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

the class ForwardedHeaderTransformerTests method removeOnly.

@Test
void removeOnly() {
    this.requestMutator.setRemoveOnly(true);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Forwarded", "for=192.0.2.60;proto=http;by=203.0.113.43");
    headers.add("X-Forwarded-Host", "example.com");
    headers.add("X-Forwarded-Port", "8080");
    headers.add("X-Forwarded-Proto", "http");
    headers.add("X-Forwarded-Prefix", "prefix");
    headers.add("X-Forwarded-Ssl", "on");
    headers.add("X-Forwarded-For", "203.0.113.195");
    ServerHttpRequest request = this.requestMutator.apply(getRequest(headers));
    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) Test(org.junit.jupiter.api.Test)

Example 24 with ServerHttpRequest

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

the class ForwardedHeaderTransformerTests method forwardedForNotPresent.

@Test
public void forwardedForNotPresent() throws URISyntaxException {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Forwarded", "host=84.198.58.199;proto=https");
    InetSocketAddress remoteAddress = new InetSocketAddress("example.client", 47011);
    ServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, new URI("https://example.com/a%20b?q=a%2Bb")).remoteAddress(remoteAddress).headers(headers).build();
    request = this.requestMutator.apply(request);
    assertThat(request.getRemoteAddress()).isEqualTo(remoteAddress);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) InetSocketAddress(java.net.InetSocketAddress) 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 25 with ServerHttpRequest

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

the class ForwardedHeaderTransformerTests method xForwardedPrefixShouldNotLeadToDecodedPath.

// gh-23305
@Test
void xForwardedPrefixShouldNotLeadToDecodedPath() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.add("X-Forwarded-Prefix", "/prefix");
    ServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, new URI("https://example.com/a%20b?q=a%2Bb")).headers(headers).build();
    request = this.requestMutator.apply(request);
    assertThat(request.getURI()).isEqualTo(new URI("https://example.com/prefix/a%20b?q=a%2Bb"));
    assertThat(request.getPath().value()).isEqualTo("/prefix/a%20b");
    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)

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