#include <assert.h>
#include <errno.h>
#include <sys/uio.h>
+#include <string.h>
#include <openssl/sha.h>
#include <openssl/bio.h>
#include "websock.h"
-#include "../include/local-def.h"
+#include "local-def.h"
#include "afb-method.h"
#include "afb-hreq.h"
static const char sec_websocket_version_s[] = "Sec-WebSocket-Version";
static const char sec_websocket_accept_s[] = "Sec-WebSocket-Accept";
static const char sec_websocket_protocol_s[] = "Sec-WebSocket-Protocol";
-static const char websocket_uuid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
+static const char websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
struct afb_websock
{
unsigned char md[SHA_DIGEST_LENGTH+1];
size_t len = strlen(key);
- char *buffer = alloca(len + sizeof websocket_uuid - 1);
+ char *buffer = alloca(len + sizeof websocket_guid - 1);
memcpy(buffer, key, len);
- memcpy(buffer + len, websocket_uuid, sizeof websocket_uuid - 1);
- SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_uuid - 1), md);
+ memcpy(buffer + len, websocket_guid, sizeof websocket_guid - 1);
+ SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_guid - 1), md);
assert(SHA_DIGEST_LENGTH == 20);
md[20] = 0;
enc64(&md[0], &result[0]);
if (!*header)
return 0;
len = strcspn(header, sep);
-printf("!!!%.*s!!!\n",len,header);
if (n == len && 0 == strncasecmp(needle, header, n))
return 1;
header += len;
/* is an upgrade to websocket ? */
upgrade = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_UPGRADE);
-printf("upgrade %s\n", upgrade);
if (upgrade == NULL || strcasecmp(upgrade, websocket_s))
return 0;
/* is a connection for upgrade ? */
connection = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONNECTION);
-printf("connection %s\n", connection);
if (connection == NULL || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
return 0;
/* has a key and a version ? */
key = afb_hreq_get_header(hreq, sec_websocket_key_s);
version = afb_hreq_get_header(hreq, sec_websocket_version_s);
-printf("key %s\n", key);
-printf("version %s\n", connection);
if (key == NULL || version == NULL)
return 0;
/* is a supported version ? */
vernum = atoi(version);
if (vernum != 13) {
- response = MHD_create_response_from_data(0,NULL,0,0);
+ response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
MHD_add_response_header (response, sec_websocket_version_s, "13");
MHD_queue_response (hreq->connection, MHD_HTTP_BAD_REQUEST, response);
MHD_destroy_response (response);
/* send the accept connection */
make_accept_value(key, acceptval);
- response = MHD_create_response_from_data(0,NULL,0,0);
+ response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
MHD_add_response_header (response, sec_websocket_accept_s, acceptval);
MHD_add_response_header (response, MHD_HTTP_HEADER_CONNECTION, MHD_HTTP_HEADER_UPGRADE);
MHD_add_response_header (response, MHD_HTTP_HEADER_UPGRADE, websocket_s);