Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qnetworkrequest.h
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
5#ifndef QNETWORKREQUEST_H
6#define QNETWORKREQUEST_H
7
8#include <QtNetwork/qtnetworkglobal.h>
9#include <QtNetwork/qhttpheaders.h>
10
11#include <QtCore/qassert.h>
12#include <QtCore/QSharedDataPointer>
13#include <QtCore/QString>
14#include <QtCore/QUrl>
15#include <QtCore/QVariant>
16
17#include <QtCore/q26numeric.h>
18#include <QtCore/q20utility.h>
19
20#include <chrono>
21
22QT_BEGIN_NAMESPACE
23
24class QSslConfiguration;
25class QHttp2Configuration;
27
28class QNetworkRequestPrivate;
29class Q_NETWORK_EXPORT QNetworkRequest
30{
31 Q_GADGET
32public:
33 enum KnownHeaders {
34 ContentTypeHeader,
35 ContentLengthHeader,
36 LocationHeader,
37 LastModifiedHeader,
38 CookieHeader,
39 SetCookieHeader,
40 ContentDispositionHeader, // added for QMultipartMessage
41 UserAgentHeader,
42 ServerHeader,
43 IfModifiedSinceHeader,
44 ETagHeader,
45 IfMatchHeader,
46 IfNoneMatchHeader,
47 NumKnownHeaders
48 };
49 Q_ENUM(KnownHeaders)
50
51 enum Attribute {
52 HttpStatusCodeAttribute,
53 HttpReasonPhraseAttribute,
54 RedirectionTargetAttribute,
55 ConnectionEncryptedAttribute,
56 CacheLoadControlAttribute,
57 CacheSaveControlAttribute,
58 SourceIsFromCacheAttribute,
59 DoNotBufferUploadDataAttribute,
60 HttpPipeliningAllowedAttribute,
61 HttpPipeliningWasUsedAttribute,
62 CustomVerbAttribute,
63 CookieLoadControlAttribute,
64 AuthenticationReuseAttribute,
65 CookieSaveControlAttribute,
66 MaximumDownloadBufferSizeAttribute, // internal
67 DownloadBufferAttribute, // internal
68 SynchronousRequestAttribute, // internal
69 BackgroundRequestAttribute,
70 EmitAllUploadProgressSignalsAttribute,
71 Http2AllowedAttribute,
72 Http2WasUsedAttribute,
73 OriginalContentLengthAttribute,
74 RedirectPolicyAttribute,
75 Http2DirectAttribute,
76 ResourceTypeAttribute, // internal
77 AutoDeleteReplyOnFinishAttribute,
78 ConnectionCacheExpiryTimeoutSecondsAttribute,
79 Http2CleartextAllowedAttribute,
80 UseCredentialsAttribute,
81 FullLocalServerNameAttribute,
82
83 User = 1000,
84 UserMax = 32767
85 };
86 enum CacheLoadControl {
87 AlwaysNetwork,
88 PreferNetwork,
89 PreferCache,
90 AlwaysCache
91 };
92 enum LoadControl {
93 Automatic = 0,
94 Manual
95 };
96
97 enum Priority {
98 HighPriority = 1,
99 NormalPriority = 3,
100 LowPriority = 5
101 };
102
103 enum RedirectPolicy {
104 ManualRedirectPolicy,
105 NoLessSafeRedirectPolicy,
106 SameOriginRedirectPolicy,
107 UserVerifiedRedirectPolicy
108 };
109
110 enum TransferTimeoutConstant {
111 DefaultTransferTimeoutConstant = 30000
112 };
113
114 static constexpr auto DefaultTransferTimeout =
115 std::chrono::milliseconds(DefaultTransferTimeoutConstant);
116
117 QNetworkRequest();
118 explicit QNetworkRequest(const QUrl &url);
119 QNetworkRequest(const QNetworkRequest &other);
120 ~QNetworkRequest();
121 QNetworkRequest &operator=(QNetworkRequest &&other) noexcept { swap(other); return *this; }
122 QNetworkRequest &operator=(const QNetworkRequest &other);
123
124 void swap(QNetworkRequest &other) noexcept { d.swap(other.d); }
125
126 bool operator==(const QNetworkRequest &other) const;
127 inline bool operator!=(const QNetworkRequest &other) const
128 { return !operator==(other); }
129
130 QUrl url() const;
131 void setUrl(const QUrl &url);
132
133 QHttpHeaders headers() const;
134 void setHeaders(const QHttpHeaders &newHeaders);
135 void setHeaders(QHttpHeaders &&newHeaders);
136
137 // "cooked" headers
138 QVariant header(KnownHeaders header) const;
139 void setHeader(KnownHeaders header, const QVariant &value);
140
141 // raw headers:
142#if QT_NETWORK_REMOVED_SINCE(6, 7)
143 bool hasRawHeader(const QByteArray &headerName) const;
144#endif
145 bool hasRawHeader(QAnyStringView headerName) const;
146 QList<QByteArray> rawHeaderList() const;
147#if QT_NETWORK_REMOVED_SINCE(6, 7)
148 QByteArray rawHeader(const QByteArray &headerName) const;
149#endif
150 QByteArray rawHeader(QAnyStringView headerName) const;
151 void setRawHeader(const QByteArray &headerName, const QByteArray &value);
152
153 // attributes
154 QVariant attribute(Attribute code, const QVariant &defaultValue = QVariant()) const;
155 void setAttribute(Attribute code, const QVariant &value);
156
157#ifndef QT_NO_SSL
158 QSslConfiguration sslConfiguration() const;
159 void setSslConfiguration(const QSslConfiguration &configuration);
160#endif
161
162 void setOriginatingObject(QObject *object);
163 QObject *originatingObject() const;
164
165 Priority priority() const;
166 void setPriority(Priority priority);
167
168 // HTTP redirect related
169 int maximumRedirectsAllowed() const;
170 void setMaximumRedirectsAllowed(int maximumRedirectsAllowed);
171
172 QString peerVerifyName() const;
173 void setPeerVerifyName(const QString &peerName);
174#if QT_CONFIG(http)
175 QHttp1Configuration http1Configuration() const;
176 void setHttp1Configuration(const QHttp1Configuration &configuration);
177
178 QHttp2Configuration http2Configuration() const;
179 void setHttp2Configuration(const QHttp2Configuration &configuration);
180
181 qint64 decompressedSafetyCheckThreshold() const;
182 void setDecompressedSafetyCheckThreshold(qint64 threshold);
183#endif // QT_CONFIG(http)
184 std::chrono::seconds tcpKeepAliveIdleTimeBeforeProbes() const;
185 void setTcpKeepAliveIdleTimeBeforeProbes(std::chrono::seconds idle)
186 {
187 const auto r = q26::saturate_cast<int>(idle.count());
188 Q_PRE(q20::cmp_equal(r, idle.count()));
189 doSetIdleTimeBeforeProbes(std::chrono::duration<int>(r));
190 }
191 std::chrono::seconds tcpKeepAliveIntervalBetweenProbes() const;
192 void setTcpKeepAliveIntervalBetweenProbes(std::chrono::seconds interval)
193 {
194 const auto r = q26::saturate_cast<int>(interval.count());
195 Q_PRE(q20::cmp_equal(r, interval.count()));
196 doSetIntervalBetweenProbes(std::chrono::duration<int>(r));
197 }
198 int tcpKeepAliveProbeCount() const;
199 void setTcpKeepAliveProbeCount(int probes);
200
201#if QT_CONFIG(http) || defined (Q_OS_WASM)
202 QT_NETWORK_INLINE_SINCE(6, 8)
203 int transferTimeout() const;
204 QT_NETWORK_INLINE_SINCE(6, 8)
205 void setTransferTimeout(int timeout);
206
207 std::chrono::milliseconds transferTimeoutAsDuration() const;
208 void setTransferTimeout(std::chrono::milliseconds duration = DefaultTransferTimeout);
209#endif // QT_CONFIG(http) || defined (Q_OS_WASM)
210private:
211 void doSetIdleTimeBeforeProbes(std::chrono::duration<int> idle);
212 void doSetIntervalBetweenProbes(std::chrono::duration<int> interval);
213 QSharedDataPointer<QNetworkRequestPrivate> d;
214 friend class QNetworkRequestPrivate;
215};
216
218
219#if QT_NETWORK_INLINE_IMPL_SINCE(6, 8)
220#if QT_CONFIG(http) || defined (Q_OS_WASM)
221int QNetworkRequest::transferTimeout() const
222{
223 return q26::saturate_cast<int>(transferTimeoutAsDuration().count());
224}
225
226void QNetworkRequest::setTransferTimeout(int timeout)
227{
228 setTransferTimeout(std::chrono::milliseconds(timeout));
229}
230#endif // QT_CONFIG(http) || defined (Q_OS_WASM)
231#endif // INLINE_SINCE 6.8
232
233QT_END_NAMESPACE
234
235QT_DECL_METATYPE_EXTERN(QNetworkRequest, Q_NETWORK_EXPORT)
236QT_DECL_METATYPE_EXTERN_TAGGED(QNetworkRequest::RedirectPolicy,
237 QNetworkRequest__RedirectPolicy, Q_NETWORK_EXPORT)
238
239#endif
The QAbstractNetworkCache class provides the interface for cache implementations.
\inmodule QtCore\reentrant
Definition qdatastream.h:50
The QHttp1Configuration class controls HTTP/1 parameters and settings.
static void save(QDataStream &out, const QNetworkCacheMetaData &metaData)
bool operator==(const QNetworkCacheMetaDataPrivate &other) const
static void load(QDataStream &in, QNetworkCacheMetaData &metaData)
The QNetworkCacheMetaData class provides cache information.
QByteArray rawHeader(QAnyStringView headerName) const
QHash< QNetworkRequest::Attribute, QVariant > AttributesMap
static std::optional< qint64 > toInt(QByteArrayView value)
static QHttpHeaders fromRawToHttp(const RawHeadersList &raw)
QList< QNetworkCookie > NetworkCookieList
static QByteArray fromCookieList(const NetworkCookieList &cookies)
void setCookedHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)
QPointer< QObject > originatingObject
std::pair< QByteArray, QByteArray > RawHeaderPair
QList< QByteArray > rawHeadersKeys() const
static RawHeadersList fromHttpToRaw(const QHttpHeaders &headers)
static std::optional< NetworkCookieList > toCookieList(const QList< QByteArray > &values)
QHttpHeaders headers() const
void setHeaders(QHttpHeaders &&newHeaders)
static std::optional< NetworkCookieList > toSetCookieList(const QList< QByteArray > &values)
void setHeaders(const QHttpHeaders &newHeaders)
const RawHeadersList & allRawHeaders() const
void setRawHeader(const QByteArray &key, const QByteArray &value)
void setHeader(QHttpHeaders::WellKnownHeader name, QByteArrayView value)
QList< RawHeaderPair > RawHeadersList
QHash< QNetworkRequest::KnownHeaders, QVariant > CookedHeadersMap
static QByteArray toHttpDate(const QDateTime &dt)
static QDateTime fromHttpDate(QByteArrayView value)
CookedHeadersMap cookedHeaders
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
Combined button and popup list for selecting options.
Q_DECLARE_TYPEINFO(QObjectPrivate::ConnectionList, Q_RELOCATABLE_TYPE)