summaryrefslogtreecommitdiffstats
path: root/src/network/access/qnetworkrequestfactory.cpp
blob: 87738d9086b5250f19b68a6614093025c6b38158 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qnetworkrequestfactory.h"
#include "qnetworkrequestfactory_p.h"

#if QT_CONFIG(ssl)
#include <QtNetwork/qsslconfiguration.h>
#endif

#include <QtCore/qloggingcategory.h>
#include <QtCore/qmap.h>

QT_BEGIN_NAMESPACE

QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QNetworkRequestFactoryPrivate)

using namespace Qt::StringLiterals;

Q_LOGGING_CATEGORY(lcQrequestfactory, "qt.network.access.request.factory")

/*!
    \class QNetworkRequestFactory
    \since 6.7
    \ingroup shared
    \inmodule QtNetwork

    \brief Convenience class for grouping remote server endpoints that share
    common network request properties.

    \preliminary

    REST servers often have endpoints that require the same headers and other data.
    Grouping such endpoints with a QNetworkRequestFactory makes it more
    convenient to issue requests to these endpoints; only the typically
    varying parts such as \e path and \e query parameters are provided
    when creating a new request.

    Basic usage steps of QNetworkRequestFactory are as follows:
    \list
        \li Instantiation
        \li Setting the data common to all requests
        \li Issuing requests
    \endlist

    An example of usage:

    \snippet code/src_network_access_qnetworkrequestfactory.cpp 0
*/

/*!
    Creates a new QNetworkRequestFactory object.
    Use setBaseUrl() to set a valid base URL for the requests.

    \sa QNetworkRequestFactory(const QUrl &baseUrl), setBaseUrl()
*/

QNetworkRequestFactory::QNetworkRequestFactory()
    : d(new QNetworkRequestFactoryPrivate)
{
}

/*!
    Creates a new QNetworkRequestFactory object, initializing the base URL to
    \a baseUrl. The base URL is used to populate subsequent network
    requests.

    If the URL contains a \e path component, it will be extracted and used
    as a base path in subsequent network requests. This means that any
    paths provided when requesting individual requests will be appended
    to this base path, as illustrated below:

    \snippet code/src_network_access_qnetworkrequestfactory.cpp 1
 */
QNetworkRequestFactory::QNetworkRequestFactory(const QUrl &baseUrl)
    : d(new QNetworkRequestFactoryPrivate(baseUrl))
{
}

/*!
    Destroys this QNetworkRequestFactory object.
 */
QNetworkRequestFactory::~QNetworkRequestFactory()
    = default;

/*!
    Creates a copy of \a other.
 */
QNetworkRequestFactory::QNetworkRequestFactory(const QNetworkRequestFactory &other)
    = default;

/*!
    Creates a copy of \a other and returns a reference to this factory.
 */
QNetworkRequestFactory &QNetworkRequestFactory::operator=(const QNetworkRequestFactory &other)
    = default;

/*!
    \fn QNetworkRequestFactory::QNetworkRequestFactory(QNetworkRequestFactory &&other) noexcept

    Move-constructs the factory from \a other.

    \note The moved-from object \a other is placed in a
    partially-formed state, in which the only valid operations are
    destruction and assignment of a new value.
*/

/*!
    \fn QNetworkRequestFactory &QNetworkRequestFactory::operator=(QNetworkRequestFactory &&other) noexcept

    Move-assigns \a other and returns a reference to this factory.

    \note The moved-from object \a other is placed in a
    partially-formed state, in which the only valid operations are
    destruction and assignment of a new value.
 */

/*!
    \fn void QNetworkRequestFactory::swap(QNetworkRequestFactory &other)

    Swaps this factory with \a other. This operation is
    very fast and never fails.
 */

/*!
    Returns the base URL used for the individual requests.

    The base URL may contain a path component. This path is used
    as path "prefix" for the paths that are provided when generating
    individual requests.

    \sa setBaseUrl()
 */
QUrl QNetworkRequestFactory::baseUrl() const
{
    return d->baseUrl;
}

/*!
    Sets the base URL used in individual requests to \a url.

    \sa baseUrl()
 */
