httpd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_encdec.h"
  6. #include "xs_json.h"
  7. #include "xs_socket.h"
  8. #include "xs_httpd.h"
  9. #include "xs_mime.h"
  10. #include "snac.h"
  11. #include <setjmp.h>
  12. #include <pthread.h>
  13. /* susie.png */
  14. const char *susie =
  15. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  16. "CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
  17. "vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
  18. "eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
  19. "AYTtEsDU9F34AAAAAElFTkSuQmCC";
  20. int server_get_handler(d_char *req, char *q_path,
  21. char **body, int *b_size, char **ctype)
  22. /* basic server services */
  23. {
  24. int status = 0;
  25. char *acpt = xs_dict_get(req, "accept");
  26. if (acpt == NULL)
  27. return 0;
  28. /* is it the server root? */
  29. if (*q_path == '\0') {
  30. /* try to open greeting.html */
  31. xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
  32. FILE *f;
  33. if ((f = fopen(fn, "r")) != NULL) {
  34. d_char *s = xs_readall(f);
  35. fclose(f);
  36. status = 200;
  37. /* replace %host% */
  38. s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host"));
  39. /* does it have a %userlist% mark? */
  40. if (xs_str_in(s, "%userlist%") != -1) {
  41. char *host = xs_dict_get(srv_config, "host");
  42. xs *list = user_list();
  43. char *p, *uid;
  44. xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n");
  45. p = list;
  46. while (xs_list_iter(&p, &uid)) {
  47. snac snac;
  48. if (user_open(&snac, uid)) {
  49. xs *u = xs_fmt(
  50. "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n",
  51. snac.actor, uid, host,
  52. xs_dict_get(snac.config, "name"));
  53. ul = xs_str_cat(ul, u);
  54. user_free(&snac);
  55. }
  56. }
  57. ul = xs_str_cat(ul, "</ul>\n");
  58. s = xs_replace_i(s, "%userlist%", ul);
  59. }
  60. *body = s;
  61. }
  62. }
  63. else
  64. if (strcmp(q_path, "/susie.png") == 0) {
  65. status = 200;
  66. *body = xs_base64_dec(susie, b_size);
  67. *ctype = "image/png";
  68. }
  69. if (status != 0)
  70. srv_debug(1, xs_fmt("server_get_handler serving '%s' %d", q_path, status));
  71. return status;
  72. }
  73. void httpd_connection(FILE *f)
  74. /* the connection processor */
  75. {
  76. xs *req;
  77. char *method;
  78. int status = 0;
  79. char *body = NULL;
  80. int b_size = 0;
  81. char *ctype = NULL;
  82. xs *headers = NULL;
  83. xs *q_path = NULL;
  84. xs *payload = NULL;
  85. int p_size = 0;
  86. char *p;
  87. req = xs_httpd_request(f, &payload, &p_size);
  88. if (req == NULL) {
  89. /* probably because a timeout */
  90. fclose(f);
  91. return;
  92. }
  93. method = xs_dict_get(req, "method");
  94. q_path = xs_dup(xs_dict_get(req, "path"));
  95. /* crop the q_path from leading / and the prefix */
  96. if (xs_endswith(q_path, "/"))
  97. q_path = xs_crop(q_path, 0, -1);
  98. p = xs_dict_get(srv_config, "prefix");
  99. if (xs_startswith(q_path, p))
  100. q_path = xs_crop(q_path, strlen(p), 0);
  101. if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) {
  102. /* cascade through */
  103. if (status == 0)
  104. status = server_get_handler(req, q_path, &body, &b_size, &ctype);
  105. if (status == 0)
  106. status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
  107. if (status == 0)
  108. status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
  109. if (status == 0)
  110. status = html_get_handler(req, q_path, &body, &b_size, &ctype);
  111. }
  112. else
  113. if (strcmp(method, "POST") == 0) {
  114. if (status == 0)
  115. status = activitypub_post_handler(req, q_path,
  116. payload, p_size, &body, &b_size, &ctype);
  117. if (status == 0)
  118. status = html_post_handler(req, q_path,
  119. payload, p_size, &body, &b_size, &ctype);
  120. }
  121. /* let's go */
  122. headers = xs_dict_new();
  123. /* unattended? it's an error */
  124. if (status == 0) {
  125. srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
  126. status = 404;
  127. }
  128. if (status == 404)
  129. body = xs_str_new("<h1>404 Not Found</h1>");
  130. if (status == 400)
  131. body = xs_str_new("<h1>400 Bad Request</h1>");
  132. if (status == 303)
  133. headers = xs_dict_append(headers, "location", body);
  134. if (status == 401)
  135. headers = xs_dict_append(headers, "WWW-Authenticate", "Basic realm=\"IDENTIFY\"");
  136. if (ctype == NULL)
  137. ctype = "text/html; charset=utf-8";
  138. headers = xs_dict_append(headers, "content-type", ctype);
  139. headers = xs_dict_append(headers, "x-creator", USER_AGENT);
  140. if (b_size == 0 && body != NULL)
  141. b_size = strlen(body);
  142. /* if it was a HEAD, no body will be sent */
  143. if (strcmp(method, "HEAD") == 0) {
  144. free(body);
  145. body = NULL;
  146. }
  147. xs_httpd_response(f, status, headers, body, b_size);
  148. fclose(f);
  149. srv_archive("RECV", req, payload, p_size, status, headers, body, b_size);
  150. free(body);
  151. }
  152. static jmp_buf on_break;
  153. void term_handler(int s)
  154. {
  155. longjmp(on_break, 1);
  156. }
  157. static void *purge_thread(void *arg)
  158. /* spawned purge */
  159. {
  160. srv_log(xs_dup("purge start"));
  161. purge_all();
  162. srv_log(xs_dup("purge end"));
  163. return NULL;
  164. }
  165. static void *queue_thread(void *arg)
  166. /* queue thread (queue management) */
  167. {
  168. pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
  169. pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
  170. time_t purge_time;
  171. /* first purge time */
  172. purge_time = time(NULL) + 15 * 60;
  173. srv_log(xs_fmt("queue thread start"));
  174. while (srv_running) {
  175. xs *list = user_list();
  176. char *p, *uid;
  177. time_t t;
  178. /* process queues for all users */
  179. p = list;
  180. while (xs_list_iter(&p, &uid)) {
  181. snac snac;
  182. if (user_open(&snac, uid)) {
  183. process_queue(&snac);
  184. user_free(&snac);
  185. }
  186. }
  187. /* time to purge? */
  188. if ((t = time(NULL)) > purge_time) {
  189. pthread_t pth;
  190. pthread_create(&pth, NULL, purge_thread, NULL);
  191. /* next purge time is tomorrow */
  192. purge_time = t + 24 * 60 * 60;
  193. }
  194. /* sleep 3 seconds */
  195. struct timespec ts;
  196. clock_gettime(CLOCK_REALTIME, &ts);
  197. ts.tv_sec += 3;
  198. pthread_mutex_lock(&dummy_mutex);
  199. while (pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts) == 0);
  200. pthread_mutex_unlock(&dummy_mutex);
  201. }
  202. srv_log(xs_fmt("queue thread stop"));
  203. return NULL;
  204. }
  205. static void *connection_thread(void *arg)
  206. /* connection thread */
  207. {
  208. httpd_connection((FILE *)arg);
  209. return NULL;
  210. }
  211. int threaded_connections = 1;
  212. void httpd(void)
  213. /* starts the server */
  214. {
  215. char *address;
  216. int port;
  217. int rs;
  218. pthread_t htid;
  219. address = xs_dict_get(srv_config, "address");
  220. port = xs_number_get(xs_dict_get(srv_config, "port"));
  221. if ((rs = xs_socket_server(address, port)) == -1) {
  222. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  223. return;
  224. }
  225. srv_running = 1;
  226. signal(SIGPIPE, SIG_IGN);
  227. signal(SIGTERM, term_handler);
  228. signal(SIGINT, term_handler);
  229. srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT));
  230. pthread_create(&htid, NULL, queue_thread, NULL);
  231. if (setjmp(on_break) == 0) {
  232. for (;;) {
  233. FILE *f = xs_socket_accept(rs);
  234. if (threaded_connections) {
  235. pthread_t cth;
  236. pthread_create(&cth, NULL, connection_thread, f);
  237. }
  238. else
  239. httpd_connection(f);
  240. }
  241. }
  242. srv_running = 0;
  243. /* wait for the helper thread to end */
  244. pthread_join(htid, NULL);
  245. srv_log(xs_fmt("httpd stop %s:%d", address, port));
  246. }