Back to index

openldap  2.4.31
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes
LDAPUrl Class Reference

This class is used to analyze and store LDAP-Urls as returned by a LDAP-Server as Referrals and Search References. More...

#include <LDAPUrl.h>

Collaboration diagram for LDAPUrl:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 LDAPUrl (const std::string &url="")
 Create a new object from a string that contains a LDAP-Url.
 ~LDAPUrl ()
 Destructor.
int getPort () const
void setPort (int port)
 Set the port value of the URL.
int getScope () const
void setScope (const std::string &scope)
 Set the Scope part of the URL.
const std::string & getURLString () const
void setURLString (const std::string &url)
 Set the URL member attribute.
const std::string & getHost () const
void setHost (const std::string &host)
 Set the Host part of the URL.
const std::string & getScheme () const
void setScheme (const std::string &scheme)
 Set the Protocol Scheme of the URL.
const std::string & getDN () const
void setDN (const std::string &dn)
 Set the DN part of the URL.
const std::string & getFilter () const
void setFilter (const std::string &filter)
 Set the Filter part of the URL.
const StringListgetAttrs () const
void setAttrs (const StringList &attrs)
 Set the Attributes part of the URL.
void setExtensions (const StringList &ext)
const StringListgetExtensions () const
void percentDecode (const std::string &src, std::string &dest)
 Percent-decode a string.
std::string & percentEncode (const std::string &src, std::string &dest, int flags=0) const
 Percent-encoded a string.

Protected Types

enum  mode {
  base, attrs, scope, filter,
  extensions
}

Protected Member Functions

void parseUrl ()
 Split the url string that is associated with this Object into it components.
void components2Url () const
 Generate an URL string from the components that were set with the various set...() methods (this function is mostly for internal use and gets called automatically whenever necessary)
void string2list (const std::string &src, StringList &sl, bool percentDecode=false)

Protected Attributes

bool regenerate
int m_Port
int m_Scope
std::string m_Host
std::string m_DN
std::string m_Filter
StringList m_Attrs
StringList m_Extensions
std::string m_urlString
std::string m_Scheme

Detailed Description

This class is used to analyze and store LDAP-Urls as returned by a LDAP-Server as Referrals and Search References.

LDAP-URLs are defined in RFC1959 and have the following format:
ldap://host:port/baseDN[?attr[?scope[?filter]]]

Definition at line 22 of file LDAPUrl.h.


Member Enumeration Documentation

enum LDAPUrl::mode [protected]
Enumerator:
base 
attrs 
scope 
filter 
extensions 

Definition at line 178 of file LDAPUrl.h.


Constructor & Destructor Documentation

LDAPUrl::LDAPUrl ( const std::string &  url = "")

Create a new object from a string that contains a LDAP-Url.

Parameters:
urlThe URL String

Definition at line 22 of file LDAPUrl.cpp.

{
    DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPUrl::LDAPUrl()" << endl);
    DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
            "   url:" << url << endl);
    m_urlString = url;
    m_Filter = "";
    m_Scheme = "ldap";
    m_Scope = 0;
    m_Port = 0;
    regenerate = false;
    if (url != "") {
        this->parseUrl();
    }
}

Destructor.

Definition at line 38 of file LDAPUrl.cpp.

{
    DEBUG(LDAP_DEBUG_DESTROY, "LDAPUrl::~LDAPUrl()" << endl);
    m_Attrs.clear();
}

Member Function Documentation

void LDAPUrl::components2Url ( ) const [protected]

Generate an URL string from the components that were set with the various set...() methods (this function is mostly for internal use and gets called automatically whenever necessary)

Definition at line 341 of file LDAPUrl.cpp.

{
    std::ostringstream url; 
    std::string 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;
    }

    url << "/";
    encoded = "";
    if ( m_DN != "" ) {
        this->percentEncode( m_DN, encoded );
        url << encoded;
    }
    string qm = "";
    if ( ! m_Attrs.empty() ){
        url << "?";
        bool first = true;
        for ( StringList::const_iterator i = m_Attrs.begin();
                i != m_Attrs.end(); i++) 
        {
            this->percentEncode( *i, encoded );
            if ( ! first ) {
                url << ",";
            } else {
                first = false;
            }
            url << encoded;
        }
    } else {
        qm.append("?");
    }
    if ( m_Scope == 1 ) {
        url << qm << "?one";
        qm = "";
    } else if ( m_Scope == 2 ) {
        url << qm << "?sub";
        qm = "";
    } else {
        qm.append("?");
    }
    if (m_Filter != "" ){
        this->percentEncode( m_Filter, encoded );
        url << qm << "?" << encoded;
        qm = "";
    } else {
        qm.append("?");
    }

    if ( ! m_Extensions.empty() ){
        url << qm << "?";
        bool first = true;
        for ( StringList::const_iterator i = m_Extensions.begin();
                i != m_Extensions.end(); i++) 
        {
            this->percentEncode( *i, encoded, 1);
            if ( ! first ) {
                url << ",";
            } else {
                first = false;
            }
            url << encoded;
        }
    }
    m_urlString=url.str();  
}
Returns:
The List of attributes that was in the URL

