use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project netty by netty.
the class HttpConversionUtil method toHttp2Headers.
/**
* Converts the given HTTP/1.x headers into HTTP/2 headers.
* The following headers are only used if they can not be found in from the {@code HOST} header or the
* {@code Request-Line} as defined by <a href="https://tools.ietf.org/html/rfc7230">rfc7230</a>
* <ul>
* <li>{@link ExtensionHeaderNames#SCHEME}</li>
* </ul>
* {@link ExtensionHeaderNames#PATH} is ignored and instead extracted from the {@code Request-Line}.
*/
public static Http2Headers toHttp2Headers(HttpMessage in, boolean validateHeaders) {
HttpHeaders inHeaders = in.headers();
final Http2Headers out = new DefaultHttp2Headers(validateHeaders, inHeaders.size());
if (in instanceof HttpRequest) {
HttpRequest request = (HttpRequest) in;
URI requestTargetUri = URI.create(request.uri());
out.path(toHttp2Path(requestTargetUri));
out.method(request.method().asciiName());
setHttp2Scheme(inHeaders, requestTargetUri, out);
if (!isOriginForm(requestTargetUri) && !isAsteriskForm(requestTargetUri)) {
// Attempt to take from HOST header before taking from the request-line
String host = inHeaders.getAsString(HttpHeaderNames.HOST);
setHttp2Authority(host == null || host.isEmpty() ? requestTargetUri.getAuthority() : host, out);
}
} else if (in instanceof HttpResponse) {
HttpResponse response = (HttpResponse) in;
out.status(response.status().codeAsText());
}
// Add the HTTP headers which have not been consumed above
toHttp2Headers(inHeaders, out);
return out;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest 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.HttpRequest in project netty by netty.
the class WebSocketServerProtocolHandlerTest method testCheckInvalidWebSocketPath.
@Test
public void testCheckInvalidWebSocketPath() {
HttpRequest httpRequest = new WebSocketRequestBuilder().httpVersion(HTTP_1_1).method(HttpMethod.GET).uri("/testabc").key(HttpHeaderNames.SEC_WEBSOCKET_KEY).connection("Upgrade").upgrade(HttpHeaderValues.WEBSOCKET).version13().build();
WebSocketServerProtocolConfig config = WebSocketServerProtocolConfig.newBuilder().websocketPath("/test").checkStartsWith(true).build();
EmbeddedChannel ch = new EmbeddedChannel(new WebSocketServerProtocolHandler(config), new HttpRequestDecoder(), new HttpResponseEncoder(), new MockOutboundHandler());
ch.writeInbound(httpRequest);
ChannelHandlerContext handshakerCtx = ch.pipeline().context(WebSocketServerProtocolHandshakeHandler.class);
assertNull(WebSocketServerProtocolHandler.getHandshaker(handshakerCtx.channel()));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project netty by netty.
the class WebSocketServerProtocolHandlerTest method writeUpgradeRequest.
private static void writeUpgradeRequest(EmbeddedChannel ch, boolean full) {
HttpRequest request = WebSocketRequestBuilder.successful();
if (full) {
ch.writeInbound(request);
} else {
if (request instanceof FullHttpRequest) {
FullHttpRequest fullHttpRequest = (FullHttpRequest) request;
HttpRequest req = new DefaultHttpRequest(fullHttpRequest.protocolVersion(), fullHttpRequest.method(), fullHttpRequest.uri(), fullHttpRequest.headers().copy());
ch.writeInbound(req);
ch.writeInbound(new DefaultHttpContent(fullHttpRequest.content().copy()));
ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
fullHttpRequest.release();
} else {
ch.writeInbound(request);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project netty by netty.
the class WebSocketServerExtensionHandlerTest method testExtensionHandlerNotRemovedByFailureWritePromise.
@Test
public void testExtensionHandlerNotRemovedByFailureWritePromise() {
// initialize
when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(mainExtensionMock);
when(mainExtensionMock.newReponseData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
// execute
WebSocketServerExtensionHandler extensionHandler = new WebSocketServerExtensionHandler(mainHandshakerMock);
EmbeddedChannel ch = new EmbeddedChannel(extensionHandler);
HttpRequest req = newUpgradeRequest("main");
ch.writeInbound(req);
HttpResponse res = newUpgradeResponse(null);
ChannelPromise failurePromise = ch.newPromise();
ch.writeOneOutbound(res, failurePromise);
failurePromise.setFailure(new IOException("Cannot write response"));
// test
assertNull(ch.readOutbound());
assertNotNull(ch.pipeline().context(extensionHandler));
assertTrue(ch.finish());
}
Aggregations