httpd.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /* susie.png */
  11. const char *susie =
  12. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  13. "CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
  14. "vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
  15. "eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
  16. "AYTtEsDU9F34AAAAAElFTkSuQmCC";
  17. int server_get_handler(d_char *req, char *q_path,
  18. char **body, int *b_size, char **ctype)
  19. /* basic server services */
  20. {
  21. int status = 0;
  22. char *req_hdrs = xs_dict_get(req, "headers");
  23. char *acpt = xs_dict_get(req_hdrs, "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. xs *os = s;
  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(os, "%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. return status;
  67. }
  68. void httpd_connection(int rs)
  69. /* the connection loop */
  70. {
  71. FILE *f;
  72. xs *req;
  73. char *req_hdrs;
  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;
  83. char *p;
  84. f = xs_socket_accept(rs);
  85. req = xs_httpd_request(f, &payload, &p_size);
  86. req_hdrs = xs_dict_get(req, "headers");
  87. method = xs_dict_get(req_hdrs, "method");
  88. q_path = xs_dup(xs_dict_get(req_hdrs, "path"));
  89. /* crop the q_path from leading / and the prefix */
  90. if (xs_endswith(q_path, "/"))
  91. q_path = xs_crop(q_path, 0, -1);
  92. p = xs_dict_get(srv_config, "prefix");
  93. if (xs_startswith(q_path, p))
  94. q_path = xs_crop(q_path, strlen(p), 0);
  95. if (strcmp(method, "GET") == 0) {
  96. /* cascade through */
  97. if (status == 0)
  98. status = server_get_handler(req, q_path, &body, &b_size, &ctype);
  99. if (status == 0)
  100. status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
  101. if (status == 0)
  102. status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
  103. }
  104. else
  105. if (strcmp(method, "POST") == 0) {
  106. if (status == 0)
  107. status = activitypub_post_handler(req, q_path,
  108. payload, p_size, &body, &b_size, &ctype);
  109. }
  110. /* let's go */
  111. headers = xs_dict_new();
  112. /* unattended? it's an error */
  113. if (status == 0)
  114. status = 404;
  115. if (status == 404)
  116. body = xs_str_new("<h1>404 Not Found</h1>");
  117. if (status == 400)
  118. body = xs_str_new("<h1>400 Bad Request</h1>");
  119. if (status == 303)
  120. headers = xs_dict_append(headers, "location", body);
  121. if (status == 401)
  122. headers = xs_dict_append(headers, "WWW-Authenticate", "Basic realm=\"IDENTIFY\"");
  123. if (ctype == NULL)
  124. ctype = "text/html; charset=utf-8";
  125. headers = xs_dict_append(headers, "content-type", ctype);
  126. headers = xs_dict_append(headers, "x-creator", "snac/2.x");
  127. if (b_size == 0 && body != NULL)
  128. b_size = strlen(body);
  129. xs_httpd_response(f, status, headers, body, b_size);
  130. fclose(f);
  131. /* obsessive archiving */
  132. {
  133. xs *date = xs_local_time("%Y%m%d%H%M%S");
  134. xs *dir = xs_fmt("%s/archive/%s", srv_basedir, date);
  135. if (mkdir(dir, 0755) != -1) {
  136. xs *meta_fn = xs_fmt("%s/meta", dir);
  137. xs *payload_fn = xs_fmt("%s/payload", dir);
  138. xs *body_fn = xs_fmt("%s/body", dir);
  139. if ((f = fopen(meta_fn, "w")) != NULL) {
  140. xs *j1 = xs_json_dumps_pp(req, 4);
  141. xs *j2 = xs_json_dumps_pp(headers, 4);
  142. fprintf(f, "req: %s\n", j1);
  143. fprintf(f, "p_size: %d\n", p_size);
  144. fprintf(f, "status: %d\n", status);
  145. fprintf(f, "response: %s\n", j2);
  146. fprintf(f, "b_size: %d\n", b_size);
  147. fclose(f);
  148. }
  149. if (p_size && payload && (f = fopen(payload_fn, "w")) != NULL) {
  150. fwrite(payload, p_size, 1, f);
  151. fclose(f);
  152. }
  153. if (b_size && body && (f = fopen(body_fn, "w")) != NULL) {
  154. fwrite(body, p_size, 1, f);
  155. fclose(f);
  156. }
  157. }
  158. }
  159. free(body);
  160. }
  161. void httpd(void)
  162. /* starts the server */
  163. {
  164. char *address;
  165. int port;
  166. int rs;
  167. address = xs_dict_get(srv_config, "address");
  168. port = xs_number_get(xs_dict_get(srv_config, "port"));
  169. if ((rs = xs_socket_server(address, port)) == -1) {
  170. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  171. return;
  172. }
  173. srv_running = 1;
  174. srv_log(xs_fmt("httpd start %s:%d", address, port));
  175. for (;;) {
  176. httpd_connection(rs);
  177. }
  178. srv_log(xs_fmt("httpd stop %s:%d", address, port));
  179. }