4#include <QtNetwork/private/qtnetworkglobal_p.h>
17#if QT_CONFIG(settings)
30#include "QtCore/qbuffer.h"
31#include "QtCore/qlist.h"
32#include "QtCore/qurl.h"
33#include "QtNetwork/private/qauthenticator_p.h"
34#include "QtNetwork/qsslconfiguration.h"
37#include "QtNetwork/private/http2protocol_p.h"
47#include "QtCore/qapplicationstatic.h"
48#include "QtCore/qloggingcategory.h"
49#include <QtCore/private/qfactoryloader_p.h>
51#if defined(Q_OS_MACOS)
52#include <QtCore/private/qcore_mac_p.h>
54#include <CoreServices/CoreServices.h>
55#include <SystemConfiguration/SystemConfiguration.h>
56#include <Security/Security.h>
71using namespace std::chrono_literals;
77#if QT_CONFIG(private_tests)
83#if defined(Q_OS_MACOS)
86 CFStringRef protocolType =
nullptr;
88 protocolType = kSecAttrProtocolFTPProxy;
91 protocolType = kSecAttrProtocolHTTPProxy;
94 protocolType = kSecAttrProtocolHTTPSProxy;
96 qCWarning(lcQnam) <<
"Cannot query user name and password for a proxy, unnknown protocol:"
101 QCFType<CFMutableDictionaryRef>
query(CFDictionaryCreateMutable(kCFAllocatorDefault,
102 0,
nullptr,
nullptr));
105 CFDictionaryAddValue(
query, kSecClass, kSecClassInternetPassword);
106 CFDictionaryAddValue(
query, kSecAttrProtocol, protocolType);
108 QCFType<CFStringRef> serverName;
109 if (proxyHostname.size()) {
110 serverName = proxyHostname.toCFString();
111 CFDictionaryAddValue(
query, kSecAttrServer, serverName);
115 CFDictionaryAddValue(
query, kSecReturnAttributes, kCFBooleanTrue);
117 CFDictionaryAddValue(
query, kSecReturnData, kCFBooleanTrue);
122 QCFType<CFTypeRef> replyData;
123 if (SecItemCopyMatching(
query, &replyData) != errSecSuccess) {
124 qCWarning(lcQnam,
"Failed to extract user name and password from the keychain.");
128 if (!replyData || CFDictionaryGetTypeID() != CFGetTypeID(replyData)) {
129 qCWarning(lcQnam,
"Query returned data in unexpected format.");
133 CFDictionaryRef accountData = replyData.as<CFDictionaryRef>();
134 const void *
value = CFDictionaryGetValue(accountData, kSecAttrAccount);
135 if (!
value || CFGetTypeID(
value) != CFStringGetTypeID()) {
136 qCWarning(lcQnam,
"Cannot find user name or its format is unknown.");
139 username = QString::fromCFString(
static_cast<CFStringRef
>(
value));
141 value = CFDictionaryGetValue(accountData, kSecValueData);
142 if (!
value || CFGetTypeID(
value) != CFDataGetTypeID()) {
143 qCWarning(lcQnam,
"Cannot find password or its format is unknown.");
146 const CFDataRef passData =
static_cast<const CFDataRef
>(
value);
157#if QT_CONFIG(private_tests)
158 (
void) debugpipeBackend();
162 (
void) fileBackend();
400 d_func()->ensureBackendPluginsLoaded();
402 qRegisterMetaType<QNetworkReply::NetworkError>();
403#ifndef QT_NO_NETWORKPROXY
404 qRegisterMetaType<QNetworkProxy>();
407 qRegisterMetaType<QList<QSslError> >();
408 qRegisterMetaType<QSslConfiguration>();
409 qRegisterMetaType<QSslPreSharedKeyAuthenticator *>();
411 qRegisterMetaType<QList<QPair<QByteArray,QByteArray> > >();
413 qRegisterMetaType<QHttpNetworkRequest>();
415 qRegisterMetaType<QNetworkReply::NetworkError>();
416 qRegisterMetaType<QSharedPointer<char> >();
428#ifndef QT_NO_NETWORKPROXY
429 delete d_func()->proxyFactory;
442#ifndef QT_NO_NETWORKPROXY
452 return d_func()->proxy;
472 delete d->proxyFactory;
474 d->proxyFactory =
nullptr;
491 return d_func()->proxyFactory;
526 delete d->proxyFactory;
542 return d->networkCache;
564 if (
d->networkCache !=
cache) {
565 delete d->networkCache;
583 d->createCookieJar();
619 d->cookieJarCreated =
true;
621 if (
d->cookieJar &&
d->cookieJar->parent() ==
this)
661 return d->stsEnabled;
686#if QT_CONFIG(settings)
689 d->stsCache.setStore(
d->stsStore.data());
693 qWarning(
"HSTS permanent store requires the feature 'settings' enabled");
708#if QT_CONFIG(settings)
710 return bool(
d->stsStore.data());
739 d->stsCache.updateFromPolicies(knownHosts);
754 return d->stsCache.policies();
797 return d_func()->postProcess(
877#if QT_CONFIG(http) || defined(Q_OS_WASM)
1113 return d->redirectPolicy;
1161#if QT_CONFIG(http) || defined(Q_OS_WASM)
1210#if QT_CONFIG(http) || defined (Q_OS_WASM)
1211 if (req.transferTimeoutAsDuration() == 0ms)
1224 if (scheme ==
"local+http"_L1) {
1225 scheme = u
"unix+http"_s;
1237 || scheme ==
"assets"_L1
1239 || scheme ==
"qrc"_L1) {
1243 if (scheme ==
"data"_L1)
1255 priv->manager =
this;
1257 priv->backend->setManagerPrivate(this->d_func());
1259 priv->backend->setReplyPrivate(
priv);
1260 priv->setup(op, req, outgoingData);
1279 QList<QNetworkCookie> cookies =
d->cookieJar->cookiesForUrl(
request.
url());
1280 if (!cookies.isEmpty())
1289 if (scheme ==
"http"_L1 || scheme ==
"https"_L1 || scheme.
isEmpty()) {
1292 priv->manager =
this;
1299 constexpr char16_t httpSchemes[][17] = {
1304 u
"preconnect-https",
1309 if (std::find(std::begin(httpSchemes), std::end(httpSchemes), scheme) != std::end(httpSchemes)) {
1312 const bool isLocalSocket = scheme.
startsWith(
"unix"_L1);
1325 if (stsUrl.port() == 80)
1326 stsUrl.setPort(443);
1327 stsUrl.setScheme(
"https"_L1);
1339 priv->manager =
this;
1348 if (
priv->backend) {
1350 priv->backend->setReplyPrivate(
priv);
1378 schemes.removeDuplicates();
1457 return d_func()->autoDeleteReplies;
1476 d_func()->autoDeleteReplies = shouldAutoDelete;
1510 return d_func()->transferTimeout;
1532 d_func()->transferTimeout = duration;
1572 emit q->preSharedKeyAuthenticationRequired(
reply, authenticator);
1585 q->connect(
reply, &QNetworkReply::encrypted,
reply,
1600 that->cookieJarCreated =
true;
1608 QUrl *urlForLastAuthentication,
1609 bool allowAuthenticationReuse)
1616 if (allowAuthenticationReuse && (urlForLastAuthentication->
isEmpty()
1617 ||
url != *urlForLastAuthentication)) {
1624 *urlForLastAuthentication =
url;
1631 && (cred.user != authenticator->
user() || cred.password != authenticator->
password())) {
1632 authenticator->
setUser(cred.user);
1634 *urlForLastAuthentication =
url;
1644 *urlForLastAuthentication =
url;
1645 emit q->authenticationRequired(
reply, authenticator);
1646 if (allowAuthenticationReuse)
1650#ifndef QT_NO_NETWORKPROXY
1659 if (
proxy != *lastProxyAuthentication && (!
priv || !
priv->hasFailed)) {
1661 if (!cred.isNull()) {
1662 authenticator->
setUser(cred.user);
1668#if defined(Q_OS_MACOS)
1677 if (!
priv->hasFailed || cred.user != username || cred.password != password) {
1678 authenticator->
setUser(username);
1693 *lastProxyAuthentication =
proxy;
1694 emit q->proxyAuthenticationRequired(
proxy, authenticator);
1700 QList<QNetworkProxy> proxies;
1703 if (proxies.isEmpty()) {
1704 qWarning(
"QNetworkAccessManager: factory %p has returned an empty result set",
1721 manager->d_func()->authenticationManager->clearCache();
1726 manager->d_func()->objectCache.clear();
1727 manager->d_func()->destroyThread();
1760#if QT_CONFIG(http) || defined(Q_OS_WASM)
1772 contentType +=
"multipart/";
1773 switch (
multiPart->d_func()->contentType) {
1775 contentType +=
"related";
1778 contentType +=
"form-data";
1781 contentType +=
"alternative";
1784 contentType +=
"mixed";
1800 if (!
device->isReadable()) {
1803 qWarning(
"could not open device for reading");
1805 qWarning(
"device is not readable");
1821 std::unique_lock locker(
mutex);
1824#if QT_CONFIG(library)
1825 qnabfLoader->update();
1828 while (qnabfLoader->instance(
index))
1834#include "moc_qnetworkaccessmanager.cpp"
IOBluetoothDevice * device
The QAbstractNetworkCache class provides the interface for cache implementations.
static QAuthenticatorPrivate * getPrivate(QAuthenticator &auth)
The QAuthenticator class provides an authentication object.
QString user() const
Returns the user used for authentication.
void setPassword(const QString &password)
Sets the password used for authentication.
QString password() const
Returns the password used for authentication.
void setUser(const QString &user)
Sets the user used for authentication.
\inmodule QtCore \reentrant
void setData(const QByteArray &data)
Sets the contents of the internal buffer to be data.
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
void reserve(qsizetype size)
Attempts to allocate memory for at least size bytes.
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
The QHttpMultiPart class resembles a MIME multipart message to be sent over HTTP.
QByteArray boundary() const
returns the boundary.
\inmodule QtCore \reentrant
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
QList< QNetworkProxy > queryProxy(const QNetworkProxyQuery &query)
QNetworkReply * postProcess(QNetworkReply *reply)
QNetworkProxyFactory * proxyFactory
void proxyAuthenticationRequired(const QUrl &url, const QNetworkProxy &proxy, bool synchronous, QAuthenticator *authenticator, QNetworkProxy *lastProxyAuthentication)
void _q_replyPreSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator)
void createCookieJar() const
void ensureBackendPluginsLoaded()
void authenticationRequired(QAuthenticator *authenticator, QNetworkReply *reply, bool synchronous, QUrl &url, QUrl *urlForLastAuthentication, bool allowAuthenticationReuse=true)
QNetworkCookieJar * cookieJar
std::shared_ptr< QNetworkAccessAuthenticationManager > authenticationManager
~QNetworkAccessManagerPrivate()
void _q_replyFinished(QNetworkReply *reply)
void _q_replyEncrypted(QNetworkReply *reply)
void _q_replySslErrors(const QList< QSslError > &errors)
static Q_AUTOTEST_EXPORT void clearConnectionCache(QNetworkAccessManager *manager)
static Q_AUTOTEST_EXPORT void clearAuthenticationCache(QNetworkAccessManager *manager)
The QNetworkAccessManager class allows the application to send network requests and receive replies.
friend class QNetworkReplyHttpImpl
void setCookieJar(QNetworkCookieJar *cookieJar)
Sets the manager's cookie jar to be the cookieJar specified.
QNetworkReply * put(const QNetworkRequest &request, QIODevice *data)
Uploads the contents of data to the destination request and returns a new QNetworkReply object that w...
void connectToHost(const QString &hostName, quint16 port=80)
~QNetworkAccessManager()
Destroys the QNetworkAccessManager object and frees up any resources.
QNetworkReply * head(const QNetworkRequest &request)
Posts a request to obtain the network headers for request and returns a new QNetworkReply object whic...
bool isStrictTransportSecurityStoreEnabled() const
QList< QHstsPolicy > strictTransportSecurityHosts() const
virtual QStringList supportedSchemes() const
QNetworkRequest::RedirectPolicy redirectPolicy() const
QNetworkReply * post(const QNetworkRequest &request, QIODevice *data)
Sends an HTTP POST request to the destination specified by request and returns a new QNetworkReply ob...
void setProxy(const QNetworkProxy &proxy)
Sets the proxy to be used in future requests to be proxy.
QStringList supportedSchemesImplementation() const
void setAutoDeleteReplies(bool autoDelete)
void setTransferTimeout(int timeout)
bool isStrictTransportSecurityEnabled() const
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
void setRedirectPolicy(QNetworkRequest::RedirectPolicy policy)
QNetworkReply * sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QIODevice *data=nullptr)
QNetworkProxy proxy() const
Returns the QNetworkProxy that the requests sent using this QNetworkAccessManager object will use.
virtual QNetworkReply * createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData=nullptr)
Returns a new QNetworkReply object to handle the operation op and request originalReq.
void setStrictTransportSecurityEnabled(bool enabled)
void clearConnectionCache()
std::chrono::milliseconds transferTimeoutAsDuration() const
QAbstractNetworkCache * cache() const
friend class QNetworkReplyFileImpl
void addStrictTransportSecurityHosts(const QList< QHstsPolicy > &knownHosts)
Operation
Indicates the operation this reply is processing.
QNetworkReply * deleteResource(const QNetworkRequest &request)
bool autoDeleteReplies() const
void connectToHostEncrypted(const QString &hostName, quint16 port=443, const QSslConfiguration &sslConfiguration=QSslConfiguration::defaultConfiguration())
void setProxyFactory(QNetworkProxyFactory *factory)
QNetworkProxyFactory * proxyFactory() const
void setCache(QAbstractNetworkCache *cache)
QNetworkCookieJar * cookieJar() const
Returns the QNetworkCookieJar that is used to store cookies obtained from the network as well as cook...
void enableStrictTransportSecurityStore(bool enabled, const QString &storeDir=QString())
QNetworkAccessManager(QObject *parent=nullptr)
Constructs a QNetworkAccessManager object that is the center of the Network Access API and sets paren...
The QNetworkCookieJar class implements a simple jar of QNetworkCookie objects.
The QNetworkProxyFactory class provides fine-grained proxy selection.
virtual QList< QNetworkProxy > queryProxy(const QNetworkProxyQuery &query=QNetworkProxyQuery())=0
This function takes the query request, query, examines the details of the type of socket or request a...
static QList< QNetworkProxy > proxyForQuery(const QNetworkProxyQuery &query)
This function takes the query request, query, examines the details of the type of socket or request a...
The QNetworkProxyQuery class is used to query the proxy settings for a socket.
The QNetworkProxy class provides a network layer proxy.
QNetworkProxy::ProxyType type() const
Returns the proxy type for this instance.
QString hostName() const
Returns the host name of the proxy host.
static void setManager(QNetworkReply *reply, QNetworkAccessManager *manager)
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
void finished()
This signal is emitted when the reply has finished processing.
QNetworkRequest request() const
Returns the request that was posted for this reply.
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.
@ CookieLoadControlAttribute
@ AutoDeleteReplyOnFinishAttribute
@ CacheLoadControlAttribute
@ RedirectPolicyAttribute
void setAttribute(Attribute code, const QVariant &value)
Sets the attribute associated with code code to be value value.
QVariant attribute(Attribute code, const QVariant &defaultValue=QVariant()) const
Returns the attribute associated with the code code.
@ NoLessSafeRedirectPolicy
void setPeerVerifyName(const QString &peerName)
QHttpHeaders headers() const
void setUrl(const QUrl &url)
Sets the URL this network request is referring to be url.
QUrl url() const
Returns the URL this network request is referring to.
CacheLoadControl
Controls the caching mechanism of QNetworkAccessManager.
QSslConfiguration sslConfiguration() const
Returns this network request's SSL configuration.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
void setParent(QObject *parent)
Makes the object a child of parent.
QThread * thread() const
Returns the thread in which the object lives.
Q_WEAK_OVERLOAD void setObjectName(const QString &name)
Sets the object's name to name.
void deleteLater()
\threadsafe
The QSslConfiguration class holds the configuration and state of an SSL connection.
QList< QByteArray > allowedNextProtocols() const
static const char ALPNProtocolHTTP2[]
static QSslConfiguration defaultConfiguration()
Returns the default SSL configuration to be used in new SSL connections.
The QSslPreSharedKeyAuthenticator class provides authentication data for pre shared keys (PSK) cipher...
static bool supportsSsl()
Returns true if this platform supports SSL; otherwise, returns false.
\macro QT_RESTRICTED_CAST_FROM_ASCII
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
int compare(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
void start(Priority=InheritPriority)
bool wait(QDeadlineTimer deadline=QDeadlineTimer(QDeadlineTimer::Forever))
QString userName(ComponentFormattingOptions options=FullyDecoded) const
Returns the user name of the URL if it is defined; otherwise an empty string is returned.
QString password(ComponentFormattingOptions=FullyDecoded) const
Returns the password of the URL if it is defined; otherwise an empty string is returned.
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
QString scheme() const
Returns the scheme of the URL.
void setScheme(const QString &scheme)
Sets the scheme of the URL to scheme.
void setHost(const QString &host, ParsingMode mode=DecodedMode)
Sets the host of the URL to host.
void setPort(int port)
Sets the port of the URL to port.
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
bool toBool() const
Returns the variant as a bool if the variant has userType() Bool.
bool isNull() const
Returns true if this is a null variant, false otherwise.
qDeleteAll(list.begin(), list.end())
QCache< int, Employee > cache
[0]
Combined button and popup list for selecting options.
#define Q_APPLICATION_STATIC(TYPE, NAME,...)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define Q_LOGGING_CATEGORY(name,...)
#define qCWarning(category,...)
#define QNetworkAccessBackendFactory_iid
static void ensureInitialized()
#define Q_RETURN_ARG(Type, data)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLfloat GLfloat GLfloat GLfloat h
GLdouble GLdouble GLdouble GLdouble q
#define QStringLiteral(str)
QUrl url("example.com")
[constructor-url-reference]
QItemEditorFactory * factory
QNetworkAccessManager manager
QHttpMultiPart * multiPart
[0]
QNetworkRequest request(url)
bool contains(const AT &t) const noexcept