Search in sources :

Example 16 with HttpMethod

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

the class CopyForcingByteBuf method backPressureTest.

/**
 * Tests {@link NettyRequest#addContent(HttpContent)} and
 * {@link NettyRequest#readInto(AsyncWritableChannel, Callback)} with different digest algorithms (including a test
 * with no digest algorithm) and checks that back pressure is applied correctly.
 * @throws Exception
 */
@Test
public void backPressureTest() throws Exception {
    String[] digestAlgorithms = { "", "MD5", "SHA-1", "SHA-256" };
    HttpMethod[] methods = { HttpMethod.POST, HttpMethod.PUT };
    for (HttpMethod method : methods) {
        for (String digestAlgorithm : digestAlgorithms) {
            backPressureTest(digestAlgorithm, true, method);
            backPressureTest(digestAlgorithm, false, method);
        }
    }
}
Also used : HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 17 with HttpMethod

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

the class NettyServerHandler method channelRead.

/*
	 * 收到消息时,返回信息
	 */
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof FullHttpRequest)) {
        result = "未知请求!";
        send(ctx, result, HttpResponseStatus.BAD_REQUEST);
        return;
    }
    FullHttpRequest httpRequest = (FullHttpRequest) msg;
    try {
        // 获取路径
        String path = httpRequest.uri();
        // 获取参数
        String body = getBody(httpRequest);
        // 获取请求方法
        HttpMethod method = httpRequest.method();
        // 如果不是这个路径,就直接返回错误
        if (!"/test".equalsIgnoreCase(path)) {
            result = "非法请求!";
            send(ctx, result, HttpResponseStatus.BAD_REQUEST);
            return;
        }
        System.out.println("接收到:" + method + " 请求");
        // 如果是GET请求
        if (HttpMethod.GET.equals(method)) {
            // 接受到的消息,做业务逻辑处理...
            System.out.println("body:" + body);
            result = "GET请求";
            send(ctx, result, HttpResponseStatus.OK);
            return;
        }
        // 如果是POST请求
        if (HttpMethod.POST.equals(method)) {
            // 接受到的消息,做业务逻辑处理...
            System.out.println("body:" + body);
            result = "POST请求";
            send(ctx, result, HttpResponseStatus.OK);
            return;
        }
        // 如果是PUT请求
        if (HttpMethod.PUT.equals(method)) {
            // 接受到的消息,做业务逻辑处理...
            System.out.println("body:" + body);
            result = "PUT请求";
            send(ctx, result, HttpResponseStatus.OK);
            return;
        }
        // 如果是DELETE请求
        if (HttpMethod.DELETE.equals(method)) {
            // 接受到的消息,做业务逻辑处理...
            System.out.println("body:" + body);
            result = "DELETE请求";
            send(ctx, result, HttpResponseStatus.OK);
            return;
        }
    } catch (Exception e) {
        System.out.println("处理请求失败!");
        e.printStackTrace();
    } finally {
        // 释放请求
        httpRequest.release();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 18 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method getRequestBuilder_with_circuit_breaker_args_sets_values_as_expected.

@DataProvider(value = { "CONNECT", "DELETE", "GET", "HEAD", "POST", "OPTIONS", "PUT", "PATCH", "TRACE", "FOO_METHOD_DOES_NOT_EXIST" }, splitBy = "\\|")
@Test
public void getRequestBuilder_with_circuit_breaker_args_sets_values_as_expected(String methodName) {
    CircuitBreaker<Response> cbMock = mock(CircuitBreaker.class);
    List<Pair<Optional<CircuitBreaker<Response>>, Boolean>> variations = Arrays.asList(Pair.of(Optional.empty(), true), Pair.of(Optional.empty(), false), Pair.of(Optional.of(cbMock), true), Pair.of(Optional.of(cbMock), false));
    variations.forEach(variation -> {
        // given
        String url = "http://localhost/some/path";
        HttpMethod method = HttpMethod.valueOf(methodName);
        Optional<CircuitBreaker<Response>> cbOpt = variation.getLeft();
        boolean disableCb = variation.getRight();
        // when
        RequestBuilderWrapper rbw = helperSpy.getRequestBuilder(url, method, cbOpt, disableCb);
        // then
        verifyRequestBuilderWrapperGeneratedAsExpected(rbw, url, methodName, cbOpt, disableCb);
    });
}
Also used : Response(com.ning.http.client.Response) CircuitBreaker(com.nike.fastbreak.CircuitBreaker) Matchers.anyString(org.mockito.Matchers.anyString) HttpMethod(io.netty.handler.codec.http.HttpMethod) Pair(com.nike.internal.util.Pair) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 19 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project turbo-rpc by hank-whu.

the class NettyRestHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, final FullHttpRequest httpRequest) throws Exception {
    boolean keepAlive = HttpUtil.isKeepAlive(httpRequest);
    String uri = httpRequest.uri();
    HttpMethod httpMethod = httpRequest.method();
    int index = uri.indexOf('&', invokerFactory.restPrefix.length());
    if (index < 0) {
        index = uri.length();
    }
    if (invokerFactory.restPrefix.length() >= index) {
        if (logger.isInfoEnabled()) {
            logger.info("not support this method " + toString(httpRequest));
        }
        doRequestFilter(httpRequest, null);
        ctx.write(new RestHttpResponse(null, httpRequest, NOT_FOUND, NOT_SUPPORT_THIS_METHOD, keepAlive), ctx.voidPromise());
        return;
    }
    String restPath = uri.substring(invokerFactory.restPrefix.length(), index);
    final Invoker<CompletableFuture<?>> invoker = invokerFactory.get(restPath);
    CompletableFuture<?> future = null;
    try {
        if (invoker == null) {
            if (logger.isInfoEnabled()) {
                logger.info("not support this method " + toString(httpRequest));
            }
            doRequestFilter(httpRequest, null);
            ctx.write(new RestHttpResponse(null, httpRequest, NOT_FOUND, NOT_SUPPORT_THIS_METHOD, keepAlive), ctx.voidPromise());
            return;
        }
        boolean allowHandle = doRequestFilter(httpRequest, invoker);
        if (!allowHandle) {
            ctx.write(new RestHttpResponse(invoker, httpRequest, SERVICE_UNAVAILABLE, RestServerFilter.SERVER_FILTER_DENY, keepAlive), ctx.voidPromise());
            return;
        }
        Object params = null;
        if (httpMethod == HttpMethod.GET) {
            params = HttpParamExtractor.extractFromQueryPath(invoker, uri, index);
        } else if (httpMethod == HttpMethod.POST) {
            params = HttpParamExtractor.extractFromBody(invoker, jsonMapper, httpRequest.content());
        } else {
            if (logger.isInfoEnabled()) {
                logger.info("only support get and post " + toString(httpRequest));
            }
            ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, ONLY_SUPPORT_GET_POST, keepAlive), ctx.voidPromise());
            return;
        }
        if (params == null) {
            future = invoker.invoke();
        } else if (params instanceof MethodParam) {
            future = invoker.invoke((MethodParam) params);
        } else if (params instanceof Object[]) {
            future = invoker.invoke((Object[]) params);
        } else {
            future = invoker.invoke((Object) params);
        }
    } catch (Throwable e) {
        if (logger.isWarnEnabled()) {
            logger.warn(uri + " error ", e);
        }
        ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, e, keepAlive), ctx.voidPromise());
        return;
    }
    if (future == null) {
        if (logger.isWarnEnabled()) {
            logger.warn("unknown error " + toString(httpRequest));
        }
        ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, UNKNOWN, keepAlive), ctx.voidPromise());
        return;
    }
    future.whenComplete((result, throwable) -> {
        if (result != null) {
            ctx.write(new RestHttpResponse(invoker, httpRequest, OK, result, keepAlive), ctx.voidPromise());
        } else if (throwable != null) {
            ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, throwable, keepAlive), ctx.voidPromise());
        } else {
            ctx.write(new RestHttpResponse(invoker, httpRequest, INTERNAL_SERVER_ERROR, UNKNOWN, keepAlive), ctx.voidPromise());
        }
    });
}
Also used : MethodParam(rpc.turbo.param.MethodParam) CompletableFuture(java.util.concurrent.CompletableFuture) RestHttpResponse(rpc.turbo.transport.server.rest.protocol.RestHttpResponse) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 20 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project turbo-rpc by hank-whu.

the class NettyRestHandler method toString.

private String toString(FullHttpRequest httpRequest) {
    String uri = httpRequest.uri();
    HttpMethod httpMethod = httpRequest.method();
    return httpMethod.name() + " " + uri;
}
Also used : HttpMethod(io.netty.handler.codec.http.HttpMethod)

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