httpd: More minor cleanup.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled

This commit is contained in:
2025-07-16 20:44:42 -04:00
parent 4c3299ead0
commit 05e54e1be0
3 changed files with 52 additions and 65 deletions

View File

@@ -210,3 +210,43 @@ void tf_httpd_endpoint_app(tf_http_request_t* request)
*data = (app_blob_t) { .request = request };
tf_ssb_run_work(ssb, _httpd_endpoint_app_blob_work, _httpd_endpoint_app_blob_after_work, data);
}
void tf_httpd_endpoint_app_socket(tf_http_request_t* request)
{
tf_task_t* task = request->user_data;
tf_ssb_t* ssb = tf_task_get_ssb(task);
JSContext* context = tf_ssb_get_context(ssb);
JSValue global = JS_GetGlobalObject(context);
JSValue exports = JS_GetPropertyStr(context, global, "exports");
JSValue app_socket = JS_GetPropertyStr(context, exports, "app_socket");
JSValue request_object = JS_NewObject(context);
JSValue headers = JS_NewObject(context);
for (int i = 0; i < request->headers_count; i++)
{
JS_SetPropertyStr(context, headers, request->headers[i].name, JS_NewString(context, request->headers[i].value));
}
JS_SetPropertyStr(context, request_object, "headers", headers);
JSValue response = tf_httpd_make_response_object(context, request);
tf_http_request_ref(request);
JSValue args[] = {
request_object,
response,
};
JSValue result = JS_Call(context, app_socket, JS_NULL, tf_countof(args), args);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
for (int i = 0; i < tf_countof(args); i++)
{
JS_FreeValue(context, args[i]);
}
JS_FreeValue(context, app_socket);
JS_FreeValue(context, exports);
JS_FreeValue(context, global);
}

View File

@@ -5,31 +5,17 @@
#include "log.h"
#include "mem.h"
#include "ssb.db.h"
#include "ssb.ebt.h"
#include "ssb.h"
#include "task.h"
#include "tls.h"
#include "tlscontext.js.h"
#include "trace.h"
#include "util.js.h"
#include "version.h"
#include "ow-crypt.h"
#include "picohttpparser.h"
#include "sodium/crypto_sign.h"
#include "sodium/utils.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <openssl/sha.h>
#if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(_WIN32)
#include <alloca.h>
#endif
#define CYAN "\e[1;36m"
#define MAGENTA "\e[1;35m"
#define YELLOW "\e[1;33m"
@@ -42,12 +28,6 @@ typedef struct _http_user_data_t
char redirect[1024];
} http_user_data_t;
typedef struct _http_handler_data_t
{
JSContext* context;
JSValue callback;
} http_handler_data_t;
static JSValue _httpd_response_write_head(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JS_SetPropertyStr(context, this_val, "response_status", JS_DupValue(context, argv[0]));
@@ -672,9 +652,7 @@ static void _httpd_endpoint_robots_txt(tf_http_request_t* request)
{
return;
}
char* response = "User-Agent: *\n"
"Disallow: /*/*/edit\n"
"Allow: /\n";
char* response = "User-Agent: *\nDisallow: /*/*/edit\nAllow: /\n";
const char* headers[] = { "Content-Type", "text/plain; charset=utf-8" };
tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0);
}
@@ -849,7 +827,10 @@ JSValue tf_httpd_authenticate_jwt(tf_ssb_t* ssb, JSContext* context, const char*
JSValue exp = JS_GetPropertyStr(context, parsed, "exp");
int64_t exp_value = 0;
JS_ToInt64(context, &exp_value, exp);
if (time(NULL) >= exp_value)
uv_timespec64_t now = { 0 };
uv_clock_gettime(UV_CLOCK_REALTIME, &now);
if (now.tv_sec * 1000 + now.tv_nsec / 1000000LL >= exp_value)
{
JS_FreeValue(context, parsed);
return JS_UNDEFINED;
@@ -875,46 +856,6 @@ bool tf_httpd_is_name_valid(const char* name)
return true;
}
static void _httpd_endpoint_app_socket(tf_http_request_t* request)
{
tf_task_t* task = request->user_data;
tf_ssb_t* ssb = tf_task_get_ssb(task);
JSContext* context = tf_ssb_get_context(ssb);
JSValue global = JS_GetGlobalObject(context);
JSValue exports = JS_GetPropertyStr(context, global, "exports");
JSValue app_socket = JS_GetPropertyStr(context, exports, "app_socket");
JSValue request_object = JS_NewObject(context);
JSValue headers = JS_NewObject(context);
for (int i = 0; i < request->headers_count; i++)
{
JS_SetPropertyStr(context, headers, request->headers[i].name, JS_NewString(context, request->headers[i].value));
}
JS_SetPropertyStr(context, request_object, "headers", headers);
JSValue response = tf_httpd_make_response_object(context, request);
tf_http_request_ref(request);
JSValue args[] = {
request_object,
response,
};
JSValue result = JS_Call(context, app_socket, JS_NULL, tf_countof(args), args);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
for (int i = 0; i < tf_countof(args); i++)
{
JS_FreeValue(context, args[i]);
}
JS_FreeValue(context, app_socket);
JS_FreeValue(context, exports);
JS_FreeValue(context, global);
}
static void _httpd_free_user_data(void* user_data)
{
tf_free(user_data);
@@ -1026,7 +967,7 @@ tf_http_t* tf_httpd_create(JSContext* context)
tf_http_add_handler(http, "/login/auto", tf_httpd_endpoint_login_auto, NULL, task);
tf_http_add_handler(http, "/login", tf_httpd_endpoint_login, NULL, task);
tf_http_add_handler(http, "/app/socket", _httpd_endpoint_app_socket, NULL, task);
tf_http_add_handler(http, "/app/socket", tf_httpd_endpoint_app_socket, NULL, task);
if (http_port > 0 || *out_http_port_file)
{

View File

@@ -193,6 +193,12 @@ void tf_httpd_endpoint_app(tf_http_request_t* request);
*/
void tf_httpd_endpoint_app_index(tf_http_request_t* request);
/**
** App WebSocket.
** @param request The HTTP request.
*/
void tf_httpd_endpoint_app_socket(tf_http_request_t* request);
/**
** Login endpoint.
** @param request The HTTP request.