KLdap

ldapurl.cpp
1/*
2 This file is part of libkldap.
3 SPDX-FileCopyrightText: 2004-2006 Szombathelyi György <gyurco@freemail.hu>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "ldapurl.h"
9
10#include "ldap_core_debug.h"
11
12using namespace KLDAPCore;
13using namespace Qt::Literals::StringLiterals;
14
15class Q_DECL_HIDDEN LdapUrl::LdapUrlPrivate
16{
17public:
18 LdapUrlPrivate()
19 : m_scope(Base)
20 {
21 }
22
23 QMap<QString, Extension> m_extensions;
24 QStringList m_attributes;
25 Scope m_scope;
26 QString m_filter;
27};
28
30 : d(new LdapUrlPrivate)
31{
32}
33
35 : QUrl(_url)
36 , d(new LdapUrlPrivate)
37{
38 parseQuery();
39}
40
42 : QUrl(that)
43 , d(new LdapUrlPrivate)
44{
45 *d = *that.d;
46}
47
49{
50 if (this == &that) {
51 return *this;
52 }
53
54 QUrl::operator=(that);
55 *d = *that.d;
56
57 return *this;
58}
59
60LdapUrl::~LdapUrl() = default;
61
62void LdapUrl::setDn(const LdapDN &dn)
63{
64 const QString tmp = dn.toString();
65 if (tmp.startsWith(QLatin1Char('/'))) {
66 setPath(tmp);
67 } else {
68 setPath(QLatin1Char('/') + tmp);
69 }
70}
71
72LdapDN LdapUrl::dn() const
73{
74 QString tmp = path();
75 if (tmp.startsWith(QLatin1Char('/'))) {
76 tmp = tmp.mid(1);
77 }
78 const LdapDN tmpDN(tmp);
79 return tmpDN;
80}
81
83{
84 return d->m_attributes;
85}
86
87void LdapUrl::setAttributes(const QStringList &attributes)
88{
89 d->m_attributes = attributes;
91}
92
94{
95 return d->m_scope;
96}
97
99{
100 d->m_scope = scope;
101 updateQuery();
102}
103
105{
106 return d->m_filter;
107}
108
109void LdapUrl::setFilter(const QString &filter)
110{
111 d->m_filter = filter;
112 updateQuery();
113}
114
115bool LdapUrl::hasExtension(const QString &key) const
116{
117 return d->m_extensions.contains(key);
118}
119
121{
123
124 it = d->m_extensions.constFind(key);
125 if (it != d->m_extensions.constEnd()) {
126 return *it;
127 } else {
128 Extension ext;
129 ext.value = ""_L1;
130 ext.critical = false;
131 return ext;
132 }
133}
134
135QString LdapUrl::extension(const QString &key, bool &critical) const
136{
137 const Extension ext = extension(key);
138 critical = ext.critical;
139 return ext.value;
140}
141
142void LdapUrl::setExtension(const QString &key, const LdapUrl::Extension &ext)
143{
144 d->m_extensions[key] = ext;
145 updateQuery();
146}
147
148void LdapUrl::setExtension(const QString &key, const QString &value, bool critical)
149{
150 Extension ext;
151 ext.value = value;
152 ext.critical = critical;
153 setExtension(key, ext);
154}
155
156void LdapUrl::setExtension(const QString &key, int value, bool critical)
157{
158 Extension ext;
159 ext.value = QString::number(value);
160 ext.critical = critical;
161 setExtension(key, ext);
162}
163
165{
166 d->m_extensions.remove(key);
167 updateQuery();
168}
169
171{
173 QString q(QLatin1Char('?'));
174
175 // set the attributes to query
176 if (!d->m_attributes.isEmpty()) {
177 q += d->m_attributes.join(QLatin1Char(','));
178 }
179
180 // set the scope
181 q += QLatin1Char('?');
182 switch (d->m_scope) {
183 case Sub:
184 q += QStringLiteral("sub");
185 break;
186 case One:
187 q += QStringLiteral("one");
188 break;
189 case Base:
190 q += QStringLiteral("base");
191 break;
192 }
193
194 // set the filter
195 q += QLatin1Char('?');
196 if (d->m_filter != "(objectClass=*)"_L1 && !d->m_filter.isEmpty()) {
197 q += QLatin1StringView(toPercentEncoding(d->m_filter));
198 }
199
200 // set the extensions
201 q += QLatin1Char('?');
202 for (it = d->m_extensions.constBegin(); it != d->m_extensions.constEnd(); ++it) {
203 if (it.value().critical) {
204 q += QLatin1Char('!');
205 }
206 q += it.key();
207 if (!it.value().value.isEmpty()) {
208 q += QLatin1Char('=') + QLatin1StringView(toPercentEncoding(it.value().value));
209 }
210 q += QLatin1Char(',');
211 }
212 while (q.endsWith(QLatin1Char('?')) || q.endsWith(QLatin1Char(','))) {
213 q.remove(q.length() - 1, 1);
214 }
215
216 setQuery(q);
217 qCDebug(LDAP_CORE_LOG) << "LDAP URL updateQuery():" << toDisplayString();
218}
219
221{
222 Extension ext;
223 QStringList extensions;
225 // remove first ?
226 if (q.startsWith(QLatin1Char('?'))) {
227 q.remove(0, 1);
228 }
229
230 // split into a list
231 const QStringList url_items = q.split(QLatin1Char('?'));
232
233 d->m_attributes.clear();
234 d->m_scope = Base;
235 d->m_filter = QStringLiteral("(objectClass=*)");
236 d->m_extensions.clear();
237
238 int i = 0;
239 QStringList::const_iterator end(url_items.constEnd());
240 for (QStringList::const_iterator it = url_items.constBegin(); it != end; ++it, i++) {
241 switch (i) {
242 case 0:
243 d->m_attributes = (*it).split(QLatin1Char(','), Qt::SkipEmptyParts);
244 break;
245 case 1:
246 if ((*it) == "sub"_L1) {
247 d->m_scope = Sub;
248 } else if ((*it) == "one"_L1) {
249 d->m_scope = One;
250 }
251 break;
252 case 2:
253 d->m_filter = fromPercentEncoding((*it).toLatin1());
254 break;
255 case 3:
256 extensions = (*it).split(QLatin1Char(','), Qt::SkipEmptyParts);
257 break;
258 }
259 }
260
261 QString name;
262 QString value;
263 QStringList::const_iterator end2(extensions.constEnd());
264 for (QStringList::const_iterator it = extensions.constBegin(); it != end2; ++it) {
265 ext.critical = false;
266 name = fromPercentEncoding((*it).section(QLatin1Char('='), 0, 0).toLatin1()).toLower();
267 value = fromPercentEncoding((*it).section(QLatin1Char('='), 1).toLatin1());
268 if (name.startsWith(QLatin1Char('!'))) {
269 ext.critical = true;
270 name.remove(0, 1);
271 }
272 qCDebug(LDAP_CORE_LOG) << "LdapUrl extensions name=" << name << "value:" << value;
273 ext.value = value.replace("%2"_L1, ","_L1);
274 setExtension(name, ext);
275 }
276}
A special url class for LDAP.
Definition ldapurl.h:30
void setFilter(const QString &filter)
Sets the filter part of the LDAP url.
Definition ldapurl.cpp:109
void setExtension(const QString &key, const Extension &extension)
Sets the specified extension key with the value and criticality in extension.
void setScope(Scope scope)
Sets the scope part of the LDAP url.
Definition ldapurl.cpp:98
LdapUrl & operator=(const LdapUrl &other)
Overwrites the values of the LDAP url with values from an other url.
Definition ldapurl.cpp:48
Scope scope() const
Returns the scope part of the LDAP url.
Definition ldapurl.cpp:93
struct { QString value; bool critical; } Extension
A class holding the extension name and state whether the extension is critical.
Definition ldapurl.h:36
~LdapUrl()
Destroys the LDAP url.
LdapDN dn() const
Returns the dn part of the LDAP url.
Definition ldapurl.cpp:72
void setAttributes(const QStringList &attributes)
Sets the attributes part of the LDAP url.
Definition ldapurl.cpp:87
enum { Base, One, Sub } Scope
Describes the scope of the LDAP url.
Definition ldapurl.h:44
QStringList attributes() const
Returns the attributes part of the LDAP url.
Definition ldapurl.cpp:82
QString filter() const
Returns the filter part of the LDAP url.
Definition ldapurl.cpp:104
void setDn(const LdapDN &dn)
Sets the dn part of the LDAP url.
Definition ldapurl.cpp:62
LdapUrl()
Constructs an empty LDAP url.
Definition ldapurl.cpp:29
void updateQuery()
Updates the query component from the attributes, scope, filter and extensions.
Definition ldapurl.cpp:170
bool hasExtension(const QString &extension) const
Returns whether the specified extension exists in the LDAP url.
Definition ldapurl.cpp:115
Extension extension(const QString &extension) const
Returns the specified extension.
Definition ldapurl.cpp:120
void removeExtension(const QString &extension)
Removes the specified extension.
Definition ldapurl.cpp:164
void parseQuery()
Parses the query argument of the URL and makes it available via the attributes(), extension(),...
Definition ldapurl.cpp:220
const_iterator constBegin() const const
const_iterator constEnd() const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QString number(double n, char format, int precision)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toLower() const const
SkipEmptyParts
FullyEncoded
QString fromPercentEncoding(const QByteArray &input)
QUrl & operator=(QUrl &&other)
QString path(ComponentFormattingOptions options) const const
QString query(ComponentFormattingOptions options) const const
void setPath(const QString &path, ParsingMode mode)
void setQuery(const QString &query, ParsingMode mode)
QString toDisplayString(FormattingOptions options) const const
QByteArray toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:34:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.