Search in sources :

Example 46 with ServerHttpRequest

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

the class CorsUtilsTests method isCorsRequest.

@Test
public void isCorsRequest() {
    ServerHttpRequest request = get("http://domain.example/").header(HttpHeaders.ORIGIN, "https://domain.com").build();
    assertThat(CorsUtils.isCorsRequest(request)).isTrue();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 47 with ServerHttpRequest

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

the class CorsUtilsTests method isPreFlightRequest.

@Test
public void isPreFlightRequest() {
    ServerHttpRequest request = options("/").header(HttpHeaders.ORIGIN, "https://domain.com").header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET").build();
    assertThat(CorsUtils.isPreFlightRequest(request)).isTrue();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 48 with ServerHttpRequest

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

the class JettyRequestUpgradeStrategy method upgrade.

@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);
    ServletContext servletContext = servletRequest.getServletContext();
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    DataBufferFactory factory = response.bufferFactory();
    // Trigger WebFlux preCommit actions before upgrade
    return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
        JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(ContextWebSocketHandler.decorate(handler, contextView), session -> new JettyWebSocketSession(session, handshakeInfo, factory));
        JettyWebSocketCreator webSocketCreator = (upgradeRequest, upgradeResponse) -> {
            if (subProtocol != null) {
                upgradeResponse.setAcceptedSubProtocol(subProtocol);
            }
            return adapter;
        };
        JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
        try {
            container.upgrade(webSocketCreator, servletRequest, servletResponse);
        } catch (Exception ex) {
            return Mono.error(ex);
        }
        return Mono.empty();
    }));
}
Also used : JettyWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter) 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) Supplier(java.util.function.Supplier) ServerWebExchange(org.springframework.web.server.ServerWebExchange) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) 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) ServletContext(jakarta.servlet.ServletContext) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) JettyWebSocketSession(org.springframework.web.reactive.socket.adapter.JettyWebSocketSession) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) JettyWebSocketSession(org.springframework.web.reactive.socket.adapter.JettyWebSocketSession) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) JettyWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) ServletContext(jakarta.servlet.ServletContext) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 49 with ServerHttpRequest

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

the class HandshakeWebSocketService method handleRequest.

@Override
public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler handler) {
    ServerHttpRequest request = exchange.getRequest();
    HttpMethod method = request.getMethod();
    HttpHeaders headers = request.getHeaders();
    if (HttpMethod.GET != method) {
        return Mono.error(new MethodNotAllowedException(request.getMethod(), Collections.singleton(HttpMethod.GET)));
    }
    if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
        return handleBadRequest(exchange, "Invalid 'Upgrade' header: " + headers);
    }
    List<String> connectionValue = headers.getConnection();
    if (!connectionValue.contains("Upgrade") && !connectionValue.contains("upgrade")) {
        return handleBadRequest(exchange, "Invalid 'Connection' header: " + headers);
    }
    String key = headers.getFirst(SEC_WEBSOCKET_KEY);
    if (key == null) {
        return handleBadRequest(exchange, "Missing \"Sec-WebSocket-Key\" header");
    }
    String protocol = selectProtocol(headers, handler);
    return initAttributes(exchange).flatMap(attributes -> this.upgradeStrategy.upgrade(exchange, handler, protocol, () -> createHandshakeInfo(exchange, request, protocol, attributes)));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpMethod(org.springframework.http.HttpMethod)

Example 50 with ServerHttpRequest

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

the class ForwardedHeaderTransformerTests method xForwardedPrefix.

@Test
void xForwardedPrefix() 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)

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