From 8f98dc65859da8d1279fcf6cccb0a7af689c4736 Mon Sep 17 00:00:00 2001 From: Ralf Haferkamp Date: Mon, 17 Mar 2008 13:57:47 +0000 Subject: [PATCH] Allow IPv6 addresses --- contrib/ldapc++/src/LDAPUrl.cpp | 54 +++++++++++++++++++++++---------- contrib/ldapc++/src/LDAPUrl.h | 2 +- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/contrib/ldapc++/src/LDAPUrl.cpp b/contrib/ldapc++/src/LDAPUrl.cpp index 00b1aebe59..5a41412335 100644 --- a/contrib/ldapc++/src/LDAPUrl.cpp +++ b/contrib/ldapc++/src/LDAPUrl.cpp @@ -6,6 +6,7 @@ #include "LDAPUrl.h" #include +#include #include "debug.h" using namespace std; @@ -193,26 +194,39 @@ void LDAPUrl::parseUrl() // no hostname and port startpos++; } else { + std::string::size_type hostend; + std::string::size_type portstart; 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; @@ -328,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; } @@ -394,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 { @@ -454,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[] = { diff --git a/contrib/ldapc++/src/LDAPUrl.h b/contrib/ldapc++/src/LDAPUrl.h index 71d2dd3b82..c2e167fea9 100644 --- a/contrib/ldapc++/src/LDAPUrl.h +++ b/contrib/ldapc++/src/LDAPUrl.h @@ -138,7 +138,7 @@ class LDAPUrl{ * @param dest The encoded result string * @param flags */ - void percentEncode( const std::string& src, + std::string& percentEncode( const std::string& src, std::string& dest, int flags=0 ) const; -- 2.39.5