void QNetworkRequestFactory::setBaseUrl(const QUrl &url)
{
    if (d->baseUrl == url)
        return;

    d.detach();
    d->baseUrl = url;
}

#if QT_CONFIG(ssl)
/*!
    Returns the SSL configuration set to this factory. The SSL configuration
    is set to each individual request.

    \sa setSslConfiguration()
 */
QSslConfiguration QNetworkRequestFactory::sslConfiguration() const
{
    return d->sslConfig;
}

/*!
    Sets the SSL configuration to \a configuration.

    \sa sslConfiguration()
 */
void QNetworkRequestFactory::setSslConfiguration(const QSslConfiguration &configuration)
{
    if (d->sslConfig == configuration)
        return;

    d.detach();
    d->sslConfig = configuration;
}
#endif

/*!
    Returns a QNetworkRequest.

    The returned request is filled with the data that this factory
    has been configured with.

    \sa createRequest(const QUrlQuery&), createRequest(const QString&, const QUrlQuery&)
*/

QNetworkRequest QNetworkRequestFactory::createRequest() const
{
    return d->newRequest(d->requestUrl());
}

/*!
    Returns a QNetworkRequest.

    The returned request's URL is formed by appending the provided \a path
    to the baseUrl (which may itself have a path component).

    \sa createRequest(const QString &, const QUrlQuery &), createRequest(), baseUrl()
*/
QNetworkRequest QNetworkRequestFactory::createRequest(const QString &path) const
{
    return d->newRequest(d->requestUrl(&path));
}

/*!
    Returns a QNetworkRequest.

    The returned request's URL is formed by appending the provided \a query
    to the baseUrl.

    \sa createRequest(const QString &, const QUrlQuery &), createRequest(), baseUrl()
*/
QNetworkRequest QNetworkRequestFactory::createRequest(const QUrlQuery &query) const
{
    return d->newRequest(d->requestUrl(nullptr, &query));
}

/*!
    Returns a QNetworkRequest.

    The returned requests URL is formed by appending the provided \a path
    and \a query to the baseUrl (which may have a path component).

    If the provided \a path contains query items, they will be combined
    with the items in \a query.

    \sa createRequest(const QUrlQuery&), createRequest(), baseUrl()
 */
QNetworkRequest QNetworkRequestFactory::createRequest(const QString &path, const QUrlQuery &query) const
{
    return d->newRequest(d->requestUrl(&path, &query));
}

/*!
    Sets \a headers that are common to all requests.

    These headers are added to individual requests' headers.
    This is a convenience mechanism for setting headers that
    repeat across requests.

    \sa commonHeaders(), clearCommonHeaders(), createRequest()
 */
void QNetworkRequestFactory::setCommonHeaders(const QHttpHeaders &headers)
{
    d.detach();
    d->headers = headers;
}

/*!
    Returns the currently set headers.

    \sa setCommonHeaders(), clearCommonHeaders()
 */
QHttpHeaders QNetworkRequestFactory::commonHeaders() const
{
    return d->headers;
}

/*!
    Clears current headers.

    \sa commonHeaders(), setCommonHeaders()
*/
void QNetworkRequestFactory::clearCommonHeaders()
{
    if (d->headers.isEmpty())
        return;
    d.detach();
    d->headers.clear();
}

/*!
    Returns the bearer token that has been set.

    The bearer token, if present, is used to set the
    \c {Authorization: Bearer my_token} header for requests. This is a common
    authorization convention and is provided as an additional convenience.

    The means to acquire the bearer token vary. Standard methods include \c OAuth2
    and the service provider's website/dashboard. It is expected that the bearer
    token changes over time. For example, when updated with a refresh token,
    always setting the new token again ensures that subsequent requests have
    the latest, valid token.

    The presence of the bearer token does not impact the \l commonHeaders()
    listing. If the \l commonHeaders() also lists \c Authorization header, it
    will be overwritten.

    \sa setBearerToken(), commonHeaders()
 */
QByteArray QNetworkRequestFactory::bearerToken() const
{
    return d->bearerToken;
}

