This is exchanging some websocket messages, now.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4698 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-12-25 23:50:55 +00:00
parent cd43bf9dfa
commit 8ab8335baa
2 changed files with 9 additions and 16 deletions

View File

@ -75,7 +75,7 @@ typedef struct _tf_http_t
} tf_http_t;
static const char* _http_connection_get_header(const tf_http_connection_t* connection, const char* name);
static void _http_connection_destroy(tf_http_connection_t* connection);
static void _http_connection_destroy(tf_http_connection_t* connection, const char* reason);
static const char* _http_status_text(int status);
tf_http_t* tf_http_create(uv_loop_t* loop)
@ -127,14 +127,14 @@ static void _http_connection_on_close(uv_handle_t* handle)
{
tf_http_connection_t* connection = handle->data;
handle->data = NULL;
_http_connection_destroy(connection);
_http_connection_destroy(connection, "handle closed");
}
static void _http_connection_destroy(tf_http_connection_t* connection)
static void _http_connection_destroy(tf_http_connection_t* connection, const char* reason)
{
if (connection->tcp.data)
{
tf_printf("CLOSE %p\n", connection);
tf_printf("CLOSE %p: %s\n", connection, reason);
uv_close((uv_handle_t*)&connection->tcp, _http_connection_on_close);
}
else if (connection->ref_count == 0)
@ -187,14 +187,13 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d
if ((bits1 & (1 << 7)) == 0)
{
/* Unmasked message. */
_http_connection_destroy(connection);
_http_connection_destroy(connection, "websocket server received unmasked bytes");
return;
}
uint8_t opcode = bits0 & 0xf;
bool fin = (bits0 & (1 << 7)) != 0;
size_t length = bits1 & 0x7f;
int mask_start = 2;
if (length == 126)
{
length = 0;
@ -215,7 +214,7 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d
}
mask_start = 10;
}
if (connection->body_length >= length + 2 + 4)
if (connection->body_length >= mask_start + length + 4)
{
uint32_t mask =
p[mask_start + 0] |
@ -373,18 +372,19 @@ static void _http_on_read(uv_stream_t* stream, ssize_t read_size, const uv_buf_t
else
{
tf_printf("phr_parse_request: %d\n", parse_result);
_http_connection_destroy(connection);
_http_connection_destroy(connection, "failed to parse request headers");
}
}
else
{
connection->buffer_length += read_size;
_http_add_body_bytes(connection, connection->buffer, connection->buffer_length);
connection->buffer_length = 0;
}
}
else
{
_http_connection_destroy(connection);
_http_connection_destroy(connection, uv_strerror(read_size));
}
}
@ -533,7 +533,6 @@ static void _http_on_shutdown(uv_shutdown_t* request, int status)
static void _http_write(tf_http_connection_t* connection, const void* data, size_t size)
{
tf_printf("WRITE connection=%p\n", connection);
uv_write_t* write = tf_malloc(sizeof(uv_write_t) + size);
*write = (uv_write_t) { .data = connection };
memcpy(write + 1, data, size);
@ -600,10 +599,6 @@ 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)
{

View File

@ -141,7 +141,6 @@ static JSValue _httpd_response_send(JSContext* context, JSValueConst this_val, i
header += 9;
}
memcpy(copy + header, message, length);
tf_printf("SEND %d\n", (int)length);
tf_http_request_send(request, copy, header + length);
tf_free(copy);
JS_FreeCString(context, message);
@ -150,7 +149,6 @@ static JSValue _httpd_response_send(JSContext* context, JSValueConst this_val, i
static void _httpd_message_callback(tf_http_request_t* request, const void* data, size_t size)
{
tf_printf("message [%.*s]\n", (int)size, (const char*)data);
JSContext* context = request->context;
JSValue response_object = JS_MKPTR(JS_TAG_OBJECT, request->user_data);
JSValue on_message = JS_GetPropertyStr(context, response_object, "onMessage");