Search in sources :

Example 1 with HttpPostRequestEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder 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 2 with HttpPostRequestEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder 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 3 with HttpPostRequestEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project vertx-web by vert-x3.

the class HttpContext method sendRequest.

private void sendRequest() {
    Future<HttpClientResponse> responseFuture = Future.<HttpClientResponse>future().setHandler(ar -> {
        Context context = Vertx.currentContext();
        if (ar.succeeded()) {
            HttpClientResponse resp = ar.result();
            Future<HttpResponse<Object>> fut = Future.future();
            fut.setHandler(r -> {
                // We are running on a context (the HTTP client mandates it)
                context.runOnContext(v -> currentResponseHandler.handle(r));
            });
            resp.exceptionHandler(err -> {
                if (!fut.isComplete()) {
                    fut.fail(err);
                }
            });
            resp.pause();
            ((BodyCodec<Object>) request.codec).create(ar2 -> {
                resp.resume();
                if (ar2.succeeded()) {
                    BodyStream<Object> stream = ar2.result();
                    stream.exceptionHandler(err -> {
                        if (!fut.isComplete()) {
                            fut.fail(err);
                        }
                    });
                    resp.endHandler(v -> {
                        if (!fut.isComplete()) {
                            stream.end();
                            if (stream.result().succeeded()) {
                                fut.complete(new HttpResponseImpl<>(resp, null, stream.result().result()));
                            } else {
                                fut.fail(stream.result().cause());
                            }
                        }
                    });
                    Pump responsePump = Pump.pump(resp, stream);
                    responsePump.start();
                } else {
                    currentResponseHandler.handle(Future.failedFuture(ar2.cause()));
                }
            });
        } else {
            currentResponseHandler.handle(Future.failedFuture(ar.cause()));
        }
    });
    HttpClientRequest req;
    String requestURI;
    if (request.queryParams() != null && request.queryParams().size() > 0) {
        QueryStringEncoder enc = new QueryStringEncoder(request.uri);
        request.queryParams().forEach(param -> enc.addParam(param.getKey(), param.getValue()));
        requestURI = enc.toString();
    } else {
        requestURI = request.uri;
    }
    int port = request.port;
    String host = request.host;
    if (request.ssl != request.options.isSsl()) {
        req = request.client.client.request(request.method, new RequestOptions().setSsl(request.ssl).setHost(host).setPort(port).setURI(requestURI));
    } else {
        if (request.protocol != null && !request.protocol.equals("http") && !request.protocol.equals("https")) {
            // we have to create an abs url again to parse it in HttpClient
            try {
                URI uri = new URI(request.protocol, null, host, port, requestURI, null, null);
                req = request.client.client.requestAbs(request.method, uri.toString());
            } catch (URISyntaxException ex) {
                currentResponseHandler.handle(Future.failedFuture(ex));
                return;
            }
        } else {
            req = request.client.client.request(request.method, port, host, requestURI);
        }
    }
    if (request.virtualHost != null) {
        String virtalHost = request.virtualHost;
        if (port != 80) {
            virtalHost += ":" + port;
        }
        req.setHost(virtalHost);
    }
    req.setFollowRedirects(request.followRedirects);
    if (request.headers != null) {
        req.headers().addAll(request.headers);
    }
    req.handler(responseFuture::tryComplete);
    if (request.timeout > 0) {
        req.setTimeout(request.timeout);
    }
    if (body != null) {
        if (contentType != null) {
            String prev = req.headers().get(HttpHeaders.CONTENT_TYPE);
            if (prev == null) {
                req.putHeader(HttpHeaders.CONTENT_TYPE, contentType);
            } else {
                contentType = prev;
            }
        }
        if (body instanceof ReadStream<?>) {
            ReadStream<Buffer> stream = (ReadStream<Buffer>) body;
            if (request.headers == null || !request.headers.contains(HttpHeaders.CONTENT_LENGTH)) {
                req.setChunked(true);
            }
            Pump pump = Pump.pump(stream, req);
            req.exceptionHandler(err -> {
                pump.stop();
                stream.endHandler(null);
                stream.resume();
                responseFuture.tryFail(err);
            });
            stream.exceptionHandler(err -> {
                req.reset();
                responseFuture.tryFail(err);
            });
            stream.endHandler(v -> {
                req.exceptionHandler(responseFuture::tryFail);
                req.end();
                pump.stop();
            });
            pump.start();
        } else {
            Buffer buffer;
            if (body instanceof Buffer) {
                buffer = (Buffer) body;
            } else if (body instanceof MultiMap) {
                try {
                    MultiMap attributes = (MultiMap) body;
                    boolean multipart = "multipart/form-data".equals(contentType);
                    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpMethod.POST, "/");
                    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, multipart);
                    for (Map.Entry<String, String> attribute : attributes) {
                        encoder.addBodyAttribute(attribute.getKey(), attribute.getValue());
                    }
                    encoder.finalizeRequest();
                    for (String headerName : request.headers().names()) {
                        req.putHeader(headerName, request.headers().get(headerName));
                    }
                    if (encoder.isChunked()) {
                        buffer = Buffer.buffer();
                        while (true) {
                            HttpContent chunk = encoder.readChunk(new UnpooledByteBufAllocator(false));
                            ByteBuf content = chunk.content();
                            if (content.readableBytes() == 0) {
                                break;
                            }
                            buffer.appendBuffer(Buffer.buffer(content));
                        }
                    } else {
                        ByteBuf content = request.content();
                        buffer = Buffer.buffer(content);
                    }
                } catch (Exception e) {
                    throw new VertxException(e);
                }
            } else if (body instanceof JsonObject) {
                buffer = Buffer.buffer(((JsonObject) body).encode());
            } else {
                buffer = Buffer.buffer(Json.encode(body));
            }
            req.exceptionHandler(responseFuture::tryFail);
            req.end(buffer);
        }
    } else {
        req.exceptionHandler(responseFuture::tryFail);
        req.end();
    }
}
Also used : RequestOptions(io.vertx.core.http.RequestOptions) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) JsonObject(io.vertx.core.json.JsonObject) URISyntaxException(java.net.URISyntaxException) ByteBuf(io.netty.buffer.ByteBuf) URI(java.net.URI) MultiMap(io.vertx.core.MultiMap) UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) VertxException(io.vertx.core.VertxException) HttpClientResponse(io.vertx.core.http.HttpClientResponse) ReadStream(io.vertx.core.streams.ReadStream) Context(io.vertx.core.Context) BodyCodec(io.vertx.ext.web.codec.BodyCodec) Buffer(io.vertx.core.buffer.Buffer) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpResponse(io.vertx.ext.web.client.HttpResponse) Pump(io.vertx.core.streams.Pump) VertxException(io.vertx.core.VertxException) URISyntaxException(java.net.URISyntaxException) HttpClientRequest(io.vertx.core.http.HttpClientRequest) JsonObject(io.vertx.core.json.JsonObject) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 4 with HttpPostRequestEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project ballerina by ballerina-lang.

