choke uploadProgress signals
[profile/ivi/qtbase.git] / src / network / access / qhttpnetworkrequest.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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),
52       autoDecompress(false), pipeliningAllowed(false), withCredentials(true)
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;
64     customVerb = other.customVerb;
65     withCredentials = other.withCredentials;
66     ssl = other.ssl;
67 }
68
69 QHttpNetworkRequestPrivate::~QHttpNetworkRequestPrivate()
70 {
71 }
72
73 bool QHttpNetworkRequestPrivate::operator==(const QHttpNetworkRequestPrivate &other) const
74 {
75     return QHttpNetworkHeaderPrivate::operator==(other)
76         && (operation == other.operation)
77         && (ssl == other.ssl)
78         && (uploadByteDevice == other.uploadByteDevice);
79 }
80
81 QByteArray QHttpNetworkRequestPrivate::methodName() const
82 {
83     switch (operation) {
84     case QHttpNetworkRequest::Get:
85         return "GET";
86         break;
87     case QHttpNetworkRequest::Head:
88         return "HEAD";
89         break;
90     case QHttpNetworkRequest::Post:
91         return "POST";
92         break;
93     case QHttpNetworkRequest::Options:
94         return "OPTIONS";
95         break;
96     case QHttpNetworkRequest::Put:
97         return "PUT";
98         break;
99     case QHttpNetworkRequest::Delete:
100         return "DELETE";
101         break;
102     case QHttpNetworkRequest::Trace:
103         return "TRACE";
104         break;
105     case QHttpNetworkRequest::Connect:
106         return "CONNECT";
107         break;
108     case QHttpNetworkRequest::Custom:
109         return customVerb;
110         break;
111     default:
112         break;
113     }
114     return QByteArray();
115 }
116
117 QByteArray QHttpNetworkRequestPrivate::uri(bool throughProxy) const
118 {
119     QUrl::FormattingOptions format(QUrl::RemoveFragment | QUrl::RemoveUserInfo | QUrl::FullyEncoded);
120
121     // for POST, query data is send as content
122     if (operation == QHttpNetworkRequest::Post && !uploadByteDevice)
123         format |= QUrl::RemoveQuery;
124     // for requests through proxy, the Request-URI contains full url
125     if (!throughProxy)
126         format |= QUrl::RemoveScheme | QUrl::RemoveAuthority;
127     QUrl copy = url;
128     if (copy.path().isEmpty())
129         copy.setPath(QStringLiteral("/"));
130     QByteArray uri = copy.toEncoded(format);
131     return uri;
132 }
133
134 QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request, bool throughProxy)
135 {
136     QList<QPair<QByteArray, QByteArray> > fields = request.header();
137     QByteArray ba;
138     ba.reserve(40 + fields.length()*25); // very rough lower bound estimation
139
140     ba += request.d->methodName();
141     ba += ' ';
142     ba += request.d->uri(throughProxy);
143
144     ba += " HTTP/";
145     ba += QByteArray::number(request.majorVersion());
146     ba += '.';
147     ba += QByteArray::number(request.minorVersion());
148     ba += "\r\n";
149
150     QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin();
151     QList<QPair<QByteArray, QByteArray> >::const_iterator endIt = fields.constEnd();
152     for (; it != endIt; ++it) {
153         ba += it->first;
154         ba += ": ";
155         ba += it->second;
156         ba += "\r\n";
157     }
158     if (request.d->operation == QHttpNetworkRequest::Post) {
159         // add content type, if not set in the request
160         if (request.headerField("content-type").isEmpty()) {
161             qWarning("content-type missing in HTTP POST, defaulting to application/octet-stream");
162             ba += "Content-Type: application/octet-stream\r\n";
163         }
164         if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
165             QByteArray query = request.d->url.query(QUrl::FullyEncoded).toLatin1();
166             ba += "Content-Length: ";
167             ba += QByteArray::number(query.size());
168             ba += "\r\n\r\n";
169             ba += query;
170         } else {
171             ba += "\r\n";
172         }
173     } else {
174         ba += "\r\n";
175     }
176      return ba;
177 }
178
179
180 // QHttpNetworkRequest
181
182 QHttpNetworkRequest::QHttpNetworkRequest(const QUrl &url, Operation operation, Priority priority)
183     : d(new QHttpNetworkRequestPrivate(operation, priority, url))
184 {
185 }
186
187 QHttpNetworkRequest::QHttpNetworkRequest(const QHttpNetworkRequest &other)
188     : QHttpNetworkHeader(other), d(other.d)
189 {
190 }
191
192 QHttpNetworkRequest::~QHttpNetworkRequest()
193 {
194 }
195
196 QUrl QHttpNetworkRequest::url() const
197 {
198     return d->url;
199 }
200 void QHttpNetworkRequest::setUrl(const QUrl &url)
201 {
202     d->url = url;
203 }
204
205 bool QHttpNetworkRequest::isSsl() const
206 {
207     return d->ssl;
208 }
209 void QHttpNetworkRequest::setSsl(bool s)
210 {
211     d->ssl = s;
212 }
213
214 qint64 QHttpNetworkRequest::contentLength() const
215 {
216     return d->contentLength();
217 }
218
219 void QHttpNetworkRequest::setContentLength(qint64 length)
220 {
221     d->setContentLength(length);
222 }
223
224 QList<QPair<QByteArray, QByteArray> > QHttpNetworkRequest::header() const
225 {
226     return d->fields;
227 }
228
229 QByteArray QHttpNetworkRequest::headerField(const QByteArray &name, const QByteArray &defaultValue) const
230 {
231     return d->headerField(name, defaultValue);
232 }
233
234 void QHttpNetworkRequest::setHeaderField(const QByteArray &name, const QByteArray &data)
235 {
236     d->setHeaderField(name, data);
237 }
238
239 QHttpNetworkRequest &QHttpNetworkRequest::operator=(const QHttpNetworkRequest &other)
240 {
241     d = other.d;
242     return *this;
243 }
244
245 bool QHttpNetworkRequest::operator==(const QHttpNetworkRequest &other) const
246 {
247     return d->operator==(*other.d);
248 }
249
250 QHttpNetworkRequest::Operation QHttpNetworkRequest::operation() const
251 {
252     return d->operation;
253 }
254
255 void QHttpNetworkRequest::setOperation(Operation operation)
256 {
257     d->operation = operation;
258 }
259
260 QByteArray QHttpNetworkRequest::customVerb() const
261 {
262     return d->customVerb;
263 }
264
265 void QHttpNetworkRequest::setCustomVerb(const QByteArray &customVerb)
266 {
267     d->customVerb = customVerb;
268 }
269
270 QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const
271 {
272     return d->priority;
273 }
274
275 void QHttpNetworkRequest::setPriority(Priority priority)
276 {
277     d->priority = priority;
278 }
279
280 bool QHttpNetworkRequest::isPipeliningAllowed() const
281 {
282     return d->pipeliningAllowed;
283 }
284
285 void QHttpNetworkRequest::setPipeliningAllowed(bool b)
286 {
287     d->pipeliningAllowed = b;
288 }
289
290 bool QHttpNetworkRequest::withCredentials() const
291 {
292     return d->withCredentials;
293 }
294
295 void QHttpNetworkRequest::setWithCredentials(bool b)
296 {
297     d->withCredentials = b;
298 }
299
300 void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd)
301 {
302     d->uploadByteDevice = bd;
303 }
304
305 QNonContiguousByteDevice* QHttpNetworkRequest::uploadByteDevice() const
306 {
307     return d->uploadByteDevice;
308 }
309
310 int QHttpNetworkRequest::majorVersion() const
311 {
312     return 1;
313 }
314
315 int QHttpNetworkRequest::minorVersion() const
316 {
317     return 1;
318 }
319
320
321 QT_END_NAMESPACE
322
323 #endif
324