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
26QT_REQUIRE_CONFIG(http);
27
28QT_BEGIN_NAMESPACE
29
30#if !defined(QT_NO_NETWORKPROXY)
31
32class QTcpSocket;
33class QHttpNetworkReply;
34class QHttpSocketEnginePrivate;
35
36class Q_AUTOTEST_EXPORT QHttpSocketEngine : public QAbstractSocketEngine
37{
38 Q_OBJECT
39public:
40 enum HttpState {
41 None,
42 ConnectSent,
43 Connected,
44 SendAuthentication,
45 ReadResponseContent,
46 ReadResponseHeader
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 readDatagram(char *data, qint64 maxlen, QIpPacketHeader *,
88 PacketHeaderOptions) override;
89 qint64 writeDatagram(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(int msecs = 30000, bool *timedOut = nullptr) override;
96 bool waitForWrite(int msecs = 30000, bool *timedOut = nullptr) override;
97 bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
98 bool checkRead, bool checkWrite,
99 int msecs = 30000, bool *timedOut = nullptr) override;
100
101 bool isReadNotificationEnabled() const override;
102 void setReadNotificationEnabled(bool enable) override;
103 bool isWriteNotificationEnabled() const override;
104 void setWriteNotificationEnabled(bool enable) override;
105 bool isExceptionNotificationEnabled() const override;
106 void setExceptionNotificationEnabled(bool enable) override;
107
108public slots:
109 void slotSocketConnected();
110 void slotSocketDisconnected();
111 void slotSocketReadNotification();
112 void slotSocketBytesWritten();
113 void slotSocketError(QAbstractSocket::SocketError error);
114 void slotSocketStateChanged(QAbstractSocket::SocketState state);
115
116private slots:
117 void emitPendingReadNotification();
118 void emitPendingWriteNotification();
119 void emitPendingConnectionNotification();
120
121private:
122 void emitReadNotification();
123 void emitWriteNotification();
124 void emitConnectionNotification();
125
126 bool readHttpHeader();
127
128 Q_DECLARE_PRIVATE(QHttpSocketEngine)
129 Q_DISABLE_COPY_MOVE(QHttpSocketEngine)
130
131};
132
133
134class QHttpSocketEnginePrivate : public QAbstractSocketEnginePrivate
135{
136 Q_DECLARE_PUBLIC(QHttpSocketEngine)
137public:
138 QHttpSocketEnginePrivate();
139 ~QHttpSocketEnginePrivate();
140
141 QNetworkProxy proxy;
142 QString peerName;
143 QTcpSocket *socket;
144 QHttpNetworkReply *reply; // only used for parsing the proxy response
145 QHttpSocketEngine::HttpState state;
146 QAuthenticator authenticator;
147 bool readNotificationEnabled;
148 bool writeNotificationEnabled;
149 bool exceptNotificationEnabled;
150 bool readNotificationPending;
151 bool writeNotificationPending;
152 bool connectionNotificationPending;
153 bool credentialsSent;
154 uint pendingResponseData;
155};
156
157class Q_AUTOTEST_EXPORT QHttpSocketEngineHandler : public QSocketEngineHandler
158{
159public:
160 virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType,
161 const QNetworkProxy &, QObject *parent) override;
162 virtual QAbstractSocketEngine *createSocketEngine(qintptr socketDescripter, QObject *parent) override;
163};
164#endif
165
166QT_END_NAMESPACE
167
168#endif // QHTTPSOCKETENGINE_H
169

source code of qtbase/src/network/socket/qhttpsocketengine_p.h