httpd.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /* susie.png */
  12. const char *susie =
  13. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  14. "CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
  15. "vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
  16. "eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
  17. "AYTtEsDU9F34AAAAAElFTkSuQmCC";
  18. int server_get_handler(d_char *req, char *q_path,
  19. char **body, int *b_size, char **ctype)
  20. /* basic server services */
  21. {
  22. int status = 0;
  23. char *acpt = xs_dict_get(req, "accept");
  24. if (acpt == NULL)
  25. return 400;
  26. /* is it the server root? */
  27. if (*q_path == '\0') {
  28. /* try to open greeting.html */
  29. xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
  30. FILE *f;
  31. if ((f = fopen(fn, "r")) != NULL) {
  32. d_char *s = xs_readall(f);
  33. fclose(f);
  34. status = 200;
  35. /* does it have a %userlist% mark? */
  36. if (xs_str_in(s, "%userlist%") != -1) {
  37. char *host = xs_dict_get(srv_config, "host");
  38. xs *list = user_list();
  39. char *p, *uid;
  40. xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n");
  41. p = list;
  42. while (xs_list_iter(&p, &uid)) {
  43. snac snac;
  44. if (user_open(&snac, uid)) {
  45. xs *u = xs_fmt(
  46. "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n",
  47. snac.actor, uid, host,
  48. xs_dict_get(snac.config, "name"));
  49. ul = xs_str_cat(ul, u);
  50. user_free(&snac);
  51. }
  52. }
  53. ul = xs_str_cat(ul, "</ul>\n");
  54. s = xs_replace_i(s, "%userlist%", ul);
  55. }
  56. *body = s;
  57. }
  58. }
  59. else
  60. if (strcmp(q_path, "/susie.png") == 0) {
  61. status = 200;
  62. *body = xs_base64_dec(susie, b_size);
  63. *ctype = "image/png";
  64. }
  65. if (status != 0)
  66. srv_debug(1, xs_fmt("server_get_handler serving '%s' %d", q_path, status));
  67. return status;
  68. }
  69. void httpd_connection(int rs)
  70. /* the connection loop */
  71. {
  72. FILE *f;
  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. f = xs_socket_accept(rs);
  85. req = xs_httpd_request(f, &payload, &p_size);
  86. if (req == NULL) {
  87. /* probably because a timeout */
  88. fclose(f);
  89. return;
  90. }
  91. method = xs_dict_get(req, "method");
  92. q_path = xs_dup(xs_dict_get(req, "path"));
  93. /* crop the q_path from leading / and the prefix */
  94. if (xs_endswith(q_path, "/"))
  95. q_path = xs_crop(q_path, 0, -1);
  96. p = xs_dict_get(srv_config, "prefix");
  97. if (xs_startswith(q_path, p))
  98. q_path = xs_crop(q_path, strlen(p), 0);
  99. if (strcmp(method, "GET") == 0) {
  100. /* cascade through */
  101. if (status == 0)
  102. status = server_get_handler(req, q_path, &body, &b_size, &ctype);
  103. if (status == 0)
  104. status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
  105. if (status == 0)
  106. status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
  107. if (status == 0)
  108. status = html_get_handler(req, q_path, &body, &b_size, &ctype);
  109. }
  110. else
  111. if (strcmp(method, "POST") == 0) {
  112. if (status == 0)
  113. status = activitypub_post_handler(req, q_path,
  114. payload, p_size, &body, &b_size, &ctype);
  115. if (status == 0)
  116. status = html_post_handler(req, q_path,
  117. payload, p_size, &body, &b_size, &ctype);
  118. }
  119. /* let's go */
  120. headers = xs_dict_new();
  121. /* unattended? it's an error */
  122. if (status == 0) {
  123. srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
  124. status = 404;
  125. }
  126. if (status == 404)
  127. body = xs_str_new("<h1>404 Not Found</h1>");
  128. if (status == 400)
  129. body = xs_str_new("<h1>400 Bad Request</h1>");
  130. if (status == 303)
  131. headers = xs_dict_append(headers, "location", body);
  132. if (status == 401)
  133. headers = xs_dict_append(headers, "WWW-Authenticate", "Basic realm=\"IDENTIFY\"");
  134. if (ctype == NULL)
  135. ctype = "text/html; charset=utf-8";
  136. headers = xs_dict_append(headers, "content-type", ctype);
  137. headers = xs_dict_append(headers, "x-creator", USER_AGENT);
  138. if (b_size == 0 && body != NULL)
  139. b_size = strlen(body);
  140. xs_httpd_response(f, status, headers, body, b_size);
  141. fclose(f);
  142. srv_archive("RECV", req, payload, p_size, status, headers, body, b_size);
  143. free(body);
  144. }
  145. static jmp_buf on_break;
  146. void term_handler(int s)
  147. {
  148. longjmp(on_break, 1);
  149. }
  150. void httpd(void)
  151. /* starts the server */
  152. {
  153. char *address;
  154. int port;
  155. int rs;
  156. address = xs_dict_get(srv_config, "address");
  157. port = xs_number_get(xs_dict_get(srv_config, "port"));
  158. if ((rs = xs_socket_server(address, port)) == -1) {
  159. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  160. return;
  161. }
  162. srv_running = 1;
  163. signal(SIGPIPE, SIG_IGN);
  164. signal(SIGTERM, term_handler);
  165. signal(SIGINT, term_handler);
  166. srv_log(xs_fmt("httpd start %s:%d", address, port));
  167. if (setjmp(on_break) == 0) {
  168. for (;;) {
  169. httpd_connection(rs);
  170. }
  171. }
  172. srv_running = 0;
  173. /* wait for the helper thread to end */
  174. /* ... */
  175. srv_log(xs_fmt("httpd stop %s:%d", address, port));
  176. }