httpd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. void server_get_handler(d_char *req, char *q_path, int *status,
  11. char **body, int *b_size, char **ctype)
  12. /* basic server services */
  13. {
  14. char *req_hdrs = xs_dict_get(req, "headers");
  15. char *acpt = xs_dict_get(req_hdrs, "accept");
  16. if (acpt == NULL) {
  17. *status = 400;
  18. return;
  19. }
  20. /* is it the server root? */
  21. if (*q_path == '\0') {
  22. /* try to open greeting.html */
  23. xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
  24. FILE *f;
  25. if ((f = fopen(fn, "r")) != NULL) {
  26. d_char *s = xs_readall(f);
  27. fclose(f);
  28. *status = 200;
  29. /* does it have a %userlist% mark? */
  30. if (xs_str_in(s, "%userlist%") != -1) {
  31. char *host = xs_dict_get(srv_config, "host");
  32. xs *list = user_list();
  33. char *p, *uid;
  34. xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n");
  35. xs *os = s;
  36. p = list;
  37. while (xs_list_iter(&p, &uid)) {
  38. snac snac;
  39. if (user_open(&snac, uid)) {
  40. xs *u = xs_fmt(
  41. "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n",
  42. snac.actor, uid, host,
  43. xs_dict_get(snac.config, "name"));
  44. ul = xs_str_cat(ul, u);
  45. user_free(&snac);
  46. }
  47. }
  48. ul = xs_str_cat(ul, "</ul>\n");
  49. s = xs_replace(os, "%userlist%", ul);
  50. }
  51. *body = s;
  52. }
  53. }
  54. }
  55. void httpd_connection(int rs)
  56. /* the connection loop */
  57. {
  58. FILE *f;
  59. xs *req;
  60. char *req_hdrs;
  61. char *method;
  62. int status = 0;
  63. char *body = NULL;
  64. int b_size = 0;
  65. char *ctype = NULL;
  66. xs *headers = NULL;
  67. xs *q_path = NULL;
  68. char *p;
  69. f = xs_socket_accept(rs);
  70. req = xs_httpd_request(f);
  71. {
  72. xs *j = xs_json_dumps_pp(req, 4);
  73. printf("%s\n", j);
  74. }
  75. req_hdrs = xs_dict_get(req, "headers");
  76. method = xs_dict_get(req_hdrs, "method");
  77. q_path = xs_dup(xs_dict_get(req_hdrs, "path"));
  78. /* crop the q_path from leading / and the prefix */
  79. if (xs_endswith(q_path, "/"))
  80. q_path = xs_crop(q_path, 0, -1);
  81. p = xs_dict_get(srv_config, "prefix");
  82. if (xs_startswith(q_path, p))
  83. q_path = xs_crop(q_path, strlen(p), 0);
  84. if (strcmp(method, "GET") == 0) {
  85. /* cascade through */
  86. if (status == 0)
  87. server_get_handler(req, q_path, &status, &body, &b_size, &ctype);
  88. if (status == 0)
  89. webfinger_get_handler(req, q_path, &status, &body, &b_size, &ctype);
  90. }
  91. else
  92. if (strcmp(method, "POST") == 0) {
  93. }
  94. /* let's go */
  95. headers = xs_dict_new();
  96. /* unattended? it's an error */
  97. if (status == 0)
  98. status = 404;
  99. if (status == 404)
  100. body = xs_str_new("<h1>404 Not Found</h1>");
  101. if (status == 400)
  102. body = xs_str_new("<h1>400 Bad Request</h1>");
  103. if (status == 303)
  104. headers = xs_dict_append(headers, "location", body);
  105. if (status == 401)
  106. headers = xs_dict_append(headers, "WWW-Authenticate", "Basic realm=\"IDENTIFY\"");
  107. if (ctype == NULL)
  108. ctype = "text/html; charset=utf-8";
  109. headers = xs_dict_append(headers, "content-type", ctype);
  110. headers = xs_dict_append(headers, "x-creator", "snac/2.x");
  111. if (b_size == 0 && body != NULL)
  112. b_size = strlen(body);
  113. xs_httpd_response(f, status, headers, body, b_size);
  114. fclose(f);
  115. free(body);
  116. }
  117. void httpd(void)
  118. /* starts the server */
  119. {
  120. char *address;
  121. int port;
  122. int rs;
  123. address = xs_dict_get(srv_config, "address");
  124. port = xs_number_get(xs_dict_get(srv_config, "port"));
  125. if ((rs = xs_socket_server(address, port)) == -1) {
  126. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  127. return;
  128. }
  129. srv_running = 1;
  130. srv_log(xs_fmt("httpd start %s:%d", address, port));
  131. for (;;) {
  132. httpd_connection(rs);
  133. }
  134. srv_log(xs_fmt("httpd stop %s:%d", address, port));
  135. }