1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QHTTPSOCKETENGINE_P_H
41 #define QHTTPSOCKETENGINE_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtNetwork/private/qtnetworkglobal_p.h>
55 #include "private/qabstractsocketengine_p.h"
56 #include "qabstractsocket.h"
57 #include "qnetworkproxy.h"
58 #include "private/qauthenticator_p.h"
59 
60 QT_REQUIRE_CONFIG(http);
61 
62 QT_BEGIN_NAMESPACE
63 
64 #if !defined(QT_NO_NETWORKPROXY)
65 
66 class QTcpSocket;
67 class QHttpNetworkReply;
68 class QHttpSocketEnginePrivate;
69 
70 class Q_AUTOTEST_EXPORT QHttpSocketEngine : public QAbstractSocketEngine
71 {
72     Q_OBJECT
73 public:
74     enum HttpState {
75         None,
76         ConnectSent,
77         Connected,
78         SendAuthentication,
79         ReadResponseContent,
80         ReadResponseHeader
81     };
82     QHttpSocketEngine(QObject *parent = nullptr);
83     ~QHttpSocketEngine();
84 
85     bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) override;
86     bool initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) override;
87 
88     void setProxy(const QNetworkProxy &networkProxy);
89 
90     qintptr socketDescriptor() const override;
91 
92     bool isValid() const override;
93 
94     bool connectInternal();
95     bool connectToHost(const QHostAddress &address, quint16 port) override;
96     bool connectToHostByName(const QString &name, quint16 port) override;
97     bool bind(const QHostAddress &address, quint16 port) override;
98     bool listen() override;
99     int accept() override;
100     void close() override;
101 
102     qint64 bytesAvailable() const override;
103 
104     qint64 read(char *data, qint64 maxlen) override;
105     qint64 write(const char *data, qint64 len) override;
106 
107 #ifndef QT_NO_UDPSOCKET
108 #ifndef QT_NO_NETWORKINTERFACE
109     bool joinMulticastGroup(const QHostAddress &groupAddress,
110                             const QNetworkInterface &interface) override;
111     bool leaveMulticastGroup(const QHostAddress &groupAddress,
112                              const QNetworkInterface &interface) override;
113     QNetworkInterface multicastInterface() const override;
114     bool setMulticastInterface(const QNetworkInterface &iface) override;
115 #endif // QT_NO_NETWORKINTERFACE
116 
117     bool hasPendingDatagrams() const override;
118     qint64 pendingDatagramSize() const override;
119 #endif // QT_NO_UDPSOCKET
120 
121     qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader *,
122                         PacketHeaderOptions) override;
123     qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &) override;
124     qint64 bytesToWrite() const override;
125 
126     int option(SocketOption option) const override;
127     bool setOption(SocketOption option, int value) override;
128 
129     bool waitForRead(int msecs = 30000, bool *timedOut = nullptr) override;
130     bool waitForWrite(int msecs = 30000, bool *timedOut = nullptr) override;
131     bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
132                             bool checkRead, bool checkWrite,
133                             int msecs = 30000, bool *timedOut = nullptr) override;
134 
135     bool isReadNotificationEnabled() const override;
136     void setReadNotificationEnabled(bool enable) override;
137     bool isWriteNotificationEnabled() const override;
138     void setWriteNotificationEnabled(bool enable) override;
139     bool isExceptionNotificationEnabled() const override;
140     void setExceptionNotificationEnabled(bool enable) override;
141 
142 public slots:
143     void slotSocketConnected();
144     void slotSocketDisconnected();
145     void slotSocketReadNotification();
146     void slotSocketBytesWritten();
147     void slotSocketError(QAbstractSocket::SocketError error);
148     void slotSocketStateChanged(QAbstractSocket::SocketState state);
149 
150 private slots:
151     void emitPendingReadNotification();
152     void emitPendingWriteNotification();
153     void emitPendingConnectionNotification();
154 
155 private:
156     void emitReadNotification();
157     void emitWriteNotification();
158     void emitConnectionNotification();
159 
160     bool readHttpHeader();
161 
162     Q_DECLARE_PRIVATE(QHttpSocketEngine)
163     Q_DISABLE_COPY_MOVE(QHttpSocketEngine)
164 
165 };
166 
167 
168 class QHttpSocketEnginePrivate : public QAbstractSocketEnginePrivate
169 {
170     Q_DECLARE_PUBLIC(QHttpSocketEngine)
171 public:
172     QHttpSocketEnginePrivate();
173     ~QHttpSocketEnginePrivate();
174 
175     QNetworkProxy proxy;
176     QString peerName;
177     QTcpSocket *socket;
178     QHttpNetworkReply *reply; // only used for parsing the proxy response
179     QHttpSocketEngine::HttpState state;
180     QAuthenticator authenticator;
181     bool readNotificationEnabled;
182     bool writeNotificationEnabled;
183     bool exceptNotificationEnabled;
184     bool readNotificationPending;
185     bool writeNotificationPending;
186     bool connectionNotificationPending;
187     bool credentialsSent;
188     uint pendingResponseData;
189 };
190 
191 class Q_AUTOTEST_EXPORT QHttpSocketEngineHandler : public QSocketEngineHandler
192 {
193 public:
194     virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType,
195                                                       const QNetworkProxy &, QObject *parent) override;
196     virtual QAbstractSocketEngine *createSocketEngine(qintptr socketDescripter, QObject *parent) override;
197 };
198 #endif
199 
200 QT_END_NAMESPACE
201 
202 #endif // QHTTPSOCKETENGINE_H
203