diff options
| -rw-r--r-- | httpd.c | 52 |
1 files changed, 52 insertions, 0 deletions
| @@ -0,0 +1,52 @@ | |||
| 1 | /* snac - A simple, minimalistic ActivityPub instance */ | ||
| 2 | /* copyright (c) 2022 grunfink - MIT license */ | ||
| 3 | |||
| 4 | #include "xs.h" | ||
| 5 | #include "xs_io.h" | ||
| 6 | #include "xs_encdec.h" | ||
| 7 | #include "xs_json.h" | ||
| 8 | #include "xs_curl.h" | ||
| 9 | #include "xs_openssl.h" | ||
| 10 | #include "xs_socket.h" | ||
| 11 | #include "xs_httpd.h" | ||
| 12 | |||
| 13 | #include "snac.h" | ||
| 14 | |||
| 15 | |||
| 16 | void httpd_connection(int rs) | ||
| 17 | /* the connection loop */ | ||
| 18 | { | ||
| 19 | FILE *f; | ||
| 20 | xs *req; | ||
| 21 | |||
| 22 | f = xs_socket_accept(rs); | ||
| 23 | |||
| 24 | req = xs_httpd_request(f); | ||
| 25 | |||
| 26 | fclose(f); | ||
| 27 | } | ||
| 28 | |||
| 29 | |||
| 30 | void httpd(void) | ||
| 31 | /* starts the server */ | ||
| 32 | { | ||
| 33 | char *address; | ||
| 34 | int port; | ||
| 35 | int rs; | ||
| 36 | |||
| 37 | address = xs_dict_get(srv_config, "address"); | ||
| 38 | port = xs_number_get(xs_dict_get(srv_config, "port")); | ||
| 39 | |||
| 40 | if ((rs = xs_socket_server(address, port)) == -1) { | ||
| 41 | srv_log(xs_fmt("cannot bind socket to %s:%d", address, port)); | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | |||
| 45 | srv_log(xs_fmt("httpd start %s:%d", address, port)); | ||
| 46 | |||
| 47 | for (;;) { | ||
| 48 | httpd_connection(rs); | ||
| 49 | } | ||
| 50 | |||
| 51 | srv_log(xs_fmt("httpd stop %s:%d", address, port)); | ||
| 52 | } | ||