Everywhere: Make session->status() callable

This commit is contained in:
Alec Murphy 2025-02-19 21:07:09 -05:00
parent ca8e7ae7f6
commit 6813c13ab3
23 changed files with 100 additions and 74 deletions

View file

@ -95,7 +95,7 @@ U0 @slon_api_v1_accounts_follow_request(U8* this_actor, U8* remote_actor)
U0 @slon_api_v1_accounts_post(SlonHttpSession* session) U0 @slon_api_v1_accounts_post(SlonHttpSession* session)
{ {
if (!@slon_api_authorized(session)) { if (!@slon_api_authorized(session)) {
@slon_http_set_status_code(session, 401); session->status(401);
return; return;
} }
@ -115,14 +115,14 @@ U0 @slon_api_v1_accounts_post(SlonHttpSession* session)
U8* some_account_id = path_segments[3]; U8* some_account_id = path_segments[3];
JsonObject* acct = @slon_api_account_by_id(some_account_id); JsonObject* acct = @slon_api_account_by_id(some_account_id);
if (!acct) { if (!acct) {
@slon_http_set_status_code(session, 404); session->status(404);
goto slon_api_v1_accounts_post_return; goto slon_api_v1_accounts_post_return;
} }
if (path_segments_count > 5) { if (path_segments_count > 5) {
U8* method = path_segments[4]; U8* method = path_segments[4];
if (!StrICmp("follow", method)) { if (!StrICmp("follow", method)) {
if (!acct->@("remote_actor")) { if (!acct->@("remote_actor")) {
@slon_http_set_status_code(session, 404); session->status(404);
goto slon_api_v1_accounts_post_return; goto slon_api_v1_accounts_post_return;
} }
// add to my following // add to my following
@ -160,9 +160,9 @@ U0 @slon_api_v1_accounts_post(SlonHttpSession* session)
@slon_http_send_json(session, relationship); @slon_http_send_json(session, relationship);
goto slon_api_v1_accounts_post_return; goto slon_api_v1_accounts_post_return;
} }
@slon_http_set_status_code(session, 404); session->status(404);
} else { } else {
@slon_http_set_status_code(session, 404); session->status(404);
} }
} }
slon_api_v1_accounts_post_return: slon_api_v1_accounts_post_return:
@ -233,7 +233,7 @@ U0 @slon_api_v1_accounts_get(SlonHttpSession* session)
Json.Delete(relationships); Json.Delete(relationships);
goto slon_api_v1_accounts_get_return; goto slon_api_v1_accounts_get_return;
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} else if (!StrICmp("verify_credentials", path_segments[3])) { } else if (!StrICmp("verify_credentials", path_segments[3])) {
if (@slon_api_authorized(session)) { if (@slon_api_authorized(session)) {
@ -242,17 +242,17 @@ U0 @slon_api_v1_accounts_get(SlonHttpSession* session)
if (acct) { if (acct) {
@slon_http_send_json(session, acct); @slon_http_send_json(session, acct);
} else { } else {
@slon_http_set_status_code(session, 404); session->status(404);
} }
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} else { } else {
// Work with account :id // Work with account :id
U8* some_account_id = path_segments[3]; U8* some_account_id = path_segments[3];
acct = @slon_api_account_by_id(some_account_id); acct = @slon_api_account_by_id(some_account_id);
if (!acct) { if (!acct) {
@slon_http_set_status_code(session, 404); session->status(404);
goto slon_api_v1_accounts_get_return; goto slon_api_v1_accounts_get_return;
} }
if (path_segments_count > 5) { if (path_segments_count > 5) {
@ -334,7 +334,7 @@ U0 @slon_api_v1_accounts_get(SlonHttpSession* session)
Json.Delete(statuses); Json.Delete(statuses);
goto slon_api_v1_accounts_get_return; goto slon_api_v1_accounts_get_return;
} }
@slon_http_set_status_code(session, 404); session->status(404);
} else { } else {
// Return the Account profile // Return the Account profile
JsonObject* profile_object = Json.Clone(acct); JsonObject* profile_object = Json.Clone(acct);
@ -367,14 +367,14 @@ U0 @slon_api_v1_accounts_patch(SlonHttpSession* session)
SLON_AUTH_ACCOUNT_ID SLON_AUTH_ACCOUNT_ID
if (!request_json || !request_json->keys) { if (!request_json || !request_json->keys) {
@slon_http_set_status_code(session, 400); session->status(400);
goto slon_api_v1_accounts_patch_return; goto slon_api_v1_accounts_patch_return;
} }
// FIXME: Support avatars/banners // FIXME: Support avatars/banners
acct = @slon_api_account_by_id(account_id); acct = @slon_api_account_by_id(account_id);
if (!acct) { if (!acct) {
@slon_http_set_status_code(session, 404); session->status(404);
goto slon_api_v1_accounts_patch_return; goto slon_api_v1_accounts_patch_return;
} }
JsonObject* source = acct->@("source"); JsonObject* source = acct->@("source");
@ -489,10 +489,10 @@ U0 @slon_api_v1_accounts_patch(SlonHttpSession* session)
@slon_db_actors_update_user(acct); @slon_db_actors_update_user(acct);
@slon_http_send_json(session, acct); @slon_http_send_json(session, acct);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} else { } else {
@slon_http_set_status_code(session, 404); session->status(404);
} }
slon_api_v1_accounts_patch_return: slon_api_v1_accounts_patch_return:
@slon_free(session, path); @slon_free(session, path);

View file

@ -7,6 +7,6 @@ U0 @slon_api_v1_blocks_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -7,6 +7,6 @@ U0 @slon_api_v1_bookmarks_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -7,6 +7,6 @@ U0 @slon_api_v1_conversations_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -7,6 +7,6 @@ U0 @slon_api_v1_custom_emojis_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -7,6 +7,6 @@ U0 @slon_api_v1_favourites_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -7,6 +7,6 @@ U0 @slon_api_v1_filters_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -7,6 +7,6 @@ U0 @slon_api_v1_follow_requests_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -7,6 +7,6 @@ U0 @slon_api_v1_followed_tags_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -11,6 +11,6 @@ U0 @slon_api_v1_notifications_get(SlonHttpSession* session)
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} }
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -45,7 +45,7 @@ U0 @slon_api_v1_statuses_delete(SlonHttpSession* session)
@slon_free(session, path); @slon_free(session, path);
@slon_http_send_json(session, SLON_EMPTY_JSON_OBJECT); @slon_http_send_json(session, SLON_EMPTY_JSON_OBJECT);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }
@ -141,6 +141,6 @@ U0 @slon_api_v1_statuses_post(SlonHttpSession* session)
@slon_free(session, id); @slon_free(session, id);
@slon_free(session, created_at); @slon_free(session, created_at);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -122,9 +122,9 @@ U0 @slon_api_v1_timelines_get(SlonHttpSession* session)
goto slon_api_v1_timelines_get_return; goto slon_api_v1_timelines_get_return;
} }
@slon_http_set_status_code(session, 404); session->status(404);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
return; return;
} }
slon_api_v1_timelines_get_return: slon_api_v1_timelines_get_return:

