Search in sources :

Example 21 with FullHttpRequest

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

the class InboundHttp2ToHttpAdapterTest method clientRequestOneDataFrame.

@Test
public void clientRequestOneDataFrame() throws Exception {
    boostrapEnv(1, 1, 1);
    final String text = "hello world";
    final ByteBuf content = Unpooled.copiedBuffer(text.getBytes());
    final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some/path/resource2", content, true);
    try {
        HttpHeaders httpHeaders = request.headers();
        httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 3);
        httpHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, text.length());
        httpHeaders.setShort(HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), (short) 16);
        final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).path(new AsciiString("/some/path/resource2"));
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers, 0, false, newPromiseClient());
                clientHandler.encoder().writeData(ctxClient(), 3, content.retainedDuplicate(), 0, true, newPromiseClient());
                clientChannel.flush();
            }
        });
        awaitRequests();
        ArgumentCaptor<FullHttpMessage> requestCaptor = ArgumentCaptor.forClass(FullHttpMessage.class);
        verify(serverListener).messageReceived(requestCaptor.capture());
        capturedRequests = requestCaptor.getAllValues();
        assertEquals(request, capturedRequests.get(0));
    } finally {
        request.release();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) AsciiString(io.netty.util.AsciiString) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 22 with FullHttpRequest

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

the class InboundHttp2ToHttpAdapterTest method clientRequestSingleHeaderCookieSplitIntoMultipleEntries.

