1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 19:46:02 +01:00

refactor(libstore): extract S3 URL parsing into separate files

Move ParsedS3URL from s3.cc/.hh into dedicated s3-url.cc/.hh files.
This separates URL parsing utilities (which are protocol-agnostic) from
the AWS SDK-specific S3Helper implementation, making the code cleaner
and enabling reuse by future curl-based S3 implementation.
This commit is contained in:
Bernardo Meurer Costa 2025-10-01 16:01:28 +00:00
parent 251479bdda
commit b72898b2aa
No known key found for this signature in database
7 changed files with 76 additions and 59 deletions

View file

@ -77,7 +77,7 @@ sources = files(
'realisation.cc', 'realisation.cc',
'references.cc', 'references.cc',
's3-binary-cache-store.cc', 's3-binary-cache-store.cc',
's3.cc', 's3-url.cc',
'serve-protocol.cc', 'serve-protocol.cc',
'ssh-store.cc', 'ssh-store.cc',
'store-reference.cc', 'store-reference.cc',

View file

@ -1,4 +1,4 @@
#include "nix/store/s3.hh" #include "nix/store/s3-url.hh"
#include "nix/util/tests/gmock-matchers.hh" #include "nix/util/tests/gmock-matchers.hh"
#if NIX_WITH_S3_SUPPORT #if NIX_WITH_S3_SUPPORT

View file

@ -72,6 +72,7 @@ headers = [ config_pub_h ] + files(
'remote-store.hh', 'remote-store.hh',
'restricted-store.hh', 'restricted-store.hh',
's3-binary-cache-store.hh', 's3-binary-cache-store.hh',
's3-url.hh',
's3.hh', 's3.hh',
'serve-protocol-connection.hh', 'serve-protocol-connection.hh',
'serve-protocol-impl.hh', 'serve-protocol-impl.hh',

View file

@ -0,0 +1,60 @@
#pragma once
///@file
#include "nix/store/config.hh"
#if NIX_WITH_S3_SUPPORT
# include "nix/util/url.hh"
# include "nix/util/util.hh"
# include <optional>
# include <string>
# include <variant>
# include <vector>
namespace nix {
/**
* Parsed S3 URL.
*/
struct ParsedS3URL
{
std::string bucket;
/**
* @see ParsedURL::path. This is a vector for the same reason.
* Unlike ParsedURL::path this doesn't include the leading empty segment,
* since the bucket name is necessary.
*/
std::vector<std::string> key;
std::optional<std::string> profile;
std::optional<std::string> region;
std::optional<std::string> scheme;
/**
* The endpoint can be either missing, be an absolute URI (with a scheme like `http:`)
* or an authority (so an IP address or a registered name).
*/
std::variant<std::monostate, ParsedURL, ParsedURL::Authority> endpoint;
std::optional<std::string> getEncodedEndpoint() const
{
return std::visit(
overloaded{
[](std::monostate) -> std::optional<std::string> { return std::nullopt; },
[](const auto & authorityOrUrl) -> std::optional<std::string> { return authorityOrUrl.to_string(); },
},
endpoint);
}
static ParsedS3URL parse(const ParsedURL & uri);
/**
* Convert this ParsedS3URL to HTTPS ParsedURL for use with curl's AWS SigV4 authentication
*/
ParsedURL toHttpsUrl() const;
auto operator<=>(const ParsedS3URL & other) const = default;
};
} // namespace nix
#endif

View file

@ -4,12 +4,9 @@
#if NIX_WITH_S3_SUPPORT #if NIX_WITH_S3_SUPPORT
# include "nix/util/ref.hh" # include "nix/util/ref.hh"
# include "nix/util/url.hh" # include "nix/store/s3-url.hh"
# include "nix/util/util.hh"
# include <optional>
# include <string> # include <string>
# include <variant>
namespace Aws { namespace Aws {
namespace Client { namespace Client {
@ -48,47 +45,6 @@ struct S3Helper
FileTransferResult getObject(const std::string & bucketName, const std::string & key); FileTransferResult getObject(const std::string & bucketName, const std::string & key);
}; };
/**
* Parsed S3 URL.
*/
struct ParsedS3URL
{
std::string bucket;
/**
* @see ParsedURL::path. This is a vector for the same reason.
* Unlike ParsedURL::path this doesn't include the leading empty segment,
* since the bucket name is necessary.
*/
std::vector<std::string> key;
std::optional<std::string> profile;
std::optional<std::string> region;
std::optional<std::string> scheme;
/**
* The endpoint can be either missing, be an absolute URI (with a scheme like `http:`)
* or an authority (so an IP address or a registered name).
*/
std::variant<std::monostate, ParsedURL, ParsedURL::Authority> endpoint;
std::optional<std::string> getEncodedEndpoint() const
{
return std::visit(
overloaded{
[](std::monostate) -> std::optional<std::string> { return std::nullopt; },
[](const auto & authorityOrUrl) -> std::optional<std::string> { return authorityOrUrl.to_string(); },
},
endpoint);
}
static ParsedS3URL parse(const ParsedURL & uri);
/**
* Convert this ParsedS3URL to HTTPS ParsedURL for use with curl's AWS SigV4 authentication
*/
ParsedURL toHttpsUrl() const;
auto operator<=>(const ParsedS3URL & other) const = default;
};
} // namespace nix } // namespace nix
#endif #endif

View file

@ -329,7 +329,7 @@ sources = files(
'remote-store.cc', 'remote-store.cc',
'restricted-store.cc', 'restricted-store.cc',
's3-binary-cache-store.cc', 's3-binary-cache-store.cc',
's3.cc', 's3-url.cc',
'serve-protocol-connection.cc', 'serve-protocol-connection.cc',
'serve-protocol.cc', 'serve-protocol.cc',
'sqlite.cc', 'sqlite.cc',

View file

@ -1,17 +1,17 @@
#include "nix/store/s3.hh" #include "nix/store/s3-url.hh"
#include "nix/util/split.hh"
#include "nix/util/url.hh"
#include "nix/util/util.hh"
#include "nix/util/canon-path.hh"
#include "nix/util/strings-inline.hh"
#include <ranges> #if NIX_WITH_S3_SUPPORT
namespace nix { # include "nix/util/error.hh"
# include "nix/util/split.hh"
# include "nix/util/strings-inline.hh"
# include <ranges>
# include <string_view>
using namespace std::string_view_literals; using namespace std::string_view_literals;
#if NIX_WITH_S3_SUPPORT namespace nix {
ParsedS3URL ParsedS3URL::parse(const ParsedURL & parsed) ParsedS3URL ParsedS3URL::parse(const ParsedURL & parsed)
try { try {
@ -116,6 +116,6 @@ ParsedURL ParsedS3URL::toHttpsUrl() const
endpoint); endpoint);
} }
#endif
} // namespace nix } // namespace nix
#endif