diff --git a/libs/sofia-sip/.update b/libs/sofia-sip/.update index 76ab95f85f..874992669b 100644 --- a/libs/sofia-sip/.update +++ b/libs/sofia-sip/.update @@ -1 +1 @@ -Tue Sep 17 06:24:17 CDT 2013 +Wed Sep 18 05:12:10 CDT 2013 diff --git a/libs/sofia-sip/libsofia-sip-ua/tport/ws.c b/libs/sofia-sip/libsofia-sip-ua/tport/ws.c index 6039aea826..7ee756c729 100644 --- a/libs/sofia-sip/libsofia-sip-ua/tport/ws.c +++ b/libs/sofia-sip/libsofia-sip-ua/tport/ws.c @@ -242,7 +242,7 @@ int ws_handshake(wsh_t *wsh) } } - if (bytes > sizeof(wsh->buffer)) { + if (bytes > sizeof(wsh->buffer) -1) { goto err; } @@ -273,12 +273,16 @@ int ws_handshake(wsh_t *wsh) sha1_digest(output, input); b64encode((unsigned char *)output, SHA1_HASH_SIZE, (unsigned char *)b64, sizeof(b64)); + if (*proto) { + snprintf(proto, sizeof(proto), "Sec-WebSocket-Protocol: %s\r\n", proto); + } + snprintf(respond, sizeof(respond), "HTTP/1.1 101 Switching Protocols\r\n" "Upgrade: websocket\r\n" "Connection: Upgrade\r\n" "Sec-WebSocket-Accept: %s\r\n" - "Sec-WebSocket-Protocol: %s\r\n\r\n", + "%s\r\n", b64, proto); @@ -319,7 +323,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes) #endif } while (r == -1 && SSL_get_error(wsh->ssl, r) == SSL_ERROR_WANT_READ && x < 100); - return r; + goto end; } do { @@ -336,6 +340,12 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes) r = -1; } + end: + + if (r > 0) { + *((char *)data + r) = '\0'; + } + return r; } @@ -530,7 +540,7 @@ ssize_t ws_close(wsh_t *wsh, int16_t reason) restore_socket(wsh->sock); - if (wsh->close_sock) { + if (wsh->close_sock && wsh->sock != ws_sock_invalid) { close(wsh->sock); } @@ -559,7 +569,11 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data) return ws_close(wsh, WS_PROTO_ERR); } - if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 9)) < need) { + if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 9)) < 0) { + return ws_close(wsh, WS_PROTO_ERR); + } + + if (wsh->datalen < need) { if ((wsh->datalen += ws_raw_read(wsh, wsh->buffer + wsh->datalen, 9 - wsh->datalen)) < need) { /* too small - protocol err */ return ws_close(wsh, WS_PROTO_ERR);