View file

@ -7,6 +7,6 @@ U0 @slon_api_v2_filters_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -203,6 +203,6 @@ U0 @slon_api_v2_search_get(SlonHttpSession* session)
@slon_http_send_json(session, results); @slon_http_send_json(session, results);
Json.Delete(results); Json.Delete(results);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -7,6 +7,6 @@ U0 @slon_api_v2_suggestions_get(SlonHttpSession* session)
// FIXME: Implement this // FIXME: Implement this
@slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY); @slon_http_send_json(session, SLON_EMPTY_JSON_ARRAY);
} else { } else {
@slon_http_set_status_code(session, 401); session->status(401);
} }
} }

View file

@ -284,7 +284,7 @@ U0 @slon_admin_server_get(SlonHttpSession* session)
{ {
if (!db->@("setup")) { if (!db->@("setup")) {
if (StrICmp("/", @slon_http_request_path(session))) { if (StrICmp("/", @slon_http_request_path(session))) {
@slon_http_set_status_code(session, 302); session->status(302);
@slon_http_set_header(session, "Location", "/"); @slon_http_set_header(session, "Location", "/");
} else { } else {
@slon_http_send_html_file(session, "M:/Slon/Static/html/admin/setup_instance.html"); @slon_http_send_html_file(session, "M:/Slon/Static/html/admin/setup_instance.html");
@ -312,7 +312,7 @@ U0 @slon_admin_server_get(SlonHttpSession* session)
return; return;
} }
@slon_http_set_status_code(session, 404); session->status(404);
} }
U0 @slon_admin_setup_instance(SlonHttpSession* session) U0 @slon_admin_setup_instance(SlonHttpSession* session)
@ -349,7 +349,7 @@ U0 @slon_admin_server_post(SlonHttpSession* session)
return; return;
} }
@slon_http_set_status_code(session, 404); session->status(404);
} }
U0 @slon_admin_http_handle_get_request(SlonHttpSession* session) U0 @slon_admin_http_handle_get_request(SlonHttpSession* session)
@ -375,7 +375,7 @@ U0 @slon_admin_http_handle_request(SlonHttpSession* session)
@slon_admin_http_handle_post_request(session); @slon_admin_http_handle_post_request(session);
break; break;
default: default:
@slon_http_set_status_code(session, 405); session->status(405);
} }
} }
@ -394,7 +394,7 @@ U0 @slon_admin_http_task(TcpSocket* s)
// Handle malformed requests (anything less than "GET / HTTP/1.0\r\n\r\n" is probably a bad request) // Handle malformed requests (anything less than "GET / HTTP/1.0\r\n\r\n" is probably a bad request)
if (session->request->buffer->size < 18) { if (session->request->buffer->size < 18) {
@slon_http_set_status_code(session, 400); session->status(400);
goto slon_admin_http_task_send_response; goto slon_admin_http_task_send_response;
} }

