use of io.micronaut.http.HttpHeaders in project micronaut-core by micronaut-projects.
the class HttpRequestTest method testForEach.
public void testForEach() {
final DefaultFullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpMethod.GET, "/test");
nettyRequest.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
HttpRequest<?> request = new NettyHttpRequest(nettyRequest, new DetachedMockFactory().Mock(ChannelHandlerContext.class), ConversionService.SHARED, new HttpServerConfiguration());
final HttpHeaders headers = request.getHeaders();
headers.forEach((name, values) -> {
assertEquals(HttpHeaders.CONTENT_TYPE, name);
assertEquals(1, values.size());
assertEquals(MediaType.APPLICATION_JSON, values.iterator().next());
});
}
use of io.micronaut.http.HttpHeaders in project micronaut-core by micronaut-projects.
the class CorsFilter method handleRequest.
/**
* Handles a CORS {@link HttpRequest}.
*
* @param request The {@link HttpRequest} object
* @return An optional {@link MutableHttpResponse}. The request should proceed normally if empty
*/
protected Optional<MutableHttpResponse<?>> handleRequest(HttpRequest request) {
HttpHeaders headers = request.getHeaders();
Optional<String> originHeader = headers.getOrigin();
if (originHeader.isPresent()) {
String requestOrigin = originHeader.get();
boolean preflight = CorsUtil.isPreflightRequest(request);
Optional<CorsOriginConfiguration> optionalConfig = getConfiguration(requestOrigin);
if (optionalConfig.isPresent()) {
CorsOriginConfiguration config = optionalConfig.get();
HttpMethod requestMethod = request.getMethod();
List<HttpMethod> allowedMethods = config.getAllowedMethods();
if (!isAnyMethod(allowedMethods)) {
HttpMethod methodToMatch = preflight ? headers.getFirst(ACCESS_CONTROL_REQUEST_METHOD, CONVERSION_CONTEXT_HTTP_METHOD).orElse(requestMethod) : requestMethod;
if (allowedMethods.stream().noneMatch(method -> method.equals(methodToMatch))) {
return Optional.of(HttpResponse.status(HttpStatus.FORBIDDEN));
}
}
if (preflight) {
Optional<List<String>> accessControlHeaders = headers.get(ACCESS_CONTROL_REQUEST_HEADERS, ConversionContext.LIST_OF_STRING);
List<String> allowedHeaders = config.getAllowedHeaders();
if (!isAny(allowedHeaders) && accessControlHeaders.isPresent()) {
if (!accessControlHeaders.get().stream().allMatch(header -> allowedHeaders.stream().anyMatch(allowedHeader -> allowedHeader.equalsIgnoreCase(header.trim())))) {
return Optional.of(HttpResponse.status(HttpStatus.FORBIDDEN));
}
}
MutableHttpResponse<Object> ok = HttpResponse.ok();
handleResponse(request, ok);
return Optional.of(ok);
}
}
}
return Optional.empty();
}
use of io.micronaut.http.HttpHeaders in project micronaut-core by micronaut-projects.
the class CorsUtil method isPreflightRequest.
/**
* @param request The {@link HttpRequest} object
* @return Return whether this request is a pre-flight request
*/
static boolean isPreflightRequest(HttpRequest request) {
HttpHeaders headers = request.getHeaders();
Optional<String> origin = headers.getOrigin();
return origin.isPresent() && headers.contains(ACCESS_CONTROL_REQUEST_METHOD) && HttpMethod.OPTIONS == request.getMethod();
}
use of io.micronaut.http.HttpHeaders in project micronaut-core by micronaut-projects.
the class DefaultHttpHostResolver method getConfiguredHost.
/**
* @param request The current request
* @param schemeHeader The scheme or protocol header name
* @param hostHeader The host header name
* @param portHeader The port header name
* @param isPortInHost If the port can be part of the host value
* @return The configured host
*/
protected String getConfiguredHost(HttpRequest request, String schemeHeader, String hostHeader, String portHeader, boolean isPortInHost) {
HttpHeaders headers = request.getHeaders();
String scheme = null;
if (schemeHeader != null) {
scheme = headers.get(schemeHeader);
}
if (scheme == null) {
scheme = request.getUri().getScheme();
}
if (scheme == null && embeddedServer != null) {
scheme = embeddedServer.get().getScheme();
}
String host = null;
if (hostHeader != null) {
host = headers.get(hostHeader);
}
if (host == null) {
host = request.getUri().getHost();
}
if (host == null && embeddedServer != null) {
host = embeddedServer.get().getHost();
}
Integer port;
if (isPortInHost && host != null && host.contains(":")) {
String[] parts = host.split(":");
host = parts[0].trim();
port = Integer.valueOf(parts[1].trim());
} else if (portHeader != null) {
port = headers.get(portHeader, Integer.class).orElse(null);
} else {
port = request.getUri().getPort();
if (port < 0) {
port = null;
}
}
return createHost(scheme, host, port);
}
Aggregations