Search in sources :

Example 51 with HttpResponse

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

the class HttpSnoopClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());
        System.err.println();
        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
            System.err.println();
        }
        if (HttpUtil.isTransferEncodingChunked(response)) {
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();
        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            ctx.close();
        }
    }
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 52 with HttpResponse

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

the class WebSocketHandshakeExceptionTest method testClientExceptionWithResponse.

@Test
public void testClientExceptionWithResponse() {
    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
    httpResponse.headers().set("x-header", "x-value");
    WebSocketClientHandshakeException clientException = new WebSocketClientHandshakeException("client message", httpResponse);
    assertNotNull(clientException.response());
    assertEquals("client message", clientException.getMessage());
    assertEquals(HttpResponseStatus.BAD_REQUEST, clientException.response().status());
    assertEquals(httpResponse.headers(), clientException.response().headers());
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 53 with HttpResponse

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

the class WebSocketServerHandshaker00Test method testPerformOpeningHandshake0.

private static void testPerformOpeningHandshake0(boolean subProtocol) {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
    FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII));
    req.headers().set(HttpHeaderNames.HOST, "server.example.com");
    req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
    req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
    req.headers().set(HttpHeaderNames.ORIGIN, "http://example.com");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, "4 @1  46546xW%0l 1 5");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1  .P00");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
    if (subProtocol) {
        new WebSocketServerHandshaker00("ws://example.com/chat", "chat", Integer.MAX_VALUE).handshake(ch, req);
    } else {
        new WebSocketServerHandshaker00("ws://example.com/chat", null, Integer.MAX_VALUE).handshake(ch, req);
    }
    EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
    ch2.writeInbound(ch.readOutbound());
    HttpResponse res = ch2.readInbound();
    assertEquals("ws://example.com/chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_LOCATION));
    if (subProtocol) {
        assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    } else {
        assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    }
    LastHttpContent content = ch2.readInbound();
    assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII));
    content.release();
    req.release();
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 54 with HttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse 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)

Example 55 with HttpResponse

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

the class WebSocketServerExtensionHandlerTest method testMainSuccess.

@Test
public void testMainSuccess() {
    // initialize
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(mainExtensionMock);
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(null);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(fallbackExtensionMock);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(null);
    when(mainExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
    when(mainExtensionMock.newReponseData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
    when(mainExtensionMock.newExtensionEncoder()).thenReturn(new DummyEncoder());
    when(mainExtensionMock.newExtensionDecoder()).thenReturn(new DummyDecoder());
    when(fallbackExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
    // execute
    WebSocketServerExtensionHandler extensionHandler = new WebSocketServerExtensionHandler(mainHandshakerMock, fallbackHandshakerMock);
    EmbeddedChannel ch = new EmbeddedChannel(extensionHandler);
    HttpRequest req = newUpgradeRequest("main, fallback");
    ch.writeInbound(req);
    HttpResponse res = newUpgradeResponse(null);
    ch.writeOutbound(res);
    HttpResponse res2 = ch.readOutbound();
    List<WebSocketExtensionData> resExts = WebSocketExtensionUtil.extractExtensions(res2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
    // test
    assertNull(ch.pipeline().context(extensionHandler));
    assertEquals(1, resExts.size());
    assertEquals("main", resExts.get(0).name());
    assertTrue(resExts.get(0).parameters().isEmpty());
    assertNotNull(ch.pipeline().get(DummyDecoder.class));
    assertNotNull(ch.pipeline().get(DummyEncoder.class));
    verify(mainHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("main"));
    verify(mainHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(fallbackHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(mainExtensionMock, atLeastOnce()).rsv();
    verify(mainExtensionMock).newReponseData();
    verify(mainExtensionMock).newExtensionEncoder();
    verify(mainExtensionMock).newExtensionDecoder();
    verify(fallbackExtensionMock, atLeastOnce()).rsv();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (io.netty.handler.codec.http.HttpResponse)443 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)164 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)109 HttpRequest (io.netty.handler.codec.http.HttpRequest)104 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)86 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)75 Test (org.junit.Test)73 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)72 Test (org.junit.jupiter.api.Test)70 HttpContent (io.netty.handler.codec.http.HttpContent)66 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)64 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)63 ByteBuf (io.netty.buffer.ByteBuf)58 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)51 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)39 IOException (java.io.IOException)39 ChannelFuture (io.netty.channel.ChannelFuture)35 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)33 Map (java.util.Map)33 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)32