@Override @Nullable public HttpMethod getMethod() { return getDelegate().getMethod(); }
/** * Check if any of the HTTP request methods match the given request and * return an instance that contains the matching HTTP request method only. * @param exchange the current exchange * @return the same instance if the condition is empty (unless the request * method is HTTP OPTIONS), a new condition with the matched request method, * or {@code null} if there is no match or the condition is empty and the * request method is OPTIONS. */ @Override @Nullable public RequestMethodsRequestCondition getMatchingCondition(ServerWebExchange exchange) { if (CorsUtils.isPreFlightRequest(exchange.getRequest())) { return matchPreFlight(exchange.getRequest()); } if (getMethods().isEmpty()) { if (RequestMethod.OPTIONS.name().equals(exchange.getRequest().getMethodValue())) { return null; // No implicit match for OPTIONS (we handle it) } return this; } return matchRequestMethod(exchange.getRequest().getMethod()); }
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; }
@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.getMethodValue(), 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)) ); }
private ServerHttpResponse prepareResponse(ServerHttpResponse response, ServerHttpRequest request) { return (request.getMethod() == HttpMethod.HEAD ? new HttpHeadResponseDecorator(response) : response); }
private String formatRequest(ServerHttpRequest request) { String rawQuery = request.getURI().getRawQuery(); String query = StringUtils.hasText(rawQuery) ? "?" + rawQuery : ""; return "HTTP " + request.getMethod() + " \"" + request.getPath() + query + "\""; }
@Nullable private HttpMethod getMethodToUse(ServerHttpRequest request, boolean isPreFlight) { return (isPreFlight ? request.getHeaders().getAccessControlRequestMethod() : request.getMethod()); }
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; }
@Override @Nullable public HttpMethod getMethod() { return getDelegate().getMethod(); }
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 String formatRequest(ServerHttpRequest request) { String rawQuery = request.getURI().getRawQuery(); String query = StringUtils.hasText(rawQuery) ? "?" + rawQuery : ""; return "HTTP " + request.getMethod() + " \"" + request.getPath() + query + "\""; }
@Nullable private HttpMethod getMethodToUse(ServerHttpRequest request, boolean isPreFlight) { return (isPreFlight ? request.getHeaders().getAccessControlRequestMethod() : request.getMethod()); }
@Override public Mono<Void> filter(ServerWebExchange exchange) { this.httpMethod = exchange.getRequest().getMethod(); return Mono.empty(); } }
/** * Returns {@code true} if the request is a valid CORS pre-flight one. */ public static boolean isPreFlightRequest(ServerHttpRequest request) { return (request.getMethod() == HttpMethod.OPTIONS && isCorsRequest(request) && request.getHeaders().get(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD) != null); }
/** * Transform an HTTP POST into another method based on {@code methodParamName}. * @param exchange the current server exchange * @param chain provides a way to delegate to the next filter * @return {@code Mono<Void>} to indicate when request processing is complete */ @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { if (exchange.getRequest().getMethod() != HttpMethod.POST) { return chain.filter(exchange); } return exchange.getFormData() .map(formData -> { String method = formData.getFirst(this.methodParamName); return StringUtils.hasLength(method) ? mapExchange(exchange, method) : exchange; }) .flatMap(chain::filter); }
@Override public final Mono<Void> writeTo(ServerWebExchange exchange, Context context) { writeStatusAndHeaders(exchange.getResponse()); Instant lastModified = Instant.ofEpochMilli(headers().getLastModified()); HttpMethod httpMethod = exchange.getRequest().getMethod(); if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(headers().getETag(), lastModified)) { return exchange.getResponse().setComplete(); } else { return writeToInternal(exchange, context); } }
return exchange.getRequest().getMethod();
boolean isHttpGetOrHead = SAFE_METHODS.contains(getRequest().getMethod()); if (this.notModified) { getResponse().setStatusCode(isHttpGetOrHead ?
@Test public void mutateRequest() throws Exception { SslInfo sslInfo = mock(SslInfo.class); ServerHttpRequest request = createHttpRequest("/").mutate().sslInfo(sslInfo).build(); assertSame(sslInfo, request.getSslInfo()); request = createHttpRequest("/").mutate().method(HttpMethod.DELETE).build(); assertEquals(HttpMethod.DELETE, request.getMethod()); String baseUri = "http://aaa.org:8080/a"; request = createHttpRequest(baseUri).mutate().uri(URI.create("http://bbb.org:9090/b")).build(); assertEquals("http://bbb.org:9090/b", request.getURI().toString()); request = createHttpRequest(baseUri).mutate().path("/b/c/d").build(); assertEquals("http://aaa.org:8080/b/c/d", request.getURI().toString()); request = createHttpRequest(baseUri).mutate().path("/app/b/c/d").contextPath("/app").build(); assertEquals("http://aaa.org:8080/app/b/c/d", request.getURI().toString()); assertEquals("/app", request.getPath().contextPath().value()); }
@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()); }