author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtNetwork module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qhttpnetworkrequest_p.h" |
|
43 |
#include "private/qnoncontiguousbytedevice_p.h" |
|
44 |
||
45 |
#ifndef QT_NO_HTTP |
|
46 |
||
47 |
QT_BEGIN_NAMESPACE |
|
48 |
||
49 |
QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(QHttpNetworkRequest::Operation op, |
|
50 |
QHttpNetworkRequest::Priority pri, const QUrl &newUrl) |
|
51 |
: QHttpNetworkHeaderPrivate(newUrl), operation(op), priority(pri), uploadByteDevice(0), |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
52 |
autoDecompress(false), pipeliningAllowed(false), withCredentials(true) |
0 | 53 |
{ |
54 |
} |
|
55 |
||
56 |
QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(const QHttpNetworkRequestPrivate &other) |
|
57 |
: QHttpNetworkHeaderPrivate(other) |
|
58 |
{ |
|
59 |
operation = other.operation; |
|
60 |
priority = other.priority; |
|
61 |
uploadByteDevice = other.uploadByteDevice; |
|
62 |
autoDecompress = other.autoDecompress; |
|
63 |
pipeliningAllowed = other.pipeliningAllowed; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
64 |
customVerb = other.customVerb; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
65 |
withCredentials = other.withCredentials; |
0 | 66 |
} |
67 |
||
68 |
QHttpNetworkRequestPrivate::~QHttpNetworkRequestPrivate() |
|
69 |
{ |
|
70 |
} |
|
71 |
||
72 |
bool QHttpNetworkRequestPrivate::operator==(const QHttpNetworkRequestPrivate &other) const |
|
73 |
{ |
|
74 |
return QHttpNetworkHeaderPrivate::operator==(other) |
|
75 |
&& (operation == other.operation) |
|
76 |
&& (uploadByteDevice == other.uploadByteDevice); |
|
77 |
} |
|
78 |
||
79 |
QByteArray QHttpNetworkRequestPrivate::methodName() const |
|
80 |
{ |
|
81 |
switch (operation) { |
|
82 |
case QHttpNetworkRequest::Get: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
83 |
return "GET"; |
0 | 84 |
break; |
85 |
case QHttpNetworkRequest::Head: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
86 |
return "HEAD"; |
0 | 87 |
break; |
88 |
case QHttpNetworkRequest::Post: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
89 |
return "POST"; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
90 |
break; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
91 |
case QHttpNetworkRequest::Options: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
92 |
return "OPTIONS"; |
0 | 93 |
break; |
94 |
case QHttpNetworkRequest::Put: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
95 |
return "PUT"; |
0 | 96 |
break; |
97 |
case QHttpNetworkRequest::Delete: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
98 |
return "DELETE"; |
0 | 99 |
break; |
100 |
case QHttpNetworkRequest::Trace: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
101 |
return "TRACE"; |
0 | 102 |
break; |
103 |
case QHttpNetworkRequest::Connect: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
104 |
return "CONNECT"; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
105 |
break; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
106 |
case QHttpNetworkRequest::Custom: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
107 |
return customVerb; |
0 | 108 |
break; |
109 |
default: |
|
110 |
break; |
|
111 |
} |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
112 |
return QByteArray(); |
0 | 113 |
} |
114 |
||
115 |
QByteArray QHttpNetworkRequestPrivate::uri(bool throughProxy) const |
|
116 |
{ |
|
117 |
QUrl::FormattingOptions format(QUrl::RemoveFragment); |
|
118 |
||
119 |
// for POST, query data is send as content |
|
120 |
if (operation == QHttpNetworkRequest::Post && !uploadByteDevice) |
|
121 |
format |= QUrl::RemoveQuery; |
|
122 |
// for requests through proxy, the Request-URI contains full url |
|
123 |
if (throughProxy) |
|
124 |
format |= QUrl::RemoveUserInfo; |
|
125 |
else |
|
126 |
format |= QUrl::RemoveScheme | QUrl::RemoveAuthority; |
|
127 |
QByteArray uri = url.toEncoded(format); |
|
128 |
if (uri.isEmpty() || (throughProxy && url.path().isEmpty())) |
|
129 |
uri += '/'; |
|
130 |
return uri; |
|
131 |
} |
|
132 |
||
133 |
QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request, bool throughProxy) |
|
134 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
135 |
QList<QPair<QByteArray, QByteArray> > fields = request.header(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
136 |
QByteArray ba; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
137 |
ba.reserve(40 + fields.length()*25); // very rough lower bound estimation |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
138 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
139 |
ba += request.d->methodName(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
140 |
ba += ' '; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
141 |
ba += request.d->uri(throughProxy); |
0 | 142 |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
143 |
ba += " HTTP/"; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
144 |
ba += QByteArray::number(request.majorVersion()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
145 |
ba += '.'; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
146 |
ba += QByteArray::number(request.minorVersion()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
147 |
ba += "\r\n"; |
0 | 148 |
|
149 |
QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
150 |
QList<QPair<QByteArray, QByteArray> >::const_iterator endIt = fields.constEnd(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
151 |
for (; it != endIt; ++it) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
152 |
ba += it->first; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
153 |
ba += ": "; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
154 |
ba += it->second; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
155 |
ba += "\r\n"; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
156 |
} |
0 | 157 |
if (request.d->operation == QHttpNetworkRequest::Post) { |
158 |
// add content type, if not set in the request |
|
159 |
if (request.headerField("content-type").isEmpty()) |
|
160 |
ba += "Content-Type: application/x-www-form-urlencoded\r\n"; |
|
161 |
if (!request.d->uploadByteDevice && request.d->url.hasQuery()) { |
|
162 |
QByteArray query = request.d->url.encodedQuery(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
163 |
ba += "Content-Length: "; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
164 |
ba += QByteArray::number(query.size()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
165 |
ba += "\r\n\r\n"; |
0 | 166 |
ba += query; |
167 |
} else { |
|
168 |
ba += "\r\n"; |
|
169 |
} |
|
170 |
} else { |
|
171 |
ba += "\r\n"; |
|
172 |
} |
|
173 |
return ba; |
|
174 |
} |
|
175 |
||
176 |
||
177 |
// QHttpNetworkRequest |
|
178 |
||
179 |
QHttpNetworkRequest::QHttpNetworkRequest(const QUrl &url, Operation operation, Priority priority) |
|
180 |
: d(new QHttpNetworkRequestPrivate(operation, priority, url)) |
|
181 |
{ |
|
182 |
} |
|
183 |
||
184 |
QHttpNetworkRequest::QHttpNetworkRequest(const QHttpNetworkRequest &other) |
|
185 |
: QHttpNetworkHeader(other), d(other.d) |
|
186 |
{ |
|
187 |
} |
|
188 |
||
189 |
QHttpNetworkRequest::~QHttpNetworkRequest() |
|
190 |
{ |
|
191 |
} |
|
192 |
||
193 |
QUrl QHttpNetworkRequest::url() const |
|
194 |
{ |
|
195 |
return d->url; |
|
196 |
} |
|
197 |
void QHttpNetworkRequest::setUrl(const QUrl &url) |
|
198 |
{ |
|
199 |
d->url = url; |
|
200 |
} |
|
201 |
||
202 |
qint64 QHttpNetworkRequest::contentLength() const |
|
203 |
{ |
|
204 |
return d->contentLength(); |
|
205 |
} |
|
206 |
||
207 |
void QHttpNetworkRequest::setContentLength(qint64 length) |
|
208 |
{ |
|
209 |
d->setContentLength(length); |
|
210 |
} |
|
211 |
||
212 |
QList<QPair<QByteArray, QByteArray> > QHttpNetworkRequest::header() const |
|
213 |
{ |
|
214 |
return d->fields; |
|
215 |
} |
|
216 |
||
217 |
QByteArray QHttpNetworkRequest::headerField(const QByteArray &name, const QByteArray &defaultValue) const |
|
218 |
{ |
|
219 |
return d->headerField(name, defaultValue); |
|
220 |
} |
|
221 |
||
222 |
void QHttpNetworkRequest::setHeaderField(const QByteArray &name, const QByteArray &data) |
|
223 |
{ |
|
224 |
d->setHeaderField(name, data); |
|
225 |
} |
|
226 |
||
227 |
QHttpNetworkRequest &QHttpNetworkRequest::operator=(const QHttpNetworkRequest &other) |
|
228 |
{ |
|
229 |
d = other.d; |
|
230 |
return *this; |
|
231 |
} |
|
232 |
||
233 |
bool QHttpNetworkRequest::operator==(const QHttpNetworkRequest &other) const |
|
234 |
{ |
|
235 |
return d->operator==(*other.d); |
|
236 |
} |
|
237 |
||
238 |
QHttpNetworkRequest::Operation QHttpNetworkRequest::operation() const |
|
239 |
{ |
|
240 |
return d->operation; |
|
241 |
} |
|
242 |
||
243 |
void QHttpNetworkRequest::setOperation(Operation operation) |
|
244 |
{ |
|
245 |
d->operation = operation; |
|
246 |
} |
|
247 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
248 |
QByteArray QHttpNetworkRequest::customVerb() const |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
249 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
250 |
return d->customVerb; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
251 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
252 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
253 |
void QHttpNetworkRequest::setCustomVerb(const QByteArray &customVerb) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
254 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
255 |
d->customVerb = customVerb; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
256 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
257 |
|
0 | 258 |
QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const |
259 |
{ |
|
260 |
return d->priority; |
|
261 |
} |
|
262 |
||
263 |
void QHttpNetworkRequest::setPriority(Priority priority) |
|
264 |
{ |
|
265 |
d->priority = priority; |
|
266 |
} |
|
267 |
||
268 |
bool QHttpNetworkRequest::isPipeliningAllowed() const |
|
269 |
{ |
|
270 |
return d->pipeliningAllowed; |
|
271 |
} |
|
272 |
||
273 |
void QHttpNetworkRequest::setPipeliningAllowed(bool b) |
|
274 |
{ |
|
275 |
d->pipeliningAllowed = b; |
|
276 |
} |
|
277 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
278 |
bool QHttpNetworkRequest::withCredentials() const |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
279 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
280 |
return d->withCredentials; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
281 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
282 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
283 |
void QHttpNetworkRequest::setWithCredentials(bool b) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
284 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
285 |
d->withCredentials = b; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
286 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
287 |
|
0 | 288 |
void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd) |
289 |
{ |
|
290 |
d->uploadByteDevice = bd; |
|
291 |
} |
|
292 |
||
293 |
QNonContiguousByteDevice* QHttpNetworkRequest::uploadByteDevice() const |
|
294 |
{ |
|
295 |
return d->uploadByteDevice; |
|
296 |
} |
|
297 |
||
298 |
int QHttpNetworkRequest::majorVersion() const |
|
299 |
{ |
|
300 |
return 1; |
|
301 |
} |
|
302 |
||
303 |
int QHttpNetworkRequest::minorVersion() const |
|
304 |
{ |
|
305 |
return 1; |
|
306 |
} |
|
307 |
||
308 |
||
309 |
QT_END_NAMESPACE |
|
310 |
||
311 |
#endif |
|
312 |