Qt 4.8
qhttpnetworkrequest.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
48 
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 
58 {
59  operation = other.operation;
60  priority = other.priority;
64  customVerb = other.customVerb;
66  ssl = other.ssl;
67 }
68 
70 {
71 }
72 
74 {
76  && (operation == other.operation)
77  && (ssl == other.ssl)
78  && (uploadByteDevice == other.uploadByteDevice);
79 }
80 
82 {
83  switch (operation) {
85  return "GET";
86  break;
88  return "HEAD";
89  break;
91  return "POST";
92  break;
94  return "OPTIONS";
95  break;
97  return "PUT";
98  break;
100  return "DELETE";
101  break;
103  return "TRACE";
104  break;
106  return "CONNECT";
107  break;
109  return customVerb;
110  break;
111  default:
112  break;
113  }
114  return QByteArray();
115 }
116 
118 {
119  QUrl::FormattingOptions format(QUrl::RemoveFragment);
120 
121  // for POST, query data is send as content
123  format |= QUrl::RemoveQuery;
124  // for requests through proxy, the Request-URI contains full url
125  if (throughProxy)
126  format |= QUrl::RemoveUserInfo;
127  else
129  QByteArray uri = url.toEncoded(format);
130  if (uri.isEmpty() || (throughProxy && url.path().isEmpty()))
131  uri += '/';
132  return uri;
133 }
134 
136 {
138  QByteArray ba;
139  ba.reserve(40 + fields.length()*25); // very rough lower bound estimation
140 
141  ba += request.d->methodName();
142  ba += ' ';
143  ba += request.d->uri(throughProxy);
144 
145  ba += " HTTP/";
146  ba += QByteArray::number(request.majorVersion());
147  ba += '.';
148  ba += QByteArray::number(request.minorVersion());
149  ba += "\r\n";
150 
151  QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin();
152  QList<QPair<QByteArray, QByteArray> >::const_iterator endIt = fields.constEnd();
153  for (; it != endIt; ++it) {
154  ba += it->first;
155  ba += ": ";
156  ba += it->second;
157  ba += "\r\n";
158  }
159  if (request.d->operation == QHttpNetworkRequest::Post) {
160  // add content type, if not set in the request
161  if (request.headerField("content-type").isEmpty()) {
162  //Content-Type is mandatory. We can't say anything about the encoding, but x-www-form-urlencoded is the most likely to work.
163  //This warning indicates a bug in application code not setting a required header.
164  //Note that if using QHttpMultipart, the content-type is set in QNetworkAccessManagerPrivate::prepareMultipart already
165  qWarning("content-type missing in HTTP POST, defaulting to application/x-www-form-urlencoded. Use QNetworkRequest::setHeader() to fix this problem.");
166  ba += "Content-Type: application/x-www-form-urlencoded\r\n";
167  }
168  if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
169  QByteArray query = request.d->url.encodedQuery();
170  ba += "Content-Length: ";
171  ba += QByteArray::number(query.size());
172  ba += "\r\n\r\n";
173  ba += query;
174  } else {
175  ba += "\r\n";
176  }
177  } else {
178  ba += "\r\n";
179  }
180  return ba;
181 }
182 
183 
184 // QHttpNetworkRequest
185 
187  : d(new QHttpNetworkRequestPrivate(operation, priority, url))
188 {
189 }
190 
192  : QHttpNetworkHeader(other), d(other.d)
193 {
194 }
195 
197 {
198 }
199 
201 {
202  return d->url;
203 }
205 {
206  d->url = url;
207 }
208 
210 {
211  return d->ssl;
212 }
214 {
215  d->ssl = s;
216 }
217 
219 {
220  return d->contentLength();
221 }
222 
224 {
225  d->setContentLength(length);
226 }
227 
229 {
230  return d->fields;
231 }
232 
234 {
235  return d->headerField(name, defaultValue);
236 }
237 
239 {
240  d->setHeaderField(name, data);
241 }
242 
244 {
245  d = other.d;
246  return *this;
247 }
248 
250 {
251  return d->operator==(*other.d);
252 }
253 
255 {
256  return d->operation;
257 }
258 
260 {
261  d->operation = operation;
262 }
263 
265 {
266  return d->customVerb;
267 }
268 
270 {
272 }
273 
275 {
276  return d->priority;
277 }
278 
280 {
281  d->priority = priority;
282 }
283 
285 {
286  return d->pipeliningAllowed;
287 }
288 
290 {
291  d->pipeliningAllowed = b;
292 }
293 
295 {
296  return d->withCredentials;
297 }
298 
300 {
301  d->withCredentials = b;
302 }
303 
305 {
306  d->uploadByteDevice = bd;
307 }
308 
310 {
311  return d->uploadByteDevice;
312 }
313 
315 {
316  return 1;
317 }
318 
320 {
321  return 1;
322 }
323 
324 
326 
327 #endif
328 
QSharedDataPointer< QHttpNetworkRequestPrivate > d
double d
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QHttpNetworkRequestPrivate(QHttpNetworkRequest::Operation op, QHttpNetworkRequest::Priority pri, const QUrl &newUrl=QUrl())
void setPriority(Priority priority)
QHttpNetworkRequest(const QUrl &url=QUrl(), Operation operation=Get, Priority priority=NormalPriority)
#define it(className, varName)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void setOperation(Operation operation)
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue=QByteArray()) const
bool operator==(const QHttpNetworkRequest &other) const
void setContentLength(qint64 length)
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
bool hasQuery() const
Returns true if this URL contains a Query (i.
Definition: qurl.cpp:5058
QString path() const
Returns the path of the URL.
Definition: qurl.cpp:4977
QList< QPair< QByteArray, QByteArray > > header() const
QNonContiguousByteDevice * uploadByteDevice() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue=QByteArray()) const
void setUrl(const QUrl &url)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
QList< QPair< QByteArray, QByteArray > > fields
Q_CORE_EXPORT void qWarning(const char *,...)
QHttpNetworkRequest::Operation operation
static const char * data(const QByteArray &arr)
QByteArray customVerb() const
__int64 qint64
Definition: qglobal.h:942
Operation operation() const
bool operator==(const QHttpNetworkHeaderPrivate &other) const
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
QHttpNetworkRequest::Priority priority
void setContentLength(qint64 length)
static QByteArray header(const QHttpNetworkRequest &request, bool throughProxy)
int length() const
This function is identical to count().
Definition: qlist.h:281
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool operator==(const QHttpNetworkRequestPrivate &other) const
QByteArray uri(bool throughProxy) const
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
A QNonContiguousByteDevice is a representation of a file, array or buffer that allows access with a r...
void setHeaderField(const QByteArray &name, const QByteArray &data)
QHttpNetworkRequest & operator=(const QHttpNetworkRequest &other)
void reserve(int size)
Attempts to allocate memory for at least size bytes.
Definition: qbytearray.h:449
QNonContiguousByteDevice * uploadByteDevice
void setCustomVerb(const QByteArray &customOperation)
static QByteArray number(int, int base=10)
Returns a byte array containing the string equivalent of the number n to base base (10 by default)...
void setHeaderField(const QByteArray &name, const QByteArray &data)
QByteArray encodedQuery() const
Returns the query string of the URL in percent encoded form.
Definition: qurl.cpp:5643
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272
void setUploadByteDevice(QNonContiguousByteDevice *bd)