Browse Source

xs_http.h: new file.

grunfink 11 tháng trước cách đây
mục cha
commit
212284c792
16 tập tin đã thay đổi với 94 bổ sung67 xóa
  1. 19 18
      Makefile
  2. 19 18
      Makefile.NetBSD
  3. 2 1
      activitypub.c
  4. 1 0
      data.c
  5. 1 0
      html.c
  6. 1 0
      http.c
  7. 2 1
      httpd.c
  8. 1 0
      main.c
  9. 1 0
      mastoapi.c
  10. 1 0
      rss.c
  11. 1 20
      snac.c
  12. 1 9
      snac.h
  13. 1 0
      utils.c
  14. 1 0
      webfinger.c
  15. 42 0
      xs_http.h
  16. 0 0
      xs_http_codes.h

+ 19 - 18
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

+ 19 - 18
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

+ 2 - 1
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));
 }
 
 

+ 1 - 0
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"
 

+ 1 - 0
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"
 

+ 1 - 0
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"
 

+ 2 - 1
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);
 

+ 1 - 0
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"
 

+ 1 - 0
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"
 

+ 1 - 0
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"
 

+ 1 - 20
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";
-    }
-}

+ 1 - 9
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;

+ 1 - 0
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"
 

+ 1 - 0
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"
 

+ 42 - 0
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 */

+ 0 - 0
http_codes.h → xs_http_codes.h