Search in sources :

Example 86 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class SpdyHttpEncoder method createHeadersFrame.

@SuppressWarnings("deprecation")
private SpdyHeadersFrame createHeadersFrame(HttpResponse httpResponse) throws Exception {
    // Get the Stream-ID from the headers
    final HttpHeaders httpHeaders = httpResponse.headers();
    int streamId = httpHeaders.getInt(SpdyHttpHeaders.Names.STREAM_ID);
    httpHeaders.remove(SpdyHttpHeaders.Names.STREAM_ID);
    // The Connection, Keep-Alive, Proxy-Connection, and Transfer-Encoding
    // headers are not valid and MUST not be sent.
    httpHeaders.remove(HttpHeaderNames.CONNECTION);
    httpHeaders.remove("Keep-Alive");
    httpHeaders.remove("Proxy-Connection");
    httpHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
    SpdyHeadersFrame spdyHeadersFrame;
    if (SpdyCodecUtil.isServerId(streamId)) {
        spdyHeadersFrame = new DefaultSpdyHeadersFrame(streamId, validateHeaders);
    } else {
        spdyHeadersFrame = new DefaultSpdySynReplyFrame(streamId, validateHeaders);
    }
    SpdyHeaders frameHeaders = spdyHeadersFrame.headers();
    // Unfold the first line of the response into name/value pairs
    frameHeaders.set(SpdyHeaders.HttpNames.STATUS, httpResponse.status().codeAsText());
    frameHeaders.set(SpdyHeaders.HttpNames.VERSION, httpResponse.protocolVersion().text());
    // Transfer the remaining HTTP headers
    Iterator<Entry<CharSequence, CharSequence>> itr = httpHeaders.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());
    }
    currentStreamId = streamId;
    spdyHeadersFrame.setLast(isLast(httpResponse));
    return spdyHeadersFrame;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Entry(java.util.Map.Entry) Map(java.util.Map)

Example 87 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class SpdyHttpEncoder method createSynStreamFrame.

@SuppressWarnings("deprecation")
private SpdySynStreamFrame createSynStreamFrame(HttpRequest httpRequest) throws Exception {
    // Get the Stream-ID, Associated-To-Stream-ID, Priority, and scheme from the headers
    final HttpHeaders httpHeaders = httpRequest.headers();
    int streamId = httpHeaders.getInt(SpdyHttpHeaders.Names.STREAM_ID);
    int associatedToStreamId = httpHeaders.getInt(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID, 0);
    byte priority = (byte) httpHeaders.getInt(SpdyHttpHeaders.Names.PRIORITY, 0);
    CharSequence scheme = httpHeaders.get(SpdyHttpHeaders.Names.SCHEME);
    httpHeaders.remove(SpdyHttpHeaders.Names.STREAM_ID);
    httpHeaders.remove(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID);
    httpHeaders.remove(SpdyHttpHeaders.Names.PRIORITY);
    httpHeaders.remove(SpdyHttpHeaders.Names.SCHEME);
    // The Connection, Keep-Alive, Proxy-Connection, and Transfer-Encoding
    // headers are not valid and MUST not be sent.
    httpHeaders.remove(HttpHeaderNames.CONNECTION);
    httpHeaders.remove("Keep-Alive");
    httpHeaders.remove("Proxy-Connection");
    httpHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
    SpdySynStreamFrame spdySynStreamFrame = new DefaultSpdySynStreamFrame(streamId, associatedToStreamId, priority, validateHeaders);
    // Unfold the first line of the message into name/value pairs
    SpdyHeaders frameHeaders = spdySynStreamFrame.headers();
    frameHeaders.set(SpdyHeaders.HttpNames.METHOD, httpRequest.method().name());
    frameHeaders.set(SpdyHeaders.HttpNames.PATH, httpRequest.uri());
    frameHeaders.set(SpdyHeaders.HttpNames.VERSION, httpRequest.protocolVersion().text());
    // Replace the HTTP host header with the SPDY host header
    CharSequence host = httpHeaders.get(HttpHeaderNames.HOST);
    httpHeaders.remove(HttpHeaderNames.HOST);
    frameHeaders.set(SpdyHeaders.HttpNames.HOST, host);
    // Set the SPDY scheme header
    if (scheme == null) {
        scheme = "https";
    }
    frameHeaders.set(SpdyHeaders.HttpNames.SCHEME, scheme);
    // Transfer the remaining HTTP headers
    Iterator<Entry<CharSequence, CharSequence>> itr = httpHeaders.iteratorCharSequence();
    while (itr.hasNext()) {
        Map.Entry<CharSequence, CharSequence> entry = itr.next();
        final CharSequence headerName = headersToLowerCase ? AsciiString.of(entry.getKey()).toLowerCase() : entry.getKey();
        frameHeaders.add(headerName, entry.getValue());
    }
    currentStreamId = spdySynStreamFrame.streamId();
    if (associatedToStreamId == 0) {
        spdySynStreamFrame.setLast(isLast(httpRequest));
    } else {
        spdySynStreamFrame.setUnidirectional(true);
    }
    return spdySynStreamFrame;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Entry(java.util.Map.Entry) Map(java.util.Map)

Example 88 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class HttpToHttp2ConnectionHandlerTest method testAuthorityFormRequestTargetHandled.

@Test
public void testAuthorityFormRequestTargetHandled() throws Exception {
    bootstrapEnv(2, 1, 0);
    final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, CONNECT, "http://www.example.com:80");
    final HttpHeaders httpHeaders = request.headers();
    httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 5);
    final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("CONNECT")).path(new AsciiString("/")).scheme(new AsciiString("http")).authority(new AsciiString("www.example.com:80"));
    ChannelPromise writePromise = newPromise();
    verifyHeadersOnly(http2Headers, writePromise, clientChannel.writeAndFlush(request, writePromise));
}
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) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.jupiter.api.Test)

