use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class HttpStaticFileServerHandler method sendRedirect.
private void sendRedirect(ChannelHandlerContext ctx, String newUri) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, FOUND, Unpooled.EMPTY_BUFFER);
response.headers().set(HttpHeaderNames.LOCATION, newUri);
sendAndCleanupConnection(ctx, response);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class HttpHelloWorldServerHandler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
if (msg instanceof HttpRequest) {
HttpRequest req = (HttpRequest) msg;
boolean keepAlive = HttpUtil.isKeepAlive(req);
FullHttpResponse response = new DefaultFullHttpResponse(req.protocolVersion(), OK, Unpooled.wrappedBuffer(CONTENT));
response.headers().set(CONTENT_TYPE, TEXT_PLAIN).setInt(CONTENT_LENGTH, response.content().readableBytes());
if (keepAlive) {
if (!req.protocolVersion().isKeepAliveDefault()) {
response.headers().set(CONNECTION, KEEP_ALIVE);
}
} else {
// Tell the client we're going to close the connection.
response.headers().set(CONNECTION, CLOSE);
}
ChannelFuture f = ctx.write(response);
if (!keepAlive) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class WebSocketClientHandshakerTest method testWebSocketClientHandshakeException.
@Test
public void testWebSocketClientHandshakeException() {
URI uri = URI.create("ws://localhost:9999/exception");
WebSocketClientHandshaker handshaker = newHandshaker(uri, null, null, false);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
response.headers().set(HttpHeaderNames.WWW_AUTHENTICATE, "realm = access token required");
try {
handshaker.finishHandshake(null, response);
} catch (WebSocketClientHandshakeException exception) {
assertEquals("Invalid handshake response getStatus: 401 Unauthorized", exception.getMessage());
assertEquals(HttpResponseStatus.UNAUTHORIZED, exception.response().status());
assertTrue(exception.response().headers().contains(HttpHeaderNames.WWW_AUTHENTICATE, "realm = access token required", false));
} finally {
response.release();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class WebSocketClientHandshakerTest method testHttpResponseAndFrameInSameBuffer.
private void testHttpResponseAndFrameInSameBuffer(boolean codec) {
String url = "ws://localhost:9999/ws";
final WebSocketClientHandshaker shaker = newHandshaker(URI.create(url));
final WebSocketClientHandshaker handshaker = new WebSocketClientHandshaker(shaker.uri(), shaker.version(), null, EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE, -1) {
@Override
protected FullHttpRequest newHandshakeRequest() {
return shaker.newHandshakeRequest();
}
@Override
protected void verify(FullHttpResponse response) {
// Not do any verification, so we not need to care sending the correct headers etc in the test,
// which would just make things more complicated.
}
@Override
protected WebSocketFrameDecoder newWebsocketDecoder() {
return shaker.newWebsocketDecoder();
}
@Override
protected WebSocketFrameEncoder newWebSocketEncoder() {
return shaker.newWebSocketEncoder();
}
};
// use randomBytes helper from utils to check that it functions properly
byte[] data = WebSocketUtil.randomBytes(24);
// Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these
// to test the actual handshaker.
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(url, null, false);
FullHttpRequest request = shaker.newHandshakeRequest();
WebSocketServerHandshaker socketServerHandshaker = factory.newHandshaker(request);
request.release();
EmbeddedChannel websocketChannel = new EmbeddedChannel(socketServerHandshaker.newWebSocketEncoder(), socketServerHandshaker.newWebsocketDecoder());
assertTrue(websocketChannel.writeOutbound(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(data))));
byte[] bytes = "HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n".getBytes(CharsetUtil.US_ASCII);
CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
compositeByteBuf.addComponent(true, Unpooled.wrappedBuffer(bytes));
for (; ; ) {
ByteBuf frameBytes = websocketChannel.readOutbound();
if (frameBytes == null) {
break;
}
compositeByteBuf.addComponent(true, frameBytes);
}
EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(Integer.MAX_VALUE), new SimpleChannelInboundHandler<FullHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
handshaker.finishHandshake(ctx.channel(), msg);
ctx.pipeline().remove(this);
}
});
if (codec) {
ch.pipeline().addFirst(new HttpClientCodec());
} else {
ch.pipeline().addFirst(new HttpRequestEncoder(), new HttpResponseDecoder());
}
// We need to first write the request as HttpClientCodec will fail if we receive a response before a request
// was written.
shaker.handshake(ch).syncUninterruptibly();
for (; ; ) {
// Just consume the bytes, we are not interested in these.
ByteBuf buf = ch.readOutbound();
if (buf == null) {
break;
}
buf.release();
}
assertTrue(ch.writeInbound(compositeByteBuf));
assertTrue(ch.finish());
BinaryWebSocketFrame frame = ch.readInbound();
ByteBuf expect = Unpooled.wrappedBuffer(data);
try {
assertEquals(expect, frame.content());
assertTrue(frame.isFinalFragment());
assertEquals(0, frame.rsv());
} finally {
expect.release();
frame.release();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class WebSocketServerProtocolHandlerTest method testHttpUpgradeRequestInvalidUpgradeHeader.
@Test
public void testHttpUpgradeRequestInvalidUpgradeHeader() {
EmbeddedChannel ch = createChannel();
FullHttpRequest httpRequestWithEntity = new WebSocketRequestBuilder().httpVersion(HTTP_1_1).method(HttpMethod.GET).uri("/test").connection("Upgrade").version00().upgrade("BogusSocket").build();
ch.writeInbound(httpRequestWithEntity);
FullHttpResponse response = responses.remove();
assertEquals(BAD_REQUEST, response.status());
assertEquals("not a WebSocket handshake request: missing upgrade", getResponseMessage(response));
response.release();
assertFalse(ch.finish());
}
Aggregations