View file

@ -113,7 +113,7 @@ U0 @slon_local_server_directory_listing(SlonHttpSession* session, U8* path)
U0 @slon_local_server_not_found(SlonHttpSession* session) U0 @slon_local_server_not_found(SlonHttpSession* session)
{ {
@slon_http_set_status_code(session, 404); session->status(404);
@slon_http_set_content_type(session, "text/html"); @slon_http_set_content_type(session, "text/html");
@slon_http_send(session, "<h2>404 Not Found</h2>", 22); @slon_http_send(session, "<h2>404 Not Found</h2>", 22);
} }
@ -162,7 +162,7 @@ U0 @slon_local_server_get(SlonHttpSession* session)
return; return;
} else { } else {
if (IsDir(scratch_buffer)) { if (IsDir(scratch_buffer)) {
@slon_http_set_status_code(session, 301); session->status(301);
StrPrint(scratch_buffer, "%s/", path); StrPrint(scratch_buffer, "%s/", path);
@slon_http_set_header(session, "Location", scratch_buffer); @slon_http_set_header(session, "Location", scratch_buffer);
} else { } else {
@ -172,7 +172,7 @@ U0 @slon_local_server_get(SlonHttpSession* session)
} }
// shouldn't get here :^) // shouldn't get here :^)
@slon_http_set_status_code(session, 400); session->status(400);
} }
U0 @slon_local_http_handle_get_request(SlonHttpSession* session) U0 @slon_local_http_handle_get_request(SlonHttpSession* session)
@ -187,7 +187,7 @@ U0 @slon_local_http_handle_request(SlonHttpSession* session)
@slon_local_http_handle_get_request(session); @slon_local_http_handle_get_request(session);
break; break;
default: default:
@slon_http_set_status_code(session, 405); session->status(405);
} }
} }
@ -206,7 +206,7 @@ U0 @slon_local_http_task(TcpSocket* s)
// Handle malformed requests (anything less than "GET / HTTP/1.0\r\n\r\n" is probably a bad request) // Handle malformed requests (anything less than "GET / HTTP/1.0\r\n\r\n" is probably a bad request)
if (session->request->buffer->size < 18) { if (session->request->buffer->size < 18) {
@slon_http_set_status_code(session, 400); session->status(400);
goto slon_local_http_task_send_response; goto slon_local_http_task_send_response;
} }

View file

