浏览代码

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.
Peter Dunkley 13 年之前
父节点
当前提交
4611d052b9
共有 2 个文件被更改,包括 6 次插入5 次删除
  1. 1 0
      modules/websocket/Makefile
  2. 5 5
      modules/websocket/ws_frame.c

+ 1 - 0
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
 

+ 5 - 5
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;
 }