33 |
33 |
34 #include "../util/io.h" |
34 #include "../util/io.h" |
35 #include "../util/pblock.h" |
35 #include "../util/pblock.h" |
36 #include "../util/util.h" |
36 #include "../util/util.h" |
37 #include "../util/strbuf.h" |
37 #include "../util/strbuf.h" |
38 #include <ucx/string.h> |
38 #include <cx/string.h> |
39 |
39 |
40 #define WS_BUFFER_LEN 2048 |
40 #define WS_BUFFER_LEN 2048 |
41 |
41 |
42 NSAPI_PUBLIC int http_handle_websocket(Session *sn, Request *rq, WebSocket *websocket) { |
42 NSAPI_PUBLIC int http_handle_websocket(Session *sn, Request *rq, WebSocket *websocket) { |
43 char *connection = pblock_findkeyval(pb_key_connection, rq->headers); |
43 char *connection = pblock_findkeyval(pb_key_connection, rq->headers); |
49 |
49 |
50 if(!connection || !upgrade) { |
50 if(!connection || !upgrade) { |
51 return REQ_NOACTION; |
51 return REQ_NOACTION; |
52 } |
52 } |
53 |
53 |
54 if(sstrcasecmp(sstr(connection), S("upgrade"))) { |
54 if(cx_strcasecmp(cx_str(connection), (cxstring)CX_STR("upgrade"))) { |
55 return REQ_NOACTION; |
55 return REQ_NOACTION; |
56 } |
56 } |
57 if(sstrcasecmp(sstr(upgrade), S("websocket"))) { |
57 if(cx_strcasecmp(cx_str(upgrade), (cxstring)CX_STR("websocket"))) { |
58 return REQ_NOACTION; |
58 return REQ_NOACTION; |
59 } |
59 } |
60 |
60 |
61 sstr_t wsaccept = sstrcat(2, sstr(wskey), S("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")); |
61 cxmutstr wsaccept = cx_strcat(2, cx_str(wskey), (cxstring)CX_STR("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")); |
62 unsigned char hash[20]; |
62 unsigned char hash[20]; |
63 SHA1((const unsigned char*)wsaccept.ptr, wsaccept.length, hash); |
63 SHA1((const unsigned char*)wsaccept.ptr, wsaccept.length, hash); |
64 char *websocket_accept = util_base64encode((char*)hash, 20); |
64 char *websocket_accept = util_base64encode((char*)hash, 20); |
65 |
65 |
66 sbuf_t *response = sbuf_new(512); |
66 sbuf_t *response = sbuf_new(512); |
67 sbuf_append(response, S("HTTP/1.1 101 Switching Protocols\r\n")); |
67 sbuf_append(response, (cxstring)CX_STR("HTTP/1.1 101 Switching Protocols\r\n")); |
68 sbuf_append(response, S("Upgrade: websocket\r\n")); |
68 sbuf_append(response, (cxstring)CX_STR("Upgrade: websocket\r\n")); |
69 sbuf_append(response, S("Connection: Upgrade\r\n")); |
69 sbuf_append(response, (cxstring)CX_STR("Connection: Upgrade\r\n")); |
70 sbuf_append(response, S("Sec-WebSocket-Accept: ")); |
70 sbuf_append(response, (cxstring)CX_STR("Sec-WebSocket-Accept: ")); |
71 sbuf_puts(response, websocket_accept); |
71 sbuf_puts(response, websocket_accept); |
72 sbuf_append(response, S("\r\n\r\n")); |
72 sbuf_append(response, (cxstring)CX_STR("\r\n\r\n")); |
73 |
73 |
74 net_write(sn->csd, response->ptr, response->length); |
74 net_write(sn->csd, response->ptr, response->length); |
75 sbuf_free(response); |
75 sbuf_free(response); |
76 free(websocket_accept); |
76 free(websocket_accept); |
77 free(wsaccept.ptr); |
77 free(wsaccept.ptr); |