diff options
| author | 2024-11-07 17:10:20 +0100 | |
|---|---|---|
| committer | 2024-11-07 17:10:20 +0100 | |
| commit | 80a084e72130f5054aadcd8157d22609091320d2 (patch) | |
| tree | 5bd1f59fb80a3543afe967093857ba44b5ee82e0 | |
| parent | Version 2.62 RELEASED. (diff) | |
| download | snac2-80a084e72130f5054aadcd8157d22609091320d2.tar.gz snac2-80a084e72130f5054aadcd8157d22609091320d2.tar.xz snac2-80a084e72130f5054aadcd8157d22609091320d2.zip | |
New function make_url(), that takes 'proxy_media' into account.
| -rw-r--r-- | html.c | 55 |
1 files changed, 53 insertions, 2 deletions
| @@ -11,6 +11,7 @@ | |||
| 11 | #include "xs_mime.h" | 11 | #include "xs_mime.h" |
| 12 | #include "xs_match.h" | 12 | #include "xs_match.h" |
| 13 | #include "xs_html.h" | 13 | #include "xs_html.h" |
| 14 | #include "xs_curl.h" | ||
| 14 | 15 | ||
| 15 | #include "snac.h" | 16 | #include "snac.h" |
| 16 | 17 | ||
| @@ -41,6 +42,24 @@ int login(snac *snac, const xs_dict *headers) | |||
| 41 | } | 42 | } |
| 42 | 43 | ||
| 43 | 44 | ||
| 45 | xs_str *make_url(snac *user, int proxy_media, const char *href) | ||
| 46 | /* makes an URL, possibly including proxying */ | ||
| 47 | { | ||
| 48 | xs_str *url = NULL; | ||
| 49 | |||
| 50 | if (user && proxy_media) { | ||
| 51 | xs *h = xs_replace(href, "https:/" "/", ""); | ||
| 52 | url = xs_fmt("%s/proxy/%s", user->actor, h); | ||
| 53 | |||
| 54 | snac_debug(user, 1, xs_fmt("Proxying %s %s", href, url)); | ||
| 55 | } | ||
| 56 | else | ||
| 57 | url = xs_dup(href); | ||
| 58 | |||
| 59 | return url; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 44 | xs_str *replace_shortnames(xs_str *s, const xs_list *tag, int ems) | 63 | xs_str *replace_shortnames(xs_str *s, const xs_list *tag, int ems) |
| 45 | /* replaces all the :shortnames: with the emojis in tag */ | 64 | /* replaces all the :shortnames: with the emojis in tag */ |
| 46 | { | 65 | { |
| @@ -1464,6 +1483,7 @@ xs_html *html_entry(snac *user, xs_dict *msg, int read_only, | |||
| 1464 | const char *v; | 1483 | const char *v; |
| 1465 | int has_title = 0; | 1484 | int has_title = 0; |
| 1466 | int collapse_threads = 0; | 1485 | int collapse_threads = 0; |
| 1486 | int proxy_media = xs_is_true(xs_dict_get(srv_config, "proxy_media")); | ||
| 1467 | 1487 | ||
| 1468 | /* do not show non-public messages in the public timeline */ | 1488 | /* do not show non-public messages in the public timeline */ |
| 1469 | if ((read_only || !user) && !is_msg_public(msg)) | 1489 | if ((read_only || !user) && !is_msg_public(msg)) |
| @@ -1954,13 +1974,15 @@ xs_html *html_entry(snac *user, xs_dict *msg, int read_only, | |||
| 1954 | const xs_dict *a; | 1974 | const xs_dict *a; |
| 1955 | while (xs_list_next(attach, &a, &c)) { | 1975 | while (xs_list_next(attach, &a, &c)) { |
| 1956 | const char *type = xs_dict_get(a, "type"); | 1976 | const char *type = xs_dict_get(a, "type"); |
| 1957 | const char *href = xs_dict_get(a, "href"); | 1977 | const char *o_href = xs_dict_get(a, "href"); |
| 1958 | const char *name = xs_dict_get(a, "name"); | 1978 | const char *name = xs_dict_get(a, "name"); |
| 1959 | 1979 | ||
| 1960 | /* if this image is already in the post content, skip */ | 1980 | /* if this image is already in the post content, skip */ |
| 1961 | if (content && xs_str_in(content, href) != -1) | 1981 | if (content && xs_str_in(content, o_href) != -1) |
| 1962 | continue; | 1982 | continue; |
| 1963 | 1983 | ||
| 1984 | xs *href = make_url(user, proxy_media && !read_only, o_href); | ||
| 1985 | |||
| 1964 | if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) { | 1986 | if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) { |
| 1965 | xs_html_add(content_attachments, | 1987 | xs_html_add(content_attachments, |
| 1966 | xs_html_tag("a", | 1988 | xs_html_tag("a", |
| @@ -2762,6 +2784,7 @@ int html_get_handler(const xs_dict *req, const char *q_path, | |||
| 2762 | const char *p_path; | 2784 | const char *p_path; |
| 2763 | int cache = 1; | 2785 | int cache = 1; |
| 2764 | int save = 1; | 2786 | int save = 1; |
| 2787 | int proxy_media = xs_is_true(xs_dict_get(srv_config, "proxy_media")); | ||
| 2765 | const char *v; | 2788 | const char *v; |
| 2766 | 2789 | ||
| 2767 | xs *l = xs_split_n(q_path, "/", 2); | 2790 | xs *l = xs_split_n(q_path, "/", 2); |
| @@ -3164,6 +3187,34 @@ int html_get_handler(const xs_dict *req, const char *q_path, | |||
| 3164 | snac_debug(&snac, 1, xs_fmt("serving RSS")); | 3187 | snac_debug(&snac, 1, xs_fmt("serving RSS")); |
| 3165 | } | 3188 | } |
| 3166 | else | 3189 | else |
| 3190 | if (xs_startswith(p_path, "proxy/") && proxy_media) { /** remote media by proxy **/ | ||
| 3191 | if (!login(&snac, req)) { | ||
| 3192 | *body = xs_dup(uid); | ||
| 3193 | status = HTTP_STATUS_UNAUTHORIZED; | ||
| 3194 | } | ||
| 3195 | else { | ||
| 3196 | xs *url = xs_replace(p_path, "proxy/", "https:/" "/"); | ||
| 3197 | xs *hdrs = xs_dict_new(); | ||
| 3198 | |||
| 3199 | xs *rsp = xs_http_request("GET", url, hdrs, | ||
| 3200 | NULL, 0, &status, body, b_size, 0); | ||
| 3201 | |||
| 3202 | if (valid_status(status)) { | ||
| 3203 | const char *ct = xs_dict_get(rsp, "content-type"); | ||
| 3204 | |||
| 3205 | /* find the content-type in the static mime types, | ||
| 3206 | and return that value instead of ct, which will | ||
| 3207 | be destroyed when out of scope */ | ||
| 3208 | for (int n = 0; xs_mime_types[n]; n += 2) { | ||
| 3209 | if (strcmp(ct, xs_mime_types[n + 1]) == 0) { | ||
| 3210 | *ctype = (char *)xs_mime_types[n + 1]; | ||
| 3211 | break; | ||
| 3212 | } | ||
| 3213 | } | ||
| 3214 | } | ||
| 3215 | } | ||
| 3216 | } | ||
| 3217 | else | ||
| 3167 | status = HTTP_STATUS_NOT_FOUND; | 3218 | status = HTTP_STATUS_NOT_FOUND; |
| 3168 | 3219 | ||
| 3169 | user_free(&snac); | 3220 | user_free(&snac); |