@ -43,6 +43,7 @@ U0 @slon_http_free_session(SlonHttpSession* session)
@slon_http_free_response(session, session->response); @slon_http_free_response(session, session->response);
@slon_http_free_request(session, session->request); @slon_http_free_request(session, session->request);
I64 bytes_used = session->bytes_used - MSize2(session); I64 bytes_used = session->bytes_used - MSize2(session);
Free(session->status);
Free(session); Free(session);
if (bytes_used) { if (bytes_used) {
AdamLog("*** Session leaked %d bytes of memory ***\n", bytes_used); AdamLog("*** Session leaked %d bytes of memory ***\n", bytes_used);
@ -72,6 +73,17 @@ SlonHttpSession* @slon_http_init_session(TcpSocket* s)
session->s = s; session->s = s;
session->request = @slon_http_init_request(session); session->request = @slon_http_init_request(session);
session->response = @slon_http_init_response(session); session->response = @slon_http_init_response(session);
// Create a copy of function and patch status
U64 a;
I64 code_size = MSize(&@slon_session_status_wrapper_function);
session->status = CAlloc(code_size, adam_task->code_heap);
MemCpy(session->status, &@slon_session_status_wrapper_function, code_size);
a = session->status;
a += 0x10;
MemSetI64(a, session, 1);
return session; return session;
} }
@ -413,7 +425,7 @@ U0 @slon_http_handle_get_request(SlonHttpSession* session)
/* clang-format on */ /* clang-format on */
@slon_http_set_status_code(session, 404); session->status(404);
} }
U0 @slon_http_handle_patch_request(SlonHttpSession* session) U0 @slon_http_handle_patch_request(SlonHttpSession* session)
@ -436,7 +448,7 @@ U0 @slon_http_handle_patch_request(SlonHttpSession* session)
/* clang-format on */ /* clang-format on */
@slon_http_set_status_code(session, 404); session->status(404);
} }
U0 @slon_http_handle_post_request(SlonHttpSession* session) U0 @slon_http_handle_post_request(SlonHttpSession* session)
@ -467,7 +479,7 @@ U0 @slon_http_handle_post_request(SlonHttpSession* session)
/* clang-format on */ /* clang-format on */
@slon_http_set_status_code(session, 404); session->status(404);
} }
U0 @slon_http_handle_request(SlonHttpSession* session) U0 @slon_http_handle_request(SlonHttpSession* session)
@ -483,7 +495,7 @@ U0 @slon_http_handle_request(SlonHttpSession* session)
@slon_http_handle_get_request(session); @slon_http_handle_get_request(session);
break; break;
case SLON_HTTP_VERB_OPTIONS: case SLON_HTTP_VERB_OPTIONS:
@slon_http_set_status_code(session, 200); session->status(200);
break; break;
case SLON_HTTP_VERB_PATCH: case SLON_HTTP_VERB_PATCH:
@slon_http_handle_patch_request(session); @slon_http_handle_patch_request(session);
@ -492,7 +504,7 @@ U0 @slon_http_handle_request(SlonHttpSession* session)
@slon_http_handle_post_request(session); @slon_http_handle_post_request(session);
break; break;
default: default:
@slon_http_set_status_code(session, 405); session->status(405);
} }
} }
@ -511,7 +523,7 @@ U0 @slon_http_task(TcpSocket* s)
// Handle malformed requests (anything less than "GET / HTTP/1.0\r\n\r\n" is probably a bad request) // Handle malformed requests (anything less than "GET / HTTP/1.0\r\n\r\n" is probably a bad request)
if (session->request->buffer->size < 18) { if (session->request->buffer->size < 18) {
@slon_http_set_status_code(session, 400); session->status(400);
goto slon_http_task_send_response; goto slon_http_task_send_response;
} }

View file

