1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qhttpnetworkrequest_p.h"
43 #include "private/qnoncontiguousbytedevice_p.h"
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)
56 QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(const QHttpNetworkRequestPrivate &other)
57 : QHttpNetworkHeaderPrivate(other)
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;
69 QHttpNetworkRequestPrivate::~QHttpNetworkRequestPrivate()
73 bool QHttpNetworkRequestPrivate::operator==(const QHttpNetworkRequestPrivate &other) const
75 return QHttpNetworkHeaderPrivate::operator==(other)
76 && (operation == other.operation)
78 && (uploadByteDevice == other.uploadByteDevice);
81 QByteArray QHttpNetworkRequestPrivate::methodName() const
84 case QHttpNetworkRequest::Get:
87 case QHttpNetworkRequest::Head:
90 case QHttpNetworkRequest::Post:
93 case QHttpNetworkRequest::Options:
96 case QHttpNetworkRequest::Put:
99 case QHttpNetworkRequest::Delete:
102 case QHttpNetworkRequest::Trace:
105 case QHttpNetworkRequest::Connect:
108 case QHttpNetworkRequest::Custom:
117 QByteArray QHttpNetworkRequestPrivate::uri(bool throughProxy) const
119 QUrl::FormattingOptions format(QUrl::RemoveFragment | QUrl::RemoveUserInfo | QUrl::FullyEncoded);
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
126 format |= QUrl::RemoveScheme | QUrl::RemoveAuthority;
128 if (copy.path().isEmpty())
129 copy.setPath(QStringLiteral("/"));
130 QByteArray uri = copy.toEncoded(format);
134 QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request, bool throughProxy)
136 QList<QPair<QByteArray, QByteArray> > fields = request.header();
138 ba.reserve(40 + fields.length()*25); // very rough lower bound estimation
140 ba += request.d->methodName();
142 ba += request.d->uri(throughProxy);
145 ba += QByteArray::number(request.majorVersion());
147 ba += QByteArray::number(request.minorVersion());
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) {
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";
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());
180 // QHttpNetworkRequest
182 QHttpNetworkRequest::QHttpNetworkRequest(const QUrl &url, Operation operation, Priority priority)
183 : d(new QHttpNetworkRequestPrivate(operation, priority, url))
187 QHttpNetworkRequest::QHttpNetworkRequest(const QHttpNetworkRequest &other)
188 : QHttpNetworkHeader(other), d(other.d)
192 QHttpNetworkRequest::~QHttpNetworkRequest()
196 QUrl QHttpNetworkRequest::url() const
200 void QHttpNetworkRequest::setUrl(const QUrl &url)
205 bool QHttpNetworkRequest::isSsl() const
209 void QHttpNetworkRequest::setSsl(bool s)
214 qint64 QHttpNetworkRequest::contentLength() const
216 return d->contentLength();
219 void QHttpNetworkRequest::setContentLength(qint64 length)
221 d->setContentLength(length);
224 QList<QPair<QByteArray, QByteArray> > QHttpNetworkRequest::header() const
229 QByteArray QHttpNetworkRequest::headerField(const QByteArray &name, const QByteArray &defaultValue) const
231 return d->headerField(name, defaultValue);
234 void QHttpNetworkRequest::setHeaderField(const QByteArray &name, const QByteArray &data)
236 d->setHeaderField(name, data);
239 QHttpNetworkRequest &QHttpNetworkRequest::operator=(const QHttpNetworkRequest &other)
245 bool QHttpNetworkRequest::operator==(const QHttpNetworkRequest &other) const
247 return d->operator==(*other.d);
250 QHttpNetworkRequest::Operation QHttpNetworkRequest::operation() const
255 void QHttpNetworkRequest::setOperation(Operation operation)
257 d->operation = operation;
260 QByteArray QHttpNetworkRequest::customVerb() const
262 return d->customVerb;
265 void QHttpNetworkRequest::setCustomVerb(const QByteArray &customVerb)
267 d->customVerb = customVerb;
270 QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const
275 void QHttpNetworkRequest::setPriority(Priority priority)
277 d->priority = priority;
280 bool QHttpNetworkRequest::isPipeliningAllowed() const
282 return d->pipeliningAllowed;
285 void QHttpNetworkRequest::setPipeliningAllowed(bool b)
287 d->pipeliningAllowed = b;
290 bool QHttpNetworkRequest::withCredentials() const
292 return d->withCredentials;
295 void QHttpNetworkRequest::setWithCredentials(bool b)
297 d->withCredentials = b;
300 void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd)
302 d->uploadByteDevice = bd;
305 QNonContiguousByteDevice* QHttpNetworkRequest::uploadByteDevice() const
307 return d->uploadByteDevice;
310 int QHttpNetworkRequest::majorVersion() const
315 int QHttpNetworkRequest::minorVersion() const