the class Util method setCarbonMessageWithMultiparts.

/**
 * Add body parts to carbon message.
 *
 * @param request Ballerina request struct
 * @param cMsg    Represent carbon message
 */
private static void setCarbonMessageWithMultiparts(BStruct request, HTTPTestRequest cMsg) {
    prepareRequestWithMultiparts(cMsg, request);
    try {
        HttpPostRequestEncoder nettyEncoder = (HttpPostRequestEncoder) request.getNativeData(MULTIPART_ENCODER);
        addMultipartsToCarbonMessage(cMsg, nettyEncoder);
    } catch (Exception e) {
        log.error("Error occurred while adding multiparts to carbon message in setCarbonMessageWithMultiparts", e.getMessage());
    }
}
Also used : HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) IOException(java.io.IOException)

Example 5 with HttpPostRequestEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project alien4cloud by alien4cloud.

the class WebSocketClientHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    if (this.authenticationUrl != null) {
        HttpRequest loginRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, this.authenticationUrl);
        loginRequest.headers().set(new AsciiString("host"), this.host);
        HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(loginRequest, false);
        bodyRequestEncoder.addBodyAttribute("j_username", user);
        bodyRequestEncoder.addBodyAttribute("j_password", password);
        bodyRequestEncoder.addBodyAttribute("submit", "Login");
        loginRequest = bodyRequestEncoder.finalizeRequest();
        if (log.isDebugEnabled()) {
            log.debug("Authentication request for user {} to {}", this.user, this.authenticationUrl);
        }
        ctx.writeAndFlush(loginRequest);
    } else {
        handShaker.handshake(ctx.channel());
    }
}
Also used : HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) AsciiString(io.netty.handler.codec.AsciiString)

Aggregations

HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)21 HttpRequest (io.netty.handler.codec.http.HttpRequest)13 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)10 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)8 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)6 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)6 MemoryFileUpload (io.netty.handler.codec.http.multipart.MemoryFileUpload)5 Test (org.junit.Test)5 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)4 HttpContent (io.netty.handler.codec.http.HttpContent)4 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)4 ResponseParts (com.github.ambry.rest.NettyClient.ResponseParts)3 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)3 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 ByteBuffer (java.nio.ByteBuffer)3 Account (com.github.ambry.account.Account)2 Container (com.github.ambry.account.Container)2 NettyConfig (com.github.ambry.config.NettyConfig)2 Channel (io.netty.channel.Channel)2