commit 212284c

grunfink  ·  2025-10-13 11:07:06 +0000 UTC
parent 4f0b97e
xs_http.h: new file.
16 files changed,  +94, -67
M data.c
M html.c
M http.c
M main.c
M rss.c
M snac.c
M snac.h
+19, -18
 1@@ -48,35 +48,36 @@ update-po:
 2 
 3 activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \
 4  xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h xs_unicode.h \
 5- xs_webmention.h snac.h http_codes.h
 6+ xs_webmention.h xs_http.h xs_http_codes.h snac.h
 7 data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \
 8  xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h \
 9- xs_po.h snac.h http_codes.h
10+ xs_po.h xs_http.h xs_http_codes.h snac.h
11 format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \
12- xs_time.h xs_match.h xs_unicode.h snac.h http_codes.h
13+ xs_time.h xs_match.h xs_unicode.h snac.h
14 html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \
15  xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \
16- xs_random.h snac.h http_codes.h
17+ xs_random.h xs_http.h xs_http_codes.h snac.h
18 http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \
19- snac.h http_codes.h
20+ xs_http.h xs_http_codes.h snac.h
21 httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \
22- xs_httpd.h xs_mime.h xs_time.h xs_openssl.h xs_fcgi.h xs_html.h \
23- xs_webmention.h snac.h http_codes.h
24+ xs_http.h xs_http_codes.h xs_httpd.h xs_mime.h xs_time.h xs_openssl.h \
25+ xs_fcgi.h xs_html.h xs_webmention.h snac.h
26 main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h xs_match.h \
27- xs_random.h snac.h http_codes.h
28+ xs_random.h xs_http.h xs_http_codes.h snac.h
29 mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \
30  xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \
31- xs_unicode.h snac.h http_codes.h
32+ xs_unicode.h xs_http.h xs_http_codes.h snac.h
33 rss.o: rss.c xs.h xs_html.h xs_regex.h xs_time.h xs_match.h xs_curl.h \
34- xs_openssl.h xs_json.h snac.h http_codes.h
35-sandbox.o: sandbox.c xs.h snac.h http_codes.h
36+ xs_openssl.h xs_json.h xs_http.h xs_http_codes.h snac.h
37+sandbox.o: sandbox.c xs.h snac.h
38 snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \
39  xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \
40- xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \
41- xs_match.h xs_fcgi.h xs_html.h xs_po.h xs_webmention.h snac.h \
42- http_codes.h
43-upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h
44+ xs_http.h xs_http_codes.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h \
45+ xs_time.h xs_glob.h xs_random.h xs_match.h xs_fcgi.h xs_html.h xs_po.h \
46+ xs_webmention.h snac.h
47+upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h
48 utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \
49- xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h
50-webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h snac.h \
51- http_codes.h
52+ xs_random.h xs_glob.h xs_curl.h xs_regex.h xs_http.h xs_http_codes.h \
53+ snac.h
54+webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h xs_http.h \
55+ xs_http_codes.h snac.h
+19, -18
 1@@ -37,35 +37,36 @@ uninstall:
 2 
 3 activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \
 4  xs_openssl.h xs_regex.h xs_time.h xs_set.h xs_match.h xs_unicode.h \
 5- xs_webmention.h snac.h http_codes.h
 6+ xs_webmention.h xs_http.h xs_http_codes.h snac.h
 7 data.o: data.c xs.h xs_hex.h xs_io.h xs_json.h xs_openssl.h xs_glob.h \
 8  xs_set.h xs_time.h xs_regex.h xs_match.h xs_unicode.h xs_random.h \
 9- xs_po.h snac.h http_codes.h
