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
This commit is contained in:
parent
1621f1753a
commit
606e82d718
14
src/http.c
14
src/http.c
@ -446,7 +446,9 @@ 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)
|
||||
{
|
||||
for (int i = 0; i < headers_count * 2; i += 2)
|
||||
{
|
||||
/* Key: Value\r\n */
|
||||
headers_length += strlen(headers[i]) + 2 + strlen(headers[i + 1]) + 2;
|
||||
@ -455,6 +457,7 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header
|
||||
sent_content_length = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* \r\n */
|
||||
headers_length += 2;
|
||||
|
||||
@ -470,10 +473,13 @@ 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
memcpy(buffer + offset, content_length_buffer, content_length_buffer_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)
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user