Skip to content

Commit

Permalink
Url: Add builtin "zypp-rawurl" schema for strings with unexpanded rep…
Browse files Browse the repository at this point in the history
…o 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.
  • Loading branch information
mlandres committed Dec 15, 2023
1 parent 82d408f commit 3219d97
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
10 changes: 6 additions & 4 deletions tests/zypp/Url_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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@" );

Expand Down
34 changes: 33 additions & 1 deletion zypp-core/Url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -457,6 +471,12 @@ namespace zypp
{
return scheme_r == "plugin";
}

bool Url::schemeIsRawUrl( const std::string & scheme_r )
{
return scheme_r == "zypp-rawurl";
}

///////////////////////////////////////////////////////////////////

// -----------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ); }
}

////////////////////////////////////////////////////////////////////
Expand Down
29 changes: 29 additions & 0 deletions zypp-core/Url.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;


// -----------------
/**
Expand Down
7 changes: 0 additions & 7 deletions zypp-core/url/UrlBase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion zypp-core/url/UrlBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,6 @@ namespace zypp
void
setViewOptions(const ViewOptions &vopts);

std::string asString1050625() const;
protected:
/**
* Utility method to cleanup an encoded path name.
Expand Down

0 comments on commit 3219d97

Please sign in to comment.