httpd.c 4.0 KB

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