@ -258,10 +258,10 @@ U0 @slon_activitypub_users_get(SlonHttpSession* session)
if (actor) { if (actor) {
@slon_http_send_ap_json(session, actor); @slon_http_send_ap_json(session, actor);
} else { } else {
@slon_http_set_status_code(session, 404); session->status(404);
} }
} else { } else {
@slon_http_set_status_code(session, 400); session->status(400);
} }
slon_activitypub_users_get_return: slon_activitypub_users_get_return:
@slon_free(session, path); @slon_free(session, path);
@ -740,7 +740,7 @@ U0 @slon_activitypub_users_inbox(SlonHttpSession* session, U8* user)
if (!StrICmp("follow", request_json->@("type"))) { if (!StrICmp("follow", request_json->@("type"))) {
if (StrICmp(session->actor_for_key_id, request_json->@("actor"))) { if (StrICmp(session->actor_for_key_id, request_json->@("actor"))) {
@slon_http_set_status_code(session, 401); session->status(401);
return; return;
} }
if (!db->o("followers")->@(user)) { if (!db->o("followers")->@(user)) {
@ -830,7 +830,7 @@ U0 @slon_activitypub_users_inbox(SlonHttpSession* session, U8* user)
// otherwise, 401 // otherwise, 401
if (!should_accept) { if (!should_accept) {
@slon_http_set_status_code(session, 401); session->status(401);
return; return;
} }
@ -911,7 +911,7 @@ U0 @slon_activitypub_users_inbox(SlonHttpSession* session, U8* user)
if (!StrICmp("like", request_json->@("type"))) { if (!StrICmp("like", request_json->@("type"))) {
if (StrICmp(session->actor_for_key_id, request_json->@("actor"))) { if (StrICmp(session->actor_for_key_id, request_json->@("actor"))) {
@slon_http_set_status_code(session, 401); session->status(401);
return; return;
} }
U8* status_id = StrFind("/", StrFind("/statuses/", request_json->@("object")) + 1) + 1; U8* status_id = StrFind("/", StrFind("/statuses/", request_json->@("object")) + 1) + 1;
@ -934,7 +934,7 @@ U0 @slon_activitypub_users_inbox(SlonHttpSession* session, U8* user)
Spawn(&@slon_activitypub_async_accept_request, o, "SlonAsyncAcceptTask"); Spawn(&@slon_activitypub_async_accept_request, o, "SlonAsyncAcceptTask");
} }
@slon_http_set_status_code(session, 200); session->status(200);
return; return;
} }
@ -948,40 +948,40 @@ U0 @slon_activitypub_users_post(SlonHttpSession* session)
U8** path_segments = String.Split(path, '/', &path_segments_count); U8** path_segments = String.Split(path, '/', &path_segments_count);
if (path_segments_count < 3) { if (path_segments_count < 3) {
@slon_http_set_status_code(session, 400); session->status(400);
goto slon_activitypub_users_post_return; goto slon_activitypub_users_post_return;
} }
U8* user = path_segments[1]; U8* user = path_segments[1];
JsonObject* actor = db->o("actors")->@(user); JsonObject* actor = db->o("actors")->@(user);
if (!actor) { if (!actor) {
@slon_http_set_status_code(session, 404); session->status(404);
goto slon_activitypub_users_post_return; goto slon_activitypub_users_post_return;
} }
U8* method = path_segments[2]; U8* method = path_segments[2];
if (!StrICmp("inbox", method)) { if (!StrICmp("inbox", method)) {
if (!request_json) { if (!request_json) {
@slon_http_set_status_code(session, 400); session->status(400);
goto slon_activitypub_users_post_return; goto slon_activitypub_users_post_return;
} }
if (!request_json->@("type")) { if (!request_json->@("type")) {
@slon_http_set_status_code(session, 400); session->status(400);
goto slon_activitypub_users_post_return; goto slon_activitypub_users_post_return;
} }
if (!StrICmp("delete", request_json->@("type"))) { if (!StrICmp("delete", request_json->@("type"))) {
@slon_http_set_status_code(session, 400); session->status(400);
goto slon_activitypub_users_post_return; goto slon_activitypub_users_post_return;
} }
if (!@slon_activitypub_http_signature_is_valid(session)) { if (!@slon_activitypub_http_signature_is_valid(session)) {
@slon_http_set_status_code(session, 401); session->status(401);
goto slon_activitypub_users_post_return; goto slon_activitypub_users_post_return;
} }
@slon_activitypub_users_inbox(session, user); @slon_activitypub_users_inbox(session, user);
goto slon_activitypub_users_post_return; goto slon_activitypub_users_post_return;
} }
@slon_http_set_status_code(session, 404); session->status(404);
slon_activitypub_users_post_return: slon_activitypub_users_post_return:
if (session->actor_for_key_id) { if (session->actor_for_key_id) {

View file

@ -61,6 +61,9 @@ class SlonHttpSession {
SlonHttpResponse* response; SlonHttpResponse* response;
I64 bytes_used; I64 bytes_used;
JsonObject* auth; JsonObject* auth;
U8* actor_for_key_id;
I64 (*status)(I64 code = NULL);
}; };
U64 @slon_calloc(SlonHttpSession* session, I64 size) U64 @slon_calloc(SlonHttpSession* session, I64 size)
@ -161,7 +164,7 @@ U0 @slon_http_set_status_code(SlonHttpSession* session, I64 status_code)
U0 @slon_http_send_ap_json(SlonHttpSession* session, U64 json) 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" // a stringified copy of "json" is created, a strnew is sent, we clean up stringified copy, sender cleans up "json"
@slon_http_set_status_code(session, 200); session->status(200);
@slon_http_set_content_type(session, "application/activity+json; charset=utf-8"); @slon_http_set_content_type(session, "application/activity+json; charset=utf-8");
U8* json_string = Json.Stringify(json); U8* json_string = Json.Stringify(json);
session->response->data = @slon_strnew(session, json_string); session->response->data = @slon_strnew(session, json_string);
@ -172,7 +175,7 @@ U0 @slon_http_send_ap_json(SlonHttpSession* session, U64 json)
U0 @slon_http_send_json(SlonHttpSession* session, U64 json) 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" // a stringified copy of "json" is created, a strnew is sent, we clean up stringified copy, sender cleans up "json"
@slon_http_set_status_code(session, 200); session->status(200);
@slon_http_set_content_type(session, "application/json; charset=utf-8"); @slon_http_set_content_type(session, "application/json; charset=utf-8");
U8* json_string = Json.Stringify(json); U8* json_string = Json.Stringify(json);
session->response->data = @slon_strnew(session, json_string); session->response->data = @slon_strnew(session, json_string);
@ -183,7 +186,7 @@ U0 @slon_http_send_json(SlonHttpSession* session, U64 json)
U0 @slon_http_send_string(SlonHttpSession* session, U8* str) U0 @slon_http_send_string(SlonHttpSession* session, U8* str)
{ {
// a strnew of "str" is sent, sender cleans up "str" // a strnew of "str" is sent, sender cleans up "str"
@slon_http_set_status_code(session, 200); session->status(200);
session->response->data = @slon_strnew(session, str); session->response->data = @slon_strnew(session, str);
session->response->size = StrLen(str); session->response->size = StrLen(str);
} }
@ -191,7 +194,7 @@ U0 @slon_http_send_string(SlonHttpSession* session, U8* str)
U0 @slon_http_send(SlonHttpSession* session, U64 data, I64 size) U0 @slon_http_send(SlonHttpSession* session, U64 data, I64 size)
{ {
// a malloc copy of "data" is sent, sender cleans up "data" // a malloc copy of "data" is sent, sender cleans up "data"
@slon_http_set_status_code(session, 200); session->status(200);
U8* data_new = @slon_malloc(session, size); U8* data_new = @slon_malloc(session, size);
MemCpy(data_new, data, size); MemCpy(data_new, data, size);
session->response->data = data_new; session->response->data = data_new;
@ -254,3 +257,14 @@ Bool @slon_http_request_has_query_string(SlonHttpSession* session)
{ {
return StrFind("?", session->request->raw_path) > 0 && !String.EndsWith("?", session->request->raw_path); return StrFind("?", session->request->raw_path) > 0 && !String.EndsWith("?", session->request->raw_path);
} }
#define SLON_WRAPPER_MAGIC_NUMBER 0xC0DECAFEC0DECAFE
I64 @slon_session_status_wrapper_function(I64 code)
{
SlonHttpSession* session = SLON_WRAPPER_MAGIC_NUMBER;
if (code) {
session->response->status_code = code;
}
return session->response->status_code;
}

View file

@ -71,7 +71,7 @@ U0 @slon_oauth_verify_access_get(SlonHttpSession* session)
JsonObject* app_object = db->o("apps")->@(client_id); JsonObject* app_object = db->o("apps")->@(client_id);
// If client_id or redirect_uri are empty, or if client app doesn't exist, Bad Request // If client_id or redirect_uri are empty, or if client app doesn't exist, Bad Request
if (!StrLen(client_id) || !StrLen(redirect_uri) || !app_object) { if (!StrLen(client_id) || !StrLen(redirect_uri) || !app_object) {
@slon_http_set_status_code(session, 400); session->status(400);
return; return;
} }
U8* client_secret = app_object->@("client_secret"); U8* client_secret = app_object->@("client_secret");
@ -140,11 +140,11 @@ U0 @slon_oauth_verify_access_get(SlonHttpSession* session)
} else { } else {
// If the account does not exist, return Not Found // If the account does not exist, return Not Found
@slon_http_set_status_code(session, 404); session->status(404);
} }
} else { } else {
// Response doesn't contain an email, therefore user is Unauthorized. // Response doesn't contain an email, therefore user is Unauthorized.
@slon_http_set_status_code(session, 401); session->status(401);
} }
return; return;
} else { } else {
@ -153,7 +153,7 @@ U0 @slon_oauth_verify_access_get(SlonHttpSession* session)
db->o("oauth")->o("requests")->set(client_id, request_json, JSON_OBJECT); db->o("oauth")->o("requests")->set(client_id, request_json, JSON_OBJECT);
@async_slon_oauth_fetch_token(client_id); @async_slon_oauth_fetch_token(client_id);
} }
@slon_http_set_status_code(session, 202); session->status(202);
} }
Json.Delete(app_object); Json.Delete(app_object);
} }
@ -170,7 +170,7 @@ U0 @slon_oauth_token_post(SlonHttpSession* session)
JsonObject* code_object = db->o("oauth")->o("codes")->@(code); JsonObject* code_object = db->o("oauth")->o("codes")->@(code);
if (!StrLen(client_id) || !StrLen(client_secret) || !code_object) { if (!StrLen(client_id) || !StrLen(client_secret) || !code_object) {
// If client_id is empty, or client_secret is empty, or the code doesn't exist, it's a Bad Request. // If client_id is empty, or client_secret is empty, or the code doesn't exist, it's a Bad Request.
@slon_http_set_status_code(session, 400); session->status(400);
return; return;
} }
@ -181,11 +181,11 @@ U0 @slon_oauth_token_post(SlonHttpSession* session)
@slon_http_send_json(session, token); @slon_http_send_json(session, token);
} else { } else {
// If the token doesn't exist, Page Expired? // If the token doesn't exist, Page Expired?
@slon_http_set_status_code(session, 419); session->status(419);
} }
} else { } else {
// If client_id and client_secret do not match, it's Unauthorized // If client_id and client_secret do not match, it's Unauthorized
@slon_http_set_status_code(session, 401); session->status(401);
} }
Json.Delete(code_object); Json.Delete(code_object);

