source: trunk/src/network/access/qhttpnetworkrequest.cpp@ 783

Last change on this file since 783 was 651, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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
47QT_BEGIN_NAMESPACE
48
49QHttpNetworkRequestPrivate::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)
53{
54}
55
56QHttpNetworkRequestPrivate::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}
65
66QHttpNetworkRequestPrivate::~QHttpNetworkRequestPrivate()
67{
68}
69
70bool QHttpNetworkRequestPrivate::operator==(const QHttpNetworkRequestPrivate &other) const
71{
72 return QHttpNetworkHeaderPrivate::operator==(other)
73 && (operation == other.operation)
74 && (uploadByteDevice == other.uploadByteDevice);
75}
76
77QByteArray QHttpNetworkRequestPrivate::methodName() const
78{
79 QByteArray ba;
80 switch (operation) {
81 case QHttpNetworkRequest::Options:
82 ba += "OPTIONS";
83 break;
84 case QHttpNetworkRequest::Get:
85 ba += "GET";
86 break;
87 case QHttpNetworkRequest::Head:
88 ba += "HEAD";
89 break;
90 case QHttpNetworkRequest::Post:
91 ba += "POST";
92 break;
93 case QHttpNetworkRequest::Put:
94 ba += "PUT";
95 break;
96 case QHttpNetworkRequest::Delete:
97 ba += "DELETE";
98 break;
99 case QHttpNetworkRequest::Trace:
100 ba += "TRACE";
101 break;
102 case QHttpNetworkRequest::Connect:
103 ba += "CONNECT";
104 break;
105 default:
106 break;
107 }
108 return ba;
109}
110
111QByteArray QHttpNetworkRequestPrivate::uri(bool throughProxy) const
112{
113 QUrl::FormattingOptions format(QUrl::RemoveFragment);
114
115 // for POST, query data is send as content
116 if (operation == QHttpNetworkRequest::Post && !uploadByteDevice)
117 format |= QUrl::RemoveQuery;
118 // for requests through proxy, the Request-URI contains full url
119 if (throughProxy)
120 format |= QUrl::RemoveUserInfo;
121 else
122 format |= QUrl::RemoveScheme | QUrl::RemoveAuthority;
123 QByteArray uri = url.toEncoded(format);
124 if (uri.isEmpty() || (throughProxy && url.path().isEmpty()))
125 uri += '/';
126 return uri;
127}
128
129QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request, bool throughProxy)
130{
131 QByteArray ba = request.d->methodName();
132 QByteArray uri = request.d->uri(throughProxy);
133 ba += ' ' + uri;
134
135 QString majorVersion = QString::number(request.majorVersion());
136 QString minorVersion = QString::number(request.minorVersion());
137 ba += " HTTP/" + majorVersion.toLatin1() + '.' + minorVersion.toLatin1() + "\r\n";
138
139 QList<QPair<QByteArray, QByteArray> > fields = request.header();
140 QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin();
141 for (; it != fields.constEnd(); ++it)
142 ba += it->first + ": " + it->second + "\r\n";
143 if (request.d->operation == QHttpNetworkRequest::Post) {
144 // add content type, if not set in the request
145 if (request.headerField("content-type").isEmpty())
146 ba += "Content-Type: application/x-www-form-urlencoded\r\n";
147 if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
148 QByteArray query = request.d->url.encodedQuery();
149 ba += "Content-Length: "+ QByteArray::number(query.size()) + "\r\n";
150 ba += "\r\n";
151 ba += query;
152 } else {
153 ba += "\r\n";
154 }
155 } else {
156 ba += "\r\n";
157 }
158 return ba;
159}
160
161
162// QHttpNetworkRequest
163
164QHttpNetworkRequest::QHttpNetworkRequest(const QUrl &url, Operation operation, Priority priority)
165 : d(new QHttpNetworkRequestPrivate(operation, priority, url))
166{
167}
168
169QHttpNetworkRequest::QHttpNetworkRequest(const QHttpNetworkRequest &other)
170 : QHttpNetworkHeader(other), d(other.d)
171{
172}
173
174QHttpNetworkRequest::~QHttpNetworkRequest()
175{
176}
177
178QUrl QHttpNetworkRequest::url() const
179{
180 return d->url;
181}
182void QHttpNetworkRequest::setUrl(const QUrl &url)
183{
184 d->url = url;
185}
186
187qint64 QHttpNetworkRequest::contentLength() const
188{
189 return d->contentLength();
190}
191
192void QHttpNetworkRequest::setContentLength(qint64 length)
193{
194 d->setContentLength(length);
195}
196
197QList<QPair<QByteArray, QByteArray> > QHttpNetworkRequest::header() const
198{
199 return d->fields;
200}
201
202QByteArray QHttpNetworkRequest::headerField(const QByteArray &name, const QByteArray &defaultValue) const
203{
204 return d->headerField(name, defaultValue);
205}
206
207void QHttpNetworkRequest::setHeaderField(const QByteArray &name, const QByteArray &data)
208{
209 d->setHeaderField(name, data);
210}
211
212QHttpNetworkRequest &QHttpNetworkRequest::operator=(const QHttpNetworkRequest &other)
213{
214 d = other.d;
215 return *this;
216}
217
218bool QHttpNetworkRequest::operator==(const QHttpNetworkRequest &other) const
219{
220 return d->operator==(*other.d);
221}
222
223QHttpNetworkRequest::Operation QHttpNetworkRequest::operation() const
224{
225 return d->operation;
226}
227
228void QHttpNetworkRequest::setOperation(Operation operation)
229{
230 d->operation = operation;
231}
232
233QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const
234{
235 return d->priority;
236}
237
238void QHttpNetworkRequest::setPriority(Priority priority)
239{
240 d->priority = priority;
241}
242
243bool QHttpNetworkRequest::isPipeliningAllowed() const
244{
245 return d->pipeliningAllowed;
246}
247
248void QHttpNetworkRequest::setPipeliningAllowed(bool b)
249{
250 d->pipeliningAllowed = b;
251}
252
253void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd)
254{
255 d->uploadByteDevice = bd;
256}
257
258QNonContiguousByteDevice* QHttpNetworkRequest::uploadByteDevice() const
259{
260 return d->uploadByteDevice;
261}
262
263int QHttpNetworkRequest::majorVersion() const
264{
265 return 1;
266}
267
268int QHttpNetworkRequest::minorVersion() const
269{
270 return 1;
271}
272
273
274QT_END_NAMESPACE
275
276#endif
277
Note: See TracBrowser for help on using the repository browser.