commit 80a084e

default  ·  2024-11-07 16:10:20 +0000 UTC
parent 2769e6b
New function make_url(), that takes 'proxy_media' into account.
1 files changed,  +53, -2
M html.c
M html.c
+53, -2
  1@@ -11,6 +11,7 @@
  2 #include "xs_mime.h"
  3 #include "xs_match.h"
  4 #include "xs_html.h"
  5+#include "xs_curl.h"
  6 
  7 #include "snac.h"
  8 
  9@@ -41,6 +42,24 @@ int login(snac *snac, const xs_dict *headers)
 10 }
 11 
 12 
 13+xs_str *make_url(snac *user, int proxy_media, const char *href)
 14+/* makes an URL, possibly including proxying */
 15+{
 16+    xs_str *url = NULL;
 17+
 18+    if (user && proxy_media) {
 19+        xs *h = xs_replace(href, "https:/" "/", "");
 20+        url = xs_fmt("%s/proxy/%s", user->actor, h);
 21+
 22+        snac_debug(user, 1, xs_fmt("Proxying %s %s", href, url));
 23+    }
 24+    else
 25+        url = xs_dup(href);
 26+
 27+    return url;
 28+}
 29+
 30+
 31 xs_str *replace_shortnames(xs_str *s, const xs_list *tag, int ems)
 32 /* replaces all the :shortnames: with the emojis in tag */
 33 {
 34@@ -1464,6 +1483,7 @@ xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
 35     const char *v;
 36     int has_title = 0;
 37     int collapse_threads = 0;
 38+    int proxy_media = xs_is_true(xs_dict_get(srv_config, "proxy_media"));
 39 
 40     /* do not show non-public messages in the public timeline */
 41     if ((read_only || !user) && !is_msg_public(msg))
 42@@ -1954,13 +1974,15 @@ xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
 43         const xs_dict *a;
 44         while (xs_list_next(attach, &a, &c)) {
 45             const char *type = xs_dict_get(a, "type");
 46-            const char *href = xs_dict_get(a, "href");
 47+            const char *o_href = xs_dict_get(a, "href");
 48             const char *name = xs_dict_get(a, "name");
 49 
 50             /* if this image is already in the post content, skip */
 51-            if (content && xs_str_in(content, href) != -1)
 52+            if (content && xs_str_in(content, o_href) != -1)
 53                 continue;
 54 
 55+            xs *href = make_url(user, proxy_media && !read_only, o_href);
 56+
 57             if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
 58                 xs_html_add(content_attachments,
 59                     xs_html_tag("a",
 60@@ -2762,6 +2784,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
 61     const char *p_path;
 62     int cache = 1;
 63     int save = 1;
 64+    int proxy_media = xs_is_true(xs_dict_get(srv_config, "proxy_media"));
 65     const char *v;
 66 
 67     xs *l = xs_split_n(q_path, "/", 2);
 68@@ -3163,6 +3186,34 @@ int html_get_handler(const xs_dict *req, const char *q_path,
 69 
 70         snac_debug(&snac, 1, xs_fmt("serving RSS"));
 71     }
 72+    else
 73+    if (xs_startswith(p_path, "proxy/") && proxy_media) { /** remote media by proxy **/
 74+        if (!login(&snac, req)) {
 75+            *body  = xs_dup(uid);
 76+            status = HTTP_STATUS_UNAUTHORIZED;
 77+        }
 78+        else {
 79+            xs *url = xs_replace(p_path, "proxy/", "https:/" "/");
 80+            xs *hdrs = xs_dict_new();
 81+
 82+            xs *rsp = xs_http_request("GET", url, hdrs,
 83+                        NULL, 0, &status, body, b_size, 0);
 84+
 85+            if (valid_status(status)) {
 86+                const char *ct = xs_dict_get(rsp, "content-type");
 87+
 88+                /* find the content-type in the static mime types,
 89+                   and return that value instead of ct, which will
 90+                   be destroyed when out of scope */
 91+                for (int n = 0; xs_mime_types[n]; n += 2) {
 92+                    if (strcmp(ct, xs_mime_types[n + 1]) == 0) {
 93+                        *ctype = (char *)xs_mime_types[n + 1];
 94+                        break;
 95+                    }
 96+                }
 97+            }
 98+        }
 99+    }
100     else
101         status = HTTP_STATUS_NOT_FOUND;
102