aboutsummaryrefslogtreecommitdiff
path: root/src/examples/websocket_threaded_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/websocket_threaded_example.c')
-rw-r--r--src/examples/websocket_threaded_example.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/examples/websocket_threaded_example.c b/src/examples/websocket_threaded_example.c
index 2310b82b..98796767 100644
--- a/src/examples/websocket_threaded_example.c
+++ b/src/examples/websocket_threaded_example.c
@@ -24,6 +24,8 @@
24 * @author Silvio Clecio (silvioprog) 24 * @author Silvio Clecio (silvioprog)
25 */ 25 */
26 26
27/* TODO: allow to send large messages. */
28
27#include "platform.h" 29#include "platform.h"
28#include <pthread.h> 30#include <pthread.h>
29#include <microhttpd.h> 31#include <microhttpd.h>
@@ -109,8 +111,10 @@ struct WsData
109 MHD_socket sock; 111 MHD_socket sock;
110}; 112};
111 113
114
112/********** begin SHA-1 **********/ 115/********** begin SHA-1 **********/
113 116
117
114#define SHA1HashSize 20 118#define SHA1HashSize 20
115 119
116#define SHA1CircularShift(bits, word) \ 120#define SHA1CircularShift(bits, word) \
@@ -341,17 +345,19 @@ SHA1Input (struct SHA1Context *context, const uint8_t *message_array,
341 345
342/********** end SHA-1 **********/ 346/********** end SHA-1 **********/
343 347
348
344/********** begin Base64 **********/ 349/********** begin Base64 **********/
345 350
351
346ssize_t 352ssize_t
347BASE64Encode (const void *in, size_t len, unsigned char **output) 353BASE64Encode (const void *in, size_t len, char **output)
348{ 354{
349#define FILLCHAR '=' 355#define FILLCHAR '='
350 const unsigned char *cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 356 const char *cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
351 "abcdefghijklmnopqrstuvwxyz" 357 "abcdefghijklmnopqrstuvwxyz"
352 "0123456789+/"; 358 "0123456789+/";
353 const unsigned char *data = in; 359 const char *data = in;
354 unsigned char *opt; 360 char *opt;
355 ssize_t ret; 361 ssize_t ret;
356 ssize_t i; 362 ssize_t i;
357 char c; 363 char c;
@@ -364,13 +370,13 @@ BASE64Encode (const void *in, size_t len, unsigned char **output)
364 for (i = 0; i < len; ++i) 370 for (i = 0; i < len; ++i)
365 { 371 {
366 c = (data[i] >> 2) & 0x3F; 372 c = (data[i] >> 2) & 0x3F;
367 opt[ret++] = cvt[(unsigned int) c]; 373 opt[ret++] = cvt[(int) c];
368 c = (data[i] << 4) & 0x3F; 374 c = (data[i] << 4) & 0x3F;
369 if (++i < len) 375 if (++i < len)
370 { 376 {
371 c |= (data[i] >> 4) & 0x0F; 377 c |= (data[i] >> 4) & 0x0F;
372 } 378 }
373 opt[ret++] = cvt[(unsigned int) c]; 379 opt[ret++] = cvt[(int) c];
374 if (i < len) 380 if (i < len)
375 { 381 {
376 c = (data[i] << 2) & 0x3F; 382 c = (data[i] << 2) & 0x3F;
@@ -378,7 +384,7 @@ BASE64Encode (const void *in, size_t len, unsigned char **output)
378 { 384 {
379 c |= (data[i] >> 6) & 0x03; 385 c |= (data[i] >> 6) & 0x03;
380 } 386 }
381 opt[ret++] = cvt[(unsigned int) c]; 387 opt[ret++] = cvt[(int) c];
382 } 388 }
383 else 389 else
384 { 390 {
@@ -388,7 +394,7 @@ BASE64Encode (const void *in, size_t len, unsigned char **output)
388 if (i < len) 394 if (i < len)
389 { 395 {
390 c = data[i] & 0x3F; 396 c = data[i] & 0x3F;
391 opt[ret++] = cvt[(unsigned int) c]; 397 opt[ret++] = cvt[(int) c];
392 } 398 }
393 else 399 else
394 { 400 {
@@ -402,6 +408,7 @@ BASE64Encode (const void *in, size_t len, unsigned char **output)
402 408
403/********** end Base64 **********/ 409/********** end Base64 **********/
404 410
411
405static enum MHD_Result 412static enum MHD_Result
406is_websocket_request (struct MHD_Connection *con, const char *upg_header, 413is_websocket_request (struct MHD_Connection *con, const char *upg_header,
407 const char *con_header) 414 const char *con_header)
@@ -458,10 +465,10 @@ send_upgrade_required (struct MHD_Connection *con)
458 465
459 466
460static enum MHD_Result 467static enum MHD_Result
461ws_get_accept_value (char *key, unsigned char **val) 468ws_get_accept_value (const char *key, char **val)
462{ 469{
463 struct SHA1Context ctx; 470 struct SHA1Context ctx;
464 unsigned char hash[SHA1HashSize]; 471 char hash[SHA1HashSize];
465 char *str; 472 char *str;
466 ssize_t len; 473 ssize_t len;
467 if (NULL == key) 474 if (NULL == key)
@@ -731,7 +738,7 @@ run_usock (void *cls)
731} 738}
732 739
733 740
734static enum MHD_Result 741static void
735uh_cb (void *cls, struct MHD_Connection *con, void *con_cls, 742uh_cb (void *cls, struct MHD_Connection *con, void *con_cls,
736 const char *extra_in, size_t extra_in_size, MHD_socket sock, 743 const char *extra_in, size_t extra_in_size, MHD_socket sock,
737 struct MHD_UpgradeResponseHandle *urh) 744 struct MHD_UpgradeResponseHandle *urh)
@@ -743,6 +750,8 @@ uh_cb (void *cls, struct MHD_Connection *con, void *con_cls,
743 (void) cls; 750 (void) cls;
744 (void) con; 751 (void) con;
745 (void) con_cls; 752 (void) con_cls;
753 (void) extra_in;
754 (void) extra_in_size;
746 sock_overflow = MHD_YES; 755 sock_overflow = MHD_YES;
747 ws = malloc (sizeof (struct WsData)); 756 ws = malloc (sizeof (struct WsData));
748 if (NULL == ws) 757 if (NULL == ws)
@@ -764,7 +773,7 @@ uh_cb (void *cls, struct MHD_Connection *con, void *con_cls,
764 { 773 {
765 free (ws); 774 free (ws);
766 MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE); 775 MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE);
767 return MHD_YES; 776 return;
768 } 777 }
769 pthread_mutex_unlock (&MUTEX); 778 pthread_mutex_unlock (&MUTEX);
770 if (0 != pthread_create (&pt, NULL, &run_usock, ws)) 779 if (0 != pthread_create (&pt, NULL, &run_usock, ws))
@@ -773,7 +782,6 @@ uh_cb (void *cls, struct MHD_Connection *con, void *con_cls,
773 a clean shutdown, as the we stop the daemon even if a worker thread 782 a clean shutdown, as the we stop the daemon even if a worker thread
774 is still running. Alas, this is a simple example... */ 783 is still running. Alas, this is a simple example... */
775 pthread_detach (pt); 784 pthread_detach (pt);
776 return MHD_YES;
777} 785}
778 786
779 787