httpd.c 4.3 KB

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