@Test
public void clientRequestSingleHeaderCookieSplitIntoMultipleEntries() throws Exception {
    boostrapEnv(1, 1, 1);
    final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some/path/resource2", true);
    try {
        HttpHeaders httpHeaders = request.headers();
        httpHeaders.set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), "https");
        httpHeaders.set(HttpHeaderNames.HOST, "example.org");
        httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 3);
        httpHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, 0);
        httpHeaders.set(HttpHeaderNames.COOKIE, "a=b; c=d; e=f");
        httpHeaders.setShort(HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), (short) 16);
        final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).scheme(new AsciiString("https")).authority(new AsciiString("example.org")).path(new AsciiString("/some/path/resource2")).add(HttpHeaderNames.COOKIE, "a=b").add(HttpHeaderNames.COOKIE, "c=d").add(HttpHeaderNames.COOKIE, "e=f");
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers, 0, true, newPromiseClient());
                clientChannel.flush();
            }
        });
        awaitRequests();
        ArgumentCaptor<FullHttpMessage> requestCaptor = ArgumentCaptor.forClass(FullHttpMessage.class);
        verify(serverListener).messageReceived(requestCaptor.capture());
        capturedRequests = requestCaptor.getAllValues();
        assertEquals(request, capturedRequests.get(0));
    } finally {
        request.release();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 23 with FullHttpRequest

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

the class SpdyHttpDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, SpdyFrame msg, List<Object> out) throws Exception {
    if (msg instanceof SpdySynStreamFrame) {
        // HTTP requests/responses are mapped one-to-one to SPDY streams.
        SpdySynStreamFrame spdySynStreamFrame = (SpdySynStreamFrame) msg;
        int streamId = spdySynStreamFrame.streamId();
        if (SpdyCodecUtil.isServerId(streamId)) {
            // SYN_STREAM frames initiated by the server are pushed resources
            int associatedToStreamId = spdySynStreamFrame.associatedStreamId();
            // it must reply with a RST_STREAM with error code INVALID_STREAM.
            if (associatedToStreamId == 0) {
                SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.INVALID_STREAM);
                ctx.writeAndFlush(spdyRstStreamFrame);
                return;
            }
            // (we only support pushed resources divided into two header blocks).
            if (spdySynStreamFrame.isLast()) {
                SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.PROTOCOL_ERROR);
                ctx.writeAndFlush(spdyRstStreamFrame);
                return;
            }
            // reply with a RST_STREAM with error code INTERNAL_ERROR.
            if (spdySynStreamFrame.isTruncated()) {
                SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.INTERNAL_ERROR);
                ctx.writeAndFlush(spdyRstStreamFrame);
                return;
            }
            try {
                FullHttpRequest httpRequestWithEntity = createHttpRequest(spdySynStreamFrame, ctx.alloc());
                // Set the Stream-ID, Associated-To-Stream-ID, iand Priority as headers
                httpRequestWithEntity.headers().setInt(Names.STREAM_ID, streamId);
                httpRequestWithEntity.headers().setInt(Names.ASSOCIATED_TO_STREAM_ID, associatedToStreamId);
                httpRequestWithEntity.headers().setInt(Names.PRIORITY, spdySynStreamFrame.priority());
                out.add(httpRequestWithEntity);
            } catch (Throwable ignored) {
                SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.PROTOCOL_ERROR);
                ctx.writeAndFlush(spdyRstStreamFrame);
            }
        } else {
            // reply with a HTTP 431 REQUEST HEADER FIELDS TOO LARGE reply.
            if (spdySynStreamFrame.isTruncated()) {
                SpdySynReplyFrame spdySynReplyFrame = new DefaultSpdySynReplyFrame(streamId);
                spdySynReplyFrame.setLast(true);
                SpdyHeaders frameHeaders = spdySynReplyFrame.headers();
                frameHeaders.setInt(STATUS, HttpResponseStatus.REQUEST_HEADER_FIELDS_TOO_LARGE.code());
                frameHeaders.setObject(VERSION, HttpVersion.HTTP_1_0);
                ctx.writeAndFlush(spdySynReplyFrame);
                return;
            }
            try {
                FullHttpRequest httpRequestWithEntity = createHttpRequest(spdySynStreamFrame, ctx.alloc());
                // Set the Stream-ID as a header
                httpRequestWithEntity.headers().setInt(Names.STREAM_ID, streamId);
                if (spdySynStreamFrame.isLast()) {
                    out.add(httpRequestWithEntity);
                } else {
                    // Request body will follow in a series of Data Frames
                    putMessage(streamId, httpRequestWithEntity);
                }
            } catch (Throwable t) {
                // If a client sends a SYN_STREAM without all of the getMethod, url (host and path),
                // scheme, and version headers the server must reply with a HTTP 400 BAD REQUEST reply.
                // Also sends HTTP 400 BAD REQUEST reply if header name/value pairs are invalid
                SpdySynReplyFrame spdySynReplyFrame = new DefaultSpdySynReplyFrame(streamId);
                spdySynReplyFrame.setLast(true);
                SpdyHeaders frameHeaders = spdySynReplyFrame.headers();
                frameHeaders.setInt(STATUS, HttpResponseStatus.BAD_REQUEST.code());
                frameHeaders.setObject(VERSION, HttpVersion.HTTP_1_0);
                ctx.writeAndFlush(spdySynReplyFrame);
            }
        }
    } else if (msg instanceof SpdySynReplyFrame) {
        SpdySynReplyFrame spdySynReplyFrame = (SpdySynReplyFrame) msg;
        int streamId = spdySynReplyFrame.streamId();
        // reply with a RST_STREAM frame with error code INTERNAL_ERROR.
        if (spdySynReplyFrame.isTruncated()) {
            SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.INTERNAL_ERROR);
            ctx.writeAndFlush(spdyRstStreamFrame);
            return;
        }
        try {
            FullHttpResponse httpResponseWithEntity = createHttpResponse(spdySynReplyFrame, ctx.alloc(), validateHeaders);
            // Set the Stream-ID as a header
            httpResponseWithEntity.headers().setInt(Names.STREAM_ID, streamId);
            if (spdySynReplyFrame.isLast()) {
                HttpUtil.setContentLength(httpResponseWithEntity, 0);
                out.add(httpResponseWithEntity);
            } else {
                // Response body will follow in a series of Data Frames
                putMessage(streamId, httpResponseWithEntity);
            }
        } catch (Throwable t) {
            // If a client receives a SYN_REPLY without valid getStatus and version headers
            // the client must reply with a RST_STREAM frame indicating a PROTOCOL_ERROR
            SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.PROTOCOL_ERROR);
            ctx.writeAndFlush(spdyRstStreamFrame);
        }
    } else if (msg instanceof SpdyHeadersFrame) {
        SpdyHeadersFrame spdyHeadersFrame = (SpdyHeadersFrame) msg;
        int streamId = spdyHeadersFrame.streamId();
        FullHttpMessage fullHttpMessage = getMessage(streamId);
        if (fullHttpMessage == null) {
            // HEADERS frames may initiate a pushed response
            if (SpdyCodecUtil.isServerId(streamId)) {
                // reply with a RST_STREAM frame with error code INTERNAL_ERROR.
                if (spdyHeadersFrame.isTruncated()) {
                    SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.INTERNAL_ERROR);
                    ctx.writeAndFlush(spdyRstStreamFrame);
                    return;
                }
                try {
                    fullHttpMessage = createHttpResponse(spdyHeadersFrame, ctx.alloc(), validateHeaders);
                    // Set the Stream-ID as a header
                    fullHttpMessage.headers().setInt(Names.STREAM_ID, streamId);
                    if (spdyHeadersFrame.isLast()) {
                        HttpUtil.setContentLength(fullHttpMessage, 0);
                        out.add(fullHttpMessage);
                    } else {
                        // Response body will follow in a series of Data Frames
                        putMessage(streamId, fullHttpMessage);
                    }
                } catch (Throwable t) {
                    // If a client receives a SYN_REPLY without valid getStatus and version headers
                    // the client must reply with a RST_STREAM frame indicating a PROTOCOL_ERROR
                    SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, SpdyStreamStatus.PROTOCOL_ERROR);
                    ctx.writeAndFlush(spdyRstStreamFrame);
                }
            }
            return;
        }
        // Ignore trailers in a truncated HEADERS frame.
        if (!spdyHeadersFrame.isTruncated()) {
            for (Map.Entry<CharSequence, CharSequence> e : spdyHeadersFrame.headers()) {
                fullHttpMessage.headers().add(e.getKey(), e.getValue());
            }
        }
        if (spdyHeadersFrame.isLast()) {
            HttpUtil.setContentLength(fullHttpMessage, fullHttpMessage.content().readableBytes());
            removeMessage(streamId);
            out.add(fullHttpMessage);
        }
    } else if (msg instanceof SpdyDataFrame) {
        SpdyDataFrame spdyDataFrame = (SpdyDataFrame) msg;
        int streamId = spdyDataFrame.streamId();
        FullHttpMessage fullHttpMessage = getMessage(streamId);
        // If message is not in map discard Data Frame.
        if (fullHttpMessage == null) {
            return;
        }
        ByteBuf content = fullHttpMessage.content();
        if (content.readableBytes() > maxContentLength - spdyDataFrame.content().readableBytes()) {
            removeMessage(streamId);
            throw new TooLongFrameException("HTTP content length exceeded " + maxContentLength + " bytes.");
        }
        ByteBuf spdyDataFrameData = spdyDataFrame.content();
        int spdyDataFrameDataLen = spdyDataFrameData.readableBytes();
        content.writeBytes(spdyDataFrameData, spdyDataFrameData.readerIndex(), spdyDataFrameDataLen);
        if (spdyDataFrame.isLast()) {
            HttpUtil.setContentLength(fullHttpMessage, content.readableBytes());
            removeMessage(streamId);
            out.add(fullHttpMessage);
        }
    } else if (msg instanceof SpdyRstStreamFrame) {
        SpdyRstStreamFrame spdyRstStreamFrame = (SpdyRstStreamFrame) msg;
        int streamId = spdyRstStreamFrame.streamId();
        removeMessage(streamId);
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) ByteBuf(io.netty.buffer.ByteBuf) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with FullHttpRequest

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

