Search in sources :

Example 36 with HttpRequest

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

the class PublicAccessLogHandlerTest method doRequestHandleWithMultipleRequest.

/**
 * Does a test to see that two consecutive requests without sending last http content for first request fails
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param uri Uri to be used during the request
 * @param useSSL {@code true} to test SSL logging.
 * @throws Exception
 */
private void doRequestHandleWithMultipleRequest(HttpMethod httpMethod, String uri, boolean useSSL) throws Exception {
    EmbeddedChannel channel = createChannel(useSSL);
    // contains one logged request header
    HttpHeaders headers1 = new DefaultHttpHeaders();
    headers1.add(HttpHeaderNames.CONTENT_LENGTH, new Random().nextLong());
    HttpRequest request = RestTestUtils.createRequest(httpMethod, uri, headers1);
    HttpUtil.setKeepAlive(request, true);
    channel.writeInbound(request);
    // contains one logged and not logged header
    HttpHeaders headers2 = new DefaultHttpHeaders();
    headers2.add(NOT_LOGGED_HEADER_KEY + "1", "headerValue1");
    headers2.add(HttpHeaderNames.CONTENT_LENGTH, new Random().nextLong());
    // sending another request w/o sending last http content
    request = RestTestUtils.createRequest(httpMethod, uri, headers2);
    HttpUtil.setKeepAlive(request, true);
    sendRequestCheckResponse(channel, request, uri, headers2, false, false, useSSL);
    Assert.assertTrue("Channel should not be closed ", channel.isOpen());
    // verify that headers from first request is not found in public access log
    String lastLogEntry = publicAccessLogger.getLastPublicAccessLogEntry();
    // verify request headers
    verifyPublicAccessLogEntryForRequestHeaders(lastLogEntry, headers1, request.method(), false);
    channel.close();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Random(java.util.Random) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel)

Example 37 with HttpRequest

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

the class FrontendIntegrationTest method multipartPostGetHeadTest.

/**
 * Tests multipart POST and verifies it via GET operations.
 * @throws Exception
 */
@Test
public void multipartPostGetHeadTest() throws Exception {
    Account refAccount = ACCOUNT_SERVICE.createAndAddRandomAccount();
    Container refContainer = refAccount.getContainerById(Container.DEFAULT_PUBLIC_CONTAINER_ID);
    doPostGetHeadDeleteTest(0, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
    doPostGetHeadDeleteTest(FRONTEND_CONFIG.frontendChunkedGetResponseThresholdInBytes * 3, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
    // failure case
    // size of content being POSTed is higher than what is allowed via multipart/form-data
    long maxAllowedSizeBytes = new NettyConfig(FRONTEND_VERIFIABLE_PROPS).nettyMultipartPostMaxSizeBytes;
    ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes((int) maxAllowedSizeBytes + 1));
    HttpHeaders headers = new DefaultHttpHeaders();
    setAmbryHeadersForPut(headers, 7200, !refContainer.isCacheable(), refAccount.getName(), "application/octet-stream", null, refAccount.getName(), refContainer.getName());
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, ByteBuffer.allocate(0));
    ResponseParts responseParts = nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status());
    assertTrue("No Date header", response.headers().getTimeMillis(HttpHeaderNames.DATE, -1) != -1);
    assertFalse("Channel should not be active", HttpUtil.isKeepAlive(response));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Account(com.github.ambry.account.Account) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Container(com.github.ambry.account.Container) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts) NettyConfig(com.github.ambry.config.NettyConfig) ByteBuffer(java.nio.ByteBuffer) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 38 with HttpRequest

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

the class FrontendIntegrationTest method multipartPostBlobAndVerify.

/**
 * Posts a blob with the given {@code headers} and {@code content}.
 * @param headers the headers required.
 * @param content the content of the blob.
 * @param usermetadata the {@link ByteBuffer} that represents user metadata
 * @return the blob ID of the blob.
 * @throws Exception
 */
private String multipartPostBlobAndVerify(HttpHeaders headers, ByteBuffer content, ByteBuffer usermetadata) throws Exception {
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, usermetadata);
    ResponseParts responseParts = nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
    return verifyPostAndReturnBlobId(responseParts);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts)

Example 39 with HttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project async-http-client by AsyncHttpClient.

the class NettyConnectListener method writeRequest.

private void writeRequest(Channel channel) {
    if (futureIsAlreadyCancelled(channel)) {
        return;
    }
    if (LOGGER.isDebugEnabled()) {
        HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
        LOGGER.debug("Using new Channel '{}' for '{}' to '{}'", channel, httpRequest.method(), httpRequest.uri());
    }
    Channels.setAttribute(channel, future);
    channelManager.registerOpenChannel(channel);
    future.attachChannel(channel, false);
    requestSender.writeRequest(future, channel);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest)

Example 40 with HttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project tesla by linking12.

the class BlackURLHttpRequestFilter method doFilter.

@Override
public HttpResponse doFilter(HttpRequest originalRequest, HttpObject httpObject, ChannelHandlerContext channelHandlerContext) {
    if (httpObject instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) httpObject;
        String url = httpRequest.uri();
        int index = url.indexOf("?");
        if (index > -1) {
            url = url.substring(0, index);
        }
        List<Pattern> patterns = super.getRule(this);
        for (Pattern pattern : patterns) {
            Matcher matcher = pattern.matcher(url);
            if (matcher.find()) {
                super.writeFilterLog(url, BlackIpHttpRequesFilter.class, pattern.pattern());
                return super.createResponse(HttpResponseStatus.FORBIDDEN, originalRequest);
            }
        }
    }
    return null;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Aggregations

HttpRequest (io.netty.handler.codec.http.HttpRequest)283 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)104 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)95 Test (org.junit.Test)83 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)66 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)50 HttpResponse (io.netty.handler.codec.http.HttpResponse)49 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)34 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)34 ByteBuf (io.netty.buffer.ByteBuf)32 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)31 HttpContent (io.netty.handler.codec.http.HttpContent)30 Test (org.junit.jupiter.api.Test)30 URI (java.net.URI)29 Channel (io.netty.channel.Channel)28 HttpMethod (io.netty.handler.codec.http.HttpMethod)26 IOException (java.io.IOException)25 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)24 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)19 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)19