Search in sources :

Example 41 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project crate by crate.

the class HttpBlobHandler method handleBlobRequest.

private void handleBlobRequest(HttpRequest request, @Nullable HttpContent content) throws IOException {
    if (possibleRedirect(request, index, digest)) {
        return;
    }
    HttpMethod method = request.method();
    if (method.equals(HttpMethod.GET)) {
        get(request, index, digest);
        reset();
    } else if (method.equals(HttpMethod.HEAD)) {
        head(request, index, digest);
    } else if (method.equals(HttpMethod.PUT)) {
        put(request, content, index, digest);
    } else if (method.equals(HttpMethod.DELETE)) {
        delete(request, index, digest);
    } else {
        simpleResponse(request, HttpResponseStatus.METHOD_NOT_ALLOWED);
    }
}
Also used : HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 42 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project crate by crate.

the class Netty4HttpServerTransport method buildCorsConfig.

// package private for testing
static Netty4CorsConfig buildCorsConfig(Settings settings) {
    if (SETTING_CORS_ENABLED.get(settings) == false) {
        return Netty4CorsConfigBuilder.forOrigins().disable().build();
    }
    String origin = SETTING_CORS_ALLOW_ORIGIN.get(settings);
    final Netty4CorsConfigBuilder builder;
    if (Strings.isNullOrEmpty(origin)) {
        builder = Netty4CorsConfigBuilder.forOrigins();
    } else if (origin.equals(ANY_ORIGIN)) {
        builder = Netty4CorsConfigBuilder.forAnyOrigin();
    } else {
        try {
            Pattern p = RestUtils.checkCorsSettingForRegex(origin);
            if (p == null) {
                builder = Netty4CorsConfigBuilder.forOrigins(RestUtils.corsSettingAsArray(origin));
            } else {
                builder = Netty4CorsConfigBuilder.forPattern(p);
            }
        } catch (PatternSyntaxException e) {
            throw new SettingsException("Bad regex in [" + SETTING_CORS_ALLOW_ORIGIN.getKey() + "]: [" + origin + "]", e);
        }
    }
    if (SETTING_CORS_ALLOW_CREDENTIALS.get(settings)) {
        builder.allowCredentials();
    }
    String[] strMethods = Strings.tokenizeToStringArray(SETTING_CORS_ALLOW_METHODS.get(settings), ",");
    HttpMethod[] methods = Arrays.stream(strMethods).map(HttpMethod::valueOf).toArray(HttpMethod[]::new);
    return builder.allowedRequestMethods(methods).maxAge(SETTING_CORS_MAX_AGE.get(settings)).allowedRequestHeaders(Strings.tokenizeToStringArray(SETTING_CORS_ALLOW_HEADERS.get(settings), ",")).shortCircuit().build();
}
Also used : Pattern(java.util.regex.Pattern) Netty4CorsConfigBuilder(org.elasticsearch.http.netty4.cors.Netty4CorsConfigBuilder) SettingsException(org.elasticsearch.common.settings.SettingsException) HttpMethod(io.netty.handler.codec.http.HttpMethod) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 43 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project rest.li by linkedin.

the class NettyRequestAdapter method toNettyRequest.

/**
 * Adapts a StreamRequest to Netty's HttpRequest
 * @param request  R2 stream request
 * @return Adapted HttpRequest.
 */
public static HttpRequest toNettyRequest(StreamRequest request) throws Exception {
    HttpMethod nettyMethod = HttpMethod.valueOf(request.getMethod());
    URL url = new URL(request.getURI().toString());
    String path = url.getFile();
    // it MUST be given as "/" (the server root).
    if (path.isEmpty()) {
        path = "/";
    }
    HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path);
    nettyRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
        // that contains a Transfer-Encoding header field.
        if (entry.getKey().equalsIgnoreCase(HttpHeaderNames.CONTENT_LENGTH.toString())) {
            continue;
        }
        nettyRequest.headers().set(entry.getKey(), entry.getValue());
    }
    nettyRequest.headers().set(HttpHeaderNames.HOST, url.getAuthority());
    // RFC 6265
    // When the user agent generates an HTTP/1.1 request, the user agent MUST
    // NOT attach more than one Cookie header field.
    String encodedCookieHeaderValues = CookieUtil.clientEncode(request.getCookies());
    if (encodedCookieHeaderValues != null) {
        nettyRequest.headers().set(HttpConstants.REQUEST_COOKIE_HEADER_NAME, encodedCookieHeaderValues);
    }
    return nettyRequest;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) AsciiString(io.netty.util.AsciiString) Map(java.util.Map) HttpMethod(io.netty.handler.codec.http.HttpMethod) URL(java.net.URL)

