Allow IPv6 addresses

This commit is contained in:
Ralf Haferkamp 2008-03-17 13:57:47 +00:00
parent adee18e145
commit 8f98dc6585
2 changed files with 39 additions and 17 deletions

View File

@ -6,6 +6,7 @@
#include "LDAPUrl.h" #include "LDAPUrl.h"
#include <sstream> #include <sstream>
#include <iomanip>
#include "debug.h" #include "debug.h"
using namespace std; using namespace std;
@ -193,26 +194,39 @@ void LDAPUrl::parseUrl()
// no hostname and port // no hostname and port
startpos++; startpos++;
} else { } else {
std::string::size_type hostend;
std::string::size_type portstart;
pos = m_urlString.find('/', startpos); pos = m_urlString.find('/', startpos);
std::string hostport = m_urlString.substr(startpos,
pos - startpos); // IPv6 Address?
DEBUG(LDAP_DEBUG_TRACE, " hostport: <" << hostport << ">" if ( m_urlString[startpos] == '[' ) {
<< std::endl); // skip
std::string::size_type portstart = m_urlString.find(':', startpos); startpos++;
if (portstart == std::string::npos || portstart > pos ) { hostend = m_urlString.find(']', startpos);
percentDecode(hostport, m_Host); 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" ) { if ( m_Scheme == "ldap" || m_Scheme == "cldap" ) {
m_Port = LDAP_DEFAULT_PORT; m_Port = LDAP_DEFAULT_PORT;
} else if ( m_Scheme == "ldaps" ) { } else if ( m_Scheme == "ldaps" ) {
m_Port = LDAPS_DEFAULT_PORT; m_Port = LDAPS_DEFAULT_PORT;
} }
} else { } 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, std::string port = m_urlString.substr(portstart+1,
pos-portstart-1); (pos == std::string::npos ? pos : pos-portstart-1) );
if ( port.length() > 0 ) { if ( port.length() > 0 ) {
std::istringstream i(port); std::istringstream i(port);
i >> m_Port; i >> m_Port;
@ -328,8 +342,15 @@ void LDAPUrl::components2Url() const
{ {
std::ostringstream url; std::ostringstream url;
std::string encoded = ""; 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 ) { if ( m_Port != 0 ) {
url << ":" << m_Port; 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, std::string &dest,
int flags) const int flags) const
{ {
@ -454,12 +475,13 @@ void LDAPUrl::percentEncode( const std::string &src,
break; break;
} }
if ( escape ) { if ( escape ) {
o << "%" << (int)(unsigned char)*i ; o << "%" << std::setw(2) << std::setfill('0') << (int)(unsigned char)*i ;
} else { } else {
o.put(*i); o.put(*i);
} }
} }
dest = o.str(); dest = o.str();
return dest;
} }
const code2string_s LDAPUrlException::code2string[] = { const code2string_s LDAPUrlException::code2string[] = {

View File

@ -138,7 +138,7 @@ class LDAPUrl{
* @param dest The encoded result string * @param dest The encoded result string
* @param flags * @param flags
*/ */
void percentEncode( const std::string& src, std::string& percentEncode( const std::string& src,
std::string& dest, std::string& dest,
int flags=0 ) const; int flags=0 ) const;