the class WebSocketClientHandshaker method handshake.

/**
     * Begins the opening handshake
     *
     * @param channel
     *            Channel
     * @param promise
     *            the {@link ChannelPromise} to be notified when the opening handshake is sent
     */
public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) {
    FullHttpRequest request = newHandshakeRequest();
    HttpResponseDecoder decoder = channel.pipeline().get(HttpResponseDecoder.class);
    if (decoder == null) {
        HttpClientCodec codec = channel.pipeline().get(HttpClientCodec.class);
        if (codec == null) {
            promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "a HttpResponseDecoder or HttpClientCodec"));
            return promise;
        }
    }
    channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                ChannelPipeline p = future.channel().pipeline();
                ChannelHandlerContext ctx = p.context(HttpRequestEncoder.class);
                if (ctx == null) {
                    ctx = p.context(HttpClientCodec.class);
                }
                if (ctx == null) {
                    promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "a HttpRequestEncoder or HttpClientCodec"));
                    return;
                }
                p.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder());
                promise.setSuccess();
            } else {
                promise.setFailure(future.cause());
            }
        }
    });
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 25 with FullHttpRequest

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

the class WebSocketClientHandshaker00 method newHandshakeRequest.

/**
     * <p>
     * Sends the opening request to the server:
     * </p>
     *
     * <pre>
     * GET /demo HTTP/1.1
     * Upgrade: WebSocket
     * Connection: Upgrade
     * Host: example.com
     * Origin: http://example.com
     * Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5
     * Sec-WebSocket-Key2: 12998 5 Y3 1  .P00
     *
     * ^n:ds[4U
     * </pre>
     *
     */
