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
4#include "qhttpnetworkheader_p.h"
5
6#include <algorithm>
7
8QT_BEGIN_NAMESPACE
9
10QHttpNetworkHeaderPrivate::QHttpNetworkHeaderPrivate(const QUrl &newUrl)
11 :url(newUrl)
12{
13}
14
15qint64 QHttpNetworkHeaderPrivate::contentLength() const
16{
17 bool ok = false;
18 // We are not using the headerField() method here because servers might send us multiple content-length
19 // headers which is crap (see QTBUG-15311). Therefore just take the first content-length header field.
20 QByteArray value = parser.firstHeaderField(name: "content-length");
21 qint64 length = value.toULongLong(ok: &ok);
22 if (ok)
23 return length;
24 return -1; // the header field is not set
25}
26
27void QHttpNetworkHeaderPrivate::setContentLength(qint64 length)
28{
29 setHeaderField(name: "Content-Length", data: QByteArray::number(length));
30}
31
32QByteArray QHttpNetworkHeaderPrivate::headerField(const QByteArray &name, const QByteArray &defaultValue) const
33{
34 QList<QByteArray> allValues = headerFieldValues(name);
35 if (allValues.isEmpty())
36 return defaultValue;
37 else
38 return allValues.join(sep: ", ");
39}
40
41QList<QByteArray> QHttpNetworkHeaderPrivate::headerFieldValues(const QByteArray &name) const
42{
43 return parser.headerFieldValues(name);
44}
45
46void QHttpNetworkHeaderPrivate::setHeaderField(const QByteArray &name, const QByteArray &data)
47{
48 parser.setHeaderField(name, data);
49}
50
51void QHttpNetworkHeaderPrivate::prependHeaderField(const QByteArray &name, const QByteArray &data)
52{
53 parser.prependHeaderField(name, data);
54}
55
56QList<QPair<QByteArray, QByteArray> > QHttpNetworkHeaderPrivate::headers() const
57{
58 return parser.headers();
59}
60
61void QHttpNetworkHeaderPrivate::clearHeaders()
62{
63 parser.clearHeaders();
64}
65
66bool QHttpNetworkHeaderPrivate::operator==(const QHttpNetworkHeaderPrivate &other) const
67{
68 return (url == other.url);
69}
70
71
72QT_END_NAMESPACE
73

source code of qtbase/src/network/access/qhttpnetworkheader.cpp