Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qnetworkrequestfactory.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#if QT_CONFIG(ssl)
8#include <QtNetwork/qsslconfiguration.h>
9#endif
10
11#include <QtCore/qloggingcategory.h>
12#include <QtCore/qmap.h>
13
15
17
18using namespace Qt::StringLiterals;
19
20Q_LOGGING_CATEGORY(lcQrequestfactory, "qt.network.access.request.factory")
21
22
62
79
84 = default;
85
90 = default;
91
96 = default;
97
135{
136 return d->baseUrl;
137}
138
145{
146 if (d->baseUrl == url)
147 return;
148
149 d.detach();
150 d->baseUrl = url;
151}
152
153#if QT_CONFIG(ssl)
160QSslConfiguration QNetworkRequestFactory::sslConfiguration() const
161{
162 return d->sslConfig;
163}
164
170void QNetworkRequestFactory::setSslConfiguration(const QSslConfiguration &configuration)
171{
172 if (d->sslConfig == configuration)
173 return;
174
175 d.detach();
176 d->sslConfig = configuration;
177}
178#endif
179
193
206
219
235
246{
247 d.detach();
248 d->headers = headers;
249}
250
260
267{
268 if (d->headers.isEmpty())
269 return;
270 d.detach();
271 d->headers.clear();
272}
273
297
304{
305 if (d->bearerToken == token)
306 return;
307
308 d.detach();
309 d->bearerToken = token;
310}
311
318{
319 if (d->bearerToken.isEmpty())
320 return;
321
322 d.detach();
323 d->bearerToken.clear();
324}
325
332{
333 return d->userName;
334}
335
347{
348 if (d->userName == userName)
349 return;
350 d.detach();
351 d->userName = userName;
352}
353
358{
359 if (d->userName.isEmpty())
360 return;
361 d.detach();
362 d->userName.clear();
363}
364
371{
372 return d->password;
373}
374
386{
387 if (d->password == password)
388 return;
389 d.detach();
390 d->password = password;
391}
392
399{
400 if (d->password.isEmpty())
401 return;
402 d.detach();
403 d->password.clear();
404}
405
413{
414 if (d->transferTimeout == timeout)
415 return;
416
417 d.detach();
419}
420
427std::chrono::milliseconds QNetworkRequestFactory::transferTimeout() const
428{
429 return d->transferTimeout;
430}
431
448
456{
457 if (d->queryParameters == query)
458 return;
459
460 d.detach();
462}
463
470{
471 if (d->queryParameters.isEmpty())
472 return;
473
474 d.detach();
476}
477
489{
490 if (d->priority == priority)
491 return;
492 d.detach();
493 d->priority = priority;
494}
495
508
538
553
564 const QVariant &defaultValue) const
565{
566 return d->attributes.value(attribute, defaultValue);
567}
568
583
592{
593 if (d->attributes.isEmpty())
594 return;
595 d.detach();
596 d->attributes.clear();
597}
598
600 = default;
601
606
608 = default;
609
611{
614#if QT_CONFIG(ssl)
615 if (!sslConfig.isNull())
616 request.setSslConfiguration(sslConfig);
617#endif
618 auto h = headers;
619 constexpr char Bearer[] = "Bearer ";
620 if (!bearerToken.isEmpty())
622 request.setHeaders(std::move(h));
623
624 request.setTransferTimeout(transferTimeout);
626
627 for (const auto &[attribute, value] : attributes.asKeyValueRange())
629
630 return request;
631}
632
634 const QUrlQuery *query) const
635{
636 const QUrl providedPath = path ? QUrl(*path) : QUrl{};
637 const QUrlQuery providedQuery = query ? *query : QUrlQuery();
638
639 if (!providedPath.scheme().isEmpty() || !providedPath.host().isEmpty()) {
640 qCWarning(lcQrequestfactory, "The provided path %ls may only contain path and query item "
641 "components, and other parts will be ignored. Set the baseUrl instead",
642 qUtf16Printable(providedPath.toDisplayString()));
643 }
644
645 QUrl resultUrl = baseUrl;
646 QUrlQuery resultQuery(providedQuery);
647 QString basePath = baseUrl.path();
648
649 resultUrl.setUserName(userName);
650 resultUrl.setPassword(password);
651
652 // Separate the path and query parameters components on the application-provided path
653 const QString requestPath{providedPath.path()};
654 const QUrlQuery pathQueryItems{providedPath};
655
656 if (!pathQueryItems.isEmpty()) {
657 // Add any query items provided as part of the path
658 const auto items = pathQueryItems.queryItems(QUrl::ComponentFormattingOption::FullyEncoded);
659 for (const auto &[key, value]: items)
660 resultQuery.addQueryItem(key, value);
661 }
662
663 if (!queryParameters.isEmpty()) {
664 // Add any query items set to this factory
665 const QList<std::pair<QString,QString>> items =
667 for (const auto &item: items)
668 resultQuery.addQueryItem(item.first, item.second);
669 }
670
671 if (!resultQuery.isEmpty())
672 resultUrl.setQuery(resultQuery);
673
674 if (requestPath.isEmpty())
675 return resultUrl;
676
677 // Ensure that the "base path" (the path that may be present
678 // in the baseUrl), and the request path are joined with one '/'
679 // If both have it, remove one, if neither has it, add one
680 if (basePath.endsWith(u'/') && requestPath.startsWith(u'/'))
681 basePath.chop(1);
682 else if (!requestPath.startsWith(u'/') && !basePath.endsWith(u'/'))
683 basePath.append(u'/');
684
685 resultUrl.setPath(basePath.append(requestPath));
686 return resultUrl;
687}
688
689#ifndef QT_NO_DEBUG_STREAM
699{
700 const QDebugStateSaver saver(debug);
701 debug.resetFormat().nospace();
702
703 debug << "QNetworkRequestFactory(baseUrl = " << factory.baseUrl()
704 << ", headers = " << factory.commonHeaders()
705 << ", queryParameters = " << factory.queryParameters().queryItems()
706 << ", bearerToken = " << (factory.bearerToken().isEmpty() ? "(empty)" : "(is set)")
707 << ", transferTimeout = " << factory.transferTimeout()
708 << ", userName = " << (factory.userName().isEmpty() ? "(empty)" : "(is set)")
709 << ", password = " << (factory.password().isEmpty() ? "(empty)" : "(is set)")
710#if QT_CONFIG(ssl)
711 << ", SSL configuration"
712 << (factory.sslConfiguration().isNull() ? " is not set (default)" : " is set")
713#else
714 << ", no SSL support"
715#endif
716 << ")";
717 return debug;
718}
719#endif // QT_NO_DEBUG_STREAM
720
\inmodule QtCore
Definition qbytearray.h:57
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
void clear()
Clears the contents of the byte array and makes it null.
\inmodule QtCore
\inmodule QtCore
void detach()
If the shared data object's reference count is greater than 1, this function creates a deep copy of t...
bool remove(const Key &key)
Removes the item that has the key from the hash.
Definition qhash.h:958
auto asKeyValueRange() &
Definition qhash.h:1228
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:1007
T value(const Key &key) const noexcept
Definition qhash.h:1054
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:951
bool isEmpty() const noexcept
Returns true if the hash contains no items; otherwise returns false.
Definition qhash.h:928
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
Q_NETWORK_EXPORT void clear()
Clears all header entries.
bool isEmpty() const noexcept
Returns true if the headers have size 0; otherwise returns false.
Q_NETWORK_EXPORT bool replaceOrAppend(QAnyStringView name, QAnyStringView newValue)
void append(parameter_type t)
Definition qlist.h:458
std::chrono::milliseconds transferTimeout
QHash< QNetworkRequest::Attribute, QVariant > attributes
QNetworkRequest newRequest(const QUrl &url) const
QUrl requestUrl(const QString *path=nullptr, const QUrlQuery *query=nullptr) const
Convenience class for grouping remote server endpoints that share common network request properties.
Q_NETWORK_EXPORT void setQueryParameters(const QUrlQuery &query)
Sets query parameters that are added to individual requests' query parameters.
Q_NETWORK_EXPORT void clearUserName()
Clears the username set to this factory.
Q_NETWORK_EXPORT void setBaseUrl(const QUrl &url)
Sets the base URL used in individual requests to url.
Q_NETWORK_EXPORT QByteArray bearerToken() const
Returns the bearer token that has been set.
Q_NETWORK_EXPORT ~QNetworkRequestFactory()
Destroys this QNetworkRequestFactory object.
Q_NETWORK_EXPORT void clearBearerToken()
Clears the bearer token.
Q_NETWORK_EXPORT QNetworkRequest::Priority priority() const
Q_NETWORK_EXPORT void setBearerToken(const QByteArray &token)
Sets the bearer token to token.
Q_NETWORK_EXPORT void setAttribute(QNetworkRequest::Attribute attribute, const QVariant &value)
Q_NETWORK_EXPORT QNetworkRequestFactory & operator=(const QNetworkRequestFactory &other)
Creates a copy of other and returns a reference to this factory.
Q_NETWORK_EXPORT void clearQueryParameters()
Clears the query parameters.
Q_NETWORK_EXPORT void clearPassword()
Clears the password set to this factory.
Q_NETWORK_EXPORT void clearCommonHeaders()
Clears current headers.
Q_NETWORK_EXPORT void clearAttributes()
Q_NETWORK_EXPORT std::chrono::milliseconds transferTimeout() const
Returns the timeout used for transfers.
Q_NETWORK_EXPORT void setUserName(const QString &userName)
Sets the username of this factory to userName.
Q_NETWORK_EXPORT void setTransferTimeout(std::chrono::milliseconds timeout)
Sets timeout used for transfers.
Q_NETWORK_EXPORT void setPassword(const QString &password)
Sets the password of this factory to password.
Q_NETWORK_EXPORT void setPriority(QNetworkRequest::Priority priority)
Q_NETWORK_EXPORT void clearAttribute(QNetworkRequest::Attribute attribute)
Q_NETWORK_EXPORT QHttpHeaders commonHeaders() const
Returns the currently set headers.
Q_NETWORK_EXPORT void setCommonHeaders(const QHttpHeaders &headers)
Sets headers that are common to all requests.
Q_NETWORK_EXPORT QString password() const
Returns the password set to this factory.
Q_NETWORK_EXPORT QNetworkRequest createRequest() const
Returns a QNetworkRequest.
Q_NETWORK_EXPORT QString userName() const
Returns the username set to this factory.
Q_NETWORK_EXPORT QVariant attribute(QNetworkRequest::Attribute attribute) const
Q_NETWORK_EXPORT QUrl baseUrl() const
Returns the base URL used for the individual requests.
Q_NETWORK_EXPORT QNetworkRequestFactory()
Creates a new QNetworkRequestFactory object.
Q_NETWORK_EXPORT QUrlQuery queryParameters() const
Returns query parameters that are added to individual requests' query parameters.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
void setHeaders(const QHttpHeaders &newHeaders)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setSslConfiguration(const QSslConfiguration &configuration)
Sets this network request's SSL configuration to be config.
void setAttribute(Attribute code, const QVariant &value)
Sets the attribute associated with code code to be value value.
void setUrl(const QUrl &url)
Sets the URL this network request is referring to be url.
void setPriority(Priority priority)
The QSslConfiguration class holds the configuration and state of an SSL connection.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
\inmodule QtCore
Definition qurlquery.h:20
void clear()
Clears this QUrlQuery object by removing all of the key-value pairs currently stored.
bool isEmpty() const
Returns true if this QUrlQuery object contains no key-value pairs, such as after being default-constr...
QList< std::pair< QString, QString > > queryItems(QUrl::ComponentFormattingOptions encoding=QUrl::PrettyDecoded) const
Returns the query string of the URL, as a map of keys and values, using the options specified in enco...
\inmodule QtCore
Definition qurl.h:94
@ FullyEncoded
Definition qurl.h:129
QString path(ComponentFormattingOptions options=FullyDecoded) const
Returns the path of the URL.
Definition qurl.cpp:2468
\inmodule QtCore
Definition qvariant.h:65
Token token
Definition keywords.cpp:444
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
EGLOutputLayerEXT EGLint attribute
#define Q_LOGGING_CATEGORY(name,...)
#define qCWarning(category,...)
QDebug operator<<(QDebug debug, const QNetworkRequestFactory &factory)
GLuint64 key
GLbitfield GLuint64 timeout
[4]
GLfloat GLfloat GLfloat GLfloat h
GLenum query
GLsizei const GLchar *const * path
#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
#define qUtf16Printable(string)
Definition qstring.h:1543
#define QT_CONFIG(feature)
QUrl url("example.com")
[constructor-url-reference]
QUrl baseUrl
QSharedPointer< T > other(t)
[5]
QGraphicsItem * item
QItemEditorFactory * factory
QList< QTreeWidgetItem * > items
QNetworkRequest request(url)