Module: sip-router
Branch: pd/websocket
Commit: 5456e4e90c6330877e0d25d14fba143f2f98f8c6
URL:    
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=5456e4e90c6330877e0d25d14fba143f2f98f8c6

Author: Peter Dunkley <[email protected]>
Committer: Peter Dunkley <[email protected]>
Date:   Sat Jun 30 00:46:20 2012 +0100

modules/websocket: Updated connection reuse and closing flags for WebSocket 
handshake.

- Updated the sample kamailio.cfg to match too.

---

 modules/websocket/example/kamailio.cfg |    5 ++++
 modules/websocket/ws_handshake.c       |   37 +++++++++++++++++++------------
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/modules/websocket/example/kamailio.cfg 
b/modules/websocket/example/kamailio.cfg
index 48cfa4a..c4bd83f 100644
--- a/modules/websocket/example/kamailio.cfg
+++ b/modules/websocket/example/kamailio.cfg
@@ -306,6 +306,9 @@ onreply_route[WS_REPLY] {
 }
 
 event_route[xhttp:request] {
+       set_reply_close();
+       set_reply_no_connect();
+       
        if ($Rp != MY_WS_PORT && $Rp != MY_WSS_PORT) {
                xlog("L_WARN", "HTTP request received on $Rp\n");
                xhttp_reply("403", "Forbidden", "", "");
@@ -330,6 +333,8 @@ event_route[xhttp:request] {
                # Optional... validate Origin
                # Optional... perform HTTP authentication
 
+               # ws_handle_handshake() exits (no further configuration file
+               # processing of the request) when complete.
                ws_handle_handshake();
        }
 
diff --git a/modules/websocket/ws_handshake.c b/modules/websocket/ws_handshake.c
index 4bacd61..bb53ef7 100644
--- a/modules/websocket/ws_handshake.c
+++ b/modules/websocket/ws_handshake.c
@@ -115,6 +115,14 @@ int ws_handle_handshake(struct sip_msg *msg)
        int version;
        struct hdr_field *hdr = msg->headers;
        struct tcp_connection *con;
+       ws_connection_t *wsc;
+
+       /* Make sure that the connection is closed after the response _and_
+          the existing connection (from the request) is reused for the
+          response.  The close flag will be unset later if the handshake is
+          successful. */
+       msg->rpl_send_flags.f |= SND_F_CON_CLOSE;
+       msg->rpl_send_flags.f |= SND_F_FORCE_CON_REUSE;
 
        if (*ws_enabled == 0)
        {
@@ -283,7 +291,18 @@ int ws_handle_handshake(struct sip_msg *msg)
        reply_key.len = base64_enc(sha1, 20,
                                (unsigned char *) reply_key.s, KEY_BUF_LEN);
 
-       /* Build headers for reply */
+       /* Add the connection to the WebSocket connection table */
+       wsconn_add(msg->rcv.proto_reserved1);
+
+       /* Make sure Kamailio core sends future messages on this connection
+          directly to this module */
+       if (con->type == PROTO_TLS)
+               con->type = con->rcv.proto = PROTO_WSS;
+       else
+               con->type = con->rcv.proto = PROTO_WS;
+
+       /* Now Kamailio is ready to receive WebSocket frames build and send a
+          101 reply */
        headers.s = headers_buf;
        headers.len = snprintf(headers.s, HDR_BUF_LEN,
                        "%.*s: %.*s\r\n"
@@ -299,21 +318,11 @@ int ws_handle_handshake(struct sip_msg *msg)
                        reply_key.s, str_hdr_sec_websocket_protocol.len,
                        str_hdr_sec_websocket_protocol.s, str_sip.len,
                        str_sip.s);
-
-       /* Send reply */
+       msg->rpl_send_flags.f &= ~SND_F_CON_CLOSE;
        if (ws_send_reply(msg, 101,
                                &str_status_switching_protocols, &headers) < 0)
-               return 0;
-
-       /* Add the connection to the WebSocket connection table */
-       wsconn_add(con->id);
-
-       /* Make sure Kamailio core sends future messages on this connection
-          directly to this module */
-       if (con->type == PROTO_TLS)
-               con->type = con->rcv.proto = PROTO_WSS;
-       else
-               con->type = con->rcv.proto = PROTO_WS;
+               if ((wsc = wsconn_get(msg->rcv.proto_reserved1)) != NULL)
+                       wsconn_rm(wsc);
 
        return 0;
 }


_______________________________________________
sr-dev mailing list
[email protected]
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev

Reply via email to