Search in sources :

Example 6 with WebSocketServerHandshakerFactory

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

the class HttpServerImpl method createHandshaker.

WebSocketServerHandshaker createHandshaker(Channel ch, HttpRequest request) {
    // As a fun part, Firefox 6.0.2 supports Websockets protocol '7'. But,
    // it doesn't send a normal 'Connection: Upgrade' header. Instead it
    // sends: 'Connection: keep-alive, Upgrade'. Brilliant.
    String connectionHeader = request.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
    if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) {
        sendError("\"Connection\" must be \"Upgrade\".", BAD_REQUEST, ch);
        return null;
    }
    if (request.getMethod() != HttpMethod.GET) {
        sendError(null, METHOD_NOT_ALLOWED, ch);
        return null;
    }
    try {
        WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(ch.pipeline(), request), subProtocols, false, options.getMaxWebsocketFrameSize(), options.isAcceptUnmaskedFrames());
        WebSocketServerHandshaker shake = factory.newHandshaker(request);
        if (shake == null) {
            log.error("Unrecognised websockets handshake");
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ch);
        }
        return shake;
    } catch (Exception e) {
        throw new VertxException(e);
    }
}
Also used : WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) VertxException(io.vertx.core.VertxException) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) VertxException(io.vertx.core.VertxException) URISyntaxException(java.net.URISyntaxException) Http2Exception(io.netty.handler.codec.http2.Http2Exception) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)

Example 7 with WebSocketServerHandshakerFactory

use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory in project activemq-artemis by apache.

the class WebSocketServerHandler method handleHttpRequest.

private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
    // Allow only GET methods.
    if (req.method() != GET) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return;
    }
    // Handshake
    String supportedProtocolsCSV = StringUtil.joinStringList(supportedProtocols, ",");
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(this.getWebSocketLocation(req), supportedProtocolsCSV, false, maxFramePayloadLength);
    this.httpRequest = req;
    this.handshaker = wsFactory.newHandshaker(req);
    if (this.handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    } else {
        ChannelFuture handshake = this.handshaker.handshake(ctx.channel(), req);
        handshake.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    // we need to insert an encoder that takes the underlying ChannelBuffer of a StompFrame.toActiveMQBuffer and
                    // wrap it in a binary web socket frame before letting the wsencoder send it on the wire
                    future.channel().pipeline().addAfter("wsencoder", "binary-websocket-encoder", BINARY_WEBSOCKET_ENCODER);
                } else {
                    // Handshake failed, fire an exceptionCaught event
                    future.channel().pipeline().fireExceptionCaught(future.cause());
                }
            }
        });
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 8 with WebSocketServerHandshakerFactory

use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory in project netty by netty.

the class WebSocketServerHandler method handleHttpRequest.

private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
    // Handle a bad request.
    if (!req.decoderResult().isSuccess()) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), BAD_REQUEST, ctx.alloc().buffer(0)));
        return;
    }
    // Allow only GET methods.
    if (!GET.equals(req.method())) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), FORBIDDEN, ctx.alloc().buffer(0)));
        return;
    }
    // Send the demo page and favicon.ico
    if ("/".equals(req.uri())) {
        ByteBuf content = WebSocketServerBenchmarkPage.getContent(getWebSocketLocation(req));
        FullHttpResponse res = new DefaultFullHttpResponse(req.protocolVersion(), OK, content);
        res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
        HttpUtil.setContentLength(res, content.readableBytes());
        sendHttpResponse(ctx, req, res);
        return;
    }
    if ("/favicon.ico".equals(req.uri())) {
        FullHttpResponse res = new DefaultFullHttpResponse(req.protocolVersion(), NOT_FOUND, ctx.alloc().buffer(0));
        sendHttpResponse(ctx, req, res);
        return;
    }
    // Handshake
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, 5 * 1024 * 1024);
    handshaker = wsFactory.newHandshaker(req);
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    } else {
        handshaker.handshake(ctx.channel(), req);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) ByteBuf(io.netty.buffer.ByteBuf)

Example 9 with WebSocketServerHandshakerFactory

use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory in project moco by dreamhead.

the class ActualWebSocketServer method connectRequest.

public void connectRequest(final ChannelHandlerContext ctx, final FullHttpRequest request) {
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
    final String actual = decoder.path();
    if (!uri.equals(actual)) {
        ctx.writeAndFlush(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
        return;
    }
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(this.uri, null, false);
    WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(request);
    Channel channel = ctx.channel();
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel);
        return;
    }
    handshaker.handshake(channel, request);
    connect(channel);
    sendConnected(channel);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) Channel(io.netty.channel.Channel) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)

Aggregations

WebSocketServerHandshakerFactory (io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)9 WebSocketServerHandshaker (io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker)5 ChannelFuture (io.netty.channel.ChannelFuture)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)4 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)4 Channel (io.netty.channel.Channel)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)2 WebSocketFrameAggregator (io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ByteBuf (io.netty.buffer.ByteBuf)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 EMPTY_HEADERS (io.netty.handler.codec.http.HttpHeaders.EMPTY_HEADERS)1 HOST (io.netty.handler.codec.http.HttpHeaders.Names.HOST)1 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)1