1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qhttpnetworkheader_p.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_HTTP
|
---|
45 |
|
---|
46 | QT_BEGIN_NAMESPACE
|
---|
47 |
|
---|
48 | QHttpNetworkHeaderPrivate::QHttpNetworkHeaderPrivate(const QUrl &newUrl)
|
---|
49 | :url(newUrl)
|
---|
50 | {
|
---|
51 | }
|
---|
52 |
|
---|
53 | QHttpNetworkHeaderPrivate::QHttpNetworkHeaderPrivate(const QHttpNetworkHeaderPrivate &other)
|
---|
54 | :QSharedData(other)
|
---|
55 | {
|
---|
56 | url = other.url;
|
---|
57 | fields = other.fields;
|
---|
58 | }
|
---|
59 |
|
---|
60 | qint64 QHttpNetworkHeaderPrivate::contentLength() const
|
---|
61 | {
|
---|
62 | bool ok = false;
|
---|
63 | QByteArray value = headerField("content-length");
|
---|
64 | qint64 length = value.toULongLong(&ok);
|
---|
65 | if (ok)
|
---|
66 | return length;
|
---|
67 | return -1; // the header field is not set
|
---|
68 | }
|
---|
69 |
|
---|
70 | void QHttpNetworkHeaderPrivate::setContentLength(qint64 length)
|
---|
71 | {
|
---|
72 | setHeaderField("Content-Length", QByteArray::number(length));
|
---|
73 | }
|
---|
74 |
|
---|
75 | QByteArray QHttpNetworkHeaderPrivate::headerField(const QByteArray &name, const QByteArray &defaultValue) const
|
---|
76 | {
|
---|
77 | QList<QByteArray> allValues = headerFieldValues(name);
|
---|
78 | if (allValues.isEmpty())
|
---|
79 | return defaultValue;
|
---|
80 |
|
---|
81 | QByteArray result;
|
---|
82 | bool first = true;
|
---|
83 | foreach (QByteArray value, allValues) {
|
---|
84 | if (!first)
|
---|
85 | result += ", ";
|
---|
86 | first = false;
|
---|
87 | result += value;
|
---|
88 | }
|
---|
89 | return result;
|
---|
90 | }
|
---|
91 |
|
---|
92 | QList<QByteArray> QHttpNetworkHeaderPrivate::headerFieldValues(const QByteArray &name) const
|
---|
93 | {
|
---|
94 | QList<QByteArray> result;
|
---|
95 | QList<QPair<QByteArray, QByteArray> >::ConstIterator it = fields.constBegin(),
|
---|
96 | end = fields.constEnd();
|
---|
97 | for ( ; it != end; ++it)
|
---|
98 | if (qstricmp(name.constData(), it->first) == 0)
|
---|
99 | result += it->second;
|
---|
100 |
|
---|
101 | return result;
|
---|
102 | }
|
---|
103 |
|
---|
104 | void QHttpNetworkHeaderPrivate::setHeaderField(const QByteArray &name, const QByteArray &data)
|
---|
105 | {
|
---|
106 | QList<QPair<QByteArray, QByteArray> >::Iterator it = fields.begin();
|
---|
107 | while (it != fields.end()) {
|
---|
108 | if (qstricmp(name.constData(), it->first) == 0)
|
---|
109 | it = fields.erase(it);
|
---|
110 | else
|
---|
111 | ++it;
|
---|
112 | }
|
---|
113 | fields.append(qMakePair(name, data));
|
---|
114 | }
|
---|
115 |
|
---|
116 | bool QHttpNetworkHeaderPrivate::operator==(const QHttpNetworkHeaderPrivate &other) const
|
---|
117 | {
|
---|
118 | return (url == other.url);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | QT_END_NAMESPACE
|
---|
123 |
|
---|
124 | #endif
|
---|