httpd.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. p = list;
  41. while (xs_list_iter(&p, &uid)) {
  42. snac snac;
  43. if (user_open(&snac, uid)) {
  44. xs *u = xs_fmt(
  45. "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n",
  46. snac.actor, uid, host,
  47. xs_dict_get(snac.config, "name"));
  48. ul = xs_str_cat(ul, u);
  49. user_free(&snac);
  50. }
  51. }
  52. ul = xs_str_cat(ul, "</ul>\n");
  53. s = xs_replace_i(s, "%userlist%", ul);
  54. }
  55. *body = s;
  56. }
  57. }
  58. else
  59. if (strcmp(q_path, "/susie.png") == 0) {
  60. status = 200;
  61. *body = xs_base64_dec(susie, b_size);
  62. *ctype = "image/png";
  63. }
  64. if (status != 0)
  65. srv_debug(1, xs_fmt("server_get_handler '%s' %d", q_path, status));
  66. return status;
  67. }
  68. void httpd_connection(int rs)
  69. /* the connection loop */
  70. {
  71. FILE *f;
  72. xs *req;
  73. char *method;
  74. int status = 0;
  75. char *body = NULL;
  76. int b_size = 0;
  77. char *ctype = NULL;
  78. xs *headers = NULL;
  79. xs *q_path = NULL;
  80. xs *payload = NULL;
  81. int p_size = 0;
  82. char *p;
  83. f = xs_socket_accept(rs);
  84. req = xs_httpd_request(f, &payload, &p_size);
  85. method = xs_dict_get(req, "method");
  86. q_path = xs_dup(xs_dict_get(req, "path"));
  87. /* crop the q_path from leading / and the prefix */
  88. if (xs_endswith(q_path, "/"))
  89. q_path = xs_crop(q_path, 0, -1);
  90. p = xs_dict_get(srv_config, "prefix");
  91. if (xs_startswith(q_path, p))
  92. q_path = xs_crop(q_path, strlen(p), 0);
  93. if (strcmp(method, "GET") == 0) {
  94. /* cascade through */
  95. if (status == 0)
  96. status = server_get_handler(req, q_path, &body, &b_size, &ctype);
  97. if (status == 0)
  98. status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
  99. if (status == 0)
  100. status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
  101. }
  102. else
  103. if (strcmp(method, "POST") == 0) {
  104. if (status == 0)
  105. status = activitypub_post_handler(req, q_path,
  106. payload, p_size, &body, &b_size, &ctype);
  107. }
  108. /* let's go */
  109. headers = xs_dict_new();
  110. /* unattended? it's an error */
  111. if (status == 0)
  112. status = 404;
  113. if (status == 404)
  114. body = xs_str_new("<h1>404 Not Found</h1>");
  115. if (status == 400)
  116. body = xs_str_new("<h1>400 Bad Request</h1>");
  117. if (status == 303)
  118. headers = xs_dict_append(headers, "location", body);
  119. if (status == 401)
  120. headers = xs_dict_append(headers, "WWW-Authenticate", "Basic realm=\"IDENTIFY\"");
  121. if (ctype == NULL)
  122. ctype = "text/html; charset=utf-8";
  123. headers = xs_dict_append(headers, "content-type", ctype);
  124. headers = xs_dict_append(headers, "x-creator", USER_AGENT);
  125. if (b_size == 0 && body != NULL)
  126. b_size = strlen(body);
  127. xs_httpd_response(f, status, headers, body, b_size);
  128. fclose(f);
  129. srv_archive("RECV", req, payload, p_size, status, headers, body, b_size);
  130. free(body);
  131. }
  132. void httpd(void)
  133. /* starts the server */
  134. {
  135. char *address;
  136. int port;
  137. int rs;
  138. address = xs_dict_get(srv_config, "address");
  139. port = xs_number_get(xs_dict_get(srv_config, "port"));
  140. if ((rs = xs_socket_server(address, port)) == -1) {
  141. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  142. return;
  143. }
  144. srv_running = 1;
  145. srv_log(xs_fmt("httpd start %s:%d", address, port));
  146. for (;;) {
  147. httpd_connection(rs);
  148. }
  149. srv_log(xs_fmt("httpd stop %s:%d", address, port));
  150. }