Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qnetworkaccessdebugpipebackend.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "QtCore/qdatastream.h"
6#include <QCoreApplication>
7#include <QStringList>
8#include <QUrlQuery>
9#include "private/qnoncontiguousbytedevice_p.h"
10
12
13using namespace Qt::StringLiterals;
14
15#ifdef QT_BUILD_INTERNAL
16
17enum {
18 ReadBufferSize = 16384,
19 WriteBufferSize = ReadBufferSize
20};
21
22QStringList QNetworkAccessDebugPipeBackendFactory::supportedSchemes() const
23{
24 return QStringList(QStringLiteral("debugpipe"));
25}
26
28QNetworkAccessDebugPipeBackendFactory::create(QNetworkAccessManager::Operation op,
29 const QNetworkRequest &request) const
30{
31 // is it an operation we know of?
32 switch (op) {
35 break;
36
37 default:
38 // no, we can't handle this operation
39 return nullptr;
40 }
41
42 QUrl url = request.url();
43 if (url.scheme() == "debugpipe"_L1)
44 return new QNetworkAccessDebugPipeBackend;
45 return nullptr;
46}
47
48QNetworkAccessDebugPipeBackend::QNetworkAccessDebugPipeBackend()
49 : QNetworkAccessBackend(QNetworkAccessBackend::TargetType::Networked),
50 bareProtocol(false),
51 hasUploadFinished(false),
52 hasDownloadFinished(false),
53 hasEverythingFinished(false),
54 bytesDownloaded(0),
55 bytesUploaded(0)
56{
57}
58
59QNetworkAccessDebugPipeBackend::~QNetworkAccessDebugPipeBackend()
60{
61 // this is signals disconnect, not network!
62 socket.disconnect(this); // we're not interested in the signals at this point
63}
64
65void QNetworkAccessDebugPipeBackend::open()
66{
67 socket.connectToHost(url().host(), url().port(12345));
68 socket.setReadBufferSize(ReadBufferSize);
69
70 // socket ready read -> we can push from socket to downstream
71 connect(&socket, SIGNAL(readyRead()), SLOT(socketReadyRead()));
72 connect(&socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), SLOT(socketError()));
73 connect(&socket, SIGNAL(disconnected()), SLOT(socketDisconnected()));
74 connect(&socket, SIGNAL(connected()), SLOT(socketConnected()));
75 // socket bytes written -> we can push more from upstream to socket
76 connect(&socket, SIGNAL(bytesWritten(qint64)), SLOT(socketBytesWritten(qint64)));
77
78 bareProtocol = QUrlQuery(url()).queryItemValue("bare"_L1) == "1"_L1;
79
80 if (operation() == QNetworkAccessManager::PutOperation) {
81 createUploadByteDevice();
82 QObject::connect(uploadByteDevice(), SIGNAL(readyRead()), this,
83 SLOT(uploadReadyReadSlot()));
84 QMetaObject::invokeMethod(this, "uploadReadyReadSlot", Qt::QueuedConnection);
85 }
86}
87
88void QNetworkAccessDebugPipeBackend::socketReadyRead()
89{
90 readyRead();
91}
92
93qint64 QNetworkAccessDebugPipeBackend::read(char *data, qint64 maxlen)
94{
95 qint64 haveRead = socket.read(data, maxlen);
96
97 if (haveRead == -1) {
98 hasDownloadFinished = true;
99 // this ensures a good last downloadProgress is emitted
101 possiblyFinish();
102 return haveRead;
103 }
104
105 bytesDownloaded += haveRead;
106 return haveRead;
107}
108
109qint64 QNetworkAccessDebugPipeBackend::bytesAvailable() const
110{
111 return socket.bytesAvailable();
112}
113
114void QNetworkAccessDebugPipeBackend::socketBytesWritten(qint64)
115{
116 pushFromUpstreamToSocket();
117}
118
119void QNetworkAccessDebugPipeBackend::uploadReadyReadSlot()
120{
121 pushFromUpstreamToSocket();
122}
123
124void QNetworkAccessDebugPipeBackend::pushFromUpstreamToSocket()
125{
126 // FIXME
127 if (operation() == QNetworkAccessManager::PutOperation) {
128 if (hasUploadFinished)
129 return;
130
131 forever {
132 if (socket.bytesToWrite() >= WriteBufferSize)
133 return;
134
135 QByteArray data(WriteBufferSize, Qt::Uninitialized);
136 qint64 haveRead = uploadByteDevice()->peek(data.data(), data.size());
137 if (haveRead == -1) {
138 // EOF
139 hasUploadFinished = true;
140 possiblyFinish();
141 break;
142 } else if (haveRead == 0) {
143 // nothing to read right now, we will be called again later
144 break;
145 } else {
146 qint64 haveWritten;
147 data.truncate(haveRead);
148 haveWritten = socket.write(std::move(data));
149
150 if (haveWritten < 0) {
151 // write error!
152 QString msg = QCoreApplication::translate("QNetworkAccessDebugPipeBackend", "Write error writing to %1: %2")
155 finished();
156 return;
157 } else {
158 uploadByteDevice()->skip(haveWritten);
159 bytesUploaded += haveWritten;
160 }
161
162 //QCoreApplication::processEvents();
163 }
164 }
165 }
166}
167
168void QNetworkAccessDebugPipeBackend::possiblyFinish()
169{
170 if (hasEverythingFinished)
171 return;
172 hasEverythingFinished = true;
173
174 if ((operation() == QNetworkAccessManager::GetOperation) && hasDownloadFinished) {
175 socket.close();
176 finished();
177 } else if ((operation() == QNetworkAccessManager::PutOperation) && hasUploadFinished) {
178 socket.close();
179 finished();
180 }
181
182
183}
184
185void QNetworkAccessDebugPipeBackend::close()
186{
187 qWarning("QNetworkAccessDebugPipeBackend::closeDownstreamChannel() %d",operation());;
188 //if (operation() == QNetworkAccessManager::GetOperation)
189 // socket.disconnectFromHost();
190}
191
192
193void QNetworkAccessDebugPipeBackend::socketError()
194{
195 qWarning("QNetworkAccessDebugPipeBackend::socketError() %d",socket.error());
197 switch (socket.error()) {
199 return; // socketDisconnected will be called
200
203 break;
204
205 default:
207 break;
208 }
209
210 error(code, QNetworkAccessDebugPipeBackend::tr("Socket error on %1: %2")
212 finished();
213 disconnect(&socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
214
215}
216
217void QNetworkAccessDebugPipeBackend::socketDisconnected()
218{
219 if (socket.bytesToWrite() == 0) {
220 // normal close
221 } else {
222 readyRead(); // @todo this is odd
223 // abnormal close
224 QString msg = QNetworkAccessDebugPipeBackend::tr("Remote host closed the connection prematurely on %1")
225 .arg(url().toString());
227 finished();
228 }
229}
230
231void QNetworkAccessDebugPipeBackend::socketConnected()
232{
233}
234
235
236#endif
237
239
240#include "moc_qnetworkaccessdebugpipebackend_p.cpp"
bool connected
virtual void setReadBufferSize(qint64 size)
Sets the size of QAbstractSocket's internal read buffer to be size bytes.
qint64 bytesToWrite() const override
Returns the number of bytes that are waiting to be written.
qint64 bytesAvailable() const override
Returns the number of incoming bytes that are waiting to be read.
void close() override
Closes the I/O device for the socket and calls disconnectFromHost() to close the socket's connection.
SocketError
This enum describes the socket errors that can occur.
virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode=ReadWrite, NetworkLayerProtocol protocol=AnyIPProtocol)
Attempts to make a connection to hostName on the given port.
SocketError error() const
Returns the type of error that last occurred.
\inmodule QtCore
Definition qbytearray.h:57
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
QNetworkAccessBackend is the base class for implementing support for schemes used by QNetworkAccessMa...
Operation
Indicates the operation this reply is processing.
NetworkError
Indicates all possible error conditions found during the processing of the request.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
QUrl url() const
Returns the URL this network request is referring to.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3099
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8606
\inmodule QtCore
Definition qurlquery.h:20
QString queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding=QUrl::PrettyDecoded) const
Returns the query value associated with key key from the URL, using the options specified in encoding...
\inmodule QtCore
Definition qurl.h:94
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1983
\inmodule QtCore
Definition qvariant.h:64
Combined button and popup list for selecting options.
@ QueuedConnection
constexpr Initialization Uninitialized
DBusConnection const char DBusError * error
EGLOutputPortEXT port
#define forever
Definition qforeach.h:78
#define qWarning
Definition qlogging.h:162
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLenum GLsizei GLuint GLint * bytesWritten
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
SSL_CTX int(*) void arg)
#define QStringLiteral(str)
long long qint64
Definition qtypes.h:55
QUrl url("example.com")
[constructor-url-reference]
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
myObject disconnect()
[26]
QTcpSocket * socket
[1]
QNetworkRequest request(url)
textPart setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""))
char * toString(const MyType &t)
[31]
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(nullptr), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
\threadsafe This is an overloaded member function, provided for convenience. It differs from the abov...