10+ xs_po.h xs_http.h xs_http_codes.h snac.h
11 format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \
12- xs_time.h xs_match.h xs_unicode.h snac.h http_codes.h
13+ xs_time.h xs_match.h xs_unicode.h snac.h
14 html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \
15  xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \
16- xs_random.h snac.h http_codes.h
17+ xs_random.h xs_http.h xs_http_codes.h snac.h
18 http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \
19- snac.h http_codes.h
20+ xs_http.h xs_http_codes.h snac.h
21 httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \
22- xs_httpd.h xs_mime.h xs_time.h xs_openssl.h xs_fcgi.h xs_html.h \
23- xs_webmention.h snac.h http_codes.h
24+ xs_http.h xs_http_codes.h xs_httpd.h xs_mime.h xs_time.h xs_openssl.h \
25+ xs_fcgi.h xs_html.h xs_webmention.h snac.h
26 main.o: main.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h xs_match.h \
27- xs_random.h snac.h http_codes.h
28+ xs_random.h xs_http.h xs_http_codes.h snac.h
29 mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \
30  xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \
31- xs_unicode.h snac.h http_codes.h
32+ xs_unicode.h xs_http.h xs_http_codes.h snac.h
33 rss.o: rss.c xs.h xs_html.h xs_regex.h xs_time.h xs_match.h xs_curl.h \
34- xs_openssl.h xs_json.h snac.h http_codes.h
35-sandbox.o: sandbox.c xs.h snac.h http_codes.h
36+ xs_openssl.h xs_json.h xs_http.h xs_http_codes.h snac.h
37+sandbox.o: sandbox.c xs.h snac.h
38 snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \
39  xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \
40- xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \
41- xs_match.h xs_fcgi.h xs_html.h xs_po.h xs_webmention.h snac.h \
42- http_codes.h
43-upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h
44+ xs_http.h xs_http_codes.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h \
45+ xs_time.h xs_glob.h xs_random.h xs_match.h xs_fcgi.h xs_html.h xs_po.h \
46+ xs_webmention.h snac.h
47+upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h
48 utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \
49- xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h
50-webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h snac.h \
51- http_codes.h
52+ xs_random.h xs_glob.h xs_curl.h xs_regex.h xs_http.h xs_http_codes.h \
53+ snac.h
54+webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h xs_http.h \
55+ xs_http_codes.h snac.h
+2, -1
 1@@ -12,6 +12,7 @@
 2 #include "xs_match.h"
 3 #include "xs_unicode.h"
 4 #include "xs_webmention.h"
 5+#include "xs_http.h"
 6 
 7 #include "snac.h"
 8 
 9@@ -3210,7 +3211,7 @@ int process_user_queue(snac *snac)
10 
11 xs_str *str_status(int status)
12 {
13-    return xs_fmt("%d %s", status, status < 0 ? xs_curl_strerr(status) : http_status_text(status));
14+    return xs_fmt("%d %s", status, status < 0 ? xs_curl_strerr(status) : xs_http_status_text(status));
15 }
16 
17 
M data.c
+1, -0
1@@ -14,6 +14,7 @@
2 #include "xs_unicode.h"
3 #include "xs_random.h"
4 #include "xs_po.h"
5+#include "xs_http.h"
6 
7 #include "snac.h"
8 
M html.c
+1, -0
1@@ -15,6 +15,7 @@
2 #include "xs_unicode.h"
3 #include "xs_url.h"
4 #include "xs_random.h"
5+#include "xs_http.h"
6 
7 #include "snac.h"
8 
M http.c
+1, -0
1@@ -7,6 +7,7 @@
2 #include "xs_curl.h"
3 #include "xs_time.h"
4 #include "xs_json.h"
5+#include "xs_http.h"
6 
7 #include "snac.h"
8 
+2, -1
 1@@ -6,6 +6,7 @@
 2 #include "xs_json.h"
 3 #include "xs_socket.h"
 4 #include "xs_unix_socket.h"
 5+#include "xs_http.h"
 6 #include "xs_httpd.h"
 7 #include "xs_mime.h"
 8 #include "xs_time.h"
 9@@ -650,7 +651,7 @@ void httpd_connection(FILE *f)
