Search in sources :

Example 6 with HttpHeaders

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());
    });
}
Also used : HttpHeaders(io.micronaut.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpServerConfiguration(io.micronaut.http.server.HttpServerConfiguration) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest) DetachedMockFactory(spock.mock.DetachedMockFactory)

Example 7 with HttpHeaders

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();
}
Also used : HttpMethod(io.micronaut.http.HttpMethod) Filter(io.micronaut.http.annotation.Filter) Publishers(io.micronaut.core.async.publisher.Publishers) ServerFilterChain(io.micronaut.http.filter.ServerFilterChain) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) HttpServerConfiguration(io.micronaut.http.server.HttpServerConfiguration) Publisher(org.reactivestreams.Publisher) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) HttpHeaders(io.micronaut.http.HttpHeaders) Collectors(java.util.stream.Collectors) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) ConversionContext(io.micronaut.core.convert.ConversionContext) Matcher(java.util.regex.Matcher) HttpStatus(io.micronaut.http.HttpStatus) ImmutableArgumentConversionContext(io.micronaut.core.convert.ImmutableArgumentConversionContext) Map(java.util.Map) HttpServerFilter(io.micronaut.http.filter.HttpServerFilter) HttpResponse(io.micronaut.http.HttpResponse) Optional(java.util.Optional) HttpRequest(io.micronaut.http.HttpRequest) ServerFilterPhase(io.micronaut.http.filter.ServerFilterPhase) Pattern(java.util.regex.Pattern) HttpHeaders(io.micronaut.http.HttpHeaders) List(java.util.List) HttpMethod(io.micronaut.http.HttpMethod)

Example 8 with HttpHeaders

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();
}
Also used : HttpHeaders(io.micronaut.http.HttpHeaders)

Example 9 with HttpHeaders

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);
}
Also used : HttpHeaders(io.micronaut.http.HttpHeaders)

Aggregations

HttpHeaders (io.micronaut.http.HttpHeaders)9 HttpServerConfiguration (io.micronaut.http.server.HttpServerConfiguration)3 HttpMethod (io.micronaut.http.HttpMethod)2 NettyHttpRequest (io.micronaut.http.server.netty.NettyHttpRequest)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 List (java.util.List)2 DetachedMockFactory (spock.mock.DetachedMockFactory)2 Publishers (io.micronaut.core.async.publisher.Publishers)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionContext (io.micronaut.core.convert.ConversionContext)1 ImmutableArgumentConversionContext (io.micronaut.core.convert.ImmutableArgumentConversionContext)1 StringUtils (io.micronaut.core.util.StringUtils)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpResponse (io.micronaut.http.HttpResponse)1 HttpStatus (io.micronaut.http.HttpStatus)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 Filter (io.micronaut.http.annotation.Filter)1 HttpServerFilter (io.micronaut.http.filter.HttpServerFilter)1 ServerFilterChain (io.micronaut.http.filter.ServerFilterChain)1