]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPUrl.h
Allow IPv6 addresses
[openldap] / contrib / ldapc++ / src / LDAPUrl.h
1 /*
2  * Copyright 2000-2006, OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6
7 #ifndef LDAP_URL_H
8 #define LDAP_URL_H
9
10 #include <StringList.h>
11
12 class LDAPUrlException;
13 /**
14  * This class is used to analyze and store LDAP-Urls as returned by a
15  * LDAP-Server as Referrals and Search References. LDAP-URLs are defined
16  * in RFC1959 and have the following format: <BR>
17  * <code>
18  * ldap://host:port/baseDN[?attr[?scope[?filter]]] <BR>
19  * </code>
20  */
21 class LDAPUrl{
22
23     public : 
24         /**
25          * Create a new object from a string that contains a LDAP-Url
26          * @param url The URL String
27          */
28         LDAPUrl(const std::string &url="");
29
30         /**
31          * Destructor
32          */
33         ~LDAPUrl();
34
35         /**
36          * @return The part of the URL that is representing the network
37          * port
38          */
39         int getPort() const;
40         
41         /**
42          * Set the port value of the URL
43          * @param dn The port value
44          */
45         void setPort(int port);
46
47         /**
48          * @return The scope part of the URL is returned. 
49          */
50         int getScope() const;
51
52         /**
53          * Set the Scope part of the URL
54          * @param scope The new scope
55          */
56         void setScope(const std::string& scope);
57
58         /**
59          * @return The complete URL as a string
60          */
61         const std::string& getURLString() const;
62
63         /**
64          * Set the URL member attribute
65          * @param url The URL String
66          */
67         void setURLString(const std::string &url);
68
69         /**
70          * @return The hostname or IP-Address of the destination host.
71          */
72         const std::string& getHost() const;
73
74         /**
75          * Set the Host part of the URL
76          * @param host The new host part
77          */
78         void setHost( const std::string &host);
79
80         /**
81          * @return The Protocol Scheme of the URL.
82          */
83         const std::string& getScheme() const;
84
85         /**
86          * Set the Protocol Scheme of the URL
87          * @param host The Protcol scheme. Allowed values are 
88          *       ldap,ldapi,ldaps and cldap
89          */
90         void setScheme( const std::string &scheme );
91
92         /**
93          * @return The Base-DN part of the URL
94          */
95         const std::string& getDN() const;
96         
97         /**
98          * Set the DN part of the URL
99          * @param dn The new DN part
100          */
101         void setDN( const std::string &dn);
102
103         
104         /**
105          * @return The Filter part of the URL
106          */
107         const std::string& getFilter() const;
108         
109         /**
110          * Set the Filter part of the URL
111          * @param filter The new Filter
112          */
113         void setFilter( const std::string &filter);
114
115         /**
116          * @return The List of attributes  that was in the URL
117          */
118         const StringList& getAttrs() const;
119         
120         /**
121          * Set the Attributes part of the URL
122          * @param attrs StringList constaining the List of Attributes
123          */
124         void setAttrs( const StringList &attrs);
125         void setExtensions( const StringList &ext);
126         const StringList& getExtensions() const;
127
128         /**
129          * Percent-decode a string
130          * @param src The string that is to be decoded
131          * @param dest The decoded result string
132          */
133         void percentDecode( const std::string& src, std::string& dest );
134         
135         /**
136          * Percent-encoded a string
137          * @param src The string that is to be encoded
138          * @param dest The encoded result string
139          * @param flags
140          */
141         std::string& percentEncode( const std::string& src, 
142                     std::string& dest, 
143                     int flags=0 ) const;
144    
145     protected : 
146         /**
147          * Split the url string that is associated with this Object into
148          * it components. The compontens of the URL can be access via the 
149          * get...() methods.
150          * (this function is mostly for internal use and gets called 
151          * automatically whenever necessary)
152          */
153         void parseUrl();
154         
155         /**
156          * Generate an URL string from the components that were set with
157          * the various set...() methods
158          * (this function is mostly for internal use and gets called 
159          * automatically whenever necessary)
160          */
161         void components2Url() const;
162         
163         void string2list(const std::string &src, StringList& sl,
164                 bool percentDecode=false);
165
166     protected :
167         mutable bool regenerate;
168         int m_Port;
169         int m_Scope;
170         std::string m_Host;
171         std::string m_DN;
172         std::string m_Filter;
173         StringList m_Attrs;
174         StringList m_Extensions;
175         mutable std::string m_urlString;
176         std::string m_Scheme;
177         enum mode { base, attrs, scope, filter, extensions };
178 };
179
180 struct code2string_s {
181     int code;
182     const char* string;
183 };
184
185 class LDAPUrlException {
186     public :
187         LDAPUrlException(int code, const std::string &msg="" );
188
189         int getCode() const;
190         const std::string getErrorMessage() const;
191         const std::string getAdditionalInfo() const;
192
193         static const int INVALID_SCHEME      = 1;
194         static const int INVALID_PORT        = 2;
195         static const int INVALID_SCOPE       = 3;
196         static const int INVALID_URL         = 4;
197         static const int URL_DECODING_ERROR  = 5; 
198         static const code2string_s code2string[]; 
199
200     private:
201         int m_code;
202         std::string m_addMsg;
203 };
204 #endif //LDAP_URL_H