httpd.c 9.4 KB

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