X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=contrib%2Fldapc%2B%2B%2Fsrc%2FLDAPUrl.cpp;h=0172a5b95c513520ca0ec2434ce4f4b5539ada3b;hb=55339651d6d6e21dc85dca7949cb094e57a1f6b2;hp=95b510af7558c544549557d636df5f9c4a27526c;hpb=f0b840c4f7f3c20a5bb420ff71c361638f84df19;p=openldap diff --git a/contrib/ldapc++/src/LDAPUrl.cpp b/contrib/ldapc++/src/LDAPUrl.cpp index 95b510af75..0172a5b95c 100644 --- a/contrib/ldapc++/src/LDAPUrl.cpp +++ b/contrib/ldapc++/src/LDAPUrl.cpp @@ -1,11 +1,13 @@ +// $OpenLDAP$ /* - * Copyright 2000-2006, OpenLDAP Foundation, All Rights Reserved. + * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved. * COPYING RESTRICTIONS APPLY, see COPYRIGHT file */ #include "LDAPUrl.h" #include +#include #include "debug.h" using namespace std; @@ -163,7 +165,7 @@ void LDAPUrl::parseUrl() DEBUG(LDAP_DEBUG_TRACE, "LDAPUrl::parseUrl()" << std::endl); // reading Scheme std::string::size_type pos = m_urlString.find(':'); - std::string::size_type startpos = m_urlString.find(':'); + std::string::size_type startpos = pos; if (pos == std::string::npos) { throw LDAPUrlException(LDAPUrlException::INVALID_URL, "No colon found in URL"); @@ -190,28 +192,41 @@ void LDAPUrl::parseUrl() startpos = pos + 3; } if ( m_urlString[startpos] == '/' ) { + // no hostname and port startpos++; } else { + std::string::size_type hostend, portstart=0; pos = m_urlString.find('/', startpos); - std::string hostport = m_urlString.substr(startpos, - pos - startpos); - DEBUG(LDAP_DEBUG_TRACE, " hostport: <" << hostport << ">" - << std::endl); - std::string::size_type portstart = m_urlString.find(':', startpos); - if (portstart == std::string::npos || portstart > pos ) { - percentDecode(hostport, m_Host); + + // IPv6 Address? + if ( m_urlString[startpos] == '[' ) { + // skip + startpos++; + hostend = m_urlString.find(']', startpos); + if ( hostend == std::string::npos ){ + throw LDAPUrlException(LDAPUrlException::INVALID_URL); + } + portstart = hostend + 1; + } else { + hostend = m_urlString.find(':', startpos); + if ( hostend == std::string::npos || portstart > pos ) { + hostend = pos; + } + portstart = hostend; + } + std::string host = m_urlString.substr(startpos, hostend - startpos); + DEBUG(LDAP_DEBUG_TRACE, " host: <" << host << ">" << std::endl); + percentDecode(host, m_Host); + + if (portstart >= m_urlString.length() || portstart >= pos ) { if ( m_Scheme == "ldap" || m_Scheme == "cldap" ) { m_Port = LDAP_DEFAULT_PORT; } else if ( m_Scheme == "ldaps" ) { m_Port = LDAPS_DEFAULT_PORT; } } else { - std::string tmp = m_urlString.substr(startpos, - portstart - startpos); - percentDecode(tmp, m_Host); - DEBUG(LDAP_DEBUG_TRACE, "Host: <" << m_Host << ">" << std::endl); std::string port = m_urlString.substr(portstart+1, - pos-portstart-1); + (pos == std::string::npos ? pos : pos-portstart-1) ); if ( port.length() > 0 ) { std::istringstream i(port); i >> m_Port; @@ -222,8 +237,8 @@ void LDAPUrl::parseUrl() DEBUG(LDAP_DEBUG_TRACE, " Port: <" << m_Port << ">" << std::endl); } + startpos = pos + 1; } - startpos = pos + 1; int parserMode = base; while ( pos != std::string::npos ) { pos = m_urlString.find('?', startpos); @@ -327,8 +342,15 @@ void LDAPUrl::components2Url() const { std::ostringstream url; std::string encoded = ""; - this->percentEncode(m_Host, encoded, PCT_ENCFLAG_SLASH); - url << m_Scheme << "://" << encoded; + + url << m_Scheme << "://"; + // IPv6 ? + if ( m_Host.find( ':', 0 ) != std::string::npos ) { + url << "[" << this->percentEncode(m_Host, encoded) << "]"; + } else { + url << this->percentEncode(m_Host, encoded, PCT_ENCFLAG_SLASH); + } + if ( m_Port != 0 ) { url << ":" << m_Port; } @@ -393,7 +415,7 @@ void LDAPUrl::components2Url() const } -void LDAPUrl::percentEncode( const std::string &src, +std::string& LDAPUrl::percentEncode( const std::string &src, std::string &dest, int flags) const { @@ -453,12 +475,13 @@ void LDAPUrl::percentEncode( const std::string &src, break; } if ( escape ) { - o << "%" << (int)(unsigned char)*i ; + o << "%" << std::setw(2) << std::setfill('0') << (int)(unsigned char)*i ; } else { o.put(*i); } } dest = o.str(); + return dest; } const code2string_s LDAPUrlException::code2string[] = {