/*!
    Sets the bearer token to \a token.

    \sa bearerToken(), clearBearerToken()
*/
void QNetworkRequestFactory::setBearerToken(const QByteArray &token)
{
    if (d->bearerToken == token)
        return;

    d.detach();
    d->bearerToken = token;
}

/*!
    Clears the bearer token.

    \sa bearerToken()
*/
void QNetworkRequestFactory::clearBearerToken()
{
    if (d->bearerToken.isEmpty())
        return;

    d.detach();
    d->bearerToken.clear();
}

/*!
    Returns the username set to this factory.

    \sa setUserName(), clearUserName(), password()
*/
QString QNetworkRequestFactory::userName() const
{
    return d->userName;
}

/*!
    Sets the username of this factory to \a userName.

    The username is set in the request URL when \l createRequest() is called.
    The QRestAccessManager / QNetworkAccessManager will attempt to use
    these credentials when the server indicates that authentication
    is required.

    \sa userName(), clearUserName(), password()
*/
void QNetworkRequestFactory::setUserName(const QString &userName)
{
    if (d->userName == userName)
        return;
    d.detach();
    d->userName = userName;
}

/*!
    Clears the username set to this factory.
*/
void QNetworkRequestFactory::clearUserName()
{
    if (d->userName.isEmpty())
        return;
    d.detach();
    d->userName.clear();
}

/*!
    Returns the password set to this factory.

    \sa password(), clearPassword(), userName()
*/
QString QNetworkRequestFactory::password() const
{
    return d->password;
}

/*!
    Sets the password of this factory to \a password.

    The password is set in the request URL when \l createRequest() is called.
    The QRestAccessManager / QNetworkAccessManager will attempt to use
    these credentials when the server indicates that authentication
    is required.

    \sa password(), clearPassword(), userName()
*/
void QNetworkRequestFactory::setPassword(const QString &password)
{
    if (d->password == password)
        return;
    d.detach();
    d->password = password;
}

/*!
    Clears the password set to this factory.

    \sa password(), setPassword(), userName()
*/
void QNetworkRequestFactory::clearPassword()
{
    if (d->password.isEmpty())
        return;
    d.detach();
    d->password.clear();
}

/*!
    Sets \a timeout used for transfers.

    \sa transferTimeout(), QNetworkRequest::setTransferTimeout(),
        QNetworkAccessManager::setTransferTimeout()
*/
void QNetworkRequestFactory::setTransferTimeout(std::chrono::milliseconds timeout)
{
    if (d->transferTimeout == timeout)
        return;

    d.detach();
    d->transferTimeout = timeout;
}

/*!
    Returns the timeout used for transfers.

    \sa setTransferTimeout(), QNetworkRequest::transferTimeout(),
        QNetworkAccessManager::transferTimeout()
*/
std::chrono::milliseconds QNetworkRequestFactory::transferTimeout() const
{
    return d->transferTimeout;
}

/*!
    Returns query parameters that are added to individual requests' query
    parameters. The query parameters are added to any potential query
    parameters provided with the individual \l createRequest() calls.

    Use cases for using repeating query parameters are server dependent,
    but typical examples include language setting \c {?lang=en}, format
    specification \c {?format=json}, API version specification
    \c {?version=1.0} and API key authentication.

    \sa setQueryParameters(), clearQueryParameters(), createRequest()
*/
QUrlQuery QNetworkRequestFactory::queryParameters() const
{
    return d->queryParameters;
}

/*!
    Sets \a query parameters that are added to individual requests' query
    parameters.

    \sa queryParameters(), clearQueryParameters()
 */
void QNetworkRequestFactory::setQueryParameters(const QUrlQuery &query)
{
    if (d->queryParameters == query)
        return;

    d.detach();
    d->queryParameters = query;
}

/*!
    Clears the query parameters.

    \sa queryParameters()
*/
void QNetworkRequestFactory::clearQueryParameters()
{
    if (d->queryParameters.isEmpty())
        return;

    d.detach();
    d->queryParameters.clear();
}

