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 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | QHttpNetworkHeaderPrivate::QHttpNetworkHeaderPrivate(const QUrl &newUrl) |
11 | :url(newUrl) |
12 | { |
13 | } |
14 | |
15 | qint64 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 | |
27 | void QHttpNetworkHeaderPrivate::setContentLength(qint64 length) |
28 | { |
29 | setHeaderField(name: "Content-Length", data: QByteArray::number(length)); |
30 | } |
31 | |
32 | QByteArray 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 | |
41 | QList<QByteArray> QHttpNetworkHeaderPrivate::headerFieldValues(const QByteArray &name) const |
42 | { |
43 | return parser.headerFieldValues(name); |
44 | } |
45 | |
46 | void QHttpNetworkHeaderPrivate::setHeaderField(const QByteArray &name, const QByteArray &data) |
47 | { |
48 | parser.setHeaderField(name, data); |
49 | } |
50 | |
51 | void QHttpNetworkHeaderPrivate::prependHeaderField(const QByteArray &name, const QByteArray &data) |
52 | { |
53 | parser.prependHeaderField(name, data); |
54 | } |
55 | |
56 | QList<QPair<QByteArray, QByteArray> > QHttpNetworkHeaderPrivate::headers() const |
57 | { |
58 | return parser.headers(); |
59 | } |
60 | |
61 | void QHttpNetworkHeaderPrivate::clearHeaders() |
62 | { |
63 | parser.clearHeaders(); |
64 | } |
65 | |
66 | bool QHttpNetworkHeaderPrivate::operator==(const QHttpNetworkHeaderPrivate &other) const |
67 | { |
68 | return (url == other.url); |
69 | } |
70 | |
71 | |
72 | QT_END_NAMESPACE |
73 |