From 0e21d35e802bf859aa14bce688cd9544458e9e9c Mon Sep 17 00:00:00 2001 From: Louis Brauer Date: Sun, 26 May 2024 21:45:41 +0200 Subject: Use enum instead of numeric status codes for HTTP statuses --- httpd.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'httpd.c') diff --git a/httpd.c b/httpd.c index a7396e8..60afe28 100644 --- a/httpd.c +++ b/httpd.c @@ -217,17 +217,17 @@ int server_get_handler(xs_dict *req, const char *q_path, *body = greeting_html(); if (*body) - status = 200; + status = HTTP_STATUS_OK; } else if (strcmp(q_path, "/susie.png") == 0 || strcmp(q_path, "/favicon.ico") == 0 ) { - status = 200; + status = HTTP_STATUS_OK; *body = xs_base64_dec(default_avatar_base64(), b_size); *ctype = "image/png"; } else if (strcmp(q_path, "/.well-known/nodeinfo") == 0) { - status = 200; + status = HTTP_STATUS_OK; *ctype = "application/json; charset=utf-8"; *body = xs_fmt("{\"links\":[" "{\"rel\":\"http:/" "/nodeinfo.diaspora.software/ns/schema/2.0\"," @@ -236,7 +236,7 @@ int server_get_handler(xs_dict *req, const char *q_path, } else if (strcmp(q_path, "/.well-known/host-meta") == 0) { - status = 200; + status = HTTP_STATUS_OK; *ctype = "application/xrd+xml"; *body = xs_fmt("\n" "" @@ -245,13 +245,13 @@ int server_get_handler(xs_dict *req, const char *q_path, } else if (strcmp(q_path, "/nodeinfo_2_0") == 0) { - status = 200; + status = HTTP_STATUS_OK; *ctype = "application/json; charset=utf-8"; *body = nodeinfo_2_0(); } else if (strcmp(q_path, "/robots.txt") == 0) { - status = 200; + status = HTTP_STATUS_OK; *ctype = "text/plain"; *body = xs_str_new("User-agent: *\n" "Disallow: /\n"); @@ -363,7 +363,7 @@ void httpd_connection(FILE *f) } else if (strcmp(method, "OPTIONS") == 0) { - status = 200; + status = HTTP_STATUS_OK; } else if (strcmp(method, "DELETE") == 0) { @@ -378,22 +378,22 @@ void httpd_connection(FILE *f) if (status == 0) { srv_archive_error("unattended_method", "unattended method", req, payload); srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path)); - status = 404; + status = HTTP_STATUS_NOT_FOUND; } - if (status == 403) + if (status == HTTP_STATUS_FORBIDDEN) body = xs_str_new("

403 Forbidden

"); - if (status == 404) + if (status == HTTP_STATUS_NOT_FOUND) body = xs_str_new("

404 Not Found

"); - if (status == 400 && body != NULL) + if (status == HTTP_STATUS_BAD_REQUEST && body != NULL) body = xs_str_new("

400 Bad Request

"); - if (status == 303) + if (status == HTTP_STATUS_SEE_OTHER) headers = xs_dict_append(headers, "location", body); - if (status == 401) { + if (status == HTTP_STATUS_UNAUTHORIZED) { xs *www_auth = xs_fmt("Basic realm=\"@%s@%s snac login\"", body, xs_dict_get(srv_config, "host")); -- cgit v1.2.3 From 81cf309e4d0ba6c2debccc21ea4f85e1e6245dc5 Mon Sep 17 00:00:00 2001 From: Louis Brauer Date: Mon, 27 May 2024 12:24:17 +0200 Subject: Implement Mastodon PATCH endpoint for account profile updates --- httpd.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'httpd.c') diff --git a/httpd.c b/httpd.c index 60afe28..1c60a56 100644 --- a/httpd.c +++ b/httpd.c @@ -360,6 +360,16 @@ void httpd_connection(FILE *f) payload, p_size, &body, &b_size, &ctype); #endif + } + else + if (strcmp(method, "PATCH") == 0) { + +#ifndef NO_MASTODON_API + if (status == 0) + status = mastoapi_patch_handler(req, q_path, + payload, p_size, &body, &b_size, &ctype); +#endif + } else if (strcmp(method, "OPTIONS") == 0) { -- cgit v1.2.3 From 26fbda787d0d5c8ba9259f79f4d2f937bd6c8ead Mon Sep 17 00:00:00 2001 From: Louis Brauer Date: Mon, 27 May 2024 16:25:20 +0200 Subject: Translate status codes to canonical status texts Use those in HTTP responses instead of "OK"/"ERROR". Apps like Tokodon show only the status text in unexpected responses. --- httpd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'httpd.c') diff --git a/httpd.c b/httpd.c index 1c60a56..d566b1e 100644 --- a/httpd.c +++ b/httpd.c @@ -442,7 +442,7 @@ void httpd_connection(FILE *f) if (p_state->use_fcgi) xs_fcgi_response(f, status, headers, body, b_size, fcgi_id); else - xs_httpd_response(f, status, headers, body, b_size); + xs_httpd_response(f, status, http_status_text(status), headers, body, b_size); fclose(f); -- cgit v1.2.3