Search in sources :

Example 1 with HeadersAdaptor

use of io.vertx.core.http.impl.headers.HeadersAdaptor in project vert.x by eclipse.

the class WebSocketHandshakeInboundHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse resp = (HttpResponse) msg;
        response = new DefaultFullHttpResponse(resp.protocolVersion(), resp.status());
        response.headers().add(resp.headers());
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        try {
            if (response != null) {
                response.content().writeBytes(content.content());
                if (msg instanceof LastHttpContent) {
                    response.trailingHeaders().add(((LastHttpContent) msg).trailingHeaders());
                    ChannelPipeline pipeline = chctx.pipeline();
                    pipeline.remove(WebSocketHandshakeInboundHandler.this);
                    ChannelHandler handler = pipeline.get(HttpContentDecompressor.class);
                    if (handler != null) {
                        // remove decompressor as its not needed anymore once connection was upgraded to WebSocket
                        ctx.pipeline().remove(handler);
                    }
                    Future<HeadersAdaptor> fut = handshakeComplete(response);
                    wsHandler.handle(fut);
                }
            }
        } finally {
            content.release();
        }
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HeadersAdaptor(io.vertx.core.http.impl.headers.HeadersAdaptor) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelHandler(io.netty.channel.ChannelHandler) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 2 with HeadersAdaptor

use of io.vertx.core.http.impl.headers.HeadersAdaptor in project vert.x by eclipse.

the class HeadersTestBase method testSetAllOnExistingMapUsingMultiMapHttp1.

@Test
public void testSetAllOnExistingMapUsingMultiMapHttp1() {
    MultiMap mainMap = new HeadersAdaptor(HeadersMultiMap.httpHeaders());
    mainMap.add("originalKey", "originalValue");
    MultiMap setAllMap = newMultiMap();
    setAllMap.add("originalKey", "newValue");
    setAllMap.add("anotherKey", "anotherValue");
    MultiMap result = mainMap.setAll(setAllMap);
    assertNotNull(result);
    assertFalse(result.isEmpty());
    assertEquals(2, result.size());
    assertEquals("newValue", result.get("originalKey"));
    assertEquals("anotherValue", result.get("anotherKey"));
}
Also used : MultiMap(io.vertx.core.MultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) HeadersAdaptor(io.vertx.core.http.impl.headers.HeadersAdaptor) Http2HeadersAdaptor(io.vertx.core.http.impl.headers.Http2HeadersAdaptor) Test(org.junit.Test)

Example 3 with HeadersAdaptor

use of io.vertx.core.http.impl.headers.HeadersAdaptor in project vert.x by eclipse.

the class WebSocketHandshakeInboundHandler method handshakeComplete.

private Future<HeadersAdaptor> handshakeComplete(FullHttpResponse response) {
    int sc = response.status().code();
    if (sc != 101) {
        String msg = "WebSocket upgrade failure: " + sc;
        ByteBuf content = response.content();
        if (content != null && content.readableBytes() > 0) {
            msg += " (" + content.toString(StandardCharsets.UTF_8) + ")";
        }
        UpgradeRejectedException failure = new UpgradeRejectedException(msg, sc);
        return Future.failedFuture(failure);
    } else {
        try {
            handshaker.finishHandshake(chctx.channel(), response);
            return Future.succeededFuture(new HeadersAdaptor(response.headers()));
        } catch (WebSocketHandshakeException e) {
            return Future.failedFuture(e);
        }
    }
}
Also used : HeadersAdaptor(io.vertx.core.http.impl.headers.HeadersAdaptor) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) ByteBuf(io.netty.buffer.ByteBuf) UpgradeRejectedException(io.vertx.core.http.UpgradeRejectedException)

Example 4 with HeadersAdaptor

use of io.vertx.core.http.impl.headers.HeadersAdaptor in project vert.x by eclipse.

the class HeadersTestBase method testSetAllOnExistingMapUsingHashMapHttp1.

@Test
public void testSetAllOnExistingMapUsingHashMapHttp1() {
    MultiMap mainMap = new HeadersAdaptor(HeadersMultiMap.httpHeaders());
    mainMap.add("originalKey", "originalValue");
    Map<String, String> setAllMap = new HashMap<>();
    setAllMap.put("originalKey", "newValue");
    setAllMap.put("anotherKey", "anotherValue");
    MultiMap result = mainMap.setAll(setAllMap);
    assertNotNull(result);
    assertFalse(result.isEmpty());
    assertEquals(2, result.size());
    assertEquals("newValue", result.get("originalKey"));
    assertEquals("anotherValue", result.get("anotherKey"));
}
Also used : MultiMap(io.vertx.core.MultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) HashMap(java.util.HashMap) HeadersAdaptor(io.vertx.core.http.impl.headers.HeadersAdaptor) Http2HeadersAdaptor(io.vertx.core.http.impl.headers.Http2HeadersAdaptor) Test(org.junit.Test)

Aggregations

HeadersAdaptor (io.vertx.core.http.impl.headers.HeadersAdaptor)4 MultiMap (io.vertx.core.MultiMap)2 HeadersMultiMap (io.vertx.core.http.impl.headers.HeadersMultiMap)2 Http2HeadersAdaptor (io.vertx.core.http.impl.headers.Http2HeadersAdaptor)2 Test (org.junit.Test)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)1 UpgradeRejectedException (io.vertx.core.http.UpgradeRejectedException)1 HashMap (java.util.HashMap)1