Search in sources :

Example 81 with HttpHeaders

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

the class SpdyHttpEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception {
    boolean valid = false;
    boolean last = false;
    if (msg instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) msg;
        SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpRequest);
        out.add(spdySynStreamFrame);
        last = spdySynStreamFrame.isLast() || spdySynStreamFrame.isUnidirectional();
        valid = true;
    }
    if (msg instanceof HttpResponse) {
        HttpResponse httpResponse = (HttpResponse) msg;
        SpdyHeadersFrame spdyHeadersFrame = createHeadersFrame(httpResponse);
        out.add(spdyHeadersFrame);
        last = spdyHeadersFrame.isLast();
        valid = true;
    }
    if (msg instanceof HttpContent && !last) {
        HttpContent chunk = (HttpContent) msg;
        chunk.content().retain();
        SpdyDataFrame spdyDataFrame = new DefaultSpdyDataFrame(currentStreamId, chunk.content());
        if (chunk instanceof LastHttpContent) {
            LastHttpContent trailer = (LastHttpContent) chunk;
            HttpHeaders trailers = trailer.trailingHeaders();
            if (trailers.isEmpty()) {
                spdyDataFrame.setLast(true);
                out.add(spdyDataFrame);
            } else {
                // Create SPDY HEADERS frame out of trailers
                SpdyHeadersFrame spdyHeadersFrame = new DefaultSpdyHeadersFrame(currentStreamId, validateHeaders);
                spdyHeadersFrame.setLast(true);
                Iterator<Entry<CharSequence, CharSequence>> itr = trailers.iteratorCharSequence();
                while (itr.hasNext()) {
                    Map.Entry<CharSequence, CharSequence> entry = itr.next();
                    final CharSequence headerName = headersToLowerCase ? AsciiString.of(entry.getKey()).toLowerCase() : entry.getKey();
                    spdyHeadersFrame.headers().add(headerName, entry.getValue());
                }
                // Write DATA frame and append HEADERS frame
                out.add(spdyDataFrame);
                out.add(spdyHeadersFrame);
            }
        } else {
            out.add(spdyDataFrame);
        }
        valid = true;
    }
    if (!valid) {
        throw new UnsupportedMessageTypeException(msg);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) Entry(java.util.Map.Entry) Map(java.util.Map) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException)

Example 82 with HttpHeaders

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

the class WebSocketClientHandshaker00 method verify.

/**
     * <p>
     * Process server response:
     * </p>
     *
     * <pre>
     * HTTP/1.1 101 WebSocket Protocol Handshake
     * Upgrade: WebSocket
     * Connection: Upgrade
     * Sec-WebSocket-Origin: http://example.com
     * Sec-WebSocket-Location: ws://example.com/demo
     * Sec-WebSocket-Protocol: sample
     *
     * 8jKS'y:G*Co,Wxa-
     * </pre>
     *
     * @param response
     *            HTTP response returned from the server for the request sent by beginOpeningHandshake00().
     * @throws WebSocketHandshakeException
     */
@Override
protected void verify(FullHttpResponse response) {
    final HttpResponseStatus status = new HttpResponseStatus(101, "WebSocket Protocol Handshake");
    if (!response.status().equals(status)) {
        throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.status());
    }
    HttpHeaders headers = response.headers();
    CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE);
    if (!WEBSOCKET.contentEqualsIgnoreCase(upgrade)) {
        throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade);
    }
    if (!headers.containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) {
        throw new WebSocketHandshakeException("Invalid handshake response connection: " + headers.get(HttpHeaderNames.CONNECTION));
    }
    ByteBuf challenge = response.content();
    if (!challenge.equals(expectedChallengeResponseBytes)) {
        throw new WebSocketHandshakeException("Invalid challenge");
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ByteBuf(io.netty.buffer.ByteBuf)

Example 83 with HttpHeaders

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

the class WebSocketExtensionUtilTest method testIsWebsocketUpgrade.

@Test
public void testIsWebsocketUpgrade() {
    HttpHeaders headers = new DefaultHttpHeaders();
    assertFalse(WebSocketExtensionUtil.isWebsocketUpgrade(headers));
    headers.add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
    assertFalse(WebSocketExtensionUtil.isWebsocketUpgrade(headers));
    headers.add(HttpHeaderNames.CONNECTION, "Keep-Alive, Upgrade");
    assertTrue(WebSocketExtensionUtil.isWebsocketUpgrade(headers));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Example 84 with HttpHeaders

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

the class WebSocketRequestBuilder method build.

public FullHttpRequest build() {
    FullHttpRequest req = new DefaultFullHttpRequest(httpVersion, method, uri);
    HttpHeaders headers = req.headers();
    if (host != null) {
        headers.set(HttpHeaderNames.HOST, host);
    }
    if (upgrade != null) {
        headers.set(HttpHeaderNames.UPGRADE, upgrade);
    }
    if (connection != null) {
        headers.set(HttpHeaderNames.CONNECTION, connection);
    }
    if (key != null) {
        headers.set(HttpHeaderNames.SEC_WEBSOCKET_KEY, key);
    }
    if (origin != null) {
        headers.set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, origin);
    }
    if (version != null) {
        headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version.toHttpHeaderValue());
    }
    return req;
}
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)

