httpd.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 *acpt = xs_dict_get(req, "accept");
  23. if (acpt == NULL)
  24. return 400;
  25. /* is it the server root? */
  26. if (*q_path == '\0') {
  27. /* try to open greeting.html */
  28. xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
  29. FILE *f;
  30. if ((f = fopen(fn, "r")) != NULL) {
  31. d_char *s = xs_readall(f);
  32. fclose(f);
  33. status = 200;
  34. /* does it have a %userlist% mark? */
  35. if (xs_str_in(s, "%userlist%") != -1) {
  36. char *host = xs_dict_get(srv_config, "host");
  37. xs *list = user_list();
  38. char *p, *uid;
  39. xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n");
  40. xs *os = s;
  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(os, "%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. return status;
  66. }
  67. void httpd_connection(int rs)
  68. /* the connection loop */
  69. {
  70. FILE *f;
  71. xs *req;
  72. char *method;
  73. int status = 0;
  74. char *body = NULL;
  75. int b_size = 0;
  76. char *ctype = NULL;
  77. xs *headers = NULL;
  78. xs *q_path = NULL;
  79. xs *payload = NULL;
  80. int p_size;
  81. char *p;
  82. f = xs_socket_accept(rs);
  83. req = xs_httpd_request(f, &payload, &p_size);
  84. method = xs_dict_get(req, "method");
  85. q_path = xs_dup(xs_dict_get(req, "path"));
  86. /* crop the q_path from leading / and the prefix */
  87. if (xs_endswith(q_path, "/"))
  88. q_path = xs_crop(q_path, 0, -1);
  89. p = xs_dict_get(srv_config, "prefix");
  90. if (xs_startswith(q_path, p))
  91. q_path = xs_crop(q_path, strlen(p), 0);
  92. if (strcmp(method, "GET") == 0) {
  93. /* cascade through */
  94. if (status == 0)
  95. status = server_get_handler(req, q_path, &body, &b_size, &ctype);
  96. if (status == 0)
  97. status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
  98. if (status == 0)
  99. status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
  100. }
  101. else
  102. if (strcmp(method, "POST") == 0) {
  103. if (status == 0)
  104. status = activitypub_post_handler(req, q_path,
  105. payload, p_size, &body, &b_size, &ctype);
  106. }
  107. /* let's go */
  108. headers = xs_dict_new();
  109. /* unattended? it's an error */
  110. if (status == 0)
  111. status = 404;
  112. if (status == 404)
  113. body = xs_str_new("<h1>404 Not Found</h1>");
  114. if (status == 400)
  115. body = xs_str_new("<h1>400 Bad Request</h1>");
  116. if (status == 303)
  117. headers = xs_dict_append(headers, "location", body);
  118. if (status == 401)
  119. headers = xs_dict_append(headers, "WWW-Authenticate", "Basic realm=\"IDENTIFY\"");
  120. if (ctype == NULL)
  121. ctype = "text/html; charset=utf-8";
  122. headers = xs_dict_append(headers, "content-type", ctype);
  123. headers = xs_dict_append(headers, "x-creator", "snac/2.x");
  124. if (b_size == 0 && body != NULL)
  125. b_size = strlen(body);
  126. xs_httpd_response(f, status, headers, body, b_size);
  127. fclose(f);
  128. srv_archive("RECV", req, payload, p_size, status, headers, body, b_size);
  129. free(body);
  130. }
  131. void httpd(void)
  132. /* starts the server */
  133. {
  134. char *address;
  135. int port;
  136. int rs;
  137. address = xs_dict_get(srv_config, "address");
  138. port = xs_number_get(xs_dict_get(srv_config, "port"));
  139. if ((rs = xs_socket_server(address, port)) == -1) {
  140. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  141. return;
  142. }
  143. srv_running = 1;
  144. srv_log(xs_fmt("httpd start %s:%d", address, port));
  145. for (;;) {
  146. httpd_connection(rs);
  147. }
  148. srv_log(xs_fmt("httpd stop %s:%d", address, port));
  149. }