diff options
| author | 2024-05-25 08:05:36 +0000 | |
|---|---|---|
| committer | 2024-05-25 08:05:36 +0000 | |
| commit | 84a767dd0878013194ed7551b5ae6ef715e841a6 (patch) | |
| tree | 9fb1b2b89e0bfbb4b8bf1e85d840c8653e646bb7 /httpd.c | |
| parent | Prevent some browsers from caching servers basic auth request (diff) | |
| parent | Backport from xs (fix regex.h compilation with tcc). (diff) | |
| download | snac2-84a767dd0878013194ed7551b5ae6ef715e841a6.tar.gz snac2-84a767dd0878013194ed7551b5ae6ef715e841a6.tar.xz snac2-84a767dd0878013194ed7551b5ae6ef715e841a6.zip | |
Merge pull request 'master' (#1) from grunfink/snac2:master into master
Reviewed-on: https://codeberg.org/louis77/snac2/pulls/1
Diffstat (limited to 'httpd.c')
| -rw-r--r-- | httpd.c | 66 |
1 files changed, 50 insertions, 16 deletions
| @@ -75,7 +75,7 @@ xs_str *nodeinfo_2_0(void) | |||
| 75 | int n_posts = 0; | 75 | int n_posts = 0; |
| 76 | xs *users = user_list(); | 76 | xs *users = user_list(); |
| 77 | xs_list *p = users; | 77 | xs_list *p = users; |
| 78 | char *v; | 78 | const char *v; |
| 79 | double now = (double)time(NULL); | 79 | double now = (double)time(NULL); |
| 80 | 80 | ||
| 81 | while (xs_list_iter(&p, &v)) { | 81 | while (xs_list_iter(&p, &v)) { |
| @@ -125,10 +125,10 @@ static xs_str *greeting_html(void) | |||
| 125 | 125 | ||
| 126 | /* does it have a %userlist% mark? */ | 126 | /* does it have a %userlist% mark? */ |
| 127 | if (xs_str_in(s, "%userlist%") != -1) { | 127 | if (xs_str_in(s, "%userlist%") != -1) { |
| 128 | char *host = xs_dict_get(srv_config, "host"); | 128 | const char *host = xs_dict_get(srv_config, "host"); |
| 129 | xs *list = user_list(); | 129 | xs *list = user_list(); |
| 130 | xs_list *p = list; | 130 | xs_list *p = list; |
| 131 | xs_str *uid; | 131 | const xs_str *uid; |
| 132 | 132 | ||
| 133 | xs_html *ul = xs_html_tag("ul", | 133 | xs_html *ul = xs_html_tag("ul", |
| 134 | xs_html_attr("class", "snac-user-list")); | 134 | xs_html_attr("class", "snac-user-list")); |
| @@ -169,18 +169,16 @@ int server_get_handler(xs_dict *req, const char *q_path, | |||
| 169 | { | 169 | { |
| 170 | int status = 0; | 170 | int status = 0; |
| 171 | 171 | ||
| 172 | (void)req; | ||
| 173 | |||
| 174 | /* is it the server root? */ | 172 | /* is it the server root? */ |
| 175 | if (*q_path == '\0') { | 173 | if (*q_path == '\0') { |
| 176 | xs_dict *q_vars = xs_dict_get(req, "q_vars"); | 174 | const xs_dict *q_vars = xs_dict_get(req, "q_vars"); |
| 177 | char *t = NULL; | 175 | const char *t = NULL; |
| 178 | 176 | ||
| 179 | if (xs_type(q_vars) == XSTYPE_DICT && (t = xs_dict_get(q_vars, "t"))) { | 177 | if (xs_type(q_vars) == XSTYPE_DICT && (t = xs_dict_get(q_vars, "t"))) { |
| 180 | /** search by tag **/ | 178 | /** search by tag **/ |
| 181 | int skip = 0; | 179 | int skip = 0; |
| 182 | int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries")); | 180 | int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries")); |
| 183 | char *v; | 181 | const char *v; |
| 184 | 182 | ||
| 185 | if ((v = xs_dict_get(q_vars, "skip")) != NULL) | 183 | if ((v = xs_dict_get(q_vars, "skip")) != NULL) |
| 186 | skip = atoi(v); | 184 | skip = atoi(v); |
| @@ -195,13 +193,25 @@ int server_get_handler(xs_dict *req, const char *q_path, | |||
| 195 | more = 1; | 193 | more = 1; |
| 196 | } | 194 | } |
| 197 | 195 | ||
| 198 | *body = html_timeline(NULL, tl, 0, skip, show, more, t, NULL, 0); | 196 | const char *accept = xs_dict_get(req, "accept"); |
| 197 | if (!xs_is_null(accept) && strcmp(accept, "application/rss+xml") == 0) { | ||
| 198 | xs *link = xs_fmt("%s/?t=%s", srv_baseurl, t); | ||
| 199 | |||
| 200 | *body = timeline_to_rss(NULL, tl, link, link, link); | ||
| 201 | *ctype = "application/rss+xml; charset=utf-8"; | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | xs *page = xs_fmt("?t=%s", t); | ||
| 205 | xs *title = xs_fmt(L("Search results for tag #%s"), t); | ||
| 206 | *body = html_timeline(NULL, tl, 0, skip, show, more, title, page, 0); | ||
| 207 | } | ||
| 199 | } | 208 | } |
| 200 | else | 209 | else |
| 201 | if (xs_type(xs_dict_get(srv_config, "show_instance_timeline")) == XSTYPE_TRUE) { | 210 | if (xs_type(xs_dict_get(srv_config, "show_instance_timeline")) == XSTYPE_TRUE) { |
| 202 | /** instance timeline **/ | 211 | /** instance timeline **/ |
| 203 | xs *tl = timeline_instance_list(0, 30); | 212 | xs *tl = timeline_instance_list(0, 30); |
| 204 | *body = html_timeline(NULL, tl, 0, 0, 0, 0, NULL, NULL, 0); | 213 | *body = html_timeline(NULL, tl, 0, 0, 0, 0, |
| 214 | L("Recent posts by users in this instance"), NULL, 0); | ||
| 205 | } | 215 | } |
| 206 | else | 216 | else |
| 207 | *body = greeting_html(); | 217 | *body = greeting_html(); |
| @@ -258,7 +268,7 @@ void httpd_connection(FILE *f) | |||
| 258 | /* the connection processor */ | 268 | /* the connection processor */ |
| 259 | { | 269 | { |
| 260 | xs *req; | 270 | xs *req; |
| 261 | char *method; | 271 | const char *method; |
| 262 | int status = 0; | 272 | int status = 0; |
| 263 | xs_str *body = NULL; | 273 | xs_str *body = NULL; |
| 264 | int b_size = 0; | 274 | int b_size = 0; |
| @@ -268,7 +278,7 @@ void httpd_connection(FILE *f) | |||
| 268 | xs *payload = NULL; | 278 | xs *payload = NULL; |
| 269 | xs *etag = NULL; | 279 | xs *etag = NULL; |
| 270 | int p_size = 0; | 280 | int p_size = 0; |
| 271 | char *p; | 281 | const char *p; |
| 272 | int fcgi_id; | 282 | int fcgi_id; |
| 273 | 283 | ||
| 274 | if (p_state->use_fcgi) | 284 | if (p_state->use_fcgi) |
| @@ -360,7 +370,7 @@ void httpd_connection(FILE *f) | |||
| 360 | #ifndef NO_MASTODON_API | 370 | #ifndef NO_MASTODON_API |
| 361 | if (status == 0) | 371 | if (status == 0) |
| 362 | status = mastoapi_delete_handler(req, q_path, | 372 | status = mastoapi_delete_handler(req, q_path, |
| 363 | &body, &b_size, &ctype); | 373 | payload, p_size, &body, &b_size, &ctype); |
| 364 | #endif | 374 | #endif |
| 365 | } | 375 | } |
| 366 | 376 | ||
| @@ -401,9 +411,9 @@ void httpd_connection(FILE *f) | |||
| 401 | headers = xs_dict_append(headers, "etag", etag); | 411 | headers = xs_dict_append(headers, "etag", etag); |
| 402 | 412 | ||
| 403 | /* if there are any additional headers, add them */ | 413 | /* if there are any additional headers, add them */ |
| 404 | xs_dict *more_headers = xs_dict_get(srv_config, "http_headers"); | 414 | const xs_dict *more_headers = xs_dict_get(srv_config, "http_headers"); |
| 405 | if (xs_type(more_headers) == XSTYPE_DICT) { | 415 | if (xs_type(more_headers) == XSTYPE_DICT) { |
| 406 | char *k, *v; | 416 | const char *k, *v; |
| 407 | int c = 0; | 417 | int c = 0; |
| 408 | while (xs_dict_next(more_headers, &k, &v, &c)) | 418 | while (xs_dict_next(more_headers, &k, &v, &c)) |
| 409 | headers = xs_dict_set(headers, k, v); | 419 | headers = xs_dict_set(headers, k, v); |
| @@ -580,7 +590,8 @@ static void *background_thread(void *arg) | |||
| 580 | 590 | ||
| 581 | { | 591 | { |
| 582 | xs *list = user_list(); | 592 | xs *list = user_list(); |
| 583 | char *p, *uid; | 593 | char *p; |
| 594 | const char *uid; | ||
| 584 | 595 | ||
| 585 | /* process queues for all users */ | 596 | /* process queues for all users */ |
| 586 | p = list; | 597 | p = list; |
| @@ -654,6 +665,13 @@ srv_state *srv_state_op(xs_str **fname, int op) | |||
| 654 | 665 | ||
| 655 | switch (op) { | 666 | switch (op) { |
| 656 | case 0: /* open for writing */ | 667 | case 0: /* open for writing */ |
| 668 | |||
| 669 | #ifdef WITHOUT_SHM | ||
| 670 | |||
| 671 | errno = ENOTSUP; | ||
| 672 | |||
| 673 | #else | ||
| 674 | |||
| 657 | if ((fd = shm_open(*fname, O_CREAT | O_RDWR, 0666)) != -1) { | 675 | if ((fd = shm_open(*fname, O_CREAT | O_RDWR, 0666)) != -1) { |
| 658 | ftruncate(fd, sizeof(*ss)); | 676 | ftruncate(fd, sizeof(*ss)); |
| 659 | 677 | ||
| @@ -664,6 +682,8 @@ srv_state *srv_state_op(xs_str **fname, int op) | |||
| 664 | close(fd); | 682 | close(fd); |
| 665 | } | 683 | } |
| 666 | 684 | ||
| 685 | #endif | ||
| 686 | |||
| 667 | if (ss == NULL) { | 687 | if (ss == NULL) { |
| 668 | /* shared memory error: just create a plain structure */ | 688 | /* shared memory error: just create a plain structure */ |
| 669 | srv_log(xs_fmt("warning: shm object error (%s)", strerror(errno))); | 689 | srv_log(xs_fmt("warning: shm object error (%s)", strerror(errno))); |
| @@ -677,6 +697,13 @@ srv_state *srv_state_op(xs_str **fname, int op) | |||
| 677 | break; | 697 | break; |
| 678 | 698 | ||
| 679 | case 1: /* open for reading */ | 699 | case 1: /* open for reading */ |
| 700 | |||
| 701 | #ifdef WITHOUT_SHM | ||
| 702 | |||
| 703 | errno = ENOTSUP; | ||
| 704 | |||
| 705 | #else | ||
| 706 | |||
| 680 | if ((fd = shm_open(*fname, O_RDONLY, 0666)) != -1) { | 707 | if ((fd = shm_open(*fname, O_RDONLY, 0666)) != -1) { |
| 681 | if ((ss = mmap(0, sizeof(*ss), PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) | 708 | if ((ss = mmap(0, sizeof(*ss), PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) |
| 682 | ss = NULL; | 709 | ss = NULL; |
| @@ -684,6 +711,8 @@ srv_state *srv_state_op(xs_str **fname, int op) | |||
| 684 | close(fd); | 711 | close(fd); |
| 685 | } | 712 | } |
| 686 | 713 | ||
| 714 | #endif | ||
| 715 | |||
| 687 | if (ss == NULL) { | 716 | if (ss == NULL) { |
| 688 | /* shared memory error */ | 717 | /* shared memory error */ |
| 689 | srv_log(xs_fmt("error: shm object error (%s) server not running?", strerror(errno))); | 718 | srv_log(xs_fmt("error: shm object error (%s) server not running?", strerror(errno))); |
| @@ -701,9 +730,14 @@ srv_state *srv_state_op(xs_str **fname, int op) | |||
| 701 | break; | 730 | break; |
| 702 | 731 | ||
| 703 | case 2: /* unlink */ | 732 | case 2: /* unlink */ |
| 733 | |||
| 734 | #ifndef WITHOUT_SHM | ||
| 735 | |||
| 704 | if (*fname) | 736 | if (*fname) |
| 705 | shm_unlink(*fname); | 737 | shm_unlink(*fname); |
| 706 | 738 | ||
| 739 | #endif | ||
| 740 | |||
| 707 | break; | 741 | break; |
| 708 | } | 742 | } |
| 709 | 743 | ||