1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qnetworkaccessdebugpipebackend_p.h"
|
---|
43 | #include "QtCore/qdatastream.h"
|
---|
44 |
|
---|
45 | QT_BEGIN_NAMESPACE
|
---|
46 |
|
---|
47 | #ifdef QT_BUILD_INTERNAL
|
---|
48 |
|
---|
49 | enum {
|
---|
50 | ReadBufferSize = 16384,
|
---|
51 | WriteBufferSize = ReadBufferSize
|
---|
52 | };
|
---|
53 |
|
---|
54 | struct QNetworkAccessDebugPipeBackend::DataPacket
|
---|
55 | {
|
---|
56 | QList<QPair<QByteArray, QByteArray> > headers;
|
---|
57 | QByteArray data;
|
---|
58 | };
|
---|
59 |
|
---|
60 | QNetworkAccessBackend *
|
---|
61 | QNetworkAccessDebugPipeBackendFactory::create(QNetworkAccessManager::Operation op,
|
---|
62 | const QNetworkRequest &request) const
|
---|
63 | {
|
---|
64 | // is it an operation we know of?
|
---|
65 | switch (op) {
|
---|
66 | case QNetworkAccessManager::GetOperation:
|
---|
67 | case QNetworkAccessManager::PutOperation:
|
---|
68 | break;
|
---|
69 |
|
---|
70 | default:
|
---|
71 | // no, we can't handle this operation
|
---|
72 | return 0;
|
---|
73 | }
|
---|
74 |
|
---|
75 | QUrl url = request.url();
|
---|
76 | if (url.scheme() == QLatin1String("debugpipe"))
|
---|
77 | return new QNetworkAccessDebugPipeBackend;
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 | QNetworkAccessDebugPipeBackend::QNetworkAccessDebugPipeBackend()
|
---|
82 | : incomingPacketSize(0), bareProtocol(false)
|
---|
83 | {
|
---|
84 | }
|
---|
85 |
|
---|
86 | QNetworkAccessDebugPipeBackend::~QNetworkAccessDebugPipeBackend()
|
---|
87 | {
|
---|
88 | socket.disconnect(this); // we're not interested in the signals at this point
|
---|
89 | }
|
---|
90 |
|
---|
91 | void QNetworkAccessDebugPipeBackend::open()
|
---|
92 | {
|
---|
93 | socket.connectToHost(url().host(), url().port(12345));
|
---|
94 | socket.setReadBufferSize(ReadBufferSize);
|
---|
95 | connect(&socket, SIGNAL(readyRead()), SLOT(socketReadyRead()));
|
---|
96 | connect(&socket, SIGNAL(bytesWritten(qint64)), SLOT(socketBytesWritten(qint64)));
|
---|
97 | connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError()));
|
---|
98 | connect(&socket, SIGNAL(disconnected()), SLOT(socketDisconnected()));
|
---|
99 |
|
---|
100 | bareProtocol = url().queryItemValue(QLatin1String("bare")) == QLatin1String("1");
|
---|
101 |
|
---|
102 | if (!bareProtocol) {
|
---|
103 | // "Handshake":
|
---|
104 | // send outgoing metadata and the URL being requested
|
---|
105 | DataPacket packet;
|
---|
106 | //packet.metaData = request().metaData();
|
---|
107 | packet.data = url().toEncoded();
|
---|
108 | send(packet);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | void QNetworkAccessDebugPipeBackend::closeDownstreamChannel()
|
---|
113 | {
|
---|
114 | if (operation() == QNetworkAccessManager::GetOperation)
|
---|
115 | socket.disconnectFromHost();
|
---|
116 | }
|
---|
117 |
|
---|
118 | void QNetworkAccessDebugPipeBackend::closeUpstreamChannel()
|
---|
119 | {
|
---|
120 | if (operation() == QNetworkAccessManager::PutOperation)
|
---|
121 | socket.disconnectFromHost();
|
---|
122 | else if (operation() == QNetworkAccessManager::PostOperation) {
|
---|
123 | send(DataPacket());
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | bool QNetworkAccessDebugPipeBackend::waitForDownstreamReadyRead(int ms)
|
---|
128 | {
|
---|
129 | readyReadEmitted = false;
|
---|
130 | if (socket.bytesAvailable()) {
|
---|
131 | socketReadyRead();
|
---|
132 | if (readyReadEmitted)
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 | socket.waitForReadyRead(ms);
|
---|
136 | return readyReadEmitted;
|
---|
137 | }
|
---|
138 |
|
---|
139 | bool QNetworkAccessDebugPipeBackend::waitForUpstreamBytesWritten(int ms)
|
---|
140 | {
|
---|
141 | bytesWrittenEmitted = false;
|
---|
142 | upstreamReadyRead();
|
---|
143 | if (bytesWrittenEmitted)
|
---|
144 | return true;
|
---|
145 |
|
---|
146 | socket.waitForBytesWritten(ms);
|
---|
147 | return bytesWrittenEmitted;
|
---|
148 | }
|
---|
149 |
|
---|
150 | void QNetworkAccessDebugPipeBackend::upstreamReadyRead()
|
---|
151 | {
|
---|
152 | int maxWrite = WriteBufferSize - socket.bytesToWrite();
|
---|
153 | if (maxWrite <= 0)
|
---|
154 | return; // can't write yet, wait for the socket to write
|
---|
155 |
|
---|
156 | if (bareProtocol) {
|
---|
157 | QByteArray data = readUpstream();
|
---|
158 | if (data.isEmpty())
|
---|
159 | return;
|
---|
160 |
|
---|
161 | socket.write(data);
|
---|
162 | upstreamBytesConsumed(data.size());
|
---|
163 | bytesWrittenEmitted = true;
|
---|
164 | return;
|
---|
165 | }
|
---|
166 |
|
---|
167 | DataPacket packet;
|
---|
168 | packet.data = readUpstream();
|
---|
169 | if (packet.data.isEmpty())
|
---|
170 | return; // we'll be called again when there's data
|
---|
171 | if (packet.data.size() > maxWrite)
|
---|
172 | packet.data.truncate(maxWrite);
|
---|
173 |
|
---|
174 | if (!send(packet)) {
|
---|
175 | QString msg = QObject::tr("Write error writing to %1: %2")
|
---|
176 | .arg(url().toString(), socket.errorString());
|
---|
177 | error(QNetworkReply::ProtocolFailure, msg);
|
---|
178 |
|
---|
179 | finished();
|
---|
180 | return;
|
---|
181 | }
|
---|
182 | upstreamBytesConsumed(packet.data.size());
|
---|
183 | bytesWrittenEmitted = true;
|
---|
184 | }
|
---|
185 |
|
---|
186 | void QNetworkAccessDebugPipeBackend::downstreamReadyWrite()
|
---|
187 | {
|
---|
188 | socketReadyRead();
|
---|
189 | }
|
---|
190 |
|
---|
191 | void QNetworkAccessDebugPipeBackend::socketReadyRead()
|
---|
192 | {
|
---|
193 | if (bareProtocol) {
|
---|
194 | qint64 bytesToRead = socket.bytesAvailable();
|
---|
195 | if (bytesToRead) {
|
---|
196 | QByteArray buffer;
|
---|
197 | buffer.resize(bytesToRead);
|
---|
198 | qint64 bytesRead = socket.read(buffer.data(), bytesToRead);
|
---|
199 | if (bytesRead < bytesToRead)
|
---|
200 | buffer.truncate(bytesRead);
|
---|
201 | writeDownstreamData(buffer);
|
---|
202 | readyReadEmitted = true;
|
---|
203 | }
|
---|
204 | return;
|
---|
205 | }
|
---|
206 |
|
---|
207 | while (canReceive() &&
|
---|
208 | (socket.state() == QAbstractSocket::UnconnectedState || nextDownstreamBlockSize())) {
|
---|
209 | DataPacket packet;
|
---|
210 | if (receive(packet)) {
|
---|
211 | if (!packet.headers.isEmpty()) {
|
---|
212 | QList<QPair<QByteArray, QByteArray> >::ConstIterator
|
---|
213 | it = packet.headers.constBegin(),
|
---|
214 | end = packet.headers.constEnd();
|
---|
215 | for ( ; it != end; ++it)
|
---|
216 | setRawHeader(it->first, it->second);
|
---|
217 | metaDataChanged();
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (!packet.data.isEmpty()) {
|
---|
221 | writeDownstreamData(packet.data);
|
---|
222 | readyReadEmitted = true;
|
---|
223 | }
|
---|
224 |
|
---|
225 | if (packet.headers.isEmpty() && packet.data.isEmpty()) {
|
---|
226 | // it's an eof
|
---|
227 | socket.close();
|
---|
228 | readyReadEmitted = true;
|
---|
229 | }
|
---|
230 | } else {
|
---|
231 | // got an error
|
---|
232 | QString msg = QObject::tr("Read error reading from %1: %2")
|
---|
233 | .arg(url().toString(), socket.errorString());
|
---|
234 | error(QNetworkReply::ProtocolFailure, msg);
|
---|
235 |
|
---|
236 | finished();
|
---|
237 | return;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | void QNetworkAccessDebugPipeBackend::socketBytesWritten(qint64)
|
---|
243 | {
|
---|
244 | upstreamReadyRead();
|
---|
245 | }
|
---|
246 |
|
---|
247 | void QNetworkAccessDebugPipeBackend::socketError()
|
---|
248 | {
|
---|
249 | QNetworkReply::NetworkError code;
|
---|
250 | switch (socket.error()) {
|
---|
251 | case QAbstractSocket::RemoteHostClosedError:
|
---|
252 | return; // socketDisconnected will be called
|
---|
253 |
|
---|
254 | case QAbstractSocket::NetworkError:
|
---|
255 | code = QNetworkReply::UnknownNetworkError;
|
---|
256 | break;
|
---|
257 |
|
---|
258 | default:
|
---|
259 | code = QNetworkReply::ProtocolFailure;
|
---|
260 | break;
|
---|
261 | }
|
---|
262 |
|
---|
263 | error(code, QObject::tr("Socket error on %1: %2")
|
---|
264 | .arg(url().toString(), socket.errorString()));
|
---|
265 | finished();
|
---|
266 | disconnect(&socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
|
---|
267 |
|
---|
268 | }
|
---|
269 |
|
---|
270 | void QNetworkAccessDebugPipeBackend::socketDisconnected()
|
---|
271 | {
|
---|
272 | socketReadyRead();
|
---|
273 | if (incomingPacketSize == 0 && socket.bytesToWrite() == 0) {
|
---|
274 | // normal close
|
---|
275 | finished();
|
---|
276 | } else {
|
---|
277 | // abnormal close
|
---|
278 | QString msg = QObject::tr("Remote host closed the connection prematurely on %1")
|
---|
279 | .arg(url().toString());
|
---|
280 | error(QNetworkReply::RemoteHostClosedError, msg);
|
---|
281 |
|
---|
282 | finished();
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | bool QNetworkAccessDebugPipeBackend::send(const DataPacket &packet)
|
---|
287 | {
|
---|
288 | QByteArray ba;
|
---|
289 | {
|
---|
290 | QDataStream stream(&ba, QIODevice::WriteOnly);
|
---|
291 | stream.setVersion(QDataStream::Qt_4_4);
|
---|
292 |
|
---|
293 | stream << packet.headers << packet.data;
|
---|
294 | }
|
---|
295 |
|
---|
296 | qint32 outgoingPacketSize = ba.size();
|
---|
297 | qint64 written = socket.write((const char*)&outgoingPacketSize, sizeof outgoingPacketSize);
|
---|
298 | written += socket.write(ba);
|
---|
299 | return quint64(written) == (outgoingPacketSize + sizeof outgoingPacketSize);
|
---|
300 | }
|
---|
301 |
|
---|
302 | bool QNetworkAccessDebugPipeBackend::receive(DataPacket &packet)
|
---|
303 | {
|
---|
304 | if (!canReceive())
|
---|
305 | return false;
|
---|
306 |
|
---|
307 | // canReceive() does the setting up for us
|
---|
308 | Q_ASSERT(socket.bytesAvailable() >= incomingPacketSize);
|
---|
309 | QByteArray incomingPacket = socket.read(incomingPacketSize);
|
---|
310 | QDataStream stream(&incomingPacket, QIODevice::ReadOnly);
|
---|
311 | stream.setVersion(QDataStream::Qt_4_4);
|
---|
312 | stream >> packet.headers >> packet.data;
|
---|
313 |
|
---|
314 | // reset for next packet:
|
---|
315 | incomingPacketSize = 0;
|
---|
316 | socket.setReadBufferSize(ReadBufferSize);
|
---|
317 | return true;
|
---|
318 | }
|
---|
319 |
|
---|
320 | bool QNetworkAccessDebugPipeBackend::canReceive()
|
---|
321 | {
|
---|
322 | if (incomingPacketSize == 0) {
|
---|
323 | // read the packet size
|
---|
324 | if (quint64(socket.bytesAvailable()) >= sizeof incomingPacketSize)
|
---|
325 | socket.read((char*)&incomingPacketSize, sizeof incomingPacketSize);
|
---|
326 | else
|
---|
327 | return false;
|
---|
328 | }
|
---|
329 |
|
---|
330 | if (incomingPacketSize == 0) {
|
---|
331 | QString msg = QObject::tr("Protocol error: packet of size 0 received");
|
---|
332 | error(QNetworkReply::ProtocolFailure, msg);
|
---|
333 | finished();
|
---|
334 |
|
---|
335 | socket.blockSignals(true);
|
---|
336 | socket.abort();
|
---|
337 | socket.blockSignals(false);
|
---|
338 | return false;
|
---|
339 | }
|
---|
340 |
|
---|
341 | return socket.bytesAvailable() >= incomingPacketSize;
|
---|
342 | }
|
---|
343 |
|
---|
344 | #endif
|
---|
345 |
|
---|
346 | QT_END_NAMESPACE
|
---|