mirror of
https://github.com/NixOS/nix.git
synced 2025-11-15 23:12:44 +01:00
Merge pull request #14296 from lovesegfault/nix-s3-more-tests
fix(nix-prefetch-url): correctly extract filename from URLs with query parameters
This commit is contained in:
commit
6420879728
4 changed files with 108 additions and 7 deletions
|
|
@ -408,6 +408,17 @@ struct VerbatimURL
|
|||
[](const ParsedURL & url) -> std::string_view { return url.scheme; }},
|
||||
raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last non-empty path segment from the URL.
|
||||
*
|
||||
* This is useful for extracting filenames from URLs.
|
||||
* For example, "https://example.com/path/to/file.txt?query=value"
|
||||
* returns "file.txt".
|
||||
*
|
||||
* @return The last non-empty path segment, or std::nullopt if no such segment exists.
|
||||
*/
|
||||
std::optional<std::string> lastPathSegment() const;
|
||||
};
|
||||
|
||||
std::ostream & operator<<(std::ostream & os, const VerbatimURL & url);
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "nix/util/split.hh"
|
||||
#include "nix/util/canon-path.hh"
|
||||
#include "nix/util/strings-inline.hh"
|
||||
#include "nix/util/file-system.hh"
|
||||
|
||||
#include <boost/url.hpp>
|
||||
|
||||
|
|
@ -440,4 +441,21 @@ std::ostream & operator<<(std::ostream & os, const VerbatimURL & url)
|
|||
return os;
|
||||
}
|
||||
|
||||
std::optional<std::string> VerbatimURL::lastPathSegment() const
|
||||
{
|
||||
try {
|
||||
auto parsedUrl = parsed();
|
||||
auto segments = parsedUrl.pathSegments(/*skipEmpty=*/true);
|
||||
if (std::ranges::empty(segments))
|
||||
return std::nullopt;
|
||||
return segments.back();
|
||||
} catch (BadURL &) {
|
||||
// Fall back to baseNameOf for unparsable URLs
|
||||
auto name = baseNameOf(to_string());
|
||||
if (name.empty())
|
||||
return std::nullopt;
|
||||
return std::string{name};
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue