httpd.c 7.2 KB

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