From 4000ee2f9064623e096d975489aec35c99982437 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Wed, 24 May 2023 11:22:34 +0200 Subject: Rework the domain iteration in the host fallback Separate the determination of the next level domain into its own function and split out the "we found a result" into its own break for improved readability. --- apt-pkg/deb/debmetaindex.cc | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'apt-pkg') diff --git a/apt-pkg/deb/debmetaindex.cc b/apt-pkg/deb/debmetaindex.cc index 4bd7dfa78..ef6bce261 100644 --- a/apt-pkg/deb/debmetaindex.cc +++ b/apt-pkg/deb/debmetaindex.cc @@ -1283,6 +1283,16 @@ class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type /*{{{*/ #undef APT_EMPTY_SERVER return ""; } + + /// \brief Given a hostname, strip one level down, e.g. a.b.c -> .b.c -> .c, this + /// allows you to match a.b.c against itself, .b.c, and .c, but not b.c + static inline std::string NextLevelDomain(std::string Host) + { + auto nextDot = Host.find(".", 1); + if (nextDot == Host.npos) + return ""; + return Host.substr(nextDot); + } bool CreateItemInternal(std::vector &List, std::string URI, std::string const &Dist, std::string const &Section, bool const &IsSrc, std::map Options) const @@ -1313,12 +1323,17 @@ class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type /*{{{*/ Server = SnapshotServer(OldDeb); delete OldDeb; } + // We did not find a server based on the release file. + // Lookup a fallback based on the host. For a.b.c, this will + // try a.b.c, .b.c, and .c to allow generalization for cc.archive.ubuntu.com if (Server.empty()) { - // We did not find a server based on the release file. - // Lookup a fallback based on the host. For a.b.c, this will try a.b.c, .b.c, and .c to allow generalization for cc.archive.ubuntu.com - for (std::string Host = ArchiveURI.Host; not Host.empty() && Server.empty(); Host = Host.find(".", 1) != Host.npos ? Host.substr(Host.find(".", 1)) : "") + for (std::string Host = ArchiveURI.Host; not Host.empty(); Host = NextLevelDomain(Host)) + { Server = _config->Find("Acquire::Snapshots::URI::Host::" + Host); + if (not Server.empty()) + break; + } } if (Server.empty() || Server == "no") { -- cgit v1.2.3-70-g09d2