Example 85 with HttpHeaders

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

the class HttpToHttp2ConnectionHandler method write.

/**
     * Handles conversion of {@link HttpMessage} and {@link HttpContent} to HTTP/2 frames.
     */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (!(msg instanceof HttpMessage || msg instanceof HttpContent)) {
        ctx.write(msg, promise);
        return;
    }
    boolean release = true;
    SimpleChannelPromiseAggregator promiseAggregator = new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());
    try {
        Http2ConnectionEncoder encoder = encoder();
        boolean endStream = false;
        if (msg instanceof HttpMessage) {
            final HttpMessage httpMsg = (HttpMessage) msg;
            // Provide the user the opportunity to specify the streamId
            currentStreamId = getStreamId(httpMsg.headers());
            // Convert and write the headers.
            Http2Headers http2Headers = HttpConversionUtil.toHttp2Headers(httpMsg, validateHeaders);
            endStream = msg instanceof FullHttpMessage && !((FullHttpMessage) msg).content().isReadable();
            writeHeaders(ctx, encoder, currentStreamId, httpMsg.headers(), http2Headers, endStream, promiseAggregator);
        }
        if (!endStream && msg instanceof HttpContent) {
            boolean isLastContent = false;
            HttpHeaders trailers = EmptyHttpHeaders.INSTANCE;
            Http2Headers http2Trailers = EmptyHttp2Headers.INSTANCE;
            if (msg instanceof LastHttpContent) {
                isLastContent = true;
                // Convert any trailing headers.
                final LastHttpContent lastContent = (LastHttpContent) msg;
                trailers = lastContent.trailingHeaders();
                http2Trailers = HttpConversionUtil.toHttp2Headers(trailers, validateHeaders);
            }
            // Write the data
            final ByteBuf content = ((HttpContent) msg).content();
            endStream = isLastContent && trailers.isEmpty();
            release = false;
            encoder.writeData(ctx, currentStreamId, content, 0, endStream, promiseAggregator.newPromise());
            if (!trailers.isEmpty()) {
                // Write trailing headers.
                writeHeaders(ctx, encoder, currentStreamId, trailers, http2Trailers, true, promiseAggregator);
            }
        }
    } catch (Throwable t) {
        onError(ctx, t);
        promiseAggregator.setFailure(t);
    } finally {
        if (release) {
            ReferenceCountUtil.release(msg);
        }
        promiseAggregator.doneAllocatingPromises();
    }
}
Also used : EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) SimpleChannelPromiseAggregator(io.netty.handler.codec.http2.Http2CodecUtil.SimpleChannelPromiseAggregator) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) HttpMessage(io.netty.handler.codec.http.HttpMessage) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ByteBuf(io.netty.buffer.ByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Aggregations

HttpHeaders (io.netty.handler.codec.http.HttpHeaders)128 Test (org.junit.Test)61 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)49 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)42 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)34 AsciiString (io.netty.util.AsciiString)27 Test (org.testng.annotations.Test)25 ByteBuf (io.netty.buffer.ByteBuf)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 ChannelPromise (io.netty.channel.ChannelPromise)15 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 Cookie (io.netty.handler.codec.http.cookie.Cookie)12 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)12 Map (java.util.Map)12 HttpTest (org.asynchttpclient.testserver.HttpTest)12 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)11 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)11 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)10 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)10