@ -94,17 +94,16 @@ ws_handshake_reply(struct http_client *c) {
const char * origin = NULL , * host = NULL ;
const char * origin = NULL , * host = NULL ;
size_t origin_sz = 0 , host_sz = 0 , handshake_sz = 0 , sz ;
size_t origin_sz = 0 , host_sz = 0 , handshake_sz = 0 , sz ;
char template 0 [ ] = " HTTP/1.1 101 Switching Protocols \r \n "
char template _start [ ] = " HTTP/1.1 101 Switching Protocols \r \n "
" Upgrade: websocket \r \n "
" Upgrade: websocket \r \n "
" Connection: Upgrade \r \n "
" Connection: Upgrade " ;
" Sec-WebSocket-Origin: " ; /* %s */
char template_accept [ ] = " \r \n " /* just after the start */
char template1 [ ] = " \r \n "
" Sec-WebSocket-Location: ws:// " ; /* %s%s */
char template2 [ ] = " \r \n "
" Origin: http:// " ; /* %s */
char template3 [ ] = " \r \n "
" Sec-WebSocket-Accept: " ; /* %s */
" Sec-WebSocket-Accept: " ; /* %s */
char template4 [ ] = " \r \n \r \n " ;
char template_sec_origin [ ] = " \r \n "
" Sec-WebSocket-Origin: " ; /* %s (optional header) */
char template_loc [ ] = " \r \n "
" Sec-WebSocket-Location: ws:// " ; /* %s%s */
char template_end [ ] = " \r \n \r \n " ;
if ( ( origin = client_get_header ( c , " Origin " ) ) ) {
if ( ( origin = client_get_header ( c , " Origin " ) ) ) {
origin_sz = strlen ( origin ) ;
origin_sz = strlen ( origin ) ;
@ -116,7 +115,7 @@ ws_handshake_reply(struct http_client *c) {
}
}
/* need those headers */
/* need those headers */
if ( ! origin | | ! origin_sz | | ! host | | ! host_sz | | ! c - > path | | ! c - > path_sz ) {
if ( ! host | | ! host_sz | | ! c - > path | | ! c - > path_sz ) {
slog ( c - > s , WEBDIS_WARNING , " Missing headers for WS handshake " , 0 ) ;
slog ( c - > s , WEBDIS_WARNING , " Missing headers for WS handshake " , 0 ) ;
return - 1 ;
return - 1 ;
}
}
@ -128,11 +127,11 @@ ws_handshake_reply(struct http_client *c) {
return - 1 ;
return - 1 ;
}
}
sz = sizeof ( template 0) - 1 + origin_sz
sz = sizeof ( template _start) - 1
+ sizeof ( template 1) - 1 + host_sz + c - > path _sz
+ sizeof ( template _accept) - 1 + handshake _sz
+ sizeof ( template2 ) - 1 + host_sz
+ ( origin & & origin_sz ? ( sizeof ( template_sec_origin ) - 1 + origin_sz ) : 0 ) /* optional origin */
+ sizeof ( template 3) - 1 + handshake _sz
+ sizeof ( template _loc) - 1 + host_sz + c - > path _sz
+ sizeof ( template 4 ) - 1 ;
+ sizeof ( template _end ) - 1 ;
p = buffer = malloc ( sz ) ;
p = buffer = malloc ( sz ) ;
if ( ! p ) {
if ( ! p ) {
@ -142,35 +141,35 @@ ws_handshake_reply(struct http_client *c) {
/* Concat all */
/* Concat all */
/* template0 */
/* template_start */
memcpy ( p , template0 , sizeof ( template0 ) - 1 ) ;
memcpy ( p , template_start , sizeof ( template_start ) - 1 ) ;
p + = sizeof ( template0 ) - 1 ;
p + = sizeof ( template_start ) - 1 ;
memcpy ( p , origin , origin_sz ) ;
p + = origin_sz ;
/* template_accept */
memcpy ( p , template_accept , sizeof ( template_accept ) - 1 ) ;
p + = sizeof ( template_accept ) - 1 ;
memcpy ( p , & sha1_handshake [ 0 ] , handshake_sz ) ;
p + = handshake_sz ;
/* template_sec_origin */
if ( origin & & origin_sz ) {
memcpy ( p , template_sec_origin , sizeof ( template_sec_origin ) - 1 ) ;
p + = sizeof ( template_sec_origin ) - 1 ;
memcpy ( p , origin , origin_sz ) ;
p + = origin_sz ;
}
/* template1 */
/* template _loc */
memcpy ( p , template1 , sizeof ( template1 ) - 1 ) ;
memcpy ( p , template _loc, sizeof ( template_loc ) - 1 ) ;
p + = sizeof ( template1 ) - 1 ;
p + = sizeof ( template _loc ) - 1 ;
memcpy ( p , host , host_sz ) ;
memcpy ( p , host , host_sz ) ;
p + = host_sz ;
p + = host_sz ;
memcpy ( p , c - > path , c - > path_sz ) ;
memcpy ( p , c - > path , c - > path_sz ) ;
p + = c - > path_sz ;
p + = c - > path_sz ;
/* template2 */
/* template_end */
memcpy ( p , template2 , sizeof ( template2 ) - 1 ) ;
memcpy ( p , template_end , sizeof ( template_end ) - 1 ) ;
p + = sizeof ( template2 ) - 1 ;
p + = sizeof ( template_end ) - 1 ;
memcpy ( p , host , host_sz ) ;
p + = host_sz ;
/* template3 */
memcpy ( p , template3 , sizeof ( template3 ) - 1 ) ;
p + = sizeof ( template3 ) - 1 ;
memcpy ( p , & sha1_handshake [ 0 ] , handshake_sz ) ;
p + = handshake_sz ;
/* template4 */
memcpy ( p , template4 , sizeof ( template4 ) - 1 ) ;
p + = sizeof ( template4 ) - 1 ;
/* build HTTP response object by hand, since we have the full response already */
/* build HTTP response object by hand, since we have the full response already */
struct http_response * r = calloc ( 1 , sizeof ( struct http_response ) ) ;
struct http_response * r = calloc ( 1 , sizeof ( struct http_response ) ) ;