Search in sources :

Example 51 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class WebSocketServerHandshaker method handshake.

/**
     * Performs the opening handshake
     *
     * When call this method you <strong>MUST NOT</strong> retain the {@link HttpRequest} which is passed in.
     *
     * @param channel
     *            Channel
     * @param req
     *            HTTP Request
     * @param responseHeaders
     *            Extra headers to add to the handshake response or {@code null} if no extra headers should be added
     * @param promise
     *            the {@link ChannelPromise} to be notified when the opening handshake is done
     * @return future
     *            the {@link ChannelFuture} which is notified when the opening handshake is done
     */
public final ChannelFuture handshake(final Channel channel, HttpRequest req, final HttpHeaders responseHeaders, final ChannelPromise promise) {
    if (req instanceof FullHttpRequest) {
        return handshake(channel, (FullHttpRequest) req, responseHeaders, promise);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{} WebSocket version {} server handshake", channel, version());
    }
    ChannelPipeline p = channel.pipeline();
    ChannelHandlerContext ctx = p.context(HttpRequestDecoder.class);
    if (ctx == null) {
        // this means the user use a HttpServerCodec
        ctx = p.context(HttpServerCodec.class);
        if (ctx == null) {
            promise.setFailure(new IllegalStateException("No HttpDecoder and no HttpServerCodec in the pipeline"));
            return promise;
        }
    }
    // Add aggregator and ensure we feed the HttpRequest so it is aggregated. A limit o 8192 should be more then
    // enough for the websockets handshake payload.
    //
    // TODO: Make handshake work without HttpObjectAggregator at all.
    String aggregatorName = "httpAggregator";
    p.addAfter(ctx.name(), aggregatorName, new HttpObjectAggregator(8192));
    p.addAfter(aggregatorName, "handshaker", new SimpleChannelInboundHandler<FullHttpRequest>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
            // Remove ourself and do the actual handshake
            ctx.pipeline().remove(this);
            handshake(channel, msg, responseHeaders, promise);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            // Remove ourself and fail the handshake promise.
            ctx.pipeline().remove(this);
            promise.tryFailure(cause);
            ctx.fireExceptionCaught(cause);
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            // Fail promise if Channel was closed
            promise.tryFailure(CLOSED_CHANNEL_EXCEPTION);
            ctx.fireChannelInactive();
        }
    });
    try {
        ctx.fireChannelRead(ReferenceCountUtil.retain(req));
    } catch (Throwable cause) {
        promise.setFailure(cause);
    }
    return promise;
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPipeline(io.netty.channel.ChannelPipeline) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 52 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class WebSocketServerProtocolHandler method forbiddenHttpRequestResponder.

static ChannelHandler forbiddenHttpRequestResponder() {
    return new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            if (msg instanceof FullHttpRequest) {
                ((FullHttpRequest) msg).release();
                FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.FORBIDDEN);
                ctx.channel().writeAndFlush(response);
            } else {
                ctx.fireChannelRead(msg);
            }
        }
    };
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 53 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class SpdyHttpDecoder method createHttpRequest.

private static FullHttpRequest createHttpRequest(SpdyHeadersFrame requestFrame, ByteBufAllocator alloc) throws Exception {
    // Create the first line of the request from the name/value pairs
    SpdyHeaders headers = requestFrame.headers();
    HttpMethod method = HttpMethod.valueOf(headers.getAsString(METHOD));
    String url = headers.getAsString(PATH);
    HttpVersion httpVersion = HttpVersion.valueOf(headers.getAsString(VERSION));
    headers.remove(METHOD);
    headers.remove(PATH);
    headers.remove(VERSION);
    boolean release = true;
    ByteBuf buffer = alloc.buffer();
    try {
        FullHttpRequest req = new DefaultFullHttpRequest(httpVersion, method, url, buffer);
        // Remove the scheme header
        headers.remove(SCHEME);
        // Replace the SPDY host header with the HTTP host header
        CharSequence host = headers.get(HOST);
        headers.remove(HOST);
        req.headers().set(HttpHeaderNames.HOST, host);
        for (Map.Entry<CharSequence, CharSequence> e : requestFrame.headers()) {
            req.headers().add(e.getKey(), e.getValue());
        }
        // The Connection and Keep-Alive headers are no longer valid
        HttpUtil.setKeepAlive(req, true);
        // Transfer-Encoding header is not valid
        req.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
        release = false;
        return req;
    } finally {
        if (release) {
            buffer.release();
        }
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 54 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class HttpProxyHandler method newInitialMessage.

@Override
protected Object newInitialMessage(ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress raddr = destinationAddress();
    final String host = NetUtil.toSocketAddressString(raddr);
    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.CONNECT, host, Unpooled.EMPTY_BUFFER, false);
    req.headers().set(HttpHeaderNames.HOST, host);
    if (authorization != null) {
        req.headers().set(HttpHeaderNames.PROXY_AUTHORIZATION, authorization);
    }
    return req;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) AsciiString(io.netty.util.AsciiString)

Example 55 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class HttpProxyHandlerTest method testInitialMessage.

private static void testInitialMessage(InetSocketAddress socketAddress, String expected) throws Exception {
    InetSocketAddress proxyAddress = new InetSocketAddress(NetUtil.LOCALHOST, 8080);
    ChannelPromise promise = mock(ChannelPromise.class);
    verifyNoMoreInteractions(promise);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.connect(same(proxyAddress), isNull(InetSocketAddress.class), same(promise))).thenReturn(promise);
    HttpProxyHandler handler = new HttpProxyHandler(new InetSocketAddress(NetUtil.LOCALHOST, 8080));
    handler.connect(ctx, socketAddress, null, promise);
    FullHttpRequest request = (FullHttpRequest) handler.newInitialMessage(ctx);
    try {
        assertEquals(HttpVersion.HTTP_1_1, request.protocolVersion());
        assertEquals(expected, request.uri());
        assertEquals(expected, request.headers().get(HttpHeaderNames.HOST));
    } finally {
        request.release();
    }
    verify(ctx).connect(proxyAddress, null, promise);
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Aggregations

FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)97 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)65 Test (org.junit.Test)51 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)32 AsciiString (io.netty.util.AsciiString)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 ByteBuf (io.netty.buffer.ByteBuf)19 ChannelPromise (io.netty.channel.ChannelPromise)16 HttpResponse (io.netty.handler.codec.http.HttpResponse)13 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)12 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)11 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)10 URI (java.net.URI)10 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)9 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)8 ChannelFuture (io.netty.channel.ChannelFuture)7 Channel (io.netty.channel.Channel)6 NullDispatcher (org.elasticsearch.http.NullDispatcher)6 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)5