Definition at line 124 of file LDAPUrl.cpp.

{
    return m_Attrs;
}

Here is the caller graph for this function:

const string & LDAPUrl::getDN ( ) const
Returns:
The Base-DN part of the URL

Definition at line 104 of file LDAPUrl.cpp.

{
    return m_DN;
}

Here is the caller graph for this function:

Definition at line 134 of file LDAPUrl.cpp.

{
    return m_Extensions;
}
const string & LDAPUrl::getFilter ( ) const
Returns:
The Filter part of the URL

Definition at line 114 of file LDAPUrl.cpp.

{
    return m_Filter;
}

Here is the caller graph for this function:

const string & LDAPUrl::getHost ( ) const
Returns:
The hostname or IP-Address of the destination host.

Definition at line 93 of file LDAPUrl.cpp.

{
    return m_Host;
}

Here is the caller graph for this function:

int LDAPUrl::getPort ( ) const
Returns:
The part of the URL that is representing the network port

Definition at line 44 of file LDAPUrl.cpp.

{
    return m_Port;
}

Here is the caller graph for this function:

const std::string & LDAPUrl::getScheme ( ) const
Returns:
The Protocol Scheme of the URL.

Definition at line 145 of file LDAPUrl.cpp.

{
    return m_Scheme;
}
Returns:
The scope part of the URL is returned.

Definition at line 55 of file LDAPUrl.cpp.

{
    return m_Scope;
}

Here is the caller graph for this function:

const string & LDAPUrl::getURLString ( ) const
Returns:
The complete URL as a string

Definition at line 75 of file LDAPUrl.cpp.

{
    if (regenerate){
        this->components2Url();
        regenerate=false;
    }
    return m_urlString;
}

Here is the caller graph for this function:

void LDAPUrl::parseUrl ( ) [protected]

Split the url string that is associated with this Object into it components.

The compontens of the URL can be access via the get...() methods. (this function is mostly for internal use and gets called automatically whenever necessary)

Definition at line 163 of file LDAPUrl.cpp.

{
    DEBUG(LDAP_DEBUG_TRACE, "LDAPUrl::parseUrl()" << std::endl);
    // reading Scheme
    std::string::size_type pos = m_urlString.find(':');
    std::string::size_type startpos = pos;
    if (pos == std::string::npos) {
        throw LDAPUrlException(LDAPUrlException::INVALID_URL,
                "No colon found in URL");
    }
    std::string scheme = m_urlString.substr(0, pos);
    DEBUG(LDAP_DEBUG_TRACE, "    scheme is <" << scheme << ">" << std::endl);

    if ( scheme == "ldap" ) {
        m_Scheme = scheme;
    } else if ( scheme == "ldaps" ) {
        m_Scheme = scheme;
    } else if ( scheme == "ldapi" ) {
        m_Scheme = scheme;
    } else if ( scheme == "cldap" ) {
        m_Scheme = scheme;
    } else {
        throw LDAPUrlException(LDAPUrlException::INVALID_SCHEME,
                "Unknown URL Scheme: \"" + scheme + "\"");
    }

    if ( m_urlString[pos+1] != '/' || m_urlString[pos+2] != '/' ) {
        throw LDAPUrlException(LDAPUrlException::INVALID_URL);
    } else {
        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);

        // 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 port = m_urlString.substr(portstart+1, 
                    (pos == std::string::npos ? pos : pos-portstart-1) );
            if ( port.length() > 0 ) {
                std::istringstream i(port);
                i >> m_Port;
                if ( i.fail() ){
                    throw LDAPUrlException(LDAPUrlException::INVALID_PORT);
                }
            }
            DEBUG(LDAP_DEBUG_TRACE, "    Port: <" << m_Port << ">" 
                    << std::endl);
        }
        startpos = pos + 1;
    }
    int parserMode = base;
    while ( pos != std::string::npos ) {
        pos = m_urlString.find('?', startpos);
        std::string actComponent = m_urlString.substr(startpos, 
                pos - startpos);
        DEBUG(LDAP_DEBUG_TRACE, "    ParserMode:" << parserMode << std::endl);
        DEBUG(LDAP_DEBUG_TRACE, "    ActComponent: <" << actComponent << ">" 
                << std::endl);
        std::string s_scope = "";
        std::string s_ext = "";
        switch(parserMode) {
            case base :
                percentDecode(actComponent, m_DN);
                DEBUG(LDAP_DEBUG_TRACE, "    BaseDN:" << m_DN << std::endl); 
                break;
            case attrs :
                DEBUG(LDAP_DEBUG_TRACE, "    reading Attributes" << std::endl);
                if (actComponent.length() != 0 ) {
                    string2list(actComponent,m_Attrs, true);
                }
                break;
            case scope :
                percentDecode(actComponent, s_scope);
                if (s_scope == "base" || s_scope == "" ) {
                    m_Scope = 0;
                } else if (s_scope == "one" ) {
                    m_Scope = 1;
                } else if (s_scope == "sub" ) {
                    m_Scope = 2;
                } else {
                    throw LDAPUrlException(LDAPUrlException::INVALID_SCOPE);
                }
                DEBUG(LDAP_DEBUG_TRACE, "    Scope: <" << s_scope << ">"
                        << std::endl);
                break;
            case filter :
                percentDecode(actComponent, m_Filter);
                DEBUG(LDAP_DEBUG_TRACE, "    filter: <" << m_Filter << ">"
                        << std::endl);
                break;
            case extensions :
                DEBUG(LDAP_DEBUG_TRACE, "    reading Extensions" << std::endl); 
                string2list(actComponent, m_Extensions, true);
                break;
            default : 
                DEBUG(LDAP_DEBUG_TRACE, "    unknown state" << std::endl); 
                break;
        }
        startpos = pos + 1;
        parserMode++;
    }
}
void LDAPUrl::percentDecode ( const std::string &  src,
std::string &  dest 
)

