Search in sources :

Example 96 with HttpRequest

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;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) AsciiString(io.netty.util.AsciiString) URI(java.net.URI)

Example 97 with HttpRequest

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);
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 98 with HttpRequest

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()));
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.jupiter.api.Test)

Example 99 with HttpRequest

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);
        }
    }
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent)

Example 100 with HttpRequest

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());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelPromise(io.netty.channel.ChannelPromise) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

HttpRequest (io.netty.handler.codec.http.HttpRequest)332 Test (org.junit.Test)115 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)111 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)102 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)71 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)67 HttpResponse (io.netty.handler.codec.http.HttpResponse)65 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)51 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)45 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)36 ByteBuf (io.netty.buffer.ByteBuf)35 HttpContent (io.netty.handler.codec.http.HttpContent)34 Test (org.junit.jupiter.api.Test)34 Channel (io.netty.channel.Channel)31 HttpMethod (io.netty.handler.codec.http.HttpMethod)31 URI (java.net.URI)30 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)28 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)26 IOException (java.io.IOException)25 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)22