httpd.c 8.0 KB

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