commit 26fbda7

Louis Brauer  ·  2024-05-27 14:25:20 +0000 UTC
parent 637a88f
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.
5 files changed,  +66, -50
M snac.c
M snac.h
+45, -0
 1@@ -0,0 +1,45 @@
 2+HTTP_STATUS(100, CONTINUE, Continue)
 3+HTTP_STATUS(101, SWITCHING_PROTOCOLS, Switching Protocols)
 4+HTTP_STATUS(102, PROCESSING, Processing)
 5+HTTP_STATUS(103, EARLY_HINTS, Early Hints)
 6+HTTP_STATUS(200, OK, OK)
 7+HTTP_STATUS(201, CREATED, Created)
 8+HTTP_STATUS(202, ACCEPTED, Accepted)
 9+HTTP_STATUS(203, NON_AUTHORITATIVE_INFORMATION, Non Authoritative Information)
10+HTTP_STATUS(204, NO_CONTENT, No Content)
11+HTTP_STATUS(205, RESET_CONTENT, Reset Content)
12+HTTP_STATUS(206, PARTIAL_CONTENT, Partial Content)
13+HTTP_STATUS(207, MULTI_STATUS, Multi Status)
14+HTTP_STATUS(208, ALREADY_REPORTED, Already Reported)
15+HTTP_STATUS(218, THIS_IS_FINE, This Is Fine)
16+HTTP_STATUS(226, IM_USED, IM Used)
17+HTTP_STATUS(300, MULTIPLE_CHOICES, Multiple Choices)
18+HTTP_STATUS(301, MOVED_PERMANENTLY, Moved Permanently)
19+HTTP_STATUS(302, FOUND, Found)
20+HTTP_STATUS(303, SEE_OTHER, See Other)
21+HTTP_STATUS(304, NOT_MODIFIED, Not Modified)
22+HTTP_STATUS(305, USE_PROXY, Use Proxy)
23+HTTP_STATUS(306, SWITCH_PROXY, Switch Proxy)
24+HTTP_STATUS(307, TEMPORARY_REDIRECT, Temporary Redirect)
25+HTTP_STATUS(308, PERMANENT_REDIRECT, Permanent Redirect)
26+HTTP_STATUS(400, BAD_REQUEST, Bad Request)
27+HTTP_STATUS(401, UNAUTHORIZED, Unauthorized)
28+HTTP_STATUS(402, PAYMENT_REQUIRED, Payment Required)
29+HTTP_STATUS(403, FORBIDDEN, Forbidden)
30+HTTP_STATUS(404, NOT_FOUND, Not Found)
31+HTTP_STATUS(405, METHOD_NOT_ALLOWED, Method Not Allowed)
32+HTTP_STATUS(406, NOT_ACCEPTABLE, Not Acceptable)
33+HTTP_STATUS(407, PROXY_AUTHENTICATION_REQUIRED, Proxy Authentication Required)
34+HTTP_STATUS(408, REQUEST_TIMEOUT, Request Timeout)
35+HTTP_STATUS(409, CONFLICT, Conflict)
36+HTTP_STATUS(410, GONE, Gone)
37+HTTP_STATUS(421, MISDIRECTED_REQUEST, Misdirected Request)
38+HTTP_STATUS(422, UNPROCESSABLE_CONTENT, Unprocessable Content)
39+HTTP_STATUS(499, CLIENT_CLOSED_REQUEST, Client Closed Request)
40+HTTP_STATUS(500, INTERNAL_SERVER_ERROR, Internal Server Error)
41+HTTP_STATUS(501, NOT_IMPLEMENTED, Not Implemented)
42+HTTP_STATUS(502, BAD_GATEWAY, Bad Gateway)
43+HTTP_STATUS(503, SERVICE_UNAVAILABLE, Service Unavailable)
44+HTTP_STATUS(504, GATEWAY_TIMEOUT, Gateway Timeout)
45+HTTP_STATUS(505, HTTP_VERSION_NOT_SUPPORTED, HTTP Version Not Supported)
46+HTTP_STATUS(507, INSUFFICIENT_STORAGE, Insufficient Storage)
+1, -1
1@@ -442,7 +442,7 @@ void httpd_connection(FILE *f)
2     if (p_state->use_fcgi)
3         xs_fcgi_response(f, status, headers, body, b_size, fcgi_id);
4     else
5-        xs_httpd_response(f, status, headers, body, b_size);
6+        xs_httpd_response(f, status, http_status_text(status), headers, body, b_size);
7 
8     fclose(f);
9 
M snac.c
+12, -0
 1@@ -170,3 +170,15 @@ int check_password(const char *uid, const char *passwd, const char *hash)
 2 
 3     return ret;
 4 }
 5+
 6+
 7+const char *http_status_text(int status)
 8+/* translate status codes to canonical status texts */
 9+{
10+    switch (status) {
11+#define HTTP_STATUS(code, name, text) case HTTP_STATUS_ ## name: return #text;
12+#include "http_codes.h"
13+#undef HTTP_STATUS
14+        default: return "Unknown";
15+    }
16+}
M snac.h
+5, -46
 1@@ -367,52 +367,11 @@ void mastoapi_purge(void);
 2 
 3 void verify_links(snac *user);
 4 
 5-/* HTTP responses RFC 9110 plus some extensions */
 6 
 7 typedef enum {
 8-    HTTP_STATUS_CONTINUE                      = 100,
 9-    HTTP_STATUS_SWITCHTING_PROTOCOLS          = 101,
10-    HTTP_STATUS_PROCESSING                    = 102,
11-    HTTP_STATUS_EARLY_HINTS                   = 103,
12-    HTTP_STATUS_OK                            = 200,
13-    HTTP_STATUS_CREATED                       = 201,
14-    HTTP_STATUS_ACCEPTED                      = 202,
15-    HTTP_STATUS_NON_AUTHORITATIVE_INFORMATION = 203,
16-    HTTP_STATUS_NO_CONTENT                    = 204,
17-    HTTP_STATUS_RESET_CONTENT                 = 205,
18-    HTTP_STATUS_PARTIAL_CONTENT               = 206,
19-    HTTP_STATUS_MULTI_STATUS                  = 207,
20-    HTTP_STATUS_ALREADY_REPORTED              = 208,
21-    HTTP_STATUS_THIS_IS_FINE                  = 218,
22-    HTTP_STATUS_IM_USED                       = 226,
23-    HTTP_STATUS_MULTIPLE_CHOICES              = 300,
24-    HTTP_STATUS_MOVED_PERMANENTLY             = 301,
25-    HTTP_STATUS_FOUND                         = 302,
26-    HTTP_STATUS_SEE_OTHER                     = 303,
27-    HTTP_STATUS_NOT_MODIFIED                  = 304,
28-    HTTP_STATUS_USE_PROXY                     = 305,
29-    HTTP_STATUS_SWITCH_PROXY                  = 306,
30-    HTTP_STATUS_TEMPORARY_REDIRECT            = 307,
31-    HTTP_STATUS_PERMANENT_REDIRECT            = 308,
32-    HTTP_STATUS_BAD_REQUEST                   = 400,
33-    HTTP_STATUS_UNAUTHORIZED                  = 401,
34-    HTTP_STATUS_PAYMENT_REQUIRED              = 402,
35-    HTTP_STATUS_FORBIDDEN                     = 403,
36-    HTTP_STATUS_NOT_FOUND                     = 404,
37-    HTTP_STATUS_METHOD_NOT_ALLOWED            = 405,
38-    HTTP_STATUS_NOT_ACCEPTABLE                = 406,
39-    HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,
40-    HTTP_STATUS_REQUEST_TIMEOUT               = 408,
41-    HTTP_STATUS_CONFLICT                      = 409,
42-    HTTP_STATUS_GONE                          = 410,
43-    HTTP_STATUS_MISDIRECTED_REQUEST           = 421,
44-    HTTP_STATUS_UNPROCESSABLE_CONTENT         = 422,
45-    HTTP_STATUS_CLIENT_CLOSED_REQUEST         = 499,
46-    HTTP_STATUS_INTERNAL_SERVER_ERROR         = 500,
47-    HTTP_STATUS_NOT_IMPLEMENTED               = 501,
48-    HTTP_STATUS_BAD_GATEWAY                   = 502,
49-    HTTP_STATUS_SERVICE_UNAVAILABLE           = 503,
50-    HTTP_STATUS_GATEWAY_TIMEOUT               = 504,
51-    HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED    = 505,
52-    HTTP_STATUS_INSUFFICIENT_STORAGE          = 507
53+#define HTTP_STATUS(code, name, text) HTTP_STATUS_ ## name = code,
54+#include "http_codes.h"
55+#undef HTTP_STATUS
56 } http_status;
57+
58+const char *http_status_text(int status);
+3, -3
 1@@ -5,7 +5,7 @@
 2 #define _XS_HTTPD_H
 3 
 4 xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size);
 5-void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size);
 6+void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size);
 7 
 8 
 9 #ifdef XS_IMPLEMENTATION
10@@ -95,14 +95,14 @@ xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size)
11 }
12 
13 
14-void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size)
15+void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size)
16 /* sends an httpd response */
17 {
18     xs *proto;
19     const xs_str *k;
20     const xs_val *v;
21 
22-    proto = xs_fmt("HTTP/1.1 %d %s", status, status / 100 == 2 ? "OK" : "ERROR");
23+    proto = xs_fmt("HTTP/1.1 %d %s", status, status_text);
24     fprintf(f, "%s\r\n", proto);
25 
26     int c = 0;