use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project camel by apache.
the class HttpServerMultiplexChannelHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
Attribute<HttpServerChannelHandler> attr = ctx.channel().attr(SERVER_HANDLER_KEY);
HttpServerChannelHandler handler = attr.get();
if (handler != null) {
handler.exceptionCaught(ctx, cause);
} else {
if (cause instanceof ClosedChannelException) {
// The channel is closed so we do nothing here
LOG.debug("Channel already closed. Ignoring this exception.");
return;
} else {
// we cannot throw the exception here
LOG.warn("HttpServerChannelHandler is not found as attachment to handle exception, send 404 back to the client.", cause);
// Now we just send 404 back to the client
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
response.headers().set(Exchange.CONTENT_LENGTH, 0);
ctx.writeAndFlush(response);
ctx.close();
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project proxyee-down by monkeyWie.
the class HttpDownUtil method getTaskInfo.
/**
* 检测是否支持断点下载
*/
public static TaskInfo getTaskInfo(HttpRequest httpRequest, HttpHeaders resHeaders, ProxyConfig proxyConfig, SslContext clientSslCtx, NioEventLoopGroup loopGroup) throws Exception {
HttpResponse httpResponse = null;
if (resHeaders == null) {
httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
// 处理重定向
if ((httpResponse.status().code() + "").indexOf("30") == 0) {
// TODO 302重定向乱码 https://link.gimhoy.com/googledrive/aHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL29wZW4/aWQ9MThlVmNKeEhwaE40RUpGTUowSk10bWNXOVhCcWJhVE1k.jpg
String redirectUrl = httpResponse.headers().get(HttpHeaderNames.LOCATION);
HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest;
requestInfo.headers().remove("Host");
requestInfo.setUri(redirectUrl);
RequestProto requestProto = ProtoUtil.getRequestProto(requestInfo);
requestInfo.headers().set("Host", requestProto.getHost());
requestInfo.setRequestProto(requestProto);
return getTaskInfo(httpRequest, null, proxyConfig, clientSslCtx, loopGroup);
}
resHeaders = httpResponse.headers();
}
TaskInfo taskInfo = new TaskInfo().setId(UUID.randomUUID().toString()).setFileName(getDownFileName(httpRequest, resHeaders)).setTotalSize(getDownFileSize(resHeaders));
// chunked编码不支持断点下载
if (resHeaders.contains(HttpHeaderNames.CONTENT_LENGTH)) {
if (httpResponse == null) {
httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
}
// 206表示支持断点下载
if (httpResponse.status().equals(HttpResponseStatus.PARTIAL_CONTENT)) {
taskInfo.setSupportRange(true);
}
}
return taskInfo;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project proxyee-down by monkeyWie.
the class HttpDownHandleInterceptFactory method create.
@Override
public HttpProxyIntercept create() {
return new HttpProxyIntercept() {
@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception {
HttpRequest httpRequest = pipeline.getHttpRequest();
ProxyConfig proxyConfig = ContentManager.CONFIG.get().getSecProxyConfig();
TaskInfo taskInfo = HttpDownUtil.getTaskInfo(httpRequest, httpResponse.headers(), proxyConfig, HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup);
HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest, proxyConfig);
ContentManager.DOWN.putBoot(httpDownInfo);
httpResponse.setStatus(HttpResponseStatus.OK);
httpResponse.headers().clear();
httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
byte[] content;
if (HttpUtil.checkHead(httpRequest, HttpHeaderNames.HOST, "^.*\\.baidupcs\\.com.*$")) {
content = "<script>window.close();</script>".getBytes();
} else {
content = "<script>window.history.go(-1);</script>".getBytes();
}
httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.length);
clientChannel.writeAndFlush(httpResponse);
HttpContent httpContent = new DefaultLastHttpContent();
httpContent.content().writeBytes(content);
clientChannel.writeAndFlush(httpContent);
clientChannel.close();
httpDownDispatch.dispatch(httpDownInfo);
}
};
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project ambry by linkedin.
the class PublicAccessLogHandler method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
long startTimeInMs = System.currentTimeMillis();
boolean shouldReset = msg instanceof LastHttpContent;
if (request != null) {
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
logHeaders("Response", response, publicAccessLogger.getResponseHeaders());
logMessage.append(", ");
logMessage.append("status=").append(response.status().code());
logMessage.append(", ");
if (HttpUtil.isTransferEncodingChunked(response)) {
responseFirstChunkStartTimeInMs = System.currentTimeMillis();
} else {
shouldReset = true;
}
} else if (!(msg instanceof HttpContent)) {
logger.error("Sending response that is not of type HttpResponse or HttpContent. Sending response to " + ctx.channel().remoteAddress() + ". Request is of type " + msg.getClass() + ". No action being taken other than logging this unexpected state.");
}
if (shouldReset) {
logDurations();
publicAccessLogger.logInfo(logMessage.toString());
reset();
}
}
nettyMetrics.publicAccessLogResponseProcessingTimeInMs.update(System.currentTimeMillis() - startTimeInMs);
super.write(ctx, msg, promise);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project ambry by linkedin.
the class NettyMessageProcessorTest method doRequestHandlerExceptionTest.
// requestHandlerExceptionTest() helpers.
/**
* Does a test where the request handler inside {@link NettyMessageProcessor} fails. Checks for the right error code
* in the response.
* @param httpMethod the {@link HttpMethod} to use for the request.
* @param expectedStatus the excepted {@link HttpResponseStatus} in the response.
*/
private void doRequestHandlerExceptionTest(HttpMethod httpMethod, HttpResponseStatus expectedStatus) {
EmbeddedChannel channel = createChannel();
channel.writeInbound(RestTestUtils.createRequest(httpMethod, "/", null));
channel.writeInbound(new DefaultLastHttpContent());
// first outbound has to be response.
HttpResponse response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", expectedStatus, response.status());
}
Aggregations