View file

@ -11,7 +11,7 @@ U0 @slon_web_user_get(SlonHttpSession* session)
if (path_segments_count == 1) { if (path_segments_count == 1) {
JsonObject* actor = db->o("actors")->@(user); JsonObject* actor = db->o("actors")->@(user);
if (!actor) { if (!actor) {
@slon_http_set_status_code(session, 404); session->status(404);
goto slon_web_user_get_return; goto slon_web_user_get_return;
} }
// gib profil pl0x // gib profil pl0x
@ -34,7 +34,7 @@ U0 @slon_web_user_get(SlonHttpSession* session)
goto slon_web_user_get_return; goto slon_web_user_get_return;
} else { } else {
// do something here (statuses, followers, media, etc.) // do something here (statuses, followers, media, etc.)
@slon_http_set_status_code(session, 404); session->status(404);
} }
slon_web_user_get_return: slon_web_user_get_return:

View file

@ -31,9 +31,9 @@ U0 @slon_webfinger(SlonHttpSession* session)
@slon_http_send_json(session, webfinger_object); @slon_http_send_json(session, webfinger_object);
Json.Delete(webfinger_object); Json.Delete(webfinger_object);
} else { } else {
@slon_http_set_status_code(session, 404); session->status(404);
} }
} else { } else {
@slon_http_set_status_code(session, 400); session->status(400);
} }
} }