/*!
    \since 6.8

    Sets the priority for any future requests created by this factory to
    \a priority.

    The default priority is \l QNetworkRequest::NormalPriority.

    \sa priority(), QNetworkRequest::setPriority()
*/
void QNetworkRequestFactory::setPriority(QNetworkRequest::Priority priority)
{
    if (d->priority == priority)
        return;
    d.detach();
    d->priority = priority;
}

/*!
    \since 6.8

    Returns the priority assigned to any future requests created by this
    factory.

    \sa setPriority(), QNetworkRequest::priority()
*/
QNetworkRequest::Priority QNetworkRequestFactory::priority() const
{
    return d->priority;
}

/*!
    \since 6.8

    Sets the value associated with \a attribute to \a value.
    If the attribute is already set, the previous value is
    replaced. The attributes are set to any future requests
    created by this factory.

    \sa attribute(), clearAttribute(), clearAttributes(),
        QNetworkRequest::Attribute
*/
void QNetworkRequestFactory::setAttribute(QNetworkRequest::Attribute attribute,
                                          const QVariant &value)
{
    if (attribute == QNetworkRequest::HttpStatusCodeAttribute
        || attribute == QNetworkRequest::HttpReasonPhraseAttribute
        || attribute == QNetworkRequest::RedirectionTargetAttribute
        || attribute == QNetworkRequest::ConnectionEncryptedAttribute
        || attribute == QNetworkRequest::SourceIsFromCacheAttribute
        || attribute == QNetworkRequest::HttpPipeliningWasUsedAttribute
        || attribute == QNetworkRequest::Http2WasUsedAttribute
        || attribute == QNetworkRequest::OriginalContentLengthAttribute)
    {
        qCWarning(lcQrequestfactory, "%i is a reply-only attribute, ignoring.", attribute);
        return;
    }
    d.detach();
    d->attributes.insert(attribute, value);
}

/*!
    \since 6.8

    Returns the value associated with \a attribute. If the
    attribute has not been set, returns a default-constructed \l QVariant.

    \sa attribute(QNetworkRequest::Attribute, const QVariant &),
        setAttribute(), clearAttributes(), QNetworkRequest::Attribute

*/
QVariant QNetworkRequestFactory::attribute(QNetworkRequest::Attribute attribute) const
{
    return d->attributes.value(attribute);
}

/*!
    \since 6.8

    Returns the value associated with \a attribute. If the
    attribute has not been set, returns \a defaultValue.

     \sa attribute(), setAttribute(), clearAttributes(),
         QNetworkRequest::Attribute
*/
QVariant QNetworkRequestFactory::attribute(QNetworkRequest::Attribute attribute,
                                           const QVariant &defaultValue) const
{
    return d->attributes.value(attribute, defaultValue);
}

/*!
    \since 6.8

    Clears \a attribute set to this factory.

    \sa attribute(), setAttribute()
*/
void QNetworkRequestFactory::clearAttribute(QNetworkRequest::Attribute attribute)
{
    if (!d->attributes.contains(attribute))
        return;
    d.detach();
    d->attributes.remove(attribute);
}

/*!
    \since 6.8

    Clears any attributes set to this factory.

    \sa attribute(), setAttribute()
*/
void QNetworkRequestFactory::clearAttributes()
{
    if (d->attributes.isEmpty())
        return;
    d.detach();
    d->attributes.clear();
}

QNetworkRequestFactoryPrivate::QNetworkRequestFactoryPrivate()
    = default;

QNetworkRequestFactoryPrivate::QNetworkRequestFactoryPrivate(const QUrl &baseUrl)
    : baseUrl(baseUrl)
{
}

QNetworkRequestFactoryPrivate::~QNetworkRequestFactoryPrivate()
    = default;

