/** * Return the query string of the current request. This is useful for * building an HTML form action target in combination with the original * request path. */ public String getQueryString() { return this.exchange.getRequest().getURI().getQuery(); }
private String formatError(Throwable ex, ServerHttpRequest request) { String reason = ex.getClass().getSimpleName() + ": " + ex.getMessage(); String path = request.getURI().getRawPath(); return "Resolved [" + reason + "] for HTTP " + request.getMethod() + " " + path; }
/** * Return the request path of the request. This is useful as HTML form * action target, also in combination with the original query string. */ public String getRequestPath() { return this.exchange.getRequest().getURI().getPath(); }
@Override public URI uri() { return request().getURI(); }
private String formatRequest(ServerHttpRequest request) { String rawQuery = request.getURI().getRawQuery(); String query = StringUtils.hasText(rawQuery) ? "?" + rawQuery : ""; return "HTTP " + request.getMethod() + " \"" + request.getPath() + query + "\""; }
/** * Transform the given relative request path to an absolute path, * taking the path of the given request as a point of reference. * The resulting path is also cleaned from sequences like "path/..". * * @param path the relative path to transform * @param exchange the current exchange * @return the absolute request path for the given resource path */ protected String toAbsolutePath(String path, ServerWebExchange exchange) { String requestPath = exchange.getRequest().getURI().getPath(); String absolutePath = (path.startsWith("/") ? path : StringUtils.applyRelativePath(requestPath, path)); return StringUtils.cleanPath(absolutePath); }
private Object createEntity(@Nullable Object body, Class<?> entityType, ServerHttpRequest request) { return (RequestEntity.class.equals(entityType) ? new RequestEntity<>(body, request.getHeaders(), request.getMethod(), request.getURI()) : new HttpEntity<>(body, request.getHeaders())); }
private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttpRequest request, @Nullable String protocol, Map<String, Object> attributes) { URI uri = request.getURI(); // Copy request headers, as they might be pooled and recycled by // the server implementation once the handshake HTTP exchange is done. HttpHeaders headers = new HttpHeaders(); headers.addAll(request.getHeaders()); Mono<Principal> principal = exchange.getPrincipal(); String logPrefix = exchange.getLogPrefix(); InetSocketAddress remoteAddress = request.getRemoteAddress(); return new HandshakeInfo(uri, headers, principal, protocol, remoteAddress, attributes, logPrefix); }
public DefaultServerHttpRequestBuilder(ServerHttpRequest original) { Assert.notNull(original, "ServerHttpRequest is required"); this.uri = original.getURI(); this.httpMethodValue = original.getMethodValue(); this.body = original.getBody(); this.httpHeaders = HttpHeaders.writableHttpHeaders(original.getHeaders()); this.cookies = new LinkedMultiValueMap<>(original.getCookies().size()); copyMultiValueMap(original.getCookies(), this.cookies); this.originalRequest = original; }
@Override public Mono<Void> handle(ServerWebExchange exchange) { if (exchange.getRequest().getURI().getPath().equals("/form-parts")) { return assertGetFormParts(exchange); } return Mono.error(new AssertionError()); }
@Test public void xForwardedHeaders() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.add("X-Forwarded-Host", "84.198.58.199"); headers.add("X-Forwarded-Port", "443"); headers.add("X-Forwarded-Proto", "https"); headers.add("foo", "bar"); ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); assertEquals(new URI("https://84.198.58.199/path"), request.getURI()); assertForwardedHeadersRemoved(request); }
private ResponseCookie initSessionCookie( ServerWebExchange exchange, String id, Duration maxAge) { ResponseCookie.ResponseCookieBuilder cookieBuilder = ResponseCookie.from(this.cookieName, id) .path(exchange.getRequest().getPath().contextPath().value() + "/") .maxAge(maxAge) .httpOnly(true) .secure("https".equalsIgnoreCase(exchange.getRequest().getURI().getScheme())) .sameSite("Lax"); if (this.cookieInitializer != null) { this.cookieInitializer.accept(cookieBuilder); } return cookieBuilder.build(); }
@Test public void forwardedHeader() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.add("Forwarded", "host=84.198.58.199;proto=https"); ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); assertEquals(new URI("https://84.198.58.199/path"), request.getURI()); assertForwardedHeadersRemoved(request); }
@Override public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { URI uri = request.getURI(); assertEquals("https", uri.getScheme()); assertNotNull(uri.getHost()); assertNotEquals(-1, uri.getPort()); assertNotNull(request.getRemoteAddress()); assertEquals("/foo", uri.getPath()); assertEquals("param=bar", uri.getQuery()); return Mono.empty(); } }
@Test // SPR-16434 public void mutatePathWithEncodedQueryParams() throws Exception { ServerHttpRequest request = createHttpRequest("/path?name=%E6%89%8E%E6%A0%B9"); request = request.mutate().path("/mutatedPath").build(); assertEquals("/mutatedPath", request.getURI().getRawPath()); assertEquals("name=%E6%89%8E%E6%A0%B9", request.getURI().getRawQuery()); }
@Override public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { URI uri = request.getURI(); assertEquals("http", uri.getScheme()); assertNotNull(uri.getHost()); assertNotEquals(-1, uri.getPort()); assertNotNull(request.getRemoteAddress()); assertEquals("/foo", uri.getPath()); assertEquals("param=bar", uri.getQuery()); return Mono.empty(); } }
@Test // SPR-17525 public void shouldNotDoubleEncode() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.add("Forwarded", "host=84.198.58.199;proto=https"); ServerHttpRequest request = MockServerHttpRequest .method(HttpMethod.GET, new URI("http://example.com/a%20b?q=a%2Bb")) .headers(headers) .build(); request = this.requestMutator.apply(request); assertEquals(new URI("https://84.198.58.199/a%20b?q=a%2Bb"), request.getURI()); assertForwardedHeadersRemoved(request); }
@Test public void xForwardedPrefix() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.add("X-Forwarded-Prefix", "/prefix"); ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); assertEquals(new URI("http://example.com/prefix/path"), request.getURI()); assertEquals("/prefix/path", request.getPath().value()); assertForwardedHeadersRemoved(request); }
@Test public void xForwardedPrefixTrailingSlash() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.add("X-Forwarded-Prefix", "/prefix////"); ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); assertEquals(new URI("http://example.com/prefix/path"), request.getURI()); assertEquals("/prefix/path", request.getPath().value()); assertForwardedHeadersRemoved(request); }
@Test public void requestEntity() throws Exception { ServerWebExchange exchange = postExchange("line1"); ResolvableType type = forClassWithGenerics(RequestEntity.class, String.class); RequestEntity<String> requestEntity = resolveValue(exchange, type); assertEquals(exchange.getRequest().getMethod(), requestEntity.getMethod()); assertEquals(exchange.getRequest().getURI(), requestEntity.getUrl()); assertEquals(exchange.getRequest().getHeaders(), requestEntity.getHeaders()); assertEquals("line1", requestEntity.getBody()); }