From 3219d9703decc5f341a6fa1bef3701274fa946ce Mon Sep 17 00:00:00 2001 From: Michael Andres Date: Wed, 22 Nov 2023 15:25:14 +0100 Subject: [PATCH] Url: Add builtin "zypp-rawurl" schema for strings with unexpanded repo variables The schema is used internally to wrap raw strings with unexpanded repo variables into type Url. The raw string is stored in the Url's fragment because it does not necessarily form a valid Url before it got expanded. --- tests/zypp/Url_test.cc | 10 ++++++---- zypp-core/Url.cc | 34 +++++++++++++++++++++++++++++++++- zypp-core/Url.h | 29 +++++++++++++++++++++++++++++ zypp-core/url/UrlBase.cc | 7 ------- zypp-core/url/UrlBase.h | 1 - 5 files changed, 68 insertions(+), 13 deletions(-) diff --git a/tests/zypp/Url_test.cc b/tests/zypp/Url_test.cc index 00ee9eb677..fe9d8ea972 100644 --- a/tests/zypp/Url_test.cc +++ b/tests/zypp/Url_test.cc @@ -91,12 +91,14 @@ BOOST_AUTO_TEST_CASE(test_url1) // asString shouldn't print the password, asCompleteString should. // further, the "//" at the begin of the path should be keept. str = "http://user:pass@localhost//srv/ftp?proxypass=@PROXYPASS@&proxy=proxy.my&proxyuser=@PROXYUSER@&Xproxypass=NOTTHIS&proxypass=@PROXYPASS@&proxypass=@PROXYPASS@"; - one = "http://user@localhost//srv/ftp?proxy=proxy.my&proxyuser=@PROXYUSER@&Xproxypass=NOTTHIS"; - two = str; + one = "http://user@localhost//srv/ftp?proxypass=@PROXYPASS@&proxy=proxy.my&proxyuser=@PROXYUSER@&Xproxypass=NOTTHIS&proxypass=@PROXYPASS@&proxypass=@PROXYPASS@"; + two = "http://user@localhost//srv/ftp?proxy=proxy.my&proxyuser=@PROXYUSER@&Xproxypass=NOTTHIS"; url = str; - BOOST_CHECK_EQUAL( one, url.asString() ); - BOOST_CHECK_EQUAL( two, url.asCompleteString() ); + BOOST_CHECK_EQUAL( str, url.asCompleteString() ); + BOOST_CHECK_EQUAL( one, hotfix1050625::asString( url ) ); + BOOST_CHECK_EQUAL( two, url.asString() ); + // hidden proxypass in the query is available when explicitly asked for BOOST_CHECK_EQUAL( url.getQueryParam( "proxypass" ), "@PROXYPASS@" ); diff --git a/zypp-core/Url.cc b/zypp-core/Url.cc index d14b9aa4a3..391107d050 100644 --- a/zypp-core/Url.cc +++ b/zypp-core/Url.cc @@ -206,6 +206,20 @@ namespace zypp addUrlByScheme("ftp", ref); addUrlByScheme("sftp", ref); addUrlByScheme("tftp", ref); + + // ===================================== + ref.reset( new UrlBase()); + ref->setViewOptions( zypp::url::ViewOption::DEFAULTS + - zypp::url::ViewOption::EMPTY_AUTHORITY + - zypp::url::ViewOption::EMPTY_PATH_NAME + - zypp::url::ViewOption::WITH_PATH_NAME + - zypp::url::ViewOption::WITH_QUERY_STR + - zypp::url::ViewOption::WITH_FRAGMENT + ); + ref->config("safe_fragment", ref->config( "safe_fragment" ) + "{}" ); // more readable: ${VAR} + ref->config("with_authority", "n"); // disallow host,... + ref->config("require_pathname", "n"); // path not required + addUrlByScheme("zypp-rawurl", ref); // in fragment: URL with unexpanded RepoVariables } bool @@ -457,6 +471,12 @@ namespace zypp { return scheme_r == "plugin"; } + + bool Url::schemeIsRawUrl( const std::string & scheme_r ) + { + return scheme_r == "zypp-rawurl"; + } + /////////////////////////////////////////////////////////////////// // ----------------------------------------------------------------- @@ -494,6 +514,18 @@ namespace zypp return m_impl->asString(opts); } + // ----------------------------------------------------------------- + std::string + Url::asRawString() const + { + return schemeIsRawUrl() ? getFragment() : asString(); + } + + std::string + Url::asRawString(const ViewOptions &opts) const + { + return schemeIsRawUrl() ? getFragment() : asString(opts); + } // ----------------------------------------------------------------- std::string @@ -859,7 +891,7 @@ namespace zypp namespace hotfix1050625 { std::string asString( const Url & url_r ) - { return url_r.m_impl->asString1050625(); } + { return url_r.asRawString( url_r.getViewOptions()+ViewOptions::hotfix1050625 ); } } //////////////////////////////////////////////////////////////////// diff --git a/zypp-core/Url.h b/zypp-core/Url.h index d1eeabfd12..f1bbf20eb2 100644 --- a/zypp-core/Url.h +++ b/zypp-core/Url.h @@ -22,6 +22,8 @@ namespace zypp class Url; namespace hotfix1050625 { + // Temp. fix to keep the proxypass in the query when writing the .repo files, + // but otherwise hiding it, when WITH_PASSWORD is not set. Returns asRawString. std::string asString( const Url & url_r ); } namespace filesystem { @@ -280,6 +282,11 @@ namespace zypp /** \overload nonstatic version */ bool schemeIsPlugin() const { return schemeIsPlugin( getScheme() ); } + /** zypp-rawurl */ + static bool schemeIsRawUrl( const std::string & scheme_r ); + /** \overload nonstatic version */ + bool schemeIsRawUrl() const { return schemeIsRawUrl( getScheme() ); } + /** * \brief Verifies the Url. * @@ -326,11 +333,33 @@ namespace zypp * in the current object (see setViewOption()) and forces to * return a string with all URL components included. * + * \note: It may contain embedded passwords otherwise hidden + * by the \ref ViewOptions. + * * \return A complete string representation of the Url object. */ std::string asCompleteString() const; + /** + * Returns the original raw string e.g. to be written back to a file. + * + * This is basically \ref asString, just for the \c zypp-rawurl: + * schema the unexpanded fragment part is returned. I.e. it returns + * a valid Url as string or a string with embedded RepoVariables + * which is expected to form a valid Url after variables have been + * replaced. + * + * Although a \c zypp-rawurl: written back \ref asCompleteString to a + * file could be used to restore the Url as well, it may be desired for + * user supplied strings to restore the original string value. + **/ + std::string + asRawString() const; + /** \overload taking custom ViewOptions to render a Url (except for zypp-rawurl:). */ + std::string + asRawString(const ViewOptions &opts) const; + // ----------------- /** diff --git a/zypp-core/url/UrlBase.cc b/zypp-core/url/UrlBase.cc index d9a86cc70c..bb554a314e 100644 --- a/zypp-core/url/UrlBase.cc +++ b/zypp-core/url/UrlBase.cc @@ -506,13 +506,6 @@ namespace zypp return asString(getViewOptions()); } - std::string UrlBase::asString1050625() const - { - // Temp. fix to keep the proxypass in the query when writing the .repo files, - // but otherwise hiding it, when WITH_PASSWORD is not set. - return asString(getViewOptions()+ViewOptions::hotfix1050625); - } - // --------------------------------------------------------------- std::string UrlBase::asString(const zypp::url::ViewOptions &opts) const diff --git a/zypp-core/url/UrlBase.h b/zypp-core/url/UrlBase.h index dfa4d3f410..557e1d51b7 100644 --- a/zypp-core/url/UrlBase.h +++ b/zypp-core/url/UrlBase.h @@ -1000,7 +1000,6 @@ namespace zypp void setViewOptions(const ViewOptions &vopts); - std::string asString1050625() const; protected: /** * Utility method to cleanup an encoded path name.