From a1ea72d719b63a4d2b6421ce2e53b7e3ab12a8a1 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 23 Feb 2024 18:50:28 +0300 Subject: Add curl constructors which allow to adjust curl command line --- libbutl/curl.cxx | 9 ++++-- libbutl/curl.hxx | 52 ++++++++++++++++++++++++++++++- libbutl/curl.ixx | 73 ++++++++++++++++++++++++++++++++++++++++++++ libbutl/curl.txx | 8 +++-- libbutl/semantic-version.hxx | 8 ++--- libbutl/semantic-version.ixx | 24 +++++++-------- 6 files changed, 151 insertions(+), 23 deletions(-) (limited to 'libbutl') diff --git a/libbutl/curl.cxx b/libbutl/curl.cxx index 0577391..7efd28d 100644 --- a/libbutl/curl.cxx +++ b/libbutl/curl.cxx @@ -142,7 +142,7 @@ namespace butl } curl::method_proto curl:: - translate (method_type m, const string& u, method_proto_options& o) + translate (method_type m, const string& u, method_proto_options& o, flags fs) { size_t n (u.find ("://")); @@ -161,8 +161,11 @@ namespace butl } else if (icasecmp (u, "http", n) == 0 || icasecmp (u, "https", n) == 0) { - o.push_back ("--fail"); // Fail on HTTP errors (e.g., 404). - o.push_back ("--location"); // Follow redirects. + if ((fs & flags::no_fail) == flags::none) + o.push_back ("--fail"); // Fail on HTTP errors (e.g., 404). + + if ((fs & flags::no_location) == flags::none) + o.push_back ("--location"); // Follow redirects. switch (m) { diff --git a/libbutl/curl.hxx b/libbutl/curl.hxx index 3fa7890..ea91807 100644 --- a/libbutl/curl.hxx +++ b/libbutl/curl.hxx @@ -91,6 +91,19 @@ namespace butl public: enum method_type {get, put, post}; + // By default the -sS and, for the HTTP protocol, --fail and --location + // options are passed to curl on the command line. Optionally, these + // options can be suppressed. + // + enum class flags: std::uint16_t + { + no_fail = 0x01, // Don't pass --fail. + no_location = 0x02, // Don't pass --location + no_sS = 0x04, // Don't pass -sS + + none = 0 // Default options set. + }; + ifdstream in; ofdstream out; @@ -121,6 +134,35 @@ namespace butl const std::string& url, A&&... options); + // Similar to the above, but allows to adjust the curl's default command + // line. + // + template + curl (I&& in, + O&& out, + E&& err, + method_type, + flags, + const std::string& url, + A&&... options); + + template + curl (const C&, + I&& in, + O&& out, + E&& err, + method_type, + flags, + const std::string& url, + A&&... options); + // Read the HTTP response status from an input stream. // // Specifically, read and parse the HTTP status line, by default skip over @@ -159,7 +201,10 @@ namespace butl using method_proto_options = small_vector; method_proto - translate (method_type, const std::string& url, method_proto_options&); + translate (method_type, + const std::string& url, + method_proto_options&, + flags); private: template @@ -199,6 +244,11 @@ namespace butl typename std::enable_if::value, O>::type map_out (O&&, method_proto, io_data&); }; + + curl::flags operator& (curl::flags, curl::flags); + curl::flags operator| (curl::flags, curl::flags); + curl::flags operator&= (curl::flags&, curl::flags); + curl::flags operator|= (curl::flags&, curl::flags); } #include diff --git a/libbutl/curl.ixx b/libbutl/curl.ixx index b7f6496..6dcfe13 100644 --- a/libbutl/curl.ixx +++ b/libbutl/curl.ixx @@ -16,6 +16,7 @@ namespace butl O&& out, E&& err, method_type m, + flags fs, const std::string& url, A&&... options) : curl ([] (const char* [], std::size_t) {}, @@ -23,8 +24,80 @@ namespace butl std::forward (out), std::forward (err), m, + fs, url, std::forward (options)...) { } + + template + inline curl:: + curl (const C& cmdc, + I&& in, + O&& out, + E&& err, + method_type m, + const std::string& url, + A&&... options) + : curl (cmdc, + std::forward (in), + std::forward (out), + std::forward (err), + m, + flags::none, + url, + std::forward (options)...) + { + } + + template + inline curl:: + curl (I&& in, + O&& out, + E&& err, + method_type m, + const std::string& url, + A&&... options) + : curl (std::forward (in), + std::forward (out), + std::forward (err), + m, + flags::none, + url, + std::forward (options)...) + { + } + + inline curl::flags + operator&= (curl::flags& x, curl::flags y) + { + return x = static_cast (static_cast (x) & + static_cast (y)); + } + + inline curl::flags + operator|= (curl::flags& x, curl::flags y) + { + return x = static_cast (static_cast (x) | + static_cast (y)); + } + + inline curl::flags + operator& (curl::flags x, curl::flags y) + { + return x &= y; + } + + inline curl::flags + operator| (curl::flags x, curl::flags y) + { + return x |= y; + } } diff --git a/libbutl/curl.txx b/libbutl/curl.txx index ee08145..fc74470 100644 --- a/libbutl/curl.txx +++ b/libbutl/curl.txx @@ -65,11 +65,12 @@ namespace butl O&& out, E&& err, method_type m, + flags fs, const std::string& url, A&&... options) { method_proto_options mpo; - method_proto mp (translate (m, url, mpo)); + method_proto mp (translate (m, url, mpo, fs)); io_data in_data; io_data out_data; @@ -81,8 +82,9 @@ namespace butl map_out (std::forward (out), mp, out_data), std::forward (err), "curl", - "-s", // Silent. - "-S", // But do show diagnostics. + ((fs & flags::no_sS) == flags::none + ? "-sS" // Silent but do show diagnostics. + : nullptr), mpo, in_data.options, out_data.options, diff --git a/libbutl/semantic-version.hxx b/libbutl/semantic-version.hxx index 1dc7d1d..4eba38a 100644 --- a/libbutl/semantic-version.hxx +++ b/libbutl/semantic-version.hxx @@ -177,16 +177,16 @@ namespace butl return o << x.string (); } - inline semantic_version::flags + semantic_version::flags operator& (semantic_version::flags, semantic_version::flags); - inline semantic_version::flags + semantic_version::flags operator| (semantic_version::flags, semantic_version::flags); - inline semantic_version::flags + semantic_version::flags operator&= (semantic_version::flags&, semantic_version::flags); - inline semantic_version::flags + semantic_version::flags operator|= (semantic_version::flags&, semantic_version::flags); } diff --git a/libbutl/semantic-version.ixx b/libbutl/semantic-version.ixx index 67cd8c0..8de1554 100644 --- a/libbutl/semantic-version.ixx +++ b/libbutl/semantic-version.ixx @@ -51,18 +51,6 @@ namespace butl } inline semantic_version::flags - operator& (semantic_version::flags x, semantic_version::flags y) - { - return x &= y; - } - - inline semantic_version::flags - operator| (semantic_version::flags x, semantic_version::flags y) - { - return x |= y; - } - - inline semantic_version::flags operator&= (semantic_version::flags& x, semantic_version::flags y) { return x = static_cast ( @@ -77,4 +65,16 @@ namespace butl static_cast (x) | static_cast (y)); } + + inline semantic_version::flags + operator& (semantic_version::flags x, semantic_version::flags y) + { + return x &= y; + } + + inline semantic_version::flags + operator| (semantic_version::flags x, semantic_version::flags y) + { + return x |= y; + } } -- cgit v1.1