diff options
| -rw-r--r-- | httpd.c | 101 | ||||
| -rw-r--r-- | utils.c | 6 |
2 files changed, 58 insertions, 49 deletions
| @@ -36,7 +36,7 @@ const char *nodeinfo_2_0_template = "" | |||
| 36 | "\"localPosts\":%d}," | 36 | "\"localPosts\":%d}," |
| 37 | "\"openRegistrations\":false,\"metadata\":{}}"; | 37 | "\"openRegistrations\":false,\"metadata\":{}}"; |
| 38 | 38 | ||
| 39 | d_char *nodeinfo_2_0(void) | 39 | xs_str *nodeinfo_2_0(void) |
| 40 | /* builds a nodeinfo json object */ | 40 | /* builds a nodeinfo json object */ |
| 41 | { | 41 | { |
| 42 | xs *users = user_list(); | 42 | xs *users = user_list(); |
| @@ -47,66 +47,75 @@ d_char *nodeinfo_2_0(void) | |||
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | 49 | ||
| 50 | int server_get_handler(xs_dict *req, char *q_path, | 50 | static xs_str *greeting_html(void) |
| 51 | char **body, int *b_size, char **ctype) | 51 | /* processes and returns greeting.html */ |
| 52 | /* basic server services */ | ||
| 53 | { | 52 | { |
| 54 | int status = 0; | 53 | /* try to open greeting.html */ |
| 54 | xs *fn = xs_fmt("%s/greeting.html", srv_basedir); | ||
| 55 | FILE *f; | ||
| 56 | xs_str *s = NULL; | ||
| 55 | 57 | ||
| 56 | (void)req; | 58 | if ((f = fopen(fn, "r")) != NULL) { |
| 59 | s = xs_readall(f); | ||
| 60 | fclose(f); | ||
| 57 | 61 | ||
| 58 | /* is it the server root? */ | 62 | /* replace %host% */ |
| 59 | if (*q_path == '\0') { | 63 | s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host")); |
| 60 | /* try to open greeting.html */ | ||
| 61 | xs *fn = xs_fmt("%s/greeting.html", srv_basedir); | ||
| 62 | FILE *f; | ||
| 63 | 64 | ||
| 64 | if ((f = fopen(fn, "r")) != NULL) { | 65 | const char *adm_email = xs_dict_get(srv_config, "admin_email"); |
| 65 | d_char *s = xs_readall(f); | 66 | if (xs_is_null(adm_email) || *adm_email == '\0') |
| 66 | fclose(f); | 67 | adm_email = "the administrator of this instance"; |
| 67 | 68 | ||
| 68 | status = 200; | 69 | /* replace %admin_email */ |
| 70 | s = xs_replace_i(s, "%admin_email%", adm_email); | ||
| 69 | 71 | ||
| 70 | /* replace %host% */ | 72 | /* does it have a %userlist% mark? */ |
| 71 | s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host")); | 73 | if (xs_str_in(s, "%userlist%") != -1) { |
| 74 | const char *host = xs_dict_get(srv_config, "host"); | ||
| 75 | xs *list = user_list(); | ||
| 76 | xs_list *p; | ||
| 77 | xs_str *uid; | ||
| 78 | xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n"); | ||
| 72 | 79 | ||
| 73 | const char *adm_email = xs_dict_get(srv_config, "admin_email"); | 80 | p = list; |
| 74 | if (xs_is_null(adm_email) || *adm_email == '\0') | 81 | while (xs_list_iter(&p, &uid)) { |
| 75 | adm_email = "the administrator of this instance"; | 82 | snac user; |
| 76 | 83 | ||
| 77 | /* replace %admin_email */ | 84 | if (user_open(&user, uid)) { |
| 78 | s = xs_replace_i(s, "%admin_email%", adm_email); | 85 | xs *uname = encode_html(xs_dict_get(user.config, "name")); |
| 79 | 86 | ||
| 80 | /* does it have a %userlist% mark? */ | 87 | xs *u = xs_fmt( |
| 81 | if (xs_str_in(s, "%userlist%") != -1) { | 88 | "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n", |
| 82 | char *host = xs_dict_get(srv_config, "host"); | 89 | user.actor, uid, host, uname); |
| 83 | xs *list = user_list(); | ||
| 84 | char *p, *uid; | ||
| 85 | xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n"); | ||
| 86 | 90 | ||
| 87 | p = list; | 91 | ul = xs_str_cat(ul, u); |
| 88 | while (xs_list_iter(&p, &uid)) { | ||
| 89 | snac snac; | ||
| 90 | 92 | ||
| 91 | if (user_open(&snac, uid)) { | 93 | user_free(&user); |
| 92 | xs *u = xs_fmt( | 94 | } |
| 93 | "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n", | 95 | } |
| 94 | snac.actor, uid, host, | ||
| 95 | xs_dict_get(snac.config, "name")); | ||
| 96 | 96 | ||
| 97 | ul = xs_str_cat(ul, u); | 97 | ul = xs_str_cat(ul, "</ul>\n"); |
| 98 | 98 | ||
| 99 | user_free(&snac); | 99 | s = xs_replace_i(s, "%userlist%", ul); |
| 100 | } | 100 | } |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | ul = xs_str_cat(ul, "</ul>\n"); | 103 | return s; |
| 104 | } | ||
| 104 | 105 | ||
| 105 | s = xs_replace_i(s, "%userlist%", ul); | ||
| 106 | } | ||
| 107 | 106 | ||
| 108 | *body = s; | 107 | int server_get_handler(xs_dict *req, const char *q_path, |
| 109 | } | 108 | char **body, int *b_size, char **ctype) |
| 109 | /* basic server services */ | ||
| 110 | { | ||
| 111 | int status = 0; | ||
| 112 | |||
| 113 | (void)req; | ||
| 114 | |||
| 115 | /* is it the server root? */ | ||
| 116 | if (*q_path == '\0') { | ||
| 117 | if ((*body = greeting_html()) != NULL) | ||
| 118 | status = 200; | ||
| 110 | } | 119 | } |
| 111 | else | 120 | else |
| 112 | if (strcmp(q_path, "/susie.png") == 0 || strcmp(q_path, "/favicon.ico") == 0 ) { | 121 | if (strcmp(q_path, "/susie.png") == 0 || strcmp(q_path, "/favicon.ico") == 0 ) { |
| @@ -150,7 +159,7 @@ void httpd_connection(FILE *f) | |||
| 150 | xs *req; | 159 | xs *req; |
| 151 | char *method; | 160 | char *method; |
| 152 | int status = 0; | 161 | int status = 0; |
| 153 | d_char *body = NULL; | 162 | xs_str *body = NULL; |
| 154 | int b_size = 0; | 163 | int b_size = 0; |
| 155 | char *ctype = NULL; | 164 | char *ctype = NULL; |
| 156 | xs *headers = xs_dict_new(); | 165 | xs *headers = xs_dict_new(); |
| @@ -13,7 +13,7 @@ | |||
| 13 | #include <sys/stat.h> | 13 | #include <sys/stat.h> |
| 14 | #include <stdlib.h> | 14 | #include <stdlib.h> |
| 15 | 15 | ||
| 16 | const char *default_srv_config = "{" | 16 | static const char *default_srv_config = "{" |
| 17 | "\"host\": \"\"," | 17 | "\"host\": \"\"," |
| 18 | "\"prefix\": \"\"," | 18 | "\"prefix\": \"\"," |
| 19 | "\"address\": \"127.0.0.1\"," | 19 | "\"address\": \"127.0.0.1\"," |
| @@ -30,7 +30,7 @@ const char *default_srv_config = "{" | |||
| 30 | "\"admin_account\": \"\"" | 30 | "\"admin_account\": \"\"" |
| 31 | "}"; | 31 | "}"; |
| 32 | 32 | ||
| 33 | const char *default_css = | 33 | static const char *default_css = |
| 34 | "body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em; word-wrap: break-word; }\n" | 34 | "body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em; word-wrap: break-word; }\n" |
| 35 | "pre { overflow-x: scroll; }\n" | 35 | "pre { overflow-x: scroll; }\n" |
| 36 | ".snac-embedded-video, img { max-width: 100% }\n" | 36 | ".snac-embedded-video, img { max-width: 100% }\n" |
| @@ -60,7 +60,7 @@ const char *default_css = | |||
| 60 | ".snac-poll-result { margin-left: auto; margin-right: auto; }\n" | 60 | ".snac-poll-result { margin-left: auto; margin-right: auto; }\n" |
| 61 | ; | 61 | ; |
| 62 | 62 | ||
| 63 | const char *greeting_html = | 63 | static const char *greeting_html = |
| 64 | "<!DOCTYPE html>\n" | 64 | "<!DOCTYPE html>\n" |
| 65 | "<html><head>\n" | 65 | "<html><head>\n" |
| 66 | "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n" | 66 | "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n" |