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);
}
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();
}
}
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();
}
}
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);
}
}
}
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;
}
}
Aggregations