Example 44 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project elasticsearch by elastic.

the class Netty4HttpServerTransportTests method testCorsConfigWithDefaults.

public void testCorsConfigWithDefaults() {
    final Set<String> methods = Strings.commaDelimitedListToSet(SETTING_CORS_ALLOW_METHODS.getDefault(Settings.EMPTY));
    final Set<String> headers = Strings.commaDelimitedListToSet(SETTING_CORS_ALLOW_HEADERS.getDefault(Settings.EMPTY));
    final long maxAge = SETTING_CORS_MAX_AGE.getDefault(Settings.EMPTY);
    final Settings settings = Settings.builder().put(SETTING_CORS_ENABLED.getKey(), true).build();
    final Netty4CorsConfig corsConfig = Netty4HttpServerTransport.buildCorsConfig(settings);
    assertFalse(corsConfig.isAnyOriginSupported());
    assertEquals(Collections.emptySet(), corsConfig.origins().get());
    assertEquals(headers, corsConfig.allowedRequestHeaders());
    assertEquals(methods, corsConfig.allowedRequestMethods().stream().map(HttpMethod::name).collect(Collectors.toSet()));
    assertEquals(maxAge, corsConfig.maxAge());
    assertFalse(corsConfig.isCredentialsAllowed());
}
Also used : Netty4CorsConfig(org.elasticsearch.http.netty4.cors.Netty4CorsConfig) Matchers.containsString(org.hamcrest.Matchers.containsString) Strings.collectionToDelimitedString(org.elasticsearch.common.Strings.collectionToDelimitedString) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 45 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project elasticsearch by elastic.

the class Netty4HttpServerTransportTests method testCorsConfig.

public void testCorsConfig() {
    final Set<String> methods = new HashSet<>(Arrays.asList("get", "options", "post"));
    final Set<String> headers = new HashSet<>(Arrays.asList("Content-Type", "Content-Length"));
    // sometimes have a leading whitespace between comma delimited elements
    final String prefix = randomBoolean() ? " " : "";
    final Settings settings = Settings.builder().put(SETTING_CORS_ENABLED.getKey(), true).put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "*").put(SETTING_CORS_ALLOW_METHODS.getKey(), collectionToDelimitedString(methods, ",", prefix, "")).put(SETTING_CORS_ALLOW_HEADERS.getKey(), collectionToDelimitedString(headers, ",", prefix, "")).put(SETTING_CORS_ALLOW_CREDENTIALS.getKey(), true).build();
    final Netty4CorsConfig corsConfig = Netty4HttpServerTransport.buildCorsConfig(settings);
    assertTrue(corsConfig.isAnyOriginSupported());
    assertEquals(headers, corsConfig.allowedRequestHeaders());
    assertEquals(methods, corsConfig.allowedRequestMethods().stream().map(HttpMethod::name).collect(Collectors.toSet()));
}
Also used : Netty4CorsConfig(org.elasticsearch.http.netty4.cors.Netty4CorsConfig) Matchers.containsString(org.hamcrest.Matchers.containsString) Strings.collectionToDelimitedString(org.elasticsearch.common.Strings.collectionToDelimitedString) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings) HttpMethod(io.netty.handler.codec.http.HttpMethod) HashSet(java.util.HashSet)

Aggregations

HttpMethod (io.netty.handler.codec.http.HttpMethod)83 Test (org.junit.Test)44 HttpRequest (io.netty.handler.codec.http.HttpRequest)24 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)23 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)20 URI (java.net.URI)15 IOException (java.io.IOException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 DefaultHttpClient (org.jocean.http.client.impl.DefaultHttpClient)11 TestChannelCreator (org.jocean.http.client.impl.TestChannelCreator)11 TestChannelPool (org.jocean.http.client.impl.TestChannelPool)11 DefaultSignalClient (org.jocean.http.rosa.impl.DefaultSignalClient)11 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)11 Subscription (rx.Subscription)11 Action2 (rx.functions.Action2)11 ByteBuf (io.netty.buffer.ByteBuf)10 HttpVersion (io.netty.handler.codec.http.HttpVersion)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 Map (java.util.Map)9 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)8