Percent-decode a string.

Parameters:
srcThe string that is to be decoded
destThe decoded result string

Definition at line 295 of file LDAPUrl.cpp.

{
    DEBUG(LDAP_DEBUG_TRACE, "LDAPUrl::percentDecode()" << std::endl); 
    std::string::size_type pos = 0;
    std::string::size_type startpos = 0;
    pos = src.find('%', startpos);
    while ( pos != std::string::npos ) {
        out += src.substr(startpos, pos - startpos);
        std::string istr(src.substr(pos+1, 2));
        std::istringstream i(istr);
        i.setf(std::ios::hex, std::ios::basefield);
        i.unsetf(std::ios::showbase);
        int hex;
        i >> hex;
        if ( i.fail() ){
            throw LDAPUrlException(LDAPUrlException::URL_DECODING_ERROR, 
                    "Invalid percent encoding");
        }
        char j = hex;
        out.push_back(j);
        startpos = pos+3;
        pos = src.find('%', startpos);
    } 
    out += src.substr(startpos, pos - startpos);
}
std::string & LDAPUrl::percentEncode ( const std::string &  src,
std::string &  dest,
int  flags = 0 
) const

Percent-encoded a string.

Parameters:
srcThe string that is to be encoded
destThe encoded result string
flags

Definition at line 418 of file LDAPUrl.cpp.

{
    std::ostringstream o;
    o.setf(std::ios::hex, std::ios::basefield);
    o.setf(std::ios::uppercase);
    o.unsetf(std::ios::showbase);
    bool escape=false;
    for ( std::string::const_iterator i = src.begin(); i != src.end(); i++ ){
        switch(*i){
            /* reserved */
            case '?' :
                escape = true;
            break;
            case ',' :
                if ( flags & PCT_ENCFLAG_COMMA ) {
                    escape = true;
                } else {
                    escape = false;
                }
            break;
            case ':' :
            case '/' :
                if ( flags & PCT_ENCFLAG_SLASH ) {
                    escape = true;
                } else {
                    escape = false;
                }
            break;
            case '#' :
            case '[' :
            case ']' :
            case '@' :
            case '!' :
            case '$' :
            case '&' :
            case '\'' :
            case '(' :
            case ')' :
            case '*' :
            case '+' :
            case ';' :
            case '=' :
            /* unreserved */
            case '-' :
            case '.' :
            case '_' :
            case '~' :
                escape = false;
            break;
            default :
                if (  std::isalnum(*i) ) {
                    escape = false;
                } else {
                    escape = true;
                }
            break;
        }
        if ( escape ) {
            o << "%" << std::setw(2) << std::setfill('0') << (int)(unsigned char)*i ;
        } else {
            o.put(*i);
        }
    }
    dest = o.str();
    return dest;
}

