From 595b3745f1091e9e4b5ffeb256c9ba25b2533221 Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Mon, 24 Apr 2023 16:53:11 +0300 Subject: libfetch, apk: display warning for permanent redirects during init fixes #10776 --- libfetch/fetch.c | 1 + libfetch/fetch.h | 4 ++++ libfetch/http.c | 2 ++ src/apk.c | 16 ++++++++++++++++ 4 files changed, 23 insertions(+) diff --git a/libfetch/fetch.c b/libfetch/fetch.c index 68d920c..efee05f 100644 --- a/libfetch/fetch.c +++ b/libfetch/fetch.c @@ -39,6 +39,7 @@ #include "fetch.h" #include "common.h" +fetch_redirect_t fetchRedirectMethod; auth_t fetchAuthMethod; int fetchLastErrCode; char fetchLastErrString[MAXERRSTRING]; diff --git a/libfetch/fetch.h b/libfetch/fetch.h index 0c07c05..15c60e9 100644 --- a/libfetch/fetch.h +++ b/libfetch/fetch.h @@ -164,6 +164,10 @@ char *fetchUnquoteFilename(struct url *); void fetchConnectionCacheInit(int, int); void fetchConnectionCacheClose(void); +/* Redirects */ +typedef void (*fetch_redirect_t)(int, const struct url *, const struct url *); +extern fetch_redirect_t fetchRedirectMethod; + /* Authentication */ typedef int (*auth_t)(struct url *); extern auth_t fetchAuthMethod; diff --git a/libfetch/http.c b/libfetch/http.c index abc3ae6..2f80240 100644 --- a/libfetch/http.c +++ b/libfetch/http.c @@ -1061,6 +1061,8 @@ http_request(struct url *URL, const char *op, struct url_stat *us, } new->offset = url->offset; new->length = url->length; + if (fetchRedirectMethod) + fetchRedirectMethod(conn->err, url, new); break; case hdr_transfer_encoding: /* XXX weak test*/ diff --git a/src/apk.c b/src/apk.c index 3baae46..3073584 100644 --- a/src/apk.c +++ b/src/apk.c @@ -488,6 +488,20 @@ static int remove_empty_strings(int count, char **args) return j; } +static void fetch_redirect(int code, const struct url *cur, const struct url *next) +{ + char *url; + + switch (code) { + case 301: // Moved Permanently + case 308: // Permanent Redirect + url = fetchStringifyURL(next); + apk_warning("Permanently redirected to %s", url); + free(url); + break; + } +} + int main(int argc, char **argv) { void *ctx = NULL; @@ -523,6 +537,7 @@ int main(int argc, char **argv) init_openssl(); setup_automatic_flags(); fetchTimeout = 60; + fetchRedirectMethod = fetch_redirect; fetchConnectionCacheInit(32, 4); r = parse_options(argc, argv, applet, ctx, &dbopts); @@ -601,6 +616,7 @@ int main(int argc, char **argv) apk_string_array_resize(&args, argc); memcpy(args->item, argv, argc * sizeof(*argv)); + fetchRedirectMethod = NULL; r = applet->main(ctx, &db, args); signal(SIGINT, SIG_IGN); -- cgit v1.2.3-70-g09d2