From 212284c792c86fca52e0172a4d097abc5bb45252 Mon Sep 17 00:00:00 2001 From: grunfink Date: Mon, 13 Oct 2025 13:07:06 +0200 Subject: xs_http.h: new file. --- Makefile | 37 +++++++++++++++++++------------------ Makefile.NetBSD | 37 +++++++++++++++++++------------------ activitypub.c | 3 ++- data.c | 1 + html.c | 1 + http.c | 1 + http_codes.h | 45 --------------------------------------------- httpd.c | 3 ++- main.c | 1 + mastoapi.c | 1 + rss.c | 1 + snac.c | 21 +-------------------- snac.h | 10 +--------- utils.c | 1 + webfinger.c | 1 + xs_http.h | 42 ++++++++++++++++++++++++++++++++++++++++++ xs_http_codes.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 17 files changed, 139 insertions(+), 112 deletions(-) delete mode 100644 http_codes.h create mode 100644 xs_http.h create mode 100644 xs_http_codes.h diff --git a/Makefile b/Makefile index 5de2882..ffeac1a 100644 --- a/Makefile +++ b/Makefile @@ -48,35 +48,36 @@ update-po: activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \ xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h xs_unicode.h \ - xs_webmention.h snac.h http_codes.h + xs_webmention.h xs_http.h xs_http_codes.h snac.h data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \ xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h \ - xs_po.h snac.h http_codes.h + xs_po.h xs_http.h xs_http_codes.h snac.h format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \ - xs_time.h xs_match.h xs_unicode.h snac.h http_codes.h + xs_time.h xs_match.h xs_unicode.h snac.h html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \ xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \ - xs_random.h snac.h http_codes.h + xs_random.h xs_http.h xs_http_codes.h snac.h http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \ - snac.h http_codes.h + xs_http.h xs_http_codes.h snac.h httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \ - xs_httpd.h xs_mime.h xs_time.h xs_openssl.h xs_fcgi.h xs_html.h \ - xs_webmention.h snac.h http_codes.h + xs_http.h xs_http_codes.h xs_httpd.h xs_mime.h xs_time.h xs_openssl.h \ + xs_fcgi.h xs_html.h xs_webmention.h snac.h main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h xs_match.h \ - xs_random.h snac.h http_codes.h + xs_random.h xs_http.h xs_http_codes.h snac.h mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \ xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \ - xs_unicode.h snac.h http_codes.h + xs_unicode.h xs_http.h xs_http_codes.h snac.h rss.o: rss.c xs.h xs_html.h xs_regex.h xs_time.h xs_match.h xs_curl.h \ - xs_openssl.h xs_json.h snac.h http_codes.h -sandbox.o: sandbox.c xs.h snac.h http_codes.h + xs_openssl.h xs_json.h xs_http.h xs_http_codes.h snac.h +sandbox.o: sandbox.c xs.h snac.h snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \ xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \ - xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \ - xs_match.h xs_fcgi.h xs_html.h xs_po.h xs_webmention.h snac.h \ - http_codes.h -upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h + xs_http.h xs_http_codes.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h \ + xs_time.h xs_glob.h xs_random.h xs_match.h xs_fcgi.h xs_html.h xs_po.h \ + xs_webmention.h snac.h +upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \ - xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h -webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h snac.h \ - http_codes.h + xs_random.h xs_glob.h xs_curl.h xs_regex.h xs_http.h xs_http_codes.h \ + snac.h +webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h xs_http.h \ + xs_http_codes.h snac.h diff --git a/Makefile.NetBSD b/Makefile.NetBSD index ac5b052..b5005ee 100644 --- a/Makefile.NetBSD +++ b/Makefile.NetBSD @@ -37,35 +37,36 @@ uninstall: activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \ xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h xs_unicode.h \ - xs_webmention.h snac.h http_codes.h + xs_webmention.h xs_http.h xs_http_codes.h snac.h data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \ xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h \ - xs_po.h snac.h http_codes.h + xs_po.h xs_http.h xs_http_codes.h snac.h format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \ - xs_time.h xs_match.h xs_unicode.h snac.h http_codes.h + xs_time.h xs_match.h xs_unicode.h snac.h html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \ xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \ - xs_random.h snac.h http_codes.h + xs_random.h xs_http.h xs_http_codes.h snac.h http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \ - snac.h http_codes.h + xs_http.h xs_http_codes.h snac.h httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \ - xs_httpd.h xs_mime.h xs_time.h xs_openssl.h xs_fcgi.h xs_html.h \ - xs_webmention.h snac.h http_codes.h + xs_http.h xs_http_codes.h xs_httpd.h xs_mime.h xs_time.h xs_openssl.h \ + xs_fcgi.h xs_html.h xs_webmention.h snac.h main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h xs_match.h \ - xs_random.h snac.h http_codes.h + xs_random.h xs_http.h xs_http_codes.h snac.h mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \ xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \ - xs_unicode.h snac.h http_codes.h + xs_unicode.h xs_http.h xs_http_codes.h snac.h rss.o: rss.c xs.h xs_html.h xs_regex.h xs_time.h xs_match.h xs_curl.h \ - xs_openssl.h xs_json.h snac.h http_codes.h -sandbox.o: sandbox.c xs.h snac.h http_codes.h + xs_openssl.h xs_json.h xs_http.h xs_http_codes.h snac.h +sandbox.o: sandbox.c xs.h snac.h snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \ xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \ - xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \ - xs_match.h xs_fcgi.h xs_html.h xs_po.h xs_webmention.h snac.h \ - http_codes.h -upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h + xs_http.h xs_http_codes.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h \ + xs_time.h xs_glob.h xs_random.h xs_match.h xs_fcgi.h xs_html.h xs_po.h \ + xs_webmention.h snac.h +upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \ - xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h -webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h snac.h \ - http_codes.h + xs_random.h xs_glob.h xs_curl.h xs_regex.h xs_http.h xs_http_codes.h \ + snac.h +webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h xs_http.h \ + xs_http_codes.h snac.h diff --git a/activitypub.c b/activitypub.c index c433788..f19c6fc 100644 --- a/activitypub.c +++ b/activitypub.c @@ -12,6 +12,7 @@ #include "xs_match.h" #include "xs_unicode.h" #include "xs_webmention.h" +#include "xs_http.h" #include "snac.h" @@ -3210,7 +3211,7 @@ int process_user_queue(snac *snac) xs_str *str_status(int status) { - return xs_fmt("%d %s", status, status < 0 ? xs_curl_strerr(status) : http_status_text(status)); + return xs_fmt("%d %s", status, status < 0 ? xs_curl_strerr(status) : xs_http_status_text(status)); } diff --git a/data.c b/data.c index 53e9438..36f5101 100644 --- a/data.c +++ b/data.c @@ -14,6 +14,7 @@ #include "xs_unicode.h" #include "xs_random.h" #include "xs_po.h" +#include "xs_http.h" #include "snac.h" diff --git a/html.c b/html.c index 777875c..584059b 100644 --- a/html.c +++ b/html.c @@ -15,6 +15,7 @@ #include "xs_unicode.h" #include "xs_url.h" #include "xs_random.h" +#include "xs_http.h" #include "snac.h" diff --git a/http.c b/http.c index 611de4f..f0b1314 100644 --- a/http.c +++ b/http.c @@ -7,6 +7,7 @@ #include "xs_curl.h" #include "xs_time.h" #include "xs_json.h" +#include "xs_http.h" #include "snac.h" diff --git a/http_codes.h b/http_codes.h deleted file mode 100644 index a1958f1..0000000 --- a/http_codes.h +++ /dev/null @@ -1,45 +0,0 @@ -HTTP_STATUS(100, CONTINUE, Continue) -HTTP_STATUS(101, SWITCHING_PROTOCOLS, Switching Protocols) -HTTP_STATUS(102, PROCESSING, Processing) -HTTP_STATUS(103, EARLY_HINTS, Early Hints) -HTTP_STATUS(200, OK, OK) -HTTP_STATUS(201, CREATED, Created) -HTTP_STATUS(202, ACCEPTED, Accepted) -HTTP_STATUS(203, NON_AUTHORITATIVE_INFORMATION, Non Authoritative Information) -HTTP_STATUS(204, NO_CONTENT, No Content) -HTTP_STATUS(205, RESET_CONTENT, Reset Content) -HTTP_STATUS(206, PARTIAL_CONTENT, Partial Content) -HTTP_STATUS(207, MULTI_STATUS, Multi Status) -HTTP_STATUS(208, ALREADY_REPORTED, Already Reported) -HTTP_STATUS(218, THIS_IS_FINE, This Is Fine) -HTTP_STATUS(226, IM_USED, IM Used) -HTTP_STATUS(300, MULTIPLE_CHOICES, Multiple Choices) -HTTP_STATUS(301, MOVED_PERMANENTLY, Moved Permanently) -HTTP_STATUS(302, FOUND, Found) -HTTP_STATUS(303, SEE_OTHER, See Other) -HTTP_STATUS(304, NOT_MODIFIED, Not Modified) -HTTP_STATUS(305, USE_PROXY, Use Proxy) -HTTP_STATUS(306, SWITCH_PROXY, Switch Proxy) -HTTP_STATUS(307, TEMPORARY_REDIRECT, Temporary Redirect) -HTTP_STATUS(308, PERMANENT_REDIRECT, Permanent Redirect) -HTTP_STATUS(400, BAD_REQUEST, Bad Request) -HTTP_STATUS(401, UNAUTHORIZED, Unauthorized) -HTTP_STATUS(402, PAYMENT_REQUIRED, Payment Required) -HTTP_STATUS(403, FORBIDDEN, Forbidden) -HTTP_STATUS(404, NOT_FOUND, Not Found) -HTTP_STATUS(405, METHOD_NOT_ALLOWED, Method Not Allowed) -HTTP_STATUS(406, NOT_ACCEPTABLE, Not Acceptable) -HTTP_STATUS(407, PROXY_AUTHENTICATION_REQUIRED, Proxy Authentication Required) -HTTP_STATUS(408, REQUEST_TIMEOUT, Request Timeout) -HTTP_STATUS(409, CONFLICT, Conflict) -HTTP_STATUS(410, GONE, Gone) -HTTP_STATUS(421, MISDIRECTED_REQUEST, Misdirected Request) -HTTP_STATUS(422, UNPROCESSABLE_CONTENT, Unprocessable Content) -HTTP_STATUS(499, CLIENT_CLOSED_REQUEST, Client Closed Request) -HTTP_STATUS(500, INTERNAL_SERVER_ERROR, Internal Server Error) -HTTP_STATUS(501, NOT_IMPLEMENTED, Not Implemented) -HTTP_STATUS(502, BAD_GATEWAY, Bad Gateway) -HTTP_STATUS(503, SERVICE_UNAVAILABLE, Service Unavailable) -HTTP_STATUS(504, GATEWAY_TIMEOUT, Gateway Timeout) -HTTP_STATUS(505, HTTP_VERSION_NOT_SUPPORTED, HTTP Version Not Supported) -HTTP_STATUS(507, INSUFFICIENT_STORAGE, Insufficient Storage) diff --git a/httpd.c b/httpd.c index de76776..254d0e5 100644 --- a/httpd.c +++ b/httpd.c @@ -6,6 +6,7 @@ #include "xs_json.h" #include "xs_socket.h" #include "xs_unix_socket.h" +#include "xs_http.h" #include "xs_httpd.h" #include "xs_mime.h" #include "xs_time.h" @@ -650,7 +651,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, http_status_text(status), headers, body, b_size); + xs_httpd_response(f, status, xs_http_status_text(status), headers, body, b_size); fclose(f); diff --git a/main.c b/main.c index 46f88d2..6915bd0 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,7 @@ #include "xs_openssl.h" #include "xs_match.h" #include "xs_random.h" +#include "xs_http.h" #include "snac.h" diff --git a/mastoapi.c b/mastoapi.c index c526307..2c111ca 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -16,6 +16,7 @@ #include "xs_mime.h" #include "xs_match.h" #include "xs_unicode.h" +#include "xs_http.h" #include "snac.h" diff --git a/rss.c b/rss.c index bb7b043..6e77205 100644 --- a/rss.c +++ b/rss.c @@ -9,6 +9,7 @@ #include "xs_curl.h" #include "xs_openssl.h" #include "xs_json.h" +#include "xs_http.h" #include "snac.h" diff --git a/snac.c b/snac.c index a35cd06..31f524f 100644 --- a/snac.c +++ b/snac.c @@ -14,6 +14,7 @@ #include "xs_socket.h" #include "xs_unix_socket.h" #include "xs_url.h" +#include "xs_http.h" #include "xs_httpd.h" #include "xs_mime.h" #include "xs_regex.h" @@ -58,13 +59,6 @@ int mkdirx(const char *pathname) } -int valid_status(int status) -/* is this HTTP status valid? */ -{ - return status >= 200 && status <= 299; -} - - xs_str *tid(int offset) /* returns a time-based Id */ { @@ -176,16 +170,3 @@ int check_password(const char *uid, const char *passwd, const char *hash) return ret; } - - -const char *http_status_text(int status) -/* translate status codes to canonical status texts */ -{ - switch (status) { - case 599: return "Timeout"; -#define HTTP_STATUS(code, name, text) case HTTP_STATUS_ ## name: return #text; -#include "http_codes.h" -#undef HTTP_STATUS - default: return "Unknown"; - } -} diff --git a/snac.h b/snac.h index 245f477..038aa40 100644 --- a/snac.h +++ b/snac.h @@ -44,7 +44,7 @@ extern int dbglevel; int mkdirx(const char *pathname); -int valid_status(int status); +#define valid_status(status) xs_http_valid_status(status) xs_str *tid(int offset); double ftime(void); @@ -464,14 +464,6 @@ void import_list_csv(snac *user, const char *fn); void import_csv(snac *user); int parse_port(const char *url, const char **errstr); -typedef enum { -#define HTTP_STATUS(code, name, text) HTTP_STATUS_ ## name = code, -#include "http_codes.h" -#undef HTTP_STATUS -} http_status; - -const char *http_status_text(int status); - typedef struct { double timestamp; char *text; diff --git a/utils.c b/utils.c index 38dcc06..de6db79 100644 --- a/utils.c +++ b/utils.c @@ -10,6 +10,7 @@ #include "xs_glob.h" #include "xs_curl.h" #include "xs_regex.h" +#include "xs_http.h" #include "snac.h" diff --git a/webfinger.c b/webfinger.c index 46b7edb..12ec42c 100644 --- a/webfinger.c +++ b/webfinger.c @@ -5,6 +5,7 @@ #include "xs_json.h" #include "xs_curl.h" #include "xs_mime.h" +#include "xs_http.h" #include "snac.h" diff --git a/xs_http.h b/xs_http.h new file mode 100644 index 0000000..f2fd296 --- /dev/null +++ b/xs_http.h @@ -0,0 +1,42 @@ +/* copyright (c) 2022 - 2025 grunfink et al. / MIT license */ + +#ifndef _XS_HTTP_H + +#define _XS_HTTP_H + +typedef enum { +#define HTTP_STATUS(code, name, text) HTTP_STATUS_ ## name = code, +#include "xs_http_codes.h" +#undef HTTP_STATUS +} http_status; + + +int xs_http_valid_status(int status); +const char *xs_http_status_text(int status); + + +#ifdef XS_IMPLEMENTATION + +int xs_http_valid_status(int status) +/* is this HTTP status valid? */ +{ + return status >= 200 && status <= 299; +} + + +const char *xs_http_status_text(int status) +/* translate status codes to canonical status texts */ +{ + switch (status) { + case 599: return "Timeout"; +#define HTTP_STATUS(code, name, text) case HTTP_STATUS_ ## name: return #text; +#include "xs_http_codes.h" +#undef HTTP_STATUS + default: return "Unknown"; + } +} + + +#endif /* XS_IMPLEMENTATION */ + +#endif /* XS_HTTP_H */ diff --git a/xs_http_codes.h b/xs_http_codes.h new file mode 100644 index 0000000..a1958f1 --- /dev/null +++ b/xs_http_codes.h @@ -0,0 +1,45 @@ +HTTP_STATUS(100, CONTINUE, Continue) +HTTP_STATUS(101, SWITCHING_PROTOCOLS, Switching Protocols) +HTTP_STATUS(102, PROCESSING, Processing) +HTTP_STATUS(103, EARLY_HINTS, Early Hints) +HTTP_STATUS(200, OK, OK) +HTTP_STATUS(201, CREATED, Created) +HTTP_STATUS(202, ACCEPTED, Accepted) +HTTP_STATUS(203, NON_AUTHORITATIVE_INFORMATION, Non Authoritative Information) +HTTP_STATUS(204, NO_CONTENT, No Content) +HTTP_STATUS(205, RESET_CONTENT, Reset Content) +HTTP_STATUS(206, PARTIAL_CONTENT, Partial Content) +HTTP_STATUS(207, MULTI_STATUS, Multi Status) +HTTP_STATUS(208, ALREADY_REPORTED, Already Reported) +HTTP_STATUS(218, THIS_IS_FINE, This Is Fine) +HTTP_STATUS(226, IM_USED, IM Used) +HTTP_STATUS(300, MULTIPLE_CHOICES, Multiple Choices) +HTTP_STATUS(301, MOVED_PERMANENTLY, Moved Permanently) +HTTP_STATUS(302, FOUND, Found) +HTTP_STATUS(303, SEE_OTHER, See Other) +HTTP_STATUS(304, NOT_MODIFIED, Not Modified) +HTTP_STATUS(305, USE_PROXY, Use Proxy) +HTTP_STATUS(306, SWITCH_PROXY, Switch Proxy) +HTTP_STATUS(307, TEMPORARY_REDIRECT, Temporary Redirect) +HTTP_STATUS(308, PERMANENT_REDIRECT, Permanent Redirect) +HTTP_STATUS(400, BAD_REQUEST, Bad Request) +HTTP_STATUS(401, UNAUTHORIZED, Unauthorized) +HTTP_STATUS(402, PAYMENT_REQUIRED, Payment Required) +HTTP_STATUS(403, FORBIDDEN, Forbidden) +HTTP_STATUS(404, NOT_FOUND, Not Found) +HTTP_STATUS(405, METHOD_NOT_ALLOWED, Method Not Allowed) +HTTP_STATUS(406, NOT_ACCEPTABLE, Not Acceptable) +HTTP_STATUS(407, PROXY_AUTHENTICATION_REQUIRED, Proxy Authentication Required) +HTTP_STATUS(408, REQUEST_TIMEOUT, Request Timeout) +HTTP_STATUS(409, CONFLICT, Conflict) +HTTP_STATUS(410, GONE, Gone) +HTTP_STATUS(421, MISDIRECTED_REQUEST, Misdirected Request) +HTTP_STATUS(422, UNPROCESSABLE_CONTENT, Unprocessable Content) +HTTP_STATUS(499, CLIENT_CLOSED_REQUEST, Client Closed Request) +HTTP_STATUS(500, INTERNAL_SERVER_ERROR, Internal Server Error) +HTTP_STATUS(501, NOT_IMPLEMENTED, Not Implemented) +HTTP_STATUS(502, BAD_GATEWAY, Bad Gateway) +HTTP_STATUS(503, SERVICE_UNAVAILABLE, Service Unavailable) +HTTP_STATUS(504, GATEWAY_TIMEOUT, Gateway Timeout) +HTTP_STATUS(505, HTTP_VERSION_NOT_SUPPORTED, HTTP Version Not Supported) +HTTP_STATUS(507, INSUFFICIENT_STORAGE, Insufficient Storage) -- cgit v1.2.3