| 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 | |
| 4 | #ifndef QHTTPSOCKETENGINE_P_H |
| 5 | #define QHTTPSOCKETENGINE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
| 19 | |
| 20 | #include <QtNetwork/qnetworkproxy.h> |
| 21 | |
| 22 | #include "qabstractsocket.h" |
| 23 | #include "private/qauthenticator_p.h" |
| 24 | #include "private/qabstractsocketengine_p.h" |
| 25 | |
| 26 | QT_REQUIRE_CONFIG(http); |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | #if !defined(QT_NO_NETWORKPROXY) |
| 31 | |
| 32 | class QTcpSocket; |
| 33 | class QHttpNetworkReply; |
| 34 | class QHttpSocketEnginePrivate; |
| 35 | |
| 36 | class Q_AUTOTEST_EXPORT QHttpSocketEngine : public QAbstractSocketEngine |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | public: |
| 40 | enum HttpState { |
| 41 | None, |
| 42 | ConnectSent, |
| 43 | Connected, |
| 44 | SendAuthentication, |
| 45 | ReadResponseContent, |
| 46 | |
| 47 | }; |
| 48 | QHttpSocketEngine(QObject *parent = nullptr); |
| 49 | ~QHttpSocketEngine(); |
| 50 | |
| 51 | bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) override; |
| 52 | bool initialize(qintptr socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) override; |
| 53 | |
| 54 | void setProxy(const QNetworkProxy &networkProxy); |
| 55 | |
| 56 | qintptr socketDescriptor() const override; |
| 57 | |
| 58 | bool isValid() const override; |
| 59 | |
| 60 | bool connectInternal(); |
| 61 | bool connectToHost(const QHostAddress &address, quint16 port) override; |
| 62 | bool connectToHostByName(const QString &name, quint16 port) override; |
| 63 | bool bind(const QHostAddress &address, quint16 port) override; |
| 64 | bool listen(int backlog) override; |
| 65 | qintptr accept() override; |
| 66 | void close() override; |
| 67 | |
| 68 | qint64 bytesAvailable() const override; |
| 69 | |
| 70 | qint64 read(char *data, qint64 maxlen) override; |
| 71 | qint64 write(const char *data, qint64 len) override; |
| 72 | |
| 73 | #ifndef QT_NO_UDPSOCKET |
| 74 | #ifndef QT_NO_NETWORKINTERFACE |
| 75 | bool joinMulticastGroup(const QHostAddress &groupAddress, |
| 76 | const QNetworkInterface &interface) override; |
| 77 | bool leaveMulticastGroup(const QHostAddress &groupAddress, |
| 78 | const QNetworkInterface &interface) override; |
| 79 | QNetworkInterface multicastInterface() const override; |
| 80 | bool setMulticastInterface(const QNetworkInterface &iface) override; |
| 81 | #endif // QT_NO_NETWORKINTERFACE |
| 82 | |
| 83 | bool hasPendingDatagrams() const override; |
| 84 | qint64 pendingDatagramSize() const override; |
| 85 | #endif // QT_NO_UDPSOCKET |
| 86 | |
| 87 | qint64 (char *data, qint64 maxlen, QIpPacketHeader *, |
| 88 | PacketHeaderOptions) override; |
| 89 | qint64 (const char *data, qint64 len, const QIpPacketHeader &) override; |
| 90 | qint64 bytesToWrite() const override; |
| 91 | |
| 92 | int option(SocketOption option) const override; |
| 93 | bool setOption(SocketOption option, int value) override; |
| 94 | |
| 95 | bool waitForRead(QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout}, |
| 96 | bool *timedOut = nullptr) override; |
| 97 | bool waitForWrite(QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout}, |
| 98 | bool *timedOut = nullptr) override; |
| 99 | bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, |
| 100 | bool checkRead, bool checkWrite, |
| 101 | QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout}, |
| 102 | bool *timedOut = nullptr) override; |
| 103 | |
| 104 | void waitForProtocolHandshake(QDeadlineTimer deadline) const; |
| 105 | |
| 106 | bool isReadNotificationEnabled() const override; |
| 107 | void setReadNotificationEnabled(bool enable) override; |
| 108 | bool isWriteNotificationEnabled() const override; |
| 109 | void setWriteNotificationEnabled(bool enable) override; |
| 110 | bool isExceptionNotificationEnabled() const override; |
| 111 | void setExceptionNotificationEnabled(bool enable) override; |
| 112 | |
| 113 | public slots: |
| 114 | void slotSocketConnected(); |
| 115 | void slotSocketDisconnected(); |
| 116 | void slotSocketReadNotification(); |
| 117 | void slotSocketBytesWritten(); |
| 118 | void slotSocketError(QAbstractSocket::SocketError error); |
| 119 | void slotSocketStateChanged(QAbstractSocket::SocketState state); |
| 120 | |
| 121 | private slots: |
| 122 | void emitPendingReadNotification(); |
| 123 | void emitPendingWriteNotification(); |
| 124 | void emitPendingConnectionNotification(); |
| 125 | |
| 126 | private: |
| 127 | void emitReadNotification(); |
| 128 | void emitWriteNotification(); |
| 129 | void emitConnectionNotification(); |
| 130 | |
| 131 | bool (); |
| 132 | |
| 133 | Q_DECLARE_PRIVATE(QHttpSocketEngine) |
| 134 | Q_DISABLE_COPY_MOVE(QHttpSocketEngine) |
| 135 | |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | class QHttpSocketEnginePrivate : public QAbstractSocketEnginePrivate |
| 140 | { |
| 141 | Q_DECLARE_PUBLIC(QHttpSocketEngine) |
| 142 | public: |
| 143 | QHttpSocketEnginePrivate(); |
| 144 | ~QHttpSocketEnginePrivate(); |
| 145 | |
| 146 | QNetworkProxy proxy; |
| 147 | QString peerName; |
| 148 | QTcpSocket *socket; |
| 149 | QHttpNetworkReply *reply; // only used for parsing the proxy response |
| 150 | QHttpSocketEngine::HttpState state; |
| 151 | QAuthenticator authenticator; |
| 152 | bool readNotificationEnabled; |
| 153 | bool writeNotificationEnabled; |
| 154 | bool exceptNotificationEnabled; |
| 155 | bool readNotificationPending; |
| 156 | bool writeNotificationPending; |
| 157 | bool connectionNotificationPending; |
| 158 | bool credentialsSent; |
| 159 | uint pendingResponseData; |
| 160 | }; |
| 161 | |
| 162 | class Q_AUTOTEST_EXPORT QHttpSocketEngineHandler : public QSocketEngineHandler |
| 163 | { |
| 164 | public: |
| 165 | virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, |
| 166 | const QNetworkProxy &, QObject *parent) override; |
| 167 | virtual QAbstractSocketEngine *createSocketEngine(qintptr socketDescripter, QObject *parent) override; |
| 168 | }; |
| 169 | #endif |
| 170 | |
| 171 | QT_END_NAMESPACE |
| 172 | |
| 173 | #endif // QHTTPSOCKETENGINE_H |
| 174 | |