10     if (p_state->use_fcgi)
11         xs_fcgi_response(f, status, headers, body, b_size, fcgi_id);
12     else
13-        xs_httpd_response(f, status, http_status_text(status), headers, body, b_size);
14+        xs_httpd_response(f, status, xs_http_status_text(status), headers, body, b_size);
15 
16     fclose(f);
17 
M main.c
+1, -0
1@@ -8,6 +8,7 @@
2 #include "xs_openssl.h"
3 #include "xs_match.h"
4 #include "xs_random.h"
5+#include "xs_http.h"
6 
7 #include "snac.h"
8 
+1, -0
1@@ -16,6 +16,7 @@
2 #include "xs_mime.h"
3 #include "xs_match.h"
4 #include "xs_unicode.h"
5+#include "xs_http.h"
6 
7 #include "snac.h"
8 
M rss.c
+1, -0
1@@ -9,6 +9,7 @@
2 #include "xs_curl.h"
3 #include "xs_openssl.h"
4 #include "xs_json.h"
5+#include "xs_http.h"
6 
7 #include "snac.h"
8 
M snac.c
+1, -20
 1@@ -14,6 +14,7 @@
 2 #include "xs_socket.h"
 3 #include "xs_unix_socket.h"
 4 #include "xs_url.h"
 5+#include "xs_http.h"
 6 #include "xs_httpd.h"
 7 #include "xs_mime.h"
 8 #include "xs_regex.h"
 9@@ -58,13 +59,6 @@ int mkdirx(const char *pathname)
10 }
11 
12 
13-int valid_status(int status)
14-/* is this HTTP status valid? */
15-{
16-    return status >= 200 && status <= 299;
17-}
18-
19-
20 xs_str *tid(int offset)
21 /* returns a time-based Id */
22 {
23@@ -176,16 +170,3 @@ int check_password(const char *uid, const char *passwd, const char *hash)
24 
25     return ret;
26 }
27-
28-
29-const char *http_status_text(int status)
30-/* translate status codes to canonical status texts */
31-{
32-    switch (status) {
33-        case 599: return "Timeout";
34-#define HTTP_STATUS(code, name, text) case HTTP_STATUS_ ## name: return #text;
35-#include "http_codes.h"
36-#undef HTTP_STATUS
37-        default: return "Unknown";
38-    }
39-}
M snac.h
+1, -9
 1@@ -44,7 +44,7 @@ extern int dbglevel;
 2 
 3 int mkdirx(const char *pathname);
 4 
 5-int valid_status(int status);
 6+#define valid_status(status) xs_http_valid_status(status)
 7 xs_str *tid(int offset);
 8 double ftime(void);
 9 
10@@ -464,14 +464,6 @@ void import_list_csv(snac *user, const char *fn);
11 void import_csv(snac *user);
12 int parse_port(const char *url, const char **errstr);
13 
14-typedef enum {
15-#define HTTP_STATUS(code, name, text) HTTP_STATUS_ ## name = code,
16-#include "http_codes.h"
17-#undef HTTP_STATUS
18-} http_status;
19-
20-const char *http_status_text(int status);
21-
22 typedef struct {
23     double timestamp;
24     char   *text;
+1, -0
1@@ -10,6 +10,7 @@
2 #include "xs_glob.h"
3 #include "xs_curl.h"
4 #include "xs_regex.h"
5+#include "xs_http.h"
6 
7 #include "snac.h"
8 
+1, -0
1@@ -5,6 +5,7 @@
2 #include "xs_json.h"
3 #include "xs_curl.h"
4 #include "xs_mime.h"
5+#include "xs_http.h"
6 
7 #include "snac.h"
8 
+42, -0
 1@@ -0,0 +1,42 @@
 2+/* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
 3+
 4+#ifndef _XS_HTTP_H
 5+
 6+#define _XS_HTTP_H
 7+
 8+typedef enum {
 9+#define HTTP_STATUS(code, name, text) HTTP_STATUS_ ## name = code,
10+#include "xs_http_codes.h"
11+#undef HTTP_STATUS
12+} http_status;
13+
14+
15+int xs_http_valid_status(int status);
16+const char *xs_http_status_text(int status);
17+
18+
19+#ifdef XS_IMPLEMENTATION
20+
21+int xs_http_valid_status(int status)
22+/* is this HTTP status valid? */
23+{
24+    return status >= 200 && status <= 299;
25+}
26+
27+
28+const char *xs_http_status_text(int status)
29+/* translate status codes to canonical status texts */
30+{
31+    switch (status) {
32+        case 599: return "Timeout";
33+#define HTTP_STATUS(code, name, text) case HTTP_STATUS_ ## name: return #text;
34+#include "xs_http_codes.h"
35+#undef HTTP_STATUS
36+        default: return "Unknown";
37+    }
38+}
39+
40+
41+#endif /* XS_IMPLEMENTATION */
42+
43+#endif /* XS_HTTP_H */
R http_codes.h => xs_http_codes.h
+0, -0