httpd.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. /* 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(int rs)
  71. /* the connection loop */
  72. {
  73. FILE *f;
  74. xs *req;
  75. char *method;
  76. int status = 0;
  77. char *body = NULL;
  78. int b_size = 0;
  79. char *ctype = NULL;
  80. xs *headers = NULL;
  81. xs *q_path = NULL;
  82. xs *payload = NULL;
  83. int p_size = 0;
  84. char *p;
  85. f = xs_socket_accept(rs);
  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 *helper_thread(void *arg)
  152. /* helper thread (queue management) */
  153. {
  154. srv_log(xs_fmt("subthread start"));
  155. while (srv_running) {
  156. xs *list = user_list();
  157. char *p, *uid;
  158. p = list;
  159. while (xs_list_iter(&p, &uid)) {
  160. snac snac;
  161. if (user_open(&snac, uid)) {
  162. process_queue(&snac);
  163. user_free(&snac);
  164. }
  165. }
  166. sleep(3);
  167. }
  168. srv_log(xs_fmt("subthread stop"));
  169. return NULL;
  170. }
  171. void httpd(void)
  172. /* starts the server */
  173. {
  174. char *address;
  175. int port;
  176. int rs;
  177. pthread_t htid;
  178. address = xs_dict_get(srv_config, "address");
  179. port = xs_number_get(xs_dict_get(srv_config, "port"));
  180. if ((rs = xs_socket_server(address, port)) == -1) {
  181. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  182. return;
  183. }
  184. srv_running = 1;
  185. signal(SIGPIPE, SIG_IGN);
  186. signal(SIGTERM, term_handler);
  187. signal(SIGINT, term_handler);
  188. srv_log(xs_fmt("httpd start %s:%d", address, port));
  189. pthread_create(&htid, NULL, helper_thread, NULL);
  190. if (setjmp(on_break) == 0) {
  191. for (;;) {
  192. httpd_connection(rs);
  193. }
  194. }
  195. srv_running = 0;
  196. /* wait for the helper thread to end */
  197. pthread_join(htid, NULL);
  198. srv_log(xs_fmt("httpd stop %s:%d", address, port));
  199. }