Search in sources :

Example 31 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project cdap by caskdata.

the class AuditLogHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (logEntry == null) {
        ctx.write(msg, promise);
        return;
    }
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        logEntry.setResponse(response);
        // If no need to log the response body, we can emit the audit log
        if (!logResponseBody) {
            emitAuditLog();
        }
    } else if (msg instanceof HttpContent && logResponseBody) {
        ByteBuf content = ((HttpContent) msg).content();
        if (content.isReadable()) {
            logEntry.appendResponseBody(content.toString(StandardCharsets.UTF_8));
        }
        // If need to log the response body, emit the audit log when all response contents are received
        if (msg instanceof LastHttpContent) {
            emitAuditLog();
        }
    }
    ctx.write(msg, promise);
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 32 with HttpContent

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

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project sidewinder by srotya.

the class HTTPDataPointDecoder method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
        if (ResourceMonitor.getInstance().isReject()) {
            logger.warning("Write rejected, insufficient memory");
            if (writeResponse(request, ctx)) {
                ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
            }
            return;
        }
        if (msg instanceof HttpRequest) {
            HttpRequest request = this.request = (HttpRequest) msg;
            if (HttpUtil.is100ContinueExpected(request)) {
                send100Continue(ctx);
            }
            QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
            path = queryStringDecoder.path();
            Map<String, List<String>> params = queryStringDecoder.parameters();
            if (!params.isEmpty()) {
                for (Entry<String, List<String>> p : params.entrySet()) {
                    String key = p.getKey();
                    if (key.equalsIgnoreCase("db")) {
                        dbName = p.getValue().get(0);
                    }
                }
            }
            if (path != null && path.contains("query")) {
                Gson gson = new Gson();
                JsonObject obj = new JsonObject();
                JsonArray ary = new JsonArray();
                ary.add(new JsonObject());
                obj.add("results", ary);
                responseString.append(gson.toJson(obj));
            }
        }
        if (msg instanceof HttpContent) {
            HttpContent httpContent = (HttpContent) msg;
            ByteBuf byteBuf = httpContent.content();
            if (byteBuf.isReadable()) {
                requestBuffer.append(byteBuf.toString(CharsetUtil.UTF_8));
            }
            if (msg instanceof LastHttpContent) {
                if (dbName == null) {
                    responseString.append("Invalid database null");
                    logger.severe("Invalid database null");
                } else {
                    String payload = requestBuffer.toString();
                    logger.fine("Request:" + payload);
                    List<Point> dps = InfluxDecoder.pointsFromString(dbName, payload);
                    meter.inc(dps.size());
                    for (Point dp : dps) {
                        try {
                            engine.writeDataPoint(dp);
                            logger.fine("Accepted:" + dp + "\t" + new Date(dp.getTimestamp()));
                        } catch (IOException e) {
                            logger.fine("Dropped:" + dp + "\t" + e.getMessage());
                            responseString.append("Dropped:" + dp);
                        }
                    }
                }
                if (writeResponse(request, ctx)) {
                    ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Point(com.srotya.sidewinder.core.rpc.Point) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) Date(java.util.Date) IOException(java.io.IOException) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) JsonArray(com.google.gson.JsonArray) List(java.util.List) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 34 with HttpContent

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

the class HttpRequestHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    try {
        if (msg instanceof HttpRequest) {
            currentRequest = (HttpRequest) msg;
            currentRequestPath = null;
            if (currentDecoder != null) {
                currentDecoder.destroy();
                currentDecoder = null;
            }
            if (currentRequest.getMethod() == HttpMethod.GET || currentRequest.getMethod() == HttpMethod.DELETE) {
                // directly delegate to the router
                ctx.fireChannelRead(currentRequest);
            } else if (currentRequest.getMethod() == HttpMethod.POST) {
                // POST comes in multiple objects. First the request, then the contents
                // keep the request and path for the remaining objects of the POST request
                currentRequestPath = new QueryStringDecoder(currentRequest.getUri(), ENCODING).path();
                currentDecoder = new HttpPostRequestDecoder(DATA_FACTORY, currentRequest, ENCODING);
            } else {
                throw new IOException("Unsupported HTTP method: " + currentRequest.getMethod().name());
            }
        } else if (currentDecoder != null && msg instanceof HttpContent) {
            // received new chunk, give it to the current decoder
            HttpContent chunk = (HttpContent) msg;
            currentDecoder.offer(chunk);
            try {
                while (currentDecoder.hasNext()) {
                    InterfaceHttpData data = currentDecoder.next();
                    if (data.getHttpDataType() == HttpDataType.FileUpload && tmpDir != null) {
                        DiskFileUpload file = (DiskFileUpload) data;
                        if (file.isCompleted()) {
                            String name = file.getFilename();
                            File target = new File(tmpDir, UUID.randomUUID() + "_" + name);
                            if (!tmpDir.exists()) {
                                logExternalUploadDirDeletion(tmpDir);
                                checkAndCreateUploadDir(tmpDir);
                            }
                            file.renameTo(target);
                            QueryStringEncoder encoder = new QueryStringEncoder(currentRequestPath);
                            encoder.addParam("filepath", target.getAbsolutePath());
                            encoder.addParam("filename", name);
                            currentRequest.setUri(encoder.toString());
                        }
                    }
                }
            } catch (EndOfDataDecoderException ignored) {
            }
            if (chunk instanceof LastHttpContent) {
                HttpRequest request = currentRequest;
                currentRequest = null;
                currentRequestPath = null;
                currentDecoder.destroy();
                currentDecoder = null;
                // fire next channel handler
                ctx.fireChannelRead(request);
            }
        } else {
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    } catch (Throwable t) {
        currentRequest = null;
        currentRequestPath = null;
        if (currentDecoder != null) {
            currentDecoder.destroy();
            currentDecoder = null;
        }
        if (ctx.channel().isActive()) {
            byte[] bytes = ExceptionUtils.stringifyException(t).getBytes(ENCODING);
            DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
            response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
            response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
            ctx.writeAndFlush(response);
        }
    }
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) EndOfDataDecoderException(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException) IOException(java.io.IOException) LastHttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent) HttpPostRequestDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder) DiskFileUpload(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload) InterfaceHttpData(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData) File(java.io.File) LastHttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent) HttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent) QueryStringEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder)

Example 35 with HttpContent

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

the class HttpPostRequestEncoder method readChunk.

/**
 * Returns the next available HttpChunk. The caller is responsible to test if this chunk is the last one (isLast()),
 * in order to stop calling this getMethod.
 *
 * @return the next available HttpChunk
 * @throws ErrorDataEncoderException
 *             if the encoding is in error
 */
@Override
public HttpContent readChunk(ByteBufAllocator allocator) throws Exception {
    if (isLastChunkSent) {
        return null;
    } else {
        HttpContent nextChunk = nextChunk();
        globalProgress += nextChunk.content().readableBytes();
        return nextChunk;
    }
}
Also used : LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)158 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)122 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)62 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)59 Test (org.junit.Test)59 ByteBuf (io.netty.buffer.ByteBuf)40 HttpResponse (io.netty.handler.codec.http.HttpResponse)37 HttpRequest (io.netty.handler.codec.http.HttpRequest)35 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)34 ArrayList (java.util.ArrayList)30 HttpObject (io.netty.handler.codec.http.HttpObject)28 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)24 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)20 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 Channel (io.netty.channel.Channel)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)15 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)14 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 IOException (java.io.IOException)13