Example 89 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class HttpToHttp2ConnectionHandlerTest method testHeadersOnlyRequest.

@Test
public void testHeadersOnlyRequest() throws Exception {
    bootstrapEnv(2, 1, 0);
    final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "http://my-user_name@www.example.org:5555/example");
    final HttpHeaders httpHeaders = request.headers();
    httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 5);
    httpHeaders.set(HttpHeaderNames.HOST, "my-user_name@www.example.org:5555");
    httpHeaders.set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), "http");
    httpHeaders.add(of("foo"), of("goo"));
    httpHeaders.add(of("foo"), of("goo2"));
    httpHeaders.add(of("foo2"), of("goo2"));
    final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).path(new AsciiString("/example")).authority(new AsciiString("www.example.org:5555")).scheme(new AsciiString("http")).add(new AsciiString("foo"), new AsciiString("goo")).add(new AsciiString("foo"), new AsciiString("goo2")).add(new AsciiString("foo2"), new AsciiString("goo2"));
    ChannelPromise writePromise = newPromise();
    verifyHeadersOnly(http2Headers, writePromise, clientChannel.writeAndFlush(request, writePromise));
}
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) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.jupiter.api.Test)

Example 90 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class HttpToHttp2ConnectionHandlerTest method testNoSchemeRequestTargetHandled.

@Test
public void testNoSchemeRequestTargetHandled() throws Exception {
    bootstrapEnv(2, 1, 0);
    final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/");
    final HttpHeaders httpHeaders = request.headers();
    httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 5);
    httpHeaders.set(HttpHeaderNames.HOST, "localhost");
    ChannelPromise writePromise = newPromise();
    ChannelFuture writeFuture = clientChannel.writeAndFlush(request, writePromise);
    assertTrue(writePromise.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(writePromise.isDone());
    assertFalse(writePromise.isSuccess());
    assertTrue(writeFuture.isDone());
    assertFalse(writeFuture.isSuccess());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) 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) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.jupiter.api.Test)

Aggregations

HttpHeaders (io.netty.handler.codec.http.HttpHeaders)286 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)149 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)83 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)73 Test (org.junit.Test)68 Test (org.junit.jupiter.api.Test)57 Test (org.testng.annotations.Test)51 HttpRequest (io.netty.handler.codec.http.HttpRequest)43 HttpResponse (io.netty.handler.codec.http.HttpResponse)40 AsciiString (io.netty.util.AsciiString)29 ByteBuf (io.netty.buffer.ByteBuf)27 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)27 BStruct (org.ballerinalang.model.values.BStruct)26 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)22 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)20 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)19 Cookie (io.netty.handler.codec.http.cookie.Cookie)19 BValue (org.ballerinalang.model.values.BValue)19 ChannelPromise (io.netty.channel.ChannelPromise)18