QNetworkRequest QNetworkRequestFactoryPrivate::newRequest(const QUrl &url) const
{
    QNetworkRequest request;
    request.setUrl(url);
#if QT_CONFIG(ssl)
    if (!sslConfig.isNull())
        request.setSslConfiguration(sslConfig);
#endif
    // Set the header entries to the request. Combine values as there
    // may be multiple values per name. Note: this would not necessarily
    // produce right result for 'Set-Cookie' header if it has multiple values,
    // but since it is a purely server-side (response) header, not relevant here.
    const auto headerNames = headers.toMultiMap().uniqueKeys(); // ### fixme: port QNR to QHH
    for (const auto &name : headerNames)
        request.setRawHeader(name, headers.combinedValue(name));

    constexpr char Bearer[] = "Bearer ";
    if (!bearerToken.isEmpty())
        request.setRawHeader("Authorization"_ba, Bearer + bearerToken);

    request.setTransferTimeout(transferTimeout);
    request.setPriority(priority);

    for (const auto &[attribute, value] : attributes.asKeyValueRange())
        request.setAttribute(attribute, value);

    return request;
}

QUrl QNetworkRequestFactoryPrivate::requestUrl(const QString *path,
                                             const QUrlQuery *query) const
{
    const QUrl providedPath = path ? QUrl(*path) : QUrl{};
    const QUrlQuery providedQuery = query ? *query : QUrlQuery();

    if (!providedPath.scheme().isEmpty() || !providedPath.host().isEmpty()) {
        qCWarning(lcQrequestfactory, "The provided path %ls may only contain path and query item "
                  "components, and other parts will be ignored. Set the baseUrl instead",
                  qUtf16Printable(providedPath.toDisplayString()));
    }

    QUrl resultUrl = baseUrl;
    QUrlQuery resultQuery(providedQuery);
    QString basePath = baseUrl.path();

    resultUrl.setUserName(userName);
    resultUrl.setPassword(password);

    // Separate the path and query parameters components on the application-provided path
    const QString requestPath{providedPath.path()};
    const QUrlQuery pathQueryItems{providedPath};

    if (!pathQueryItems.isEmpty()) {
        // Add any query items provided as part of the path
        const auto items = pathQueryItems.queryItems(QUrl::ComponentFormattingOption::FullyEncoded);
        for (const auto &[key, value]: items)
            resultQuery.addQueryItem(key, value);
    }

    if (!queryParameters.isEmpty()) {
        // Add any query items set to this factory
        const QList<std::pair<QString,QString>> items =
                queryParameters.queryItems(QUrl::ComponentFormattingOption::FullyEncoded);
        for (const auto &item: items)
            resultQuery.addQueryItem(item.first, item.second);
    }

    if (!resultQuery.isEmpty())
        resultUrl.setQuery(resultQuery);

    if (requestPath.isEmpty())
        return resultUrl;

    // Ensure that the "base path" (the path that may be present
    // in the baseUrl), and the request path are joined with one '/'
    // If both have it, remove one, if neither has it, add one
    if (basePath.endsWith(u'/') && requestPath.startsWith(u'/'))
        basePath.chop(1);
    else if (!requestPath.startsWith(u'/') && !basePath.endsWith(u'/'))
        basePath.append(u'/');

    resultUrl.setPath(basePath.append(requestPath));
    return resultUrl;
}

#ifndef QT_NO_DEBUG_STREAM
/*!
    \fn QDebug QNetworkRequestFactory::operator<<(QDebug debug,
                                        const QNetworkRequestFactory &factory)

    Writes \a factory into \a debug stream.

    \sa {Debugging Techniques}
*/
QDebug operator<<(QDebug debug, const QNetworkRequestFactory &factory)
{
    const QDebugStateSaver saver(debug);
    debug.resetFormat().nospace();

    debug << "QNetworkRequestFactory(baseUrl = " << factory.baseUrl()
          << ", headers = " << factory.commonHeaders()
          << ", queryParameters = " << factory.queryParameters().queryItems()
          << ", bearerToken = " << (factory.bearerToken().isEmpty() ? "(empty)" : "(is set)")
          << ", transferTimeout = " << factory.transferTimeout()
          << ", userName = " << (factory.userName().isEmpty() ? "(empty)" : "(is set)")
          << ", password = " << (factory.password().isEmpty() ? "(empty)" : "(is set)")
#if QT_CONFIG(ssl)
          << ", SSL configuration"
          << (factory.sslConfiguration().isNull() ? " is not set (default)" : " is set")
#else
          << ", no SSL support"
#endif
          << ")";
    return debug;
}
#endif // QT_NO_DEBUG_STREAM

QT_END_NAMESPACE