httpd.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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(FILE *f)
  71. /* the connection processor */
  72. {
  73. xs *req;
  74. char *method;
  75. int status = 0;
  76. 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) {
  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. xs_httpd_response(f, status, headers, body, b_size);
  140. fclose(f);
  141. srv_archive("RECV", req, payload, p_size, status, headers, body, b_size);
  142. free(body);
  143. }
  144. static jmp_buf on_break;
  145. void term_handler(int s)
  146. {
  147. longjmp(on_break, 1);
  148. }
  149. static void *queue_thread(void *arg)
  150. /* queue thread (queue management) */
  151. {
  152. pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
  153. pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
  154. srv_log(xs_fmt("queue thread 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 seconds */
  167. struct timespec ts;
  168. clock_gettime(CLOCK_REALTIME, &ts);
  169. ts.tv_sec += 3;
  170. pthread_mutex_lock(&dummy_mutex);
  171. while (pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts) == 0);
  172. pthread_mutex_unlock(&dummy_mutex);
  173. }
  174. srv_log(xs_fmt("queue thread stop"));
  175. return NULL;
  176. }
  177. static void *connection_thread(void *arg)
  178. /* connection thread */
  179. {
  180. httpd_connection((FILE *)arg);
  181. return NULL;
  182. }
  183. int threaded_connections = 1;
  184. void httpd(void)
  185. /* starts the server */
  186. {
  187. char *address;
  188. int port;
  189. int rs;
  190. pthread_t htid;
  191. address = xs_dict_get(srv_config, "address");
  192. port = xs_number_get(xs_dict_get(srv_config, "port"));
  193. if ((rs = xs_socket_server(address, port)) == -1) {
  194. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  195. return;
  196. }
  197. srv_running = 1;
  198. signal(SIGPIPE, SIG_IGN);
  199. signal(SIGTERM, term_handler);
  200. signal(SIGINT, term_handler);
  201. srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT));
  202. pthread_create(&htid, NULL, queue_thread, NULL);
  203. if (setjmp(on_break) == 0) {
  204. for (;;) {
  205. FILE *f = xs_socket_accept(rs);
  206. if (threaded_connections) {
  207. pthread_t cth;
  208. pthread_create(&cth, NULL, connection_thread, f);
  209. }
  210. else
  211. httpd_connection(f);
  212. }
  213. }
  214. srv_running = 0;
  215. /* wait for the helper thread to end */
  216. pthread_join(htid, NULL);
  217. srv_log(xs_fmt("httpd stop %s:%d", address, port));
  218. }