1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
13 #include <zypp/base/String.h>
17 #if defined(WITH_URL_PARSE_DEBUG)
21 //////////////////////////////////////////////////////////////////////
23 { ////////////////////////////////////////////////////////////////////
26 using namespace zypp::url;
29 // -----------------------------------------------------------------
31 * url = [scheme:] [//authority] /path [?query] [#fragment]
33 #define RX_SPLIT_URL "^(([^:/?#]+):)?" \
40 // -----------------------------------------------------------------
42 class LDAPUrl: public UrlBase
50 LDAPUrl(const LDAPUrl &url): UrlBase(url)
56 return new LDAPUrl(*this);
59 virtual UrlBase::Schemes
60 getKnownSchemes() const
62 UrlBase::Schemes schemes(2);
71 // => put base-dn to path params :-)
72 // ahm... will this work propelly?
73 //config("sep_pathparams", "/");
75 config("psep_querystr", "?");
76 config("vsep_querystr", "");
78 config("safe_pathname", "/=,");
79 config("safe_querystr", "(),=");
82 config("rx_username", "");
83 config("rx_password", "");
84 config("rx_fragment", "");
89 // -----------------------------------------------------------------
94 typedef std::map<std::string,UrlRef> UrlBySchemeMap;
95 UrlBySchemeMap urlByScheme;
102 ref.reset( new LDAPUrl());
103 addUrlByScheme("ldap", ref);
104 addUrlByScheme("ldaps", ref);
107 ref.reset( new UrlBase());
108 ref->setViewOptions( ref->getViewOptions() -
109 zypp::url::ViewOption::EMPTY_AUTHORITY);
110 ref->config("rx_username", "");
111 ref->config("rx_password", "");
112 addUrlByScheme("nfs", ref);
114 ref->config("safe_pathname", "@");
115 ref->config("rx_hostname", "");
116 ref->config("rx_port", "");
117 addUrlByScheme("mailto", ref);
121 addUrlByScheme(const std::string &scheme,
124 if( !scheme.empty() && urlImpl &&
125 urlImpl->isValidScheme(scheme))
129 urlByScheme[scheme] = ref;
136 getUrlByScheme(const std::string &scheme) const
138 UrlBySchemeMap::const_iterator i(urlByScheme.find(scheme));
139 if( i != urlByScheme.end())
147 getKnownSchemes() const
149 UrlBySchemeMap::const_iterator i(urlByScheme.begin());
150 UrlBase::Schemes schemes;
152 schemes.reserve(urlByScheme.size());
153 for( ; i != urlByScheme.begin(); ++i)
155 schemes.push_back(i->first);
162 // -----------------------------------------------------------------
163 UrlByScheme g_urlSchemeRepository;
166 // -----------------------------------------------------------------
172 // -----------------------------------------------------------------
174 : m_impl( new UrlBase())
179 // -----------------------------------------------------------------
180 Url::Url(const Url &url)
181 : m_impl( url.m_impl)
185 throw std::invalid_argument(
186 "Unable to clone Url object"
192 // -----------------------------------------------------------------
193 Url::Url(const std::string &encodedUrl)
194 : m_impl( parseUrl(encodedUrl))
198 throw std::invalid_argument(
199 "Unable to parse Url components"
205 // -----------------------------------------------------------------
208 Url::registerScheme(const std::string &scheme,
211 return g_urlSchemeRepository.addUrlByScheme(scheme, urlImpl);
215 // -----------------------------------------------------------------
218 Url::parseUrl(const std::string &encodedUrl)
221 str::regex rex(RX_SPLIT_URL);
223 bool ret = str::regex_match(encodedUrl, out, rex);
226 #if defined(WITH_URL_PARSE_DEBUG)
227 std::cerr << "Regex parsed URL components("
228 << out.size() << "):" << std::endl;
229 std::cerr << "URL " << encodedUrl << std::endl;
230 for(size_t n=0; n < out.size(); n++)
232 std::cerr << "[" << n << "] " << out[n].str() << std::endl;
236 if(ret && out.size() == 10)
238 url = g_urlSchemeRepository.getUrlByScheme(out[2].str());
241 url.reset( new UrlBase());
243 url->init(out[2].str(),
253 // -----------------------------------------------------------------
256 Url::getAllKnownSchemes()
258 return g_urlSchemeRepository.getKnownSchemes();
262 // -----------------------------------------------------------------
264 Url::getKnownSchemes() const
266 return m_impl->getKnownSchemes();
270 // -----------------------------------------------------------------
272 Url::isValidScheme(const std::string &scheme) const
274 return m_impl->isValidScheme(scheme);
278 // -----------------------------------------------------------------
280 Url::toString() const
282 return m_impl->toString();
286 // -----------------------------------------------------------------
288 Url::toString(const ViewOptions &opts) const
290 return m_impl->toString(opts);
294 // -----------------------------------------------------------------
296 Url::getScheme() const
298 return m_impl->getScheme();
302 // -----------------------------------------------------------------
304 Url::getAuthority() const
306 return m_impl->getAuthority();
309 // -----------------------------------------------------------------
311 Url::getPathData() const
313 return m_impl->getPathData();
316 // -----------------------------------------------------------------
318 Url::getQueryString() const
320 return m_impl->getQueryString();
323 // -----------------------------------------------------------------
325 Url::getFragment(zypp::url::EEncoding eflag) const
327 return m_impl->getFragment(eflag);
331 // -----------------------------------------------------------------
333 Url::getUsername(EEncoding eflag) const
335 return m_impl->getUsername(eflag);
338 // -----------------------------------------------------------------
340 Url::getPassword(EEncoding eflag) const
342 return m_impl->getPassword(eflag);
345 // -----------------------------------------------------------------
347 Url::getHost(EEncoding eflag) const
349 return m_impl->getHost(eflag);
352 // -----------------------------------------------------------------
356 return m_impl->getPort();
359 // -----------------------------------------------------------------
361 Url::getPathName(EEncoding eflag) const
363 return m_impl->getPathName(eflag);
366 // -----------------------------------------------------------------
368 Url::getPathParams() const
370 return m_impl->getPathParams();
374 // -----------------------------------------------------------------
376 Url::getPathParamsVec() const
378 return m_impl->getPathParamsVec();
382 // -----------------------------------------------------------------
384 Url::getPathParamsMap(EEncoding eflag) const
386 return m_impl->getPathParamsMap(eflag);
390 // -----------------------------------------------------------------
392 Url::getQueryStringVec() const
394 return m_impl->getQueryStringVec();
398 // -----------------------------------------------------------------
400 Url::getQueryStringMap(EEncoding eflag) const
402 return m_impl->getQueryStringMap(eflag);
406 // -----------------------------------------------------------------
408 Url::setScheme(const std::string &scheme)
410 if(scheme == m_impl->getScheme())
414 if( m_impl->isKnownScheme(scheme))
416 m_impl->setScheme(scheme);
420 UrlRef url = g_urlSchemeRepository.getUrlByScheme(scheme);
423 url.reset( new UrlBase());
427 m_impl->getAuthority(),
428 m_impl->getPathData(),
429 m_impl->getQueryString(),
430 m_impl->getFragment(zypp::url::E_ENCODED)
436 // -----------------------------------------------------------------
438 Url::setAuthority(const std::string &authority)
440 m_impl->setAuthority(authority);
444 // -----------------------------------------------------------------
446 Url::setPathData(const std::string &pathdata)
448 m_impl->setPathData(pathdata);
452 // -----------------------------------------------------------------
454 Url::setQueryString(const std::string &querystr)
456 m_impl->setQueryString(querystr);
460 // -----------------------------------------------------------------
462 Url::setFragment(const std::string &fragment)
464 m_impl->setFragment(fragment);
468 // -----------------------------------------------------------------
470 Url::setUsername(const std::string &user)
472 m_impl->setUsername(user);
476 // -----------------------------------------------------------------
478 Url::setPassword(const std::string &pass)
480 m_impl->setPassword(pass);
484 // -----------------------------------------------------------------
486 Url::setHost(const std::string &host)
488 m_impl->setHost(host);
492 // -----------------------------------------------------------------
494 Url::setPort(const std::string &port)
496 m_impl->setPort(port);
500 // -----------------------------------------------------------------
502 Url::setPathName(const std::string &path)
504 m_impl->setPathName(path);
508 // -----------------------------------------------------------------
510 Url::setPathParams(const std::string ¶ms)
512 m_impl->setPathParams(params);
516 // -----------------------------------------------------------------
518 Url::setPathParamsVec(const zypp::url::ParamVec &pvec)
520 m_impl->setPathParamsVec(pvec);
524 // -----------------------------------------------------------------
526 Url::setPathParamsMap(const zypp::url::ParamMap &pmap)
528 m_impl->setPathParamsMap(pmap);
532 // -----------------------------------------------------------------
534 Url::setQueryStringVec(const zypp::url::ParamVec &pvec)
536 m_impl->setQueryStringVec(pvec);
540 // -----------------------------------------------------------------
542 Url::setQueryStringMap(const zypp::url::ParamMap &pmap)
544 m_impl->setQueryStringMap(pmap);
547 // -----------------------------------------------------------------
548 std::ostream & operator<<( std::ostream & str, const Url & url )
549 { return str << url.toString(); }
553 ////////////////////////////////////////////////////////////////////
555 //////////////////////////////////////////////////////////////////////
557 ** vim: set ts=2 sts=2 sw=2 ai et: