Search in sources :

Example 1 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project vert.x by eclipse.

the class HttpTest method getHeaders.

protected static MultiMap getHeaders(int num) {
    Map<String, String> map = genMap(num);
    MultiMap headers = new HeadersAdaptor(new DefaultHttpHeaders());
    for (Map.Entry<String, String> entry : map.entrySet()) {
        headers.add(entry.getKey(), entry.getValue());
    }
    return headers;
}
Also used : MultiMap(io.vertx.core.MultiMap) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) MultiMap(io.vertx.core.MultiMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with DefaultHttpHeaders

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

the class WebSocketClient method main.

public static void main(String[] args) throws Exception {
    URI uri = new URI(URL);
    String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
    final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    final int port;
    if (uri.getPort() == -1) {
        if ("ws".equalsIgnoreCase(scheme)) {
            port = 80;
        } else if ("wss".equalsIgnoreCase(scheme)) {
            port = 443;
        } else {
            port = -1;
        }
    } else {
        port = uri.getPort();
    }
    if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
        System.err.println("Only WS(S) is supported.");
        return;
    }
    final boolean ssl = "wss".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
        // If you change it to V00, ping is not supported and remember to change
        // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
        final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()));
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
                }
                p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, handler);
            }
        });
        Channel ch = b.connect(uri.getHost(), port).sync().channel();
        handler.handshakeFuture().sync();
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String msg = console.readLine();
            if (msg == null) {
                break;
            } else if ("bye".equals(msg.toLowerCase())) {
                ch.writeAndFlush(new CloseWebSocketFrame());
                ch.closeFuture().sync();
                break;
            } else if ("ping".equals(msg.toLowerCase())) {
                WebSocketFrame frame = new PingWebSocketFrame(Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 }));
                ch.writeAndFlush(frame);
            } else {
                WebSocketFrame frame = new TextWebSocketFrame(msg);
                ch.writeAndFlush(frame);
            }
        }
    } finally {
        group.shutdownGracefully();
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InputStreamReader(java.io.InputStreamReader) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) URI(java.net.URI) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) BufferedReader(java.io.BufferedReader) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) Bootstrap(io.netty.bootstrap.Bootstrap) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 3 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project riposte by Nike-Inc.

the class RoutingHandlerTest method beforeMethod.

