mirror of
https://git.checksum.fail/alec/slon.git
synced 2025-12-08 12:09:55 +02:00
Everywhere: Make session->status() callable
This commit is contained in:
@@ -258,10 +258,10 @@ U0 @slon_activitypub_users_get(SlonHttpSession* session)
|
||||
if (actor) {
|
||||
@slon_http_send_ap_json(session, actor);
|
||||
} else {
|
||||
@slon_http_set_status_code(session, 404);
|
||||
session->status(404);
|
||||
}
|
||||
} else {
|
||||
@slon_http_set_status_code(session, 400);
|
||||
session->status(400);
|
||||
}
|
||||
slon_activitypub_users_get_return:
|
||||
@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(session->actor_for_key_id, request_json->@("actor"))) {
|
||||
@slon_http_set_status_code(session, 401);
|
||||
session->status(401);
|
||||
return;
|
||||
}
|
||||
if (!db->o("followers")->@(user)) {
|
||||
@@ -830,7 +830,7 @@ U0 @slon_activitypub_users_inbox(SlonHttpSession* session, U8* user)
|
||||
|
||||
// otherwise, 401
|
||||
if (!should_accept) {
|
||||
@slon_http_set_status_code(session, 401);
|
||||
session->status(401);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -911,7 +911,7 @@ U0 @slon_activitypub_users_inbox(SlonHttpSession* session, U8* user)
|
||||
|
||||
if (!StrICmp("like", request_json->@("type"))) {
|
||||
if (StrICmp(session->actor_for_key_id, request_json->@("actor"))) {
|
||||
@slon_http_set_status_code(session, 401);
|
||||
session->status(401);
|
||||
return;
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
@slon_http_set_status_code(session, 200);
|
||||
session->status(200);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -948,40 +948,40 @@ U0 @slon_activitypub_users_post(SlonHttpSession* session)
|
||||
U8** path_segments = String.Split(path, '/', &path_segments_count);
|
||||
|
||||
if (path_segments_count < 3) {
|
||||
@slon_http_set_status_code(session, 400);
|
||||
session->status(400);
|
||||
goto slon_activitypub_users_post_return;
|
||||
}
|
||||
|
||||
U8* user = path_segments[1];
|
||||
JsonObject* actor = db->o("actors")->@(user);
|
||||
if (!actor) {
|
||||
@slon_http_set_status_code(session, 404);
|
||||
session->status(404);
|
||||
goto slon_activitypub_users_post_return;
|
||||
}
|
||||
|
||||
U8* method = path_segments[2];
|
||||
if (!StrICmp("inbox", method)) {
|
||||
if (!request_json) {
|
||||
@slon_http_set_status_code(session, 400);
|
||||
session->status(400);
|
||||
goto slon_activitypub_users_post_return;
|
||||
}
|
||||
if (!request_json->@("type")) {
|
||||
@slon_http_set_status_code(session, 400);
|
||||
session->status(400);
|
||||
goto slon_activitypub_users_post_return;
|
||||
}
|
||||
if (!StrICmp("delete", request_json->@("type"))) {
|
||||
@slon_http_set_status_code(session, 400);
|
||||
session->status(400);
|
||||
goto slon_activitypub_users_post_return;
|
||||
}
|
||||
if (!@slon_activitypub_http_signature_is_valid(session)) {
|
||||
@slon_http_set_status_code(session, 401);
|
||||
session->status(401);
|
||||
goto slon_activitypub_users_post_return;
|
||||
}
|
||||
@slon_activitypub_users_inbox(session, user);
|
||||
goto slon_activitypub_users_post_return;
|
||||
}
|
||||
|
||||
@slon_http_set_status_code(session, 404);
|
||||
session->status(404);
|
||||
|
||||
slon_activitypub_users_post_return:
|
||||
if (session->actor_for_key_id) {
|
||||
|
||||
@@ -61,6 +61,9 @@ class SlonHttpSession {
|
||||
SlonHttpResponse* response;
|
||||
I64 bytes_used;
|
||||
JsonObject* auth;
|
||||
U8* actor_for_key_id;
|
||||
|
||||
I64 (*status)(I64 code = NULL);
|
||||
};
|
||||
|
||||
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)
|
||||
{
|
||||
// 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");
|
||||
U8* json_string = Json.Stringify(json);
|
||||
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)
|
||||
{
|
||||
// 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");
|
||||
U8* json_string = Json.Stringify(json);
|
||||
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)
|
||||
{
|
||||
// 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->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)
|
||||
{
|
||||
// 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);
|
||||
MemCpy(data_new, data, size);
|
||||
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);
|
||||
}
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ U0 @slon_oauth_verify_access_get(SlonHttpSession* session)
|
||||
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 (!StrLen(client_id) || !StrLen(redirect_uri) || !app_object) {
|
||||
@slon_http_set_status_code(session, 400);
|
||||
session->status(400);
|
||||
return;
|
||||
}
|
||||
U8* client_secret = app_object->@("client_secret");
|
||||
@@ -140,11 +140,11 @@ U0 @slon_oauth_verify_access_get(SlonHttpSession* session)
|
||||
|
||||
} else {
|
||||
// If the account does not exist, return Not Found
|
||||
@slon_http_set_status_code(session, 404);
|
||||
session->status(404);
|
||||
}
|
||||
} else {
|
||||
// Response doesn't contain an email, therefore user is Unauthorized.
|
||||
@slon_http_set_status_code(session, 401);
|
||||
session->status(401);
|
||||
}
|
||||
return;
|
||||
} 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);
|
||||
@async_slon_oauth_fetch_token(client_id);
|
||||
}
|
||||
@slon_http_set_status_code(session, 202);
|
||||
session->status(202);
|
||||
}
|
||||
Json.Delete(app_object);
|
||||
}
|
||||
@@ -170,7 +170,7 @@ U0 @slon_oauth_token_post(SlonHttpSession* session)
|
||||
JsonObject* code_object = db->o("oauth")->o("codes")->@(code);
|
||||
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.
|
||||
@slon_http_set_status_code(session, 400);
|
||||
session->status(400);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -181,11 +181,11 @@ U0 @slon_oauth_token_post(SlonHttpSession* session)
|
||||
@slon_http_send_json(session, token);
|
||||
} else {
|
||||
// If the token doesn't exist, Page Expired?
|
||||
@slon_http_set_status_code(session, 419);
|
||||
session->status(419);
|
||||
}
|
||||
} else {
|
||||
// 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);
|
||||
|
||||
@@ -11,7 +11,7 @@ U0 @slon_web_user_get(SlonHttpSession* session)
|
||||
if (path_segments_count == 1) {
|
||||
JsonObject* actor = db->o("actors")->@(user);
|
||||
if (!actor) {
|
||||
@slon_http_set_status_code(session, 404);
|
||||
session->status(404);
|
||||
goto slon_web_user_get_return;
|
||||
}
|
||||
// gib profil pl0x
|
||||
@@ -34,7 +34,7 @@ U0 @slon_web_user_get(SlonHttpSession* session)
|
||||
goto slon_web_user_get_return;
|
||||
} else {
|
||||
// do something here (statuses, followers, media, etc.)
|
||||
@slon_http_set_status_code(session, 404);
|
||||
session->status(404);
|
||||
}
|
||||
|
||||
slon_web_user_get_return:
|
||||
|
||||
@@ -31,9 +31,9 @@ U0 @slon_webfinger(SlonHttpSession* session)
|
||||
@slon_http_send_json(session, webfinger_object);
|
||||
Json.Delete(webfinger_object);
|
||||
} else {
|
||||
@slon_http_set_status_code(session, 404);
|
||||
session->status(404);
|
||||
}
|
||||
} else {
|
||||
@slon_http_set_status_code(session, 400);
|
||||
session->status(400);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user