@Override
protected FullHttpRequest newHandshakeRequest() {
    // Make keys
    int spaces1 = WebSocketUtil.randomNumber(1, 12);
    int spaces2 = WebSocketUtil.randomNumber(1, 12);
    int max1 = Integer.MAX_VALUE / spaces1;
    int max2 = Integer.MAX_VALUE / spaces2;
    int number1 = WebSocketUtil.randomNumber(0, max1);
    int number2 = WebSocketUtil.randomNumber(0, max2);
    int product1 = number1 * spaces1;
    int product2 = number2 * spaces2;
    String key1 = Integer.toString(product1);
    String key2 = Integer.toString(product2);
    key1 = insertRandomCharacters(key1);
    key2 = insertRandomCharacters(key2);
    key1 = insertSpaces(key1, spaces1);
    key2 = insertSpaces(key2, spaces2);
    byte[] key3 = WebSocketUtil.randomBytes(8);
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.putInt(number1);
    byte[] number1Array = buffer.array();
    buffer = ByteBuffer.allocate(4);
    buffer.putInt(number2);
    byte[] number2Array = buffer.array();
    byte[] challenge = new byte[16];
    System.arraycopy(number1Array, 0, challenge, 0, 4);
    System.arraycopy(number2Array, 0, challenge, 4, 4);
    System.arraycopy(key3, 0, challenge, 8, 8);
    expectedChallengeResponseBytes = Unpooled.wrappedBuffer(WebSocketUtil.md5(challenge));
    // Get path
    URI wsURL = uri();
    String path = rawPath(wsURL);
    int wsPort = websocketPort(wsURL);
    String host = wsURL.getHost();
    // Format request
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
    HttpHeaders headers = request.headers();
    headers.add(HttpHeaderNames.UPGRADE, WEBSOCKET).add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE).add(HttpHeaderNames.HOST, websocketHostValue(wsURL)).add(HttpHeaderNames.ORIGIN, websocketOriginValue(host, wsPort)).add(HttpHeaderNames.SEC_WEBSOCKET_KEY1, key1).add(HttpHeaderNames.SEC_WEBSOCKET_KEY2, key2);
    String expectedSubprotocol = expectedSubprotocol();
    if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
        headers.add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
    }
    if (customHeaders != null) {
        headers.add(customHeaders);
    }
    // Set Content-Length to workaround some known defect.
    // See also: http://www.ietf.org/mail-archive/web/hybi/current/msg02149.html
    headers.set(HttpHeaderNames.CONTENT_LENGTH, key3.length);
    request.content().writeBytes(key3);
    return request;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) AsciiString(io.netty.util.AsciiString) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI)

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