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);
106 ref.reset( new UrlBase());
107 // don't show empty authority
108 ref->setViewOptions( ref->getViewOptions() -
109 zypp::url::ViewOption::EMPTY_AUTHORITY);
110 ref->config("rx_username", ""); // disallow username
111 ref->config("rx_password", ""); // disallow password
112 // FIXME: hmm... also host+port?
113 addUrlByScheme("nfs", ref);
115 ref->config("safe_pathname", "@"); // don't encode @ in path
116 ref->config("rx_hostname", ""); // disallow hostname
117 ref->config("rx_port", ""); // disallow port
118 addUrlByScheme("mailto", ref);
122 addUrlByScheme(const std::string &scheme,
125 if( !scheme.empty() && urlImpl &&
126 urlImpl->isValidScheme(scheme))
130 urlByScheme[scheme] = ref;
137 getUrlByScheme(const std::string &scheme) const
139 UrlBySchemeMap::const_iterator i(urlByScheme.find(scheme));
140 if( i != urlByScheme.end())
148 getKnownSchemes() const
150 UrlBySchemeMap::const_iterator i(urlByScheme.begin());
151 UrlBase::Schemes schemes;
153 schemes.reserve(urlByScheme.size());
154 for( ; i != urlByScheme.begin(); ++i)
156 schemes.push_back(i->first);
163 // -----------------------------------------------------------------
164 UrlByScheme g_urlSchemeRepository;
167 // -----------------------------------------------------------------
173 // -----------------------------------------------------------------
175 : m_impl( new UrlBase())
180 // -----------------------------------------------------------------
181 Url::Url(const Url &url)
182 : m_impl( url.m_impl)
186 throw std::invalid_argument(
187 "Unable to clone Url object"
193 // -----------------------------------------------------------------
194 Url::Url(const std::string &encodedUrl)
195 : m_impl( parseUrl(encodedUrl))
199 throw std::invalid_argument(
200 "Unable to parse Url components"
206 // -----------------------------------------------------------------
209 Url::registerScheme(const std::string &scheme,
212 return g_urlSchemeRepository.addUrlByScheme(scheme, urlImpl);
216 // -----------------------------------------------------------------
219 Url::parseUrl(const std::string &encodedUrl)
222 str::regex rex(RX_SPLIT_URL);
224 bool ret = str::regex_match(encodedUrl, out, rex);
227 #if defined(WITH_URL_PARSE_DEBUG)
228 std::cerr << "Regex parsed URL components("
229 << out.size() << "):" << std::endl;
230 std::cerr << "URL " << encodedUrl << std::endl;
231 for(size_t n=0; n < out.size(); n++)
233 std::cerr << "[" << n << "] " << out[n].str() << std::endl;
237 if(ret && out.size() == 10)
239 url = g_urlSchemeRepository.getUrlByScheme(out[2].str());
242 url.reset( new UrlBase());
244 url->init(out[2].str(),
254 // -----------------------------------------------------------------
257 Url::getAllKnownSchemes()
259 return g_urlSchemeRepository.getKnownSchemes();
263 // -----------------------------------------------------------------
265 Url::getKnownSchemes() const
267 return m_impl->getKnownSchemes();
271 // -----------------------------------------------------------------
273 Url::isValidScheme(const std::string &scheme) const
275 return m_impl->isValidScheme(scheme);
279 // -----------------------------------------------------------------
281 Url::toString() const
283 return m_impl->toString();
287 // -----------------------------------------------------------------
289 Url::toString(const ViewOptions &opts) const
291 return m_impl->toString(opts);
295 // -----------------------------------------------------------------
297 Url::getScheme() const
299 return m_impl->getScheme();
303 // -----------------------------------------------------------------
305 Url::getAuthority() const
307 return m_impl->getAuthority();
310 // -----------------------------------------------------------------
312 Url::getPathData() const
314 return m_impl->getPathData();
318 // -----------------------------------------------------------------
320 Url::getQueryString() const
322 return m_impl->getQueryString();
326 // -----------------------------------------------------------------
328 Url::getFragment(zypp::url::EEncoding eflag) const
330 return m_impl->getFragment(eflag);
334 // -----------------------------------------------------------------
336 Url::getUsername(EEncoding eflag) const
338 return m_impl->getUsername(eflag);
342 // -----------------------------------------------------------------
344 Url::getPassword(EEncoding eflag) const
346 return m_impl->getPassword(eflag);
350 // -----------------------------------------------------------------
352 Url::getHost(EEncoding eflag) const
354 return m_impl->getHost(eflag);
358 // -----------------------------------------------------------------
362 return m_impl->getPort();
366 // -----------------------------------------------------------------
368 Url::getPathName(EEncoding eflag) const
370 return m_impl->getPathName(eflag);
374 // -----------------------------------------------------------------
376 Url::getPathParams() const
378 return m_impl->getPathParams();
382 // -----------------------------------------------------------------
384 Url::getPathParamsVec() const
386 return m_impl->getPathParamsVec();
390 // -----------------------------------------------------------------
392 Url::getPathParamsMap(EEncoding eflag) const
394 return m_impl->getPathParamsMap(eflag);
398 // -----------------------------------------------------------------
400 Url::getPathParam(const std::string ¶m, EEncoding eflag) const
402 return m_impl->getPathParam(param, eflag);
406 // -----------------------------------------------------------------
408 Url::getQueryStringVec() const
410 return m_impl->getQueryStringVec();
414 // -----------------------------------------------------------------
416 Url::getQueryStringMap(EEncoding eflag) const
418 return m_impl->getQueryStringMap(eflag);
422 // -----------------------------------------------------------------
424 Url::getQueryParam(const std::string ¶m, EEncoding eflag) const
426 return m_impl->getQueryParam(param, eflag);
430 // -----------------------------------------------------------------
432 Url::setScheme(const std::string &scheme)
434 if(scheme == m_impl->getScheme())
438 if( m_impl->isKnownScheme(scheme))
440 m_impl->setScheme(scheme);
444 UrlRef url = g_urlSchemeRepository.getUrlByScheme(scheme);
447 url.reset( new UrlBase());
451 m_impl->getAuthority(),
452 m_impl->getPathData(),
453 m_impl->getQueryString(),
454 m_impl->getFragment(zypp::url::E_ENCODED)
460 // -----------------------------------------------------------------
462 Url::setAuthority(const std::string &authority)
464 m_impl->setAuthority(authority);
468 // -----------------------------------------------------------------
470 Url::setPathData(const std::string &pathdata)
472 m_impl->setPathData(pathdata);
476 // -----------------------------------------------------------------
478 Url::setQueryString(const std::string &querystr)
480 m_impl->setQueryString(querystr);
484 // -----------------------------------------------------------------
486 Url::setFragment(const std::string &fragment)
488 m_impl->setFragment(fragment);
492 // -----------------------------------------------------------------
494 Url::setUsername(const std::string &user)
496 m_impl->setUsername(user);
500 // -----------------------------------------------------------------
502 Url::setPassword(const std::string &pass)
504 m_impl->setPassword(pass);
508 // -----------------------------------------------------------------
510 Url::setHost(const std::string &host)
512 m_impl->setHost(host);
516 // -----------------------------------------------------------------
518 Url::setPort(const std::string &port)
520 m_impl->setPort(port);
524 // -----------------------------------------------------------------
526 Url::setPathName(const std::string &path)
528 m_impl->setPathName(path);
532 // -----------------------------------------------------------------
534 Url::setPathParams(const std::string ¶ms)
536 m_impl->setPathParams(params);
540 // -----------------------------------------------------------------
542 Url::setPathParamsVec(const zypp::url::ParamVec &pvec)
544 m_impl->setPathParamsVec(pvec);
548 // -----------------------------------------------------------------
550 Url::setPathParamsMap(const zypp::url::ParamMap &pmap)
552 m_impl->setPathParamsMap(pmap);
556 // -----------------------------------------------------------------
558 Url::setPathParam(const std::string ¶m, const std::string &value)
560 m_impl->setPathParam(param, value);
564 // -----------------------------------------------------------------
566 Url::setQueryStringVec(const zypp::url::ParamVec &pvec)
568 m_impl->setQueryStringVec(pvec);
572 // -----------------------------------------------------------------
574 Url::setQueryStringMap(const zypp::url::ParamMap &pmap)
576 m_impl->setQueryStringMap(pmap);
579 // -----------------------------------------------------------------
581 Url::setQueryParam(const std::string ¶m, const std::string &value)
583 m_impl->setQueryParam(param, value);
587 // -----------------------------------------------------------------
588 std::ostream & operator<<( std::ostream & str, const Url & url )
590 return str << url.toString();
594 ////////////////////////////////////////////////////////////////////
596 //////////////////////////////////////////////////////////////////////
598 ** vim: set ts=2 sts=2 sw=2 ai et: