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