httpd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 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 "xs_mime.h"
  10. #include "xs_time.h"
  11. #include "snac.h"
  12. #include <setjmp.h>
  13. #include <pthread.h>
  14. #include <semaphore.h>
  15. #include <fcntl.h>
  16. #include <stdint.h>
  17. #include <sys/resource.h> // for getrlimit()
  18. #ifdef USE_POLL_FOR_SLEEP
  19. #include <poll.h>
  20. #endif
  21. /* nodeinfo 2.0 template */
  22. const char *nodeinfo_2_0_template = ""
  23. "{\"version\":\"2.0\","
  24. "\"software\":{\"name\":\"snac\",\"version\":\"" VERSION "\"},"
  25. "\"protocols\":[\"activitypub\"],"
  26. "\"services\":{\"outbound\":[],\"inbound\":[]},"
  27. "\"usage\":{\"users\":{\"total\":%d,\"activeMonth\":%d,\"activeHalfyear\":%d},"
  28. "\"localPosts\":%d},"
  29. "\"openRegistrations\":false,\"metadata\":{}}";
  30. d_char *nodeinfo_2_0(void)
  31. /* builds a nodeinfo json object */
  32. {
  33. xs *users = user_list();
  34. int n_users = xs_list_len(users);
  35. int n_posts = 0; /* to be implemented someday */
  36. return xs_fmt(nodeinfo_2_0_template, n_users, n_users, n_users, n_posts);
  37. }
  38. int server_get_handler(xs_dict *req, char *q_path,
  39. char **body, int *b_size, char **ctype)
  40. /* basic server services */
  41. {
  42. int status = 0;
  43. (void)req;
  44. /* is it the server root? */
  45. if (*q_path == '\0') {
  46. /* try to open greeting.html */
  47. xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
  48. FILE *f;
  49. if ((f = fopen(fn, "r")) != NULL) {
  50. d_char *s = xs_readall(f);
  51. fclose(f);
  52. status = 200;
  53. /* replace %host% */
  54. s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host"));
  55. const char *adm_email = xs_dict_get(srv_config, "admin_email");
  56. if (xs_is_null(adm_email) || *adm_email == '\0')
  57. adm_email = "the administrator of this instance";
  58. /* replace %admin_email */
  59. s = xs_replace_i(s, "%admin_email%", adm_email);
  60. /* does it have a %userlist% mark? */
  61. if (xs_str_in(s, "%userlist%") != -1) {
  62. char *host = xs_dict_get(srv_config, "host");
  63. xs *list = user_list();
  64. char *p, *uid;
  65. xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n");
  66. p = list;
  67. while (xs_list_iter(&p, &uid)) {
  68. snac snac;
  69. if (user_open(&snac, uid)) {
  70. xs *u = xs_fmt(
  71. "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n",
  72. snac.actor, uid, host,
  73. xs_dict_get(snac.config, "name"));
  74. ul = xs_str_cat(ul, u);
  75. user_free(&snac);
  76. }
  77. }
  78. ul = xs_str_cat(ul, "</ul>\n");
  79. s = xs_replace_i(s, "%userlist%", ul);
  80. }
  81. *body = s;
  82. }
  83. }
  84. else
  85. if (strcmp(q_path, "/susie.png") == 0 || strcmp(q_path, "/favicon.ico") == 0 ) {
  86. status = 200;
  87. *body = xs_base64_dec(default_avatar_base64(), b_size);
  88. *ctype = "image/png";
  89. }
  90. else
  91. if (strcmp(q_path, "/.well-known/nodeinfo") == 0) {
  92. status = 200;
  93. *ctype = "application/json; charset=utf-8";
  94. *body = xs_fmt("{\"links\":["
  95. "{\"rel\":\"http:/" "/nodeinfo.diaspora.software/ns/schema/2.0\","
  96. "\"href\":\"%s/nodeinfo_2_0\"}]}",
  97. srv_baseurl);
  98. }
  99. else
  100. if (strcmp(q_path, "/nodeinfo_2_0") == 0) {
  101. status = 200;
  102. *ctype = "application/json; charset=utf-8";
  103. *body = nodeinfo_2_0();
  104. }
  105. else
  106. if (strcmp(q_path, "/robots.txt") == 0) {
  107. status = 200;
  108. *ctype = "text/plain";
  109. *body = xs_str_new("User-agent: *\n"
  110. "Disallow: /\n");
  111. }
  112. if (status != 0)
  113. srv_debug(1, xs_fmt("server_get_handler serving '%s' %d", q_path, status));
  114. return status;
  115. }
  116. void httpd_connection(FILE *f)
  117. /* the connection processor */
  118. {
  119. xs *req;
  120. char *method;
  121. int status = 0;
  122. d_char *body = NULL;
  123. int b_size = 0;
  124. char *ctype = NULL;
  125. xs *headers = NULL;
  126. xs *q_path = NULL;
  127. xs *payload = NULL;
  128. int p_size = 0;
  129. char *p;
  130. req = xs_httpd_request(f, &payload, &p_size);
  131. if (req == NULL) {
  132. /* probably because a timeout */
  133. fclose(f);
  134. return;
  135. }
  136. method = xs_dict_get(req, "method");
  137. q_path = xs_dup(xs_dict_get(req, "path"));
  138. /* crop the q_path from leading / and the prefix */
  139. if (xs_endswith(q_path, "/"))
  140. q_path = xs_crop_i(q_path, 0, -1);
  141. p = xs_dict_get(srv_config, "prefix");
  142. if (xs_startswith(q_path, p))
  143. q_path = xs_crop_i(q_path, strlen(p), 0);
  144. if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) {
  145. /* cascade through */
  146. if (status == 0)
  147. status = server_get_handler(req, q_path, &body, &b_size, &ctype);
  148. if (status == 0)
  149. status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
  150. if (status == 0)
  151. status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
  152. #ifndef NO_MASTODON_API
  153. if (status == 0)
  154. status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
  155. if (status == 0)
  156. status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype);
  157. #endif /* NO_MASTODON_API */
  158. if (status == 0)
  159. status = html_get_handler(req, q_path, &body, &b_size, &ctype);
  160. }
  161. else
  162. if (strcmp(method, "POST") == 0) {
  163. #ifndef NO_MASTODON_API
  164. if (status == 0)
  165. status = oauth_post_handler(req, q_path,
  166. payload, p_size, &body, &b_size, &ctype);
  167. if (status == 0)
  168. status = mastoapi_post_handler(req, q_path,
  169. payload, p_size, &body, &b_size, &ctype);
  170. #endif
  171. if (status == 0)
  172. status = activitypub_post_handler(req, q_path,
  173. payload, p_size, &body, &b_size, &ctype);
  174. if (status == 0)
  175. status = html_post_handler(req, q_path,
  176. payload, p_size, &body, &b_size, &ctype);
  177. }
  178. else
  179. if (strcmp(method, "PUT") == 0) {
  180. #ifndef NO_MASTODON_API
  181. if (status == 0)
  182. status = mastoapi_put_handler(req, q_path,
  183. payload, p_size, &body, &b_size, &ctype);
  184. #endif
  185. }
  186. /* let's go */
  187. headers = xs_dict_new();
  188. /* unattended? it's an error */
  189. if (status == 0) {
  190. srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
  191. status = 404;
  192. }
  193. if (status == 404)
  194. body = xs_str_new("<h1>404 Not Found</h1>");
  195. if (status == 400 && body != NULL)
  196. body = xs_str_new("<h1>400 Bad Request</h1>");
  197. if (status == 303)
  198. headers = xs_dict_append(headers, "location", body);
  199. if (status == 401)
  200. headers = xs_dict_append(headers, "WWW-Authenticate", "Basic realm=\"IDENTIFY\"");
  201. if (ctype == NULL)
  202. ctype = "text/html; charset=utf-8";
  203. headers = xs_dict_append(headers, "content-type", ctype);
  204. headers = xs_dict_append(headers, "x-creator", USER_AGENT);
  205. if (b_size == 0 && body != NULL)
  206. b_size = strlen(body);
  207. /* if it was a HEAD, no body will be sent */
  208. if (strcmp(method, "HEAD") == 0)
  209. body = xs_free(body);
  210. xs_httpd_response(f, status, headers, body, b_size);
  211. fclose(f);
  212. srv_archive("RECV", NULL, req, payload, p_size, status, headers, body, b_size);
  213. /* JSON validation check */
  214. if (strcmp(ctype, "application/json") == 0) {
  215. xs *j = xs_json_loads(body);
  216. if (j == NULL) {
  217. srv_log(xs_fmt("bad JSON"));
  218. srv_archive_error("bad_json", "bad JSON", req, body);
  219. }
  220. }
  221. xs_free(body);
  222. }
  223. static jmp_buf on_break;
  224. void term_handler(int s)
  225. {
  226. (void)s;
  227. longjmp(on_break, 1);
  228. }
  229. /** job control **/
  230. /* mutex to access the lists of jobs */
  231. static pthread_mutex_t job_mutex;
  232. /* semaphre to trigger job processing */
  233. static sem_t *job_sem;
  234. /* fifo of jobs */
  235. xs_list *job_fifo = NULL;
  236. int job_fifo_ready(void)
  237. /* returns true if the job fifo is ready */
  238. {
  239. return job_fifo != NULL;
  240. }
  241. void job_post(const xs_val *job, int urgent)
  242. /* posts a job for the threads to process it */
  243. {
  244. if (job != NULL) {
  245. /* lock the mutex */
  246. pthread_mutex_lock(&job_mutex);
  247. /* add to the fifo */
  248. if (job_fifo != NULL) {
  249. if (urgent)
  250. job_fifo = xs_list_insert(job_fifo, 0, job);
  251. else
  252. job_fifo = xs_list_append(job_fifo, job);
  253. }
  254. /* unlock the mutex */
  255. pthread_mutex_unlock(&job_mutex);
  256. }
  257. /* ask for someone to attend it */
  258. sem_post(job_sem);
  259. }
  260. void job_wait(xs_val **job)
  261. /* waits for an available job */
  262. {
  263. *job = NULL;
  264. if (sem_wait(job_sem) == 0) {
  265. /* lock the mutex */
  266. pthread_mutex_lock(&job_mutex);
  267. /* dequeue */
  268. if (job_fifo != NULL)
  269. job_fifo = xs_list_shift(job_fifo, job);
  270. /* unlock the mutex */
  271. pthread_mutex_unlock(&job_mutex);
  272. }
  273. }
  274. #ifndef MAX_THREADS
  275. #define MAX_THREADS 256
  276. #endif
  277. static void *job_thread(void *arg)
  278. /* job thread */
  279. {
  280. int pid = (int)(uintptr_t)arg;
  281. srv_debug(1, xs_fmt("job thread %d started", pid));
  282. for (;;) {
  283. xs *job = NULL;
  284. job_wait(&job);
  285. srv_debug(2, xs_fmt("job thread %d wake up", pid));
  286. if (job == NULL)
  287. break;
  288. if (xs_type(job) == XSTYPE_DATA) {
  289. /* it's a socket */
  290. FILE *f = NULL;
  291. xs_data_get(job, &f);
  292. if (f != NULL)
  293. httpd_connection(f);
  294. }
  295. else {
  296. /* it's a q_item */
  297. process_queue_item(job);
  298. }
  299. }
  300. srv_debug(1, xs_fmt("job thread %d stopped", pid));
  301. return NULL;
  302. }
  303. /* background thread sleep control */
  304. static pthread_mutex_t sleep_mutex;
  305. static pthread_cond_t sleep_cond;
  306. static void *background_thread(void *arg)
  307. /* background thread (queue management and other things) */
  308. {
  309. time_t purge_time;
  310. (void)arg;
  311. /* first purge time */
  312. purge_time = time(NULL) + 10 * 60;
  313. srv_log(xs_fmt("background thread started"));
  314. while (srv_running) {
  315. time_t t;
  316. int cnt = 0;
  317. {
  318. xs *list = user_list();
  319. char *p, *uid;
  320. /* process queues for all users */
  321. p = list;
  322. while (xs_list_iter(&p, &uid)) {
  323. snac snac;
  324. if (user_open(&snac, uid)) {
  325. cnt += process_user_queue(&snac);
  326. user_free(&snac);
  327. }
  328. }
  329. }
  330. /* global queue */
  331. cnt += process_queue();
  332. /* time to purge? */
  333. if ((t = time(NULL)) > purge_time) {
  334. /* next purge time is tomorrow */
  335. purge_time = t + 24 * 60 * 60;
  336. xs *q_item = xs_dict_new();
  337. q_item = xs_dict_append(q_item, "type", "purge");
  338. job_post(q_item, 0);
  339. }
  340. if (cnt == 0) {
  341. /* sleep 3 seconds */
  342. #ifdef USE_POLL_FOR_SLEEP
  343. poll(NULL, 0, 3 * 1000);
  344. #else
  345. struct timespec ts;
  346. clock_gettime(CLOCK_REALTIME, &ts);
  347. ts.tv_sec += 3;
  348. pthread_mutex_lock(&sleep_mutex);
  349. while (pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &ts) == 0);
  350. pthread_mutex_unlock(&sleep_mutex);
  351. #endif
  352. }
  353. }
  354. srv_log(xs_fmt("background thread stopped"));
  355. return NULL;
  356. }
  357. void httpd(void)
  358. /* starts the server */
  359. {
  360. char *address;
  361. int port;
  362. int rs;
  363. pthread_t threads[MAX_THREADS] = {0};
  364. int n_threads = 0;
  365. int n;
  366. time_t start_time = time(NULL);
  367. char sem_name[24];
  368. address = xs_dict_get(srv_config, "address");
  369. port = xs_number_get(xs_dict_get(srv_config, "port"));
  370. if ((rs = xs_socket_server(address, port)) == -1) {
  371. srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
  372. return;
  373. }
  374. srv_running = 1;
  375. signal(SIGPIPE, SIG_IGN);
  376. signal(SIGTERM, term_handler);
  377. signal(SIGINT, term_handler);
  378. srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT));
  379. /* show the number of usable file descriptors */
  380. struct rlimit r;
  381. getrlimit(RLIMIT_NOFILE, &r);
  382. srv_debug(0, xs_fmt("available (rlimit) fds: %d (cur) / %d (max)",
  383. (int) r.rlim_cur, (int) r.rlim_max));
  384. /* initialize the job control engine */
  385. pthread_mutex_init(&job_mutex, NULL);
  386. sprintf(sem_name, "/job_%d", getpid());
  387. job_sem = sem_open(sem_name, O_CREAT, 0644, 0);
  388. job_fifo = xs_list_new();
  389. /* initialize sleep control */
  390. pthread_mutex_init(&sleep_mutex, NULL);
  391. pthread_cond_init(&sleep_cond, NULL);
  392. n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
  393. #ifdef _SC_NPROCESSORS_ONLN
  394. if (n_threads == 0) {
  395. /* get number of CPUs on the machine */
  396. n_threads = sysconf(_SC_NPROCESSORS_ONLN);
  397. }
  398. #endif
  399. if (n_threads < 4)
  400. n_threads = 4;
  401. if (n_threads > MAX_THREADS)
  402. n_threads = MAX_THREADS;
  403. srv_debug(0, xs_fmt("using %d threads", n_threads));
  404. /* thread #0 is the background thread */
  405. pthread_create(&threads[0], NULL, background_thread, NULL);
  406. /* the rest of threads are for job processing */
  407. char *ptr = (char *) 0x1;
  408. for (n = 1; n < n_threads; n++)
  409. pthread_create(&threads[n], NULL, job_thread, ptr++);
  410. if (setjmp(on_break) == 0) {
  411. for (;;) {
  412. FILE *f = xs_socket_accept(rs);
  413. if (f != NULL) {
  414. xs *job = xs_data_new(&f, sizeof(FILE *));
  415. job_post(job, 1);
  416. }
  417. else
  418. break;
  419. }
  420. }
  421. srv_running = 0;
  422. /* send as many empty jobs as working threads */
  423. for (n = 1; n < n_threads; n++)
  424. job_post(NULL, 0);
  425. /* wait for all the threads to exit */
  426. for (n = 0; n < n_threads; n++)
  427. pthread_join(threads[n], NULL);
  428. pthread_mutex_lock(&job_mutex);
  429. job_fifo = xs_free(job_fifo);
  430. pthread_mutex_unlock(&job_mutex);
  431. sem_close(job_sem);
  432. sem_unlink(sem_name);
  433. xs *uptime = xs_str_time_diff(time(NULL) - start_time);
  434. srv_log(xs_fmt("httpd stop %s:%d (run time: %s)", address, port, uptime));
  435. }