Search in sources :

Example 1 with WebSocketFrameDecoder

use of org.jboss.netty.handler.codec.http.websocket.WebSocketFrameDecoder in project socket.io-netty by ibdknox.

the class WebSocketServerHandler method handleHttpRequest.

private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
    String reqURI = req.getUri();
    if (reqURI.contains(POLLING_PATH)) {
        String[] parts = reqURI.split("/");
        String ID = parts.length > 3 ? parts[3] : "";
        PollingIOClient client = (PollingIOClient) this.pollingClients.get(ID);
        if (client == null) {
            //new client
            client = connectPoller(ctx);
            client.Reconnect(ctx, req);
            return;
        }
        if (req.getMethod() == GET) {
            client.heartbeat();
            client.Reconnect(ctx, req);
        } else {
            //we got a message
            QueryStringDecoder decoder = new QueryStringDecoder("/?" + req.getContent().toString(CharsetUtil.UTF_8));
            String message = decoder.getParameters().get("data").get(0);
            handleMessage(client, message);
            //make sure the connection is closed once we send a response
            setKeepAlive(req, false);
            //send a response that allows for cross domain access
            HttpResponse resp = new DefaultHttpResponse(HTTP_1_1, OK);
            resp.addHeader("Access-Control-Allow-Origin", "*");
            sendHttpResponse(ctx, req, resp);
        }
        return;
    }
    // Serve the WebSocket handshake request.
    String location = "";
    if (reqURI.equals(WEBSOCKET_PATH)) {
        location = getWebSocketLocation(req);
    } else if (reqURI.equals(FLASHSOCKET_PATH)) {
        location = getFlashSocketLocation(req);
    }
    if (location != "" && Values.UPGRADE.equalsIgnoreCase(req.getHeader(CONNECTION)) && WEBSOCKET.equalsIgnoreCase(req.getHeader(Names.UPGRADE))) {
        // Create the WebSocket handshake response.
        HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
        res.addHeader(Names.UPGRADE, WEBSOCKET);
        res.addHeader(CONNECTION, Values.UPGRADE);
        // Fill in the headers and contents depending on handshake method.
        if (req.containsHeader(SEC_WEBSOCKET_KEY1) && req.containsHeader(SEC_WEBSOCKET_KEY2)) {
            // New handshake method with a challenge:
            res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
            res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req));
            String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL);
            if (protocol != null) {
                res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol);
            }
            // Calculate the answer of the challenge.
            String key1 = req.getHeader(SEC_WEBSOCKET_KEY1);
            String key2 = req.getHeader(SEC_WEBSOCKET_KEY2);
            int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length());
            int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length());
            long c = req.getContent().readLong();
            ChannelBuffer input = ChannelBuffers.buffer(16);
            input.writeInt(a);
            input.writeInt(b);
            input.writeLong(c);
            ChannelBuffer output = ChannelBuffers.wrappedBuffer(MessageDigest.getInstance("MD5").digest(input.array()));
            res.setContent(output);
        } else {
            // Old handshake method with no challenge:
            res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
            res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req));
            String protocol = req.getHeader(WEBSOCKET_PROTOCOL);
            if (protocol != null) {
                res.addHeader(WEBSOCKET_PROTOCOL, protocol);
            }
        }
        // Upgrade the connection and send the handshake response.
        ChannelPipeline p = ctx.getChannel().getPipeline();
        p.remove("aggregator");
        p.replace("decoder", "wsdecoder", new WebSocketFrameDecoder());
        ctx.getChannel().write(res);
        p.replace("encoder", "wsencoder", new WebSocketFrameEncoder());
        connectSocket(ctx);
        return;
    }
    // Send an error page otherwise.
    sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN));
}
Also used : HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) QueryStringDecoder(org.jboss.netty.handler.codec.http.QueryStringDecoder) WebSocketFrameEncoder(org.jboss.netty.handler.codec.http.websocket.WebSocketFrameEncoder) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) WebSocketFrameDecoder(org.jboss.netty.handler.codec.http.websocket.WebSocketFrameDecoder)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)1 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)1 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)1 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)1 HttpResponseStatus (org.jboss.netty.handler.codec.http.HttpResponseStatus)1 QueryStringDecoder (org.jboss.netty.handler.codec.http.QueryStringDecoder)1 WebSocketFrameDecoder (org.jboss.netty.handler.codec.http.websocket.WebSocketFrameDecoder)1 WebSocketFrameEncoder (org.jboss.netty.handler.codec.http.websocket.WebSocketFrameEncoder)1