minor fixes in ws.c

This commit is contained in:
Anthony Minessale 2013-09-18 05:12:23 +05:00
parent 1d992ea9ea
commit f1d0357e92
2 changed files with 20 additions and 6 deletions

View File

@ -1 +1 @@
Tue Sep 17 06:24:17 CDT 2013
Wed Sep 18 05:12:10 CDT 2013

View File

@ -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);