[sr-dev] git:pd/websocket: modules/websocket: now using libunistring:u8_check() to work out whether to send text or binary websocket frames

Peter Dunkley peter.dunkley at crocodile-rcs.com
Sat Jun 23 23:01:25 CEST 2012


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

Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date:   Sat Jun 23 21:57:37 2012 +0100

modules/websocket: now using libunistring:u8_check() to work out whether to send text or binary websocket frames

- There is no straight-forward (or practical) way to be sure that a SIP request
  only contains UTF-8 characters and therefore should be sent as text instead
  of binary.  However, you can sometimes tell whether the request definitely
  isn't UTF-8 by checking for invalid byte sequences - and when it is definitely
  not UTF-8 frames _MUST_ be sent as binary.

---

 modules/websocket/Makefile   |    1 +
 modules/websocket/ws_frame.c |   10 +++++-----
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/modules/websocket/Makefile b/modules/websocket/Makefile
index dade653..8304bfb 100644
--- a/modules/websocket/Makefile
+++ b/modules/websocket/Makefile
@@ -17,6 +17,7 @@ else
 	DEFS+= $(shell pkg-config --cflags libssl)
 	LIBS=  $(shell pkg-config --libs libssl)
 endif
+LIBS+= -lunistring
 
 DEFS+=-DOPENSER_MOD_INTERFACE
 
diff --git a/modules/websocket/ws_frame.c b/modules/websocket/ws_frame.c
index 6b565c3..ec5f8a5 100644
--- a/modules/websocket/ws_frame.c
+++ b/modules/websocket/ws_frame.c
@@ -22,6 +22,7 @@
  */
 
 #include <limits.h>
+#include <unistr.h>
 #include "../../receive.h"
 #include "../../stats.h"
 #include "../../str.h"
@@ -592,9 +593,10 @@ int ws_frame_transmit(void *data)
 
 	memset(&frame, 0, sizeof(frame));
 	frame.fin = 1;
-	/* Can't be sure whether this message is UTF-8 or not so always send
-	   as binary */
-	frame.opcode = OPCODE_BINARY_FRAME;
+	/* Can't be sure whether this message is UTF-8 or not so check to see
+	   if it "might" be UTF-8 and send as binary if it definitely isn't */
+	frame.opcode = (u8_check((uint8_t *) wsev->buf, wsev->len) == NULL) ?
+				OPCODE_TEXT_FRAME: OPCODE_BINARY_FRAME;
 	frame.payload_len = wsev->len;
 	frame.payload_data = wsev->buf;
 	frame.wsc = wsconn_get(wsev->id);
@@ -602,11 +604,9 @@ int ws_frame_transmit(void *data)
 	if (encode_and_send_ws_frame(&frame, CONN_CLOSE_DONT) < 0)
 	{	
 		LM_ERR("sending SIP message\n");
-		if (wsev->buf) pkg_free(wsev->buf);
 		return -1;
 	}
 
-	if (wsev->buf) pkg_free(wsev->buf);
 	return 0;
 }
 




More information about the sr-dev mailing list