@Before
public void beforeMethod() {
    stateMock = mock(HttpProcessingState.class);
    ctxMock = mock(ChannelHandlerContext.class);
    channelMock = mock(Channel.class);
    stateAttrMock = mock(Attribute.class);
    requestInfoMock = mock(RequestInfo.class);
    endpointMock = mock(StandardEndpoint.class);
    matcherMock = mock(Matcher.class);
    endpoints = new ArrayList<>(Collections.singleton(endpointMock));
    httpHeaders = new DefaultHttpHeaders();
    maxRequestSizeInBytes = 10;
    msg = mock(HttpRequest.class);
    doReturn(channelMock).when(ctxMock).channel();
    doReturn(stateAttrMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(stateMock).when(stateAttrMock).get();
    doReturn(endpointMock).when(stateMock).getEndpointForExecution();
    doReturn(matcherMock).when(endpointMock).requestMatcher();
    doReturn(Optional.of(defaultPath)).when(matcherMock).matchesPath(any(RequestInfo.class));
    doReturn(true).when(matcherMock).matchesMethod(any(RequestInfo.class));
    doReturn(requestInfoMock).when(stateMock).getRequestInfo();
    doReturn(httpHeaders).when(msg).headers();
    handlerSpy = spy(new RoutingHandler(endpoints, maxRequestSizeInBytes));
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Attribute(io.netty.util.Attribute) Matcher(com.nike.riposte.util.Matcher) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) Channel(io.netty.channel.Channel) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RequestInfo(com.nike.riposte.server.http.RequestInfo) Before(org.junit.Before)

Example 4 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project riposte by Nike-Inc.

the class FullResponseInfoTest method builder_sets_values_as_expected.

@Test
public void builder_sets_values_as_expected() {
    // given
    String content = UUID.randomUUID().toString();
    int httpStatusCode = 200;
    HttpHeaders headers = new DefaultHttpHeaders();
    String mimeType = "text/text";
    Charset contentCharset = CharsetUtil.ISO_8859_1;
    Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
    boolean preventCompressedOutput = true;
    // when
    FullResponseInfo<String> responseInfo = ResponseInfo.<String>newBuilder().withContentForFullResponse(content).withHttpStatusCode(httpStatusCode).withHeaders(headers).withDesiredContentWriterMimeType(mimeType).withDesiredContentWriterEncoding(contentCharset).withCookies(cookies).withPreventCompressedOutput(preventCompressedOutput).build();
    // then
    assertThat(responseInfo.getContentForFullResponse(), is(content));
    assertThat(responseInfo.getHttpStatusCode(), is(httpStatusCode));
    assertThat(responseInfo.getHeaders(), is(headers));
    assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType));
    assertThat(responseInfo.getDesiredContentWriterEncoding(), is(contentCharset));
    assertThat(responseInfo.getCookies(), is(cookies));
    assertThat(responseInfo.getUncompressedRawContentLength(), nullValue());
    assertThat(responseInfo.getFinalContentLength(), nullValue());
    assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedOutput));
    assertThat(responseInfo.isChunkedResponse(), is(false));
    assertThat(responseInfo.isResponseSendingStarted(), is(false));
    assertThat(responseInfo.isResponseSendingLastChunkSent(), is(false));
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Charset(java.nio.charset.Charset) Test(org.junit.Test)

Example 5 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project riposte by Nike-Inc.

the class FullResponseInfoTest method uber_constructor_for_full_response_sets_fields_as_expected.

@Test
public void uber_constructor_for_full_response_sets_fields_as_expected() {
    // given
    String content = UUID.randomUUID().toString();
    int httpStatusCode = 200;
    HttpHeaders headers = new DefaultHttpHeaders();
    String mimeType = "text/text";
    Charset contentCharset = CharsetUtil.UTF_8;
    Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
    boolean preventCompressedResponse = true;
    // when
    FullResponseInfo<String> responseInfo = new FullResponseInfo<>(content, httpStatusCode, headers, mimeType, contentCharset, cookies, preventCompressedResponse);
    // then
    assertThat(responseInfo.getContentForFullResponse(), is(content));
    assertThat(responseInfo.getHttpStatusCode(), is(httpStatusCode));
    assertThat(responseInfo.getHeaders(), is(headers));
    assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType));
    assertThat(responseInfo.getDesiredContentWriterEncoding(), is(contentCharset));
    assertThat(responseInfo.getCookies(), is(cookies));
    assertThat(responseInfo.getUncompressedRawContentLength(), nullValue());
    assertThat(responseInfo.getFinalContentLength(), nullValue());
    assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedResponse));
    assertThat(responseInfo.isChunkedResponse(), is(false));
    assertThat(responseInfo.isResponseSendingStarted(), is(false));
    assertThat(responseInfo.isResponseSendingLastChunkSent(), is(false));
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Charset(java.nio.charset.Charset) Test(org.junit.Test)

Aggregations

DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)49 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)36 Test (org.testng.annotations.Test)20 Test (org.junit.Test)18 Cookie (io.netty.handler.codec.http.cookie.Cookie)14 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)12 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 HttpTest (org.asynchttpclient.testserver.HttpTest)12 AsyncCompletionHandlerAdapter (org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)9 Charset (java.nio.charset.Charset)8 HttpResponseHeaders (org.asynchttpclient.HttpResponseHeaders)6 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 RequestInfo (com.nike.riposte.server.http.RequestInfo)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Map (java.util.Map)4 ByteBuf (io.netty.buffer.ByteBuf)3 Channel (io.netty.channel.Channel)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3