From 606e82d718d55c520a3713500ea36b90549fb6fa Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 24 Dec 2023 21:39:51 +0000 Subject: [PATCH] Saw a websocket message go across the wire with this. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4693 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/http.c | 24 +++++++++++++++++------- src/httpd.js.c | 9 +++++---- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/http.c b/src/http.c index 1f86c3ec..9f84f0f9 100644 --- a/src/http.c +++ b/src/http.c @@ -446,13 +446,16 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header /* HTTP/1.x 200 OK\r\n */ bool sent_content_length = false; int headers_length = 8 + 1 + 3 + 1 + strlen(status_text) + 2; - for (int i = 0; i < headers_count; i += 2) + if (headers) { - /* Key: Value\r\n */ - headers_length += strlen(headers[i]) + 2 + strlen(headers[i + 1]) + 2; - if (strcasecmp(headers[i], "content-length") == 0) + for (int i = 0; i < headers_count * 2; i += 2) { - sent_content_length = true; + /* Key: Value\r\n */ + headers_length += strlen(headers[i]) + 2 + strlen(headers[i + 1]) + 2; + if (strcasecmp(headers[i], "content-length") == 0) + { + sent_content_length = true; + } } } /* \r\n */ @@ -470,9 +473,12 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header *write = (uv_write_t) { .data = request->connection }; char* buffer = (char*)(write + 1); int offset = snprintf(buffer, headers_length + 1, "HTTP/1.%d %03d %s\r\n", request->connection->minor_version, status, status_text); - for (int i = 0; i < headers_count; i += 2) + if (headers) { - offset += snprintf(buffer + offset, headers_length + 1 - offset, "%s: %s\r\n", headers[i], headers[i + 1]); + for (int i = 0; i < headers_count * 2; i += 2) + { + offset += snprintf(buffer + offset, headers_length + 1 - offset, "%s: %s\r\n", headers[i], headers[i + 1]); + } } if (!sent_content_length) { @@ -485,6 +491,10 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header { memcpy(buffer + offset, body, content_length); } + if (status == 101) + { + tf_printf("WRITE [%.*s]\n", (int)(headers_length + content_length), buffer); + } int r = uv_write(write, (uv_stream_t*)&request->connection->tcp, &(uv_buf_t) { .base = buffer, .len = headers_length + content_length }, 1, _http_on_write); if (r) { diff --git a/src/httpd.js.c b/src/httpd.js.c index aee9f275..8d32a1ce 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -124,17 +124,18 @@ static void _httpd_callback(tf_http_request_t* request) SHA1(key_magic, size, digest); char key[41] = { 0 }; tf_base64_encode(digest, sizeof(digest), key, sizeof(key)); - tf_printf("ACCEPT %s\n", key); - enum { k_headers_count = 4 }; const char* headers[k_headers_count * 2] = { "Upgrade", "websocket", - "Connection", "upgrade", + "Connection", "Upgrade", "Sec-WebSocket-Accept", key, "Sec-WebSocket-Version", "13", }; - tf_http_respond(request, 101, headers, k_headers_count, NULL, 0); + bool send_version = + !tf_http_request_get_header(request, "sec-websocket-version") || + strcmp(tf_http_request_get_header(request, "sec-websocket-version"), "13") != 0; + tf_http_respond(request, 101, headers, send_version ? k_headers_count : (k_headers_count - 1), NULL, 0); return; } }