use of io.netty.handler.codec.http.DefaultLastHttpContent in project proxyee-down by monkeyWie.
the class CookieIntercept method beforeRequest.
@Override
public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception {
String acceptValue = httpRequest.headers().get(HttpHeaderNames.ACCEPT);
if (acceptValue != null && acceptValue.contains("application/x-sniff-cookie")) {
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, new DefaultHttpHeaders());
httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0);
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
AsciiString customHeadKey = AsciiString.cached("X-Sniff-Cookie");
String cookie = pipeline.getHttpRequest().headers().get(HttpHeaderNames.COOKIE);
httpResponse.headers().set(customHeadKey, cookie == null ? "" : cookie);
httpResponse.headers().set(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS, customHeadKey);
String origin = httpRequest.headers().get(HttpHeaderNames.ORIGIN);
if (StringUtil.isNullOrEmpty(origin)) {
String referer = httpRequest.headers().get(HttpHeaderNames.REFERER);
URL url = new URL(referer);
origin = url.getHost();
}
httpResponse.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
httpResponse.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, true);
clientChannel.writeAndFlush(httpResponse);
clientChannel.writeAndFlush(new DefaultLastHttpContent());
clientChannel.close();
} else {
super.beforeRequest(clientChannel, httpRequest, pipeline);
}
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project reactor-netty by reactor.
the class HttpOperationsTest method httpAndJsonDecoders.
@Test
public void httpAndJsonDecoders() {
EmbeddedChannel channel = new EmbeddedChannel();
NettyContext testContext = () -> channel;
ChannelHandler handler = new JsonObjectDecoder(true);
testContext.addHandlerLast("foo", handler);
HttpOperations.autoAddHttpExtractor(testContext, "foo", handler);
String json1 = "[{\"some\": 1} , {\"valu";
String json2 = "e\": true, \"test\": 1}]";
Object[] content = new Object[3];
content[0] = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
content[1] = new DefaultHttpContent(Unpooled.copiedBuffer(json1, CharsetUtil.UTF_8));
content[2] = new DefaultLastHttpContent(Unpooled.copiedBuffer(json2, CharsetUtil.UTF_8));
channel.writeInbound(content);
Object t = channel.readInbound();
assertThat(t, instanceOf(HttpResponse.class));
assertThat(t, not(instanceOf(HttpContent.class)));
t = channel.readInbound();
assertThat(t, instanceOf(ByteBuf.class));
assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"some\": 1}"));
((ByteBuf) t).release();
t = channel.readInbound();
assertThat(t, instanceOf(ByteBuf.class));
assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"value\": true, \"test\": 1}"));
((ByteBuf) t).release();
t = channel.readInbound();
assertThat(t, is(LastHttpContent.EMPTY_LAST_CONTENT));
((LastHttpContent) t).release();
t = channel.readInbound();
assertThat(t, nullValue());
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project reactor-netty by reactor.
the class HttpClientOperationsTest method addNamedDecoderReplaysLastHttp.
@Test
public void addNamedDecoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
ops.addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project reactor-netty by reactor.
the class HttpClientOperationsTest method addNamedEncoderReplaysLastHttp.
@Test
public void addNamedEncoderReplaysLastHttp() throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
ops.addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
Object content = channel.readInbound();
assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
assertThat(channel.readInbound(), nullValue());
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project proxyee-down by monkeyWie.
the class HttpDownUtil method getResponse.
/**
* 取请求响应
*/
public static HttpResponse getResponse(HttpRequest httpRequest, ProxyConfig proxyConfig, SslContext clientSslCtx, NioEventLoopGroup loopGroup) throws Exception {
final HttpResponse[] httpResponses = new HttpResponse[1];
CountDownLatch cdl = new CountDownLatch(1);
HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest;
RequestProto requestProto = requestInfo.requestProto();
Bootstrap bootstrap = new Bootstrap();
// 注册线程池
bootstrap.group(loopGroup).channel(// 使用NioSocketChannel来作为连接用的channel类
NioSocketChannel.class).handler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
if (proxyConfig != null) {
ch.pipeline().addLast(ProxyHandleFactory.build(proxyConfig));
}
if (requestProto.getSsl()) {
ch.pipeline().addLast(clientSslCtx.newHandler(ch.alloc()));
}
ch.pipeline().addLast("httpCodec", new HttpClientCodec());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx0, Object msg0) throws Exception {
if (msg0 instanceof HttpResponse) {
HttpResponse httpResponse = (HttpResponse) msg0;
httpResponses[0] = httpResponse;
ctx0.channel().close();
cdl.countDown();
}
}
});
}
});
if (proxyConfig != null) {
// 代理服务器解析DNS和连接
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
cf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
httpRequest.headers().set(HttpHeaderNames.RANGE, "bytes=0-0");
cf.channel().writeAndFlush(httpRequest);
if (requestInfo.content() != null) {
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(requestInfo.content());
cf.channel().writeAndFlush(content);
}
} else {
cdl.countDown();
}
});
cdl.await(30, TimeUnit.SECONDS);
if (httpResponses[0] == null) {
throw new TimeoutException("getResponse timeout");
}
return httpResponses[0];
}
Aggregations