}
}
- if (bytes > sizeof(wsh->buffer)) {
+ if (bytes > sizeof(wsh->buffer) -1) {
goto err;
}
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);
#endif
} while (r == -1 && SSL_get_error(wsh->ssl, r) == SSL_ERROR_WANT_READ && x < 100);
- return r;
+ goto end;
}
do {
r = -1;
}
+ end:
+
+ if (r > 0) {
+ *((char *)data + r) = '\0';
+ }
+
return r;
}
restore_socket(wsh->sock);
- if (wsh->close_sock) {
+ if (wsh->close_sock && wsh->sock != ws_sock_invalid) {
close(wsh->sock);
}
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);