From ff5a07dc04561456293dc3e7ce8aeadd6382d819 Mon Sep 17 00:00:00 2001 From: Alec Murphy Date: Fri, 21 Feb 2025 18:03:12 -0500 Subject: [PATCH] Everywhere: Make session->content_type() callable --- Slon/Api/V2/Instance.HC | 2 +- Slon/Endpoints/Get/Web.HC | 8 ++++---- Slon/Http/AdminServer.HC | 4 ++-- Slon/Http/LocalServer.HC | 22 +++++++++++----------- Slon/Http/Server.HC | 10 ++++++++++ Slon/Modules/Http.HC | 15 +++++++++++---- Slon/Modules/Meta.HC | 2 +- Slon/Modules/NodeInfo.HC | 2 +- Slon/Modules/OAuth.HC | 2 +- Slon/Modules/Web.HC | 2 +- 10 files changed, 43 insertions(+), 26 deletions(-) diff --git a/Slon/Api/V2/Instance.HC b/Slon/Api/V2/Instance.HC index 6d5f118..6a1b119 100644 --- a/Slon/Api/V2/Instance.HC +++ b/Slon/Api/V2/Instance.HC @@ -4,6 +4,6 @@ U0 @slon_api_v2_instance_get(SlonHttpSession* session) no_warn request_json; StrPrint(scratch_buffer, "{\"domain\":\"%s\"}", db->o("instance")->@("uri")); - @slon_http_set_content_type(session, "application/json; charset=utf-8"); + session->content_type("application/json; charset=utf-8"); session->send(scratch_buffer); } diff --git a/Slon/Endpoints/Get/Web.HC b/Slon/Endpoints/Get/Web.HC index d2231a4..3231a0e 100644 --- a/Slon/Endpoints/Get/Web.HC +++ b/Slon/Endpoints/Get/Web.HC @@ -1,23 +1,23 @@ if (String.EndsWith(".css", @slon_http_request_path(session))) { - @slon_http_set_content_type(session, "text/css"); + session->content_type("text/css"); @slon_http_send_file(session, "M:/Slon/Static/css/main.css"); return; } if (!StrICmp("/js/header.js", @slon_http_request_path(session))) { - @slon_http_set_content_type(session, "text/javascript"); + session->content_type("text/javascript"); @slon_http_send_file(session, "M:/Slon/Static/js/header.js"); return; } if (String.EndsWith(".js", @slon_http_request_path(session))) { - @slon_http_set_content_type(session, "text/javascript"); + session->content_type("text/javascript"); @slon_http_send_file(session, "M:/Slon/Static/js/statuses.js"); return; } if (!StrICmp("/alec.png", @slon_http_request_path(session))) { - @slon_http_set_content_type(session, "image/png"); + session->content_type("image/png"); @slon_http_send_file(session, "A:/avatar-circle-4bpp.png"); return; } diff --git a/Slon/Http/AdminServer.HC b/Slon/Http/AdminServer.HC index 94a5552..1c1ab94 100644 --- a/Slon/Http/AdminServer.HC +++ b/Slon/Http/AdminServer.HC @@ -254,7 +254,7 @@ U0 @slon_admin_new_account(SlonHttpSession* session) if (db->o("actors")->@(request_json->@("username"))) { StrPrint(scratch_buffer, "{\"error\":\"account already exists\"}"); - @slon_http_set_content_type(session, "application/json"); + session->content_type("application/json"); session->send(scratch_buffer, StrLen(scratch_buffer)); } else { @slon_admin_create_account(session); @@ -276,7 +276,7 @@ U0 @slon_admin_info_stats(SlonHttpSession* session) String.Append(scratch_buffer, "\"uptime\":\"%d\"", cnts.jiffies); String.Append(scratch_buffer, "}"); - @slon_http_set_content_type(session, "application/json"); + session->content_type("application/json"); session->send(scratch_buffer, StrLen(scratch_buffer)); } diff --git a/Slon/Http/LocalServer.HC b/Slon/Http/LocalServer.HC index e8c94d1..e0527bf 100644 --- a/Slon/Http/LocalServer.HC +++ b/Slon/Http/LocalServer.HC @@ -2,38 +2,38 @@ U0 @slon_local_server_set_mime_type(SlonHttpSession* session, U8* filepath) { // FIXME: Do this programmatically like the Jakt version, this is awful if (String.EndsWith(".html", filepath)) { - @slon_http_set_content_type(session, "text/html"); + session->content_type("text/html"); return; } if (String.EndsWith(".txt", filepath)) { - @slon_http_set_content_type(session, "text/plain"); + session->content_type("text/plain"); return; } if (String.EndsWith(".css", filepath)) { - @slon_http_set_content_type(session, "text/css"); + session->content_type("text/css"); return; } if (String.EndsWith(".js", filepath)) { - @slon_http_set_content_type(session, "text/javascript"); + session->content_type("text/javascript"); return; } if (String.EndsWith(".json", filepath)) { - @slon_http_set_content_type(session, "application/json"); + session->content_type("application/json"); return; } if (String.EndsWith(".gif", filepath)) { - @slon_http_set_content_type(session, "image/gif"); + session->content_type("image/gif"); return; } if (String.EndsWith(".png", filepath)) { - @slon_http_set_content_type(session, "image/png"); + session->content_type("image/png"); return; } if (String.EndsWith(".jpeg", filepath) || String.EndsWith(".jpg", filepath)) { - @slon_http_set_content_type(session, "image/jpeg"); + session->content_type("image/jpeg"); return; } - @slon_http_set_content_type(session, "application/octet-stream"); + session->content_type("application/octet-stream"); } U0 @slon_local_server_send_file(SlonHttpSession* session, U8* filepath) @@ -106,7 +106,7 @@ U0 @slon_local_server_directory_listing(SlonHttpSession* session, U8* path) String.Append(html, "
Slon static file webserver for (TempleOS) Server
"); String.Append(html, ""); - @slon_http_set_content_type(session, "text/html"); + session->content_type("text/html"); session->send(html, StrLen(html)); @slon_free(session, html); } @@ -114,7 +114,7 @@ U0 @slon_local_server_directory_listing(SlonHttpSession* session, U8* path) U0 @slon_local_server_not_found(SlonHttpSession* session) { session->status(404); - @slon_http_set_content_type(session, "text/html"); + session->content_type("text/html"); session->send("

404 Not Found

", 22); } diff --git a/Slon/Http/Server.HC b/Slon/Http/Server.HC index 6e454df..b4699b5 100644 --- a/Slon/Http/Server.HC +++ b/Slon/Http/Server.HC @@ -47,6 +47,7 @@ U0 @slon_http_free_session(SlonHttpSession* session) @slon_http_free_response(session, session->response); @slon_http_free_request(session, session->request); I64 bytes_used = session->bytes_used - MSize2(session); + Free(session->content_type); Free(session->verb); Free(session->header); Free(session->status); @@ -166,6 +167,15 @@ SlonHttpSession* @slon_http_init_session(TcpSocket* s) a += 0x0b; MemSetI64(a, session, 1); + // Create a copy of function and patch content_type + code_size = MSize(&@slon_session_content_type_wrapper_function); + session->content_type = CAlloc(code_size, adam_task->code_heap); + MemCpy(session->content_type, &@slon_session_content_type_wrapper_function, code_size); + + a = session->content_type; + a += 0x10; + MemSetI64(a, session, 1); + return session; } diff --git a/Slon/Modules/Http.HC b/Slon/Modules/Http.HC index 74ca805..9fb9197 100644 --- a/Slon/Modules/Http.HC +++ b/Slon/Modules/Http.HC @@ -73,6 +73,7 @@ class SlonHttpSession { I64 (*path_count)(); I64 (*status)(I64 code = NULL); I64 (*verb)(Bool return_str = FALSE); + U0 (*content_type)(U8* value); }; U64 @slon_calloc(SlonHttpSession* session, I64 size) @@ -173,7 +174,7 @@ U0 @slon_http_send_ap_json(SlonHttpSession* session, U64 json) { // a stringified copy of "json" is created, a strnew is sent, we clean up stringified copy, sender cleans up "json" session->status(200); - @slon_http_set_content_type(session, "application/activity+json; charset=utf-8"); + session->content_type("application/activity+json; charset=utf-8"); U8* json_string = Json.Stringify(json); session->response->data = @slon_strnew(session, json_string); session->response->size = StrLen(session->response->data); @@ -184,7 +185,7 @@ U0 @slon_http_send_json(SlonHttpSession* session, U64 json) { // a stringified copy of "json" is created, a strnew is sent, we clean up stringified copy, sender cleans up "json" session->status(200); - @slon_http_set_content_type(session, "application/json; charset=utf-8"); + session->content_type("application/json; charset=utf-8"); U8* json_string = Json.Stringify(json); session->response->data = @slon_strnew(session, json_string); session->response->size = StrLen(session->response->data); @@ -223,13 +224,13 @@ U0 @slon_http_send_file(SlonHttpSession* session, U8* path) U0 @slon_http_send_html_file(SlonHttpSession* session, U8* path) { - @slon_http_set_content_type(session, "text/html"); + session->content_type("text/html"); @slon_http_send_file(session, path); } U0 @slon_http_send_json_file(SlonHttpSession* session, U8* path, U8* content_type = "application/json; charset=utf-8") { - @slon_http_set_content_type(session, content_type); + session->content_type(content_type); @slon_http_send_file(session, path); } @@ -345,3 +346,9 @@ I64 @slon_session_path_count_wrapper_function() SlonHttpSession* session = SLON_WRAPPER_MAGIC_NUMBER; return session->request->path_segments_count; } + +U0 @slon_session_content_type_wrapper_function(U8* value) +{ + SlonHttpSession* session = SLON_WRAPPER_MAGIC_NUMBER; + session->header("content-type", value); +} diff --git a/Slon/Modules/Meta.HC b/Slon/Modules/Meta.HC index d84adf6..51403cb 100644 --- a/Slon/Modules/Meta.HC +++ b/Slon/Modules/Meta.HC @@ -4,6 +4,6 @@ U0 @slon_host_meta(SlonHttpSession* session) no_warn request_json; StrPrint(scratch_buffer, "", db->o("instance")->@("uri")); - @slon_http_set_content_type(session, "application/xrd+xml; charset=utf-8"); + session->content_type("application/xrd+xml; charset=utf-8"); session->send(scratch_buffer); } diff --git a/Slon/Modules/NodeInfo.HC b/Slon/Modules/NodeInfo.HC index 524c04e..5da0599 100644 --- a/Slon/Modules/NodeInfo.HC +++ b/Slon/Modules/NodeInfo.HC @@ -4,7 +4,7 @@ U0 @slon_nodeinfo(SlonHttpSession* session) no_warn request_json; StrPrint(scratch_buffer, "{\"links\":[{\"rel\":\"http://nodeinfo.diaspora.software/ns/schema/2.0\",\"href\":\"https://%s/nodeinfo/2.0\"}]}", db->o("instance")->@("uri")); - @slon_http_set_content_type(session, "application/json; charset=utf-8"); + session->content_type("application/json; charset=utf-8"); session->send(scratch_buffer); } diff --git a/Slon/Modules/OAuth.HC b/Slon/Modules/OAuth.HC index 06077f6..2fdfe86 100644 --- a/Slon/Modules/OAuth.HC +++ b/Slon/Modules/OAuth.HC @@ -7,7 +7,7 @@ U0 @slon_oauth_well_known(SlonHttpSession* session) StrPrint(scratch_buffer, "{\"issuer\":\"https://%s\",\"authorization_endpoint\":\"https://%s/oauth/authorize\",\"response_types_supported\":[\"code\"],\"app_registration_endpoint\":\"https://%s/api/v1/apps\"}", db->o("instance")->@("uri"), db->o("instance")->@("uri"), db->o("instance")->@("uri")); - @slon_http_set_content_type(session, "application/json; charset=utf-8"); + session->content_type("application/json; charset=utf-8"); session->send(scratch_buffer); } diff --git a/Slon/Modules/Web.HC b/Slon/Modules/Web.HC index 0baea33..dba79b2 100644 --- a/Slon/Modules/Web.HC +++ b/Slon/Modules/Web.HC @@ -25,7 +25,7 @@ U0 @slon_web_user_get(SlonHttpSession* session) String.Append(html_data, ""); - @slon_http_set_content_type(session, "text/html"); + session->content_type("text/html"); session->send(html_data, StrLen(html_data)); Free(html_file_data);