| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 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 "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 | }
|
|---|
| 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:
|
|---|
| 83 | return "GET";
|
|---|
| 84 | break;
|
|---|
| 85 | case QHttpNetworkRequest::Head:
|
|---|
| 86 | return "HEAD";
|
|---|
| 87 | break;
|
|---|
| 88 | case QHttpNetworkRequest::Post:
|
|---|
| 89 | return "POST";
|
|---|
| 90 | break;
|
|---|
| 91 | case QHttpNetworkRequest::Options:
|
|---|
| 92 | return "OPTIONS";
|
|---|
| 93 | break;
|
|---|
| 94 | case QHttpNetworkRequest::Put:
|
|---|
| 95 | return "PUT";
|
|---|
| 96 | break;
|
|---|
| 97 | case QHttpNetworkRequest::Delete:
|
|---|
| 98 | return "DELETE";
|
|---|
| 99 | break;
|
|---|
| 100 | case QHttpNetworkRequest::Trace:
|
|---|
| 101 | return "TRACE";
|
|---|
| 102 | break;
|
|---|
| 103 | case QHttpNetworkRequest::Connect:
|
|---|
| 104 | return "CONNECT";
|
|---|
| 105 | break;
|
|---|
| 106 | case QHttpNetworkRequest::Custom:
|
|---|
| 107 | return customVerb;
|
|---|
| 108 | break;
|
|---|
| 109 | default:
|
|---|
| 110 | break;
|
|---|
| 111 | }
|
|---|
| 112 | return QByteArray();
|
|---|
| 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 | {
|
|---|
| 135 | QList<QPair<QByteArray, QByteArray> > fields = request.header();
|
|---|
| 136 | QByteArray ba;
|
|---|
| 137 | ba.reserve(40 + fields.length()*25); // very rough lower bound estimation
|
|---|
| 138 |
|
|---|
| 139 | ba += request.d->methodName();
|
|---|
| 140 | ba += ' ';
|
|---|
| 141 | ba += request.d->uri(throughProxy);
|
|---|
| 142 |
|
|---|
| 143 | ba += " HTTP/";
|
|---|
| 144 | ba += QByteArray::number(request.majorVersion());
|
|---|
| 145 | ba += '.';
|
|---|
| 146 | ba += QByteArray::number(request.minorVersion());
|
|---|
| 147 | ba += "\r\n";
|
|---|
| 148 |
|
|---|
| 149 | QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin();
|
|---|
| 150 | QList<QPair<QByteArray, QByteArray> >::const_iterator endIt = fields.constEnd();
|
|---|
| 151 | for (; it != endIt; ++it) {
|
|---|
| 152 | ba += it->first;
|
|---|
| 153 | ba += ": ";
|
|---|
| 154 | ba += it->second;
|
|---|
| 155 | ba += "\r\n";
|
|---|
| 156 | }
|
|---|
| 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();
|
|---|
| 163 | ba += "Content-Length: ";
|
|---|
| 164 | ba += QByteArray::number(query.size());
|
|---|
| 165 | ba += "\r\n\r\n";
|
|---|
| 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 |
|
|---|
| 248 | QByteArray QHttpNetworkRequest::customVerb() const
|
|---|
| 249 | {
|
|---|
| 250 | return d->customVerb;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | void QHttpNetworkRequest::setCustomVerb(const QByteArray &customVerb)
|
|---|
| 254 | {
|
|---|
| 255 | d->customVerb = customVerb;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 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 |
|
|---|
| 278 | bool QHttpNetworkRequest::withCredentials() const
|
|---|
| 279 | {
|
|---|
| 280 | return d->withCredentials;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | void QHttpNetworkRequest::setWithCredentials(bool b)
|
|---|
| 284 | {
|
|---|
| 285 | d->withCredentials = b;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 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 |
|
|---|