Set the Attributes part of the URL.

Parameters:
attrsStringList constaining the List of Attributes

Definition at line 128 of file LDAPUrl.cpp.

{
    m_Attrs = attrs;
    regenerate = true;
}
void LDAPUrl::setDN ( const std::string &  dn)

Set the DN part of the URL.

Parameters:
dnThe new DN part

Definition at line 108 of file LDAPUrl.cpp.

{
    m_DN = dn;
    regenerate = true;
}

Here is the caller graph for this function:

Definition at line 139 of file LDAPUrl.cpp.

{
    m_Extensions = ext;
    regenerate = true;
}
void LDAPUrl::setFilter ( const std::string &  filter)

Set the Filter part of the URL.

Parameters:
filterThe new Filter

Definition at line 118 of file LDAPUrl.cpp.

{
    m_Filter = filter;
    regenerate = true;
}
void LDAPUrl::setHost ( const std::string &  host)

Set the Host part of the URL.

Parameters:
hostThe new host part

Definition at line 98 of file LDAPUrl.cpp.

{
    m_Host = host;
    regenerate = true;
}

Set the port value of the URL.

Parameters:
dnThe port value

Definition at line 49 of file LDAPUrl.cpp.

{
    m_Port = port;
    regenerate = true;
}
void LDAPUrl::setScheme ( const std::string &  scheme)

Set the Protocol Scheme of the URL.

Parameters:
hostThe Protcol scheme. Allowed values are ldap,ldapi,ldaps and cldap

Definition at line 150 of file LDAPUrl.cpp.

{
    if (scheme == "ldap" || scheme == "ldaps" || 
            scheme == "ldapi" || scheme == "cldap" ) 
    {
        m_Scheme = scheme;
        regenerate = true;
    } else {
        throw LDAPUrlException(LDAPUrlException::INVALID_SCHEME,
                "Unknown URL scheme: \"" + scheme + "\"");
    }
}
void LDAPUrl::setScope ( const std::string &  scope)

Set the Scope part of the URL.

Parameters:
scopeThe new scope

Definition at line 60 of file LDAPUrl.cpp.

{
    if (scope == "base" || scope == "" ) {
        m_Scope = 0;
    } else if (scope == "one" ) {
        m_Scope = 1;
    } else if (scope == "sub" ) {
        m_Scope = 2;
    } else {
        throw LDAPUrlException(LDAPUrlException::INVALID_SCOPE, 
                "Scope was:" + scope); 
    }
    regenerate = true;
}
void LDAPUrl::setURLString ( const std::string &  url)

Set the URL member attribute.

Parameters:
urlThe URL String

Definition at line 84 of file LDAPUrl.cpp.

{
    m_urlString = url;
    if (url != "") {
        this->parseUrl();
    }
    regenerate = false;
}
void LDAPUrl::string2list ( const std::string &  src,
StringList sl,
bool  percentDecode = false 
) [protected]

Definition at line 321 of file LDAPUrl.cpp.

{
    std::string::size_type comma_startpos = 0;
    std::string::size_type comma_pos = 0;
    std::string actItem;
    while ( comma_pos != std::string::npos ) {
        comma_pos = src.find(',', comma_startpos);
        actItem = src.substr(comma_startpos, comma_pos - comma_startpos);
        if (percentDecode){
            std::string decoded;
            this->percentDecode(actItem,decoded);
            actItem = decoded;
        }
        sl.add(actItem);
        comma_startpos = comma_pos + 1;
    }
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 174 of file LDAPUrl.h.

std::string LDAPUrl::m_DN [protected]

Definition at line 172 of file LDAPUrl.h.

Definition at line 175 of file LDAPUrl.h.

std::string LDAPUrl::m_Filter [protected]

Definition at line 173 of file LDAPUrl.h.

std::string LDAPUrl::m_Host [protected]

Definition at line 171 of file LDAPUrl.h.

int LDAPUrl::m_Port [protected]

Definition at line 169 of file LDAPUrl.h.

std::string LDAPUrl::m_Scheme [protected]

Definition at line 177 of file LDAPUrl.h.

int LDAPUrl::m_Scope [protected]

Definition at line 170 of file LDAPUrl.h.

std::string LDAPUrl::m_urlString [mutable, protected]

Definition at line 176 of file LDAPUrl.h.

bool LDAPUrl::regenerate [mutable, protected]

Definition at line 168 of file LDAPUrl.h.


The documentation for this class was generated from the following files: