Changeset View
Changeset View
Standalone View
Standalone View
src/kpac/script.cpp
Show First 20 Lines • Show All 130 Lines • ▼ Show 20 Line(s) | 115 | { | |||
---|---|---|---|---|---|
131 | 131 | | |||
132 | return false; | 132 | return false; | ||
133 | } | 133 | } | ||
134 | 134 | | |||
135 | static QString addressListToString(const QList<QHostAddress> &addressList, | 135 | static QString addressListToString(const QList<QHostAddress> &addressList, | ||
136 | const QHash<QString, QString> &actualEntryMap) | 136 | const QHash<QString, QString> &actualEntryMap) | ||
137 | { | 137 | { | ||
138 | QString result; | 138 | QString result; | ||
139 | Q_FOREACH (const QHostAddress &address, addressList) { | 139 | for (const QHostAddress &address : addressList) { | ||
140 | if (!result.isEmpty()) { | 140 | if (!result.isEmpty()) { | ||
141 | result += QLatin1Char(';'); | 141 | result += QLatin1Char(';'); | ||
142 | } | 142 | } | ||
143 | result += actualEntryMap.value(address.toString()); | 143 | result += actualEntryMap.value(address.toString()); | ||
144 | } | 144 | } | ||
145 | return result; | 145 | return result; | ||
146 | } | 146 | } | ||
147 | 147 | | |||
148 | class Address | 148 | class Address | ||
149 | { | 149 | { | ||
150 | public: | 150 | public: | ||
151 | struct Error {}; | 151 | struct Error {}; | ||
152 | static Address resolve(const QString &host) | 152 | static Address resolve(const QString &host) | ||
153 | { | 153 | { | ||
154 | return Address(host); | 154 | return Address(host); | ||
155 | } | 155 | } | ||
156 | 156 | | |||
157 | QList<QHostAddress> addresses() const | 157 | const QList<QHostAddress> &addresses() const | ||
dfaure: Why not just change this one to return `const &` and then use it in all range-fors?
[note that… | |||||
Because I didn't pay attention to public v.s. private API :) Fixing. ahmadsamir: Because I didn't pay attention to public v.s. private API :)
Fixing. | |||||
158 | { | 158 | { | ||
159 | return m_addressList; | 159 | return m_addressList; | ||
160 | } | 160 | } | ||
161 | 161 | | |||
162 | QHostAddress address() const | 162 | QHostAddress address() const | ||
it's just weird to have two methods that do the same thing, with a different name. dfaure: it's just weird to have two methods that do the same thing, with a different name. | |||||
163 | { | 163 | { | ||
164 | if (m_addressList.isEmpty()) { | 164 | if (m_addressList.isEmpty()) { | ||
165 | return QHostAddress(); | 165 | return QHostAddress(); | ||
166 | } | 166 | } | ||
167 | 167 | | |||
168 | return m_addressList.first(); | 168 | return m_addressList.first(); | ||
169 | } | 169 | } | ||
170 | 170 | | |||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Line(s) | |||||
222 | 222 | | |||
223 | // isResolvable(host) | 223 | // isResolvable(host) | ||
224 | // @returns true if host is resolvable to a IPv4 address. | 224 | // @returns true if host is resolvable to a IPv4 address. | ||
225 | Q_INVOKABLE QJSValue IsResolvable(QString host) | 225 | Q_INVOKABLE QJSValue IsResolvable(QString host) | ||
226 | { | 226 | { | ||
227 | try { | 227 | try { | ||
228 | const Address info = Address::resolve(host); | 228 | const Address info = Address::resolve(host); | ||
229 | bool hasResolvableIPv4Address = false; | 229 | bool hasResolvableIPv4Address = false; | ||
230 | 230 | for (const QHostAddress &address : info.addresses()) { | |||
231 | Q_FOREACH (const QHostAddress &address, info.addresses()) { | | |||
232 | if (!isSpecialAddress(address) && isIPv4Address(address)) { | 231 | if (!isSpecialAddress(address) && isIPv4Address(address)) { | ||
233 | hasResolvableIPv4Address = true; | 232 | hasResolvableIPv4Address = true; | ||
234 | break; | 233 | break; | ||
235 | } | 234 | } | ||
236 | } | 235 | } | ||
237 | 236 | | |||
238 | return QJSValue(hasResolvableIPv4Address); | 237 | return QJSValue(hasResolvableIPv4Address); | ||
239 | } catch (const Address::Error &) { | 238 | } catch (const Address::Error &) { | ||
240 | return QJSValue(false); | 239 | return QJSValue(false); | ||
241 | } | 240 | } | ||
242 | } | 241 | } | ||
243 | 242 | | |||
244 | // isInNet(host, subnet, mask) | 243 | // isInNet(host, subnet, mask) | ||
245 | // @returns true if the IPv4 address of host is within the specified subnet | 244 | // @returns true if the IPv4 address of host is within the specified subnet | ||
246 | // and mask, false otherwise. | 245 | // and mask, false otherwise. | ||
247 | Q_INVOKABLE QJSValue IsInNet(QString host, QString subnet, QString mask) | 246 | Q_INVOKABLE QJSValue IsInNet(QString host, QString subnet, QString mask) | ||
248 | { | 247 | { | ||
249 | try { | 248 | try { | ||
250 | const Address info = Address::resolve(host); | 249 | const Address info = Address::resolve(host); | ||
251 | bool isInSubNet = false; | 250 | bool isInSubNet = false; | ||
252 | const QString subnetStr = subnet + QLatin1Char('/') + mask; | 251 | const QString subnetStr = subnet + QLatin1Char('/') + mask; | ||
253 | const QPair<QHostAddress, int> subnet = QHostAddress::parseSubnet(subnetStr); | 252 | const QPair<QHostAddress, int> subnet = QHostAddress::parseSubnet(subnetStr); | ||
254 | Q_FOREACH (const QHostAddress &address, info.addresses()) { | 253 | for (const QHostAddress &address : info.addresses()) { | ||
255 | if (!isSpecialAddress(address) && isIPv4Address(address) && address.isInSubnet(subnet)) { | 254 | if (!isSpecialAddress(address) && isIPv4Address(address) && address.isInSubnet(subnet)) { | ||
256 | isInSubNet = true; | 255 | isInSubNet = true; | ||
257 | break; | 256 | break; | ||
258 | } | 257 | } | ||
259 | } | 258 | } | ||
260 | return QJSValue(isInSubNet); | 259 | return QJSValue(isInSubNet); | ||
261 | } catch (const Address::Error &) { | 260 | } catch (const Address::Error &) { | ||
262 | return QJSValue(false); | 261 | return QJSValue(false); | ||
263 | } | 262 | } | ||
264 | } | 263 | } | ||
265 | 264 | | |||
266 | // dnsResolve(host) | 265 | // dnsResolve(host) | ||
267 | // @returns the IPv4 address for host or an empty string if host is not resolvable. | 266 | // @returns the IPv4 address for host or an empty string if host is not resolvable. | ||
268 | Q_INVOKABLE QJSValue DNSResolve(QString host) | 267 | Q_INVOKABLE QJSValue DNSResolve(QString host) | ||
269 | { | 268 | { | ||
270 | try { | 269 | try { | ||
271 | const Address info = Address::resolve(host); | 270 | const Address info = Address::resolve(host); | ||
272 | QString resolvedAddress(QLatin1String("")); | 271 | QString resolvedAddress(QLatin1String("")); | ||
273 | Q_FOREACH (const QHostAddress &address, info.addresses()) { | 272 | for (const QHostAddress &address : info.addresses()) { | ||
274 | if (!isSpecialAddress(address) && isIPv4Address(address)) { | 273 | if (!isSpecialAddress(address) && isIPv4Address(address)) { | ||
275 | resolvedAddress = address.toString(); | 274 | resolvedAddress = address.toString(); | ||
276 | break; | 275 | break; | ||
277 | } | 276 | } | ||
278 | } | 277 | } | ||
279 | return QJSValue(resolvedAddress); | 278 | return QJSValue(resolvedAddress); | ||
280 | } catch (const Address::Error &) { | 279 | } catch (const Address::Error &) { | ||
281 | return QJSValue(QString(QLatin1String(""))); | 280 | return QJSValue(QString(QLatin1String(""))); | ||
▲ Show 20 Lines • Show All 209 Lines • ▼ Show 20 Line(s) | |||||
491 | 490 | | |||
492 | // isResolvableEx(host) | 491 | // isResolvableEx(host) | ||
493 | // @returns true if host is resolvable to an IPv4 or IPv6 address. | 492 | // @returns true if host is resolvable to an IPv4 or IPv6 address. | ||
494 | Q_INVOKABLE QJSValue IsResolvableEx(QString host) | 493 | Q_INVOKABLE QJSValue IsResolvableEx(QString host) | ||
495 | { | 494 | { | ||
496 | try { | 495 | try { | ||
497 | const Address info = Address::resolve(host); | 496 | const Address info = Address::resolve(host); | ||
498 | bool hasResolvableIPAddress = false; | 497 | bool hasResolvableIPAddress = false; | ||
499 | Q_FOREACH (const QHostAddress &address, info.addresses()) { | 498 | for (const QHostAddress &address : info.addresses()) { | ||
500 | if (isIPv4Address(address) || isIPv6Address(address)) { | 499 | if (isIPv4Address(address) || isIPv6Address(address)) { | ||
501 | hasResolvableIPAddress = true; | 500 | hasResolvableIPAddress = true; | ||
502 | break; | 501 | break; | ||
503 | } | 502 | } | ||
504 | } | 503 | } | ||
505 | return QJSValue(hasResolvableIPAddress); | 504 | return QJSValue(hasResolvableIPAddress); | ||
506 | } catch (const Address::Error &) { | 505 | } catch (const Address::Error &) { | ||
507 | return QJSValue(false); | 506 | return QJSValue(false); | ||
508 | } | 507 | } | ||
509 | } | 508 | } | ||
510 | 509 | | |||
511 | // isInNetEx(ipAddress, ipPrefix ) | 510 | // isInNetEx(ipAddress, ipPrefix ) | ||
512 | // @returns true if ipAddress is within the specified ipPrefix. | 511 | // @returns true if ipAddress is within the specified ipPrefix. | ||
513 | Q_INVOKABLE QJSValue IsInNetEx(QString ipAddress, QString ipPrefix) | 512 | Q_INVOKABLE QJSValue IsInNetEx(QString ipAddress, QString ipPrefix) | ||
514 | { | 513 | { | ||
515 | try { | 514 | try { | ||
516 | const Address info = Address::resolve(ipAddress); | 515 | const Address info = Address::resolve(ipAddress); | ||
517 | bool isInSubNet = false; | 516 | bool isInSubNet = false; | ||
518 | const QString subnetStr = ipPrefix; | 517 | const QString subnetStr = ipPrefix; | ||
519 | const QPair<QHostAddress, int> subnet = QHostAddress::parseSubnet(subnetStr); | 518 | const QPair<QHostAddress, int> subnet = QHostAddress::parseSubnet(subnetStr); | ||
520 | 519 | for (const QHostAddress &address : info.addresses()) { | |||
521 | Q_FOREACH (const QHostAddress &address, info.addresses()) { | | |||
522 | if (isSpecialAddress(address)) { | 520 | if (isSpecialAddress(address)) { | ||
523 | continue; | 521 | continue; | ||
524 | } | 522 | } | ||
525 | 523 | | |||
526 | if (address.isInSubnet(subnet)) { | 524 | if (address.isInSubnet(subnet)) { | ||
527 | isInSubNet = true; | 525 | isInSubNet = true; | ||
528 | break; | 526 | break; | ||
529 | } | 527 | } | ||
Show All 9 Lines | |||||
539 | // for host or an empty string if host is not resolvable. | 537 | // for host or an empty string if host is not resolvable. | ||
540 | Q_INVOKABLE QJSValue DNSResolveEx(QString host) | 538 | Q_INVOKABLE QJSValue DNSResolveEx(QString host) | ||
541 | { | 539 | { | ||
542 | try { | 540 | try { | ||
543 | const Address info = Address::resolve(host); | 541 | const Address info = Address::resolve(host); | ||
544 | 542 | | |||
545 | QStringList addressList; | 543 | QStringList addressList; | ||
546 | QString resolvedAddress(QLatin1String("")); | 544 | QString resolvedAddress(QLatin1String("")); | ||
547 | 545 | for (const QHostAddress &address : info.addresses()) { | |||
548 | Q_FOREACH (const QHostAddress &address, info.addresses()) { | | |||
549 | if (!isSpecialAddress(address)) { | 546 | if (!isSpecialAddress(address)) { | ||
550 | addressList << address.toString(); | 547 | addressList << address.toString(); | ||
551 | } | 548 | } | ||
552 | } | 549 | } | ||
553 | if (!addressList.isEmpty()) { | 550 | if (!addressList.isEmpty()) { | ||
554 | resolvedAddress = addressList.join(QLatin1Char(';')); | 551 | resolvedAddress = addressList.join(QLatin1Char(';')); | ||
555 | } | 552 | } | ||
556 | 553 | | |||
▲ Show 20 Lines • Show All 157 Lines • Show Last 20 Lines |
Why not just change this one to return const & and then use it in all range-fors?
[note that returning a const ref is bad in public API, but this is an internal method so we can always change it again if needed]