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// Qt-Security score:significant reason:default
4
5#ifndef QHTTPNETWORKCONNECTIONCHANNEL_H
6#define QHTTPNETWORKCONNECTIONCHANNEL_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of the Network Access API. This header file may change from
14// version to version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtNetwork/private/qtnetworkglobal_p.h>
20#include <QtNetwork/qnetworkrequest.h>
21#include <QtNetwork/qnetworkreply.h>
22#include <QtNetwork/qabstractsocket.h>
23
24#include <private/qobject_p.h>
25#include <qauthenticator.h>
26#include <qnetworkproxy.h>
27#include <qbuffer.h>
28
29#include <private/qhttpnetworkheader_p.h>
30#include <private/qhttpnetworkrequest_p.h>
31#include <private/qhttpnetworkreply_p.h>
32
33#include <private/qhttpnetworkconnection_p.h>
34#include <private/qabstractprotocolhandler_p.h>
35
36#ifndef QT_NO_SSL
37# include <QtNetwork/qsslsocket.h>
38# include <QtNetwork/qsslerror.h>
39# include <QtNetwork/qsslconfiguration.h>
40#else
41# include <QtNetwork/qtcpsocket.h>
42#endif
43#if QT_CONFIG(localserver)
44# include <QtNetwork/qlocalsocket.h>
45#endif
46
47
48#include <QtCore/qpointer.h>
49
50#include <memory>
51#include <optional>
52#include <utility>
53
54QT_REQUIRE_CONFIG(http);
55
56QT_BEGIN_NAMESPACE
57
58class QHttpNetworkRequest;
59class QHttpNetworkReply;
60class QByteArray;
61
62#ifndef HttpMessagePair
63typedef std::pair<QHttpNetworkRequest, QHttpNetworkReply*> HttpMessagePair;
64#endif
65
66class QHttpNetworkConnectionChannel final : public QObject {
67 Q_OBJECT
68public:
69 // TODO: Refactor this to add an EncryptingState (and remove pendingEncrypt).
70 // Also add an Unconnected state so IdleState does not have double meaning.
71 enum ChannelState {
72 IdleState = 0, // ready to send request
73 ConnectingState = 1, // connecting to host
74 WritingState = 2, // writing the data
75 WaitingState = 4, // waiting for reply
76 ReadingState = 8, // reading the reply
77 ClosingState = 16,
78 BusyState = (ConnectingState|WritingState|WaitingState|ReadingState|ClosingState)
79 };
80 QIODevice *socket;
81 bool ssl;
82 bool isInitialized;
83 bool waitingForPotentialAbort = false;
84 bool needInvokeReceiveReply = false;
85 bool needInvokeReadyRead = false;
86 bool needInvokeSendRequest = false;
87 ChannelState state;
88 QHttpNetworkRequest request; // current request, only used for HTTP
89 QHttpNetworkReply *reply; // current reply for this request, only used for HTTP
90 qint64 written;
91 qint64 bytesTotal;
92 bool resendCurrent;
93 int lastStatus; // last status received on this channel
94 bool pendingEncrypt; // for https (send after encrypted)
95 int reconnectAttempts; // maximum 2 reconnection attempts
96 QAuthenticator authenticator;
97 QAuthenticator proxyAuthenticator;
98 bool authenticationCredentialsSent;
99 bool proxyCredentialsSent;
100 std::unique_ptr<QAbstractProtocolHandler> protocolHandler;
101 QMultiMap<int, HttpMessagePair> h2RequestsToSend;
102 bool switchedToHttp2 = false;
103#ifndef QT_NO_SSL
104 bool ignoreAllSslErrors;
105 QList<QSslError> ignoreSslErrorsList;
106 std::optional<QSslConfiguration> sslConfiguration;
107 void ignoreSslErrors();
108 void ignoreSslErrors(const QList<QSslError> &errors);
109 void setSslConfiguration(const QSslConfiguration &config);
110 void requeueHttp2Requests(); // when we wanted HTTP/2 but got HTTP/1.1
111#endif
112 // to emit the signal for all in-flight replies:
113 void emitFinishedWithError(QNetworkReply::NetworkError error, const char *message);
114
115 // HTTP pipelining -> http://en.wikipedia.org/wiki/Http_pipelining
116 enum PipeliningSupport {
117 PipeliningSupportUnknown, // default for a new connection
118 PipeliningProbablySupported, // after having received a server response that indicates support
119 PipeliningNotSupported // currently not used
120 };
121 PipeliningSupport pipeliningSupported;
122 QList<HttpMessagePair> alreadyPipelinedRequests;
123 QByteArray pipeline; // temporary buffer that gets sent to socket in pipelineFlush
124 void pipelineInto(HttpMessagePair &pair);
125 void pipelineFlush();
126 void requeueCurrentlyPipelinedRequests();
127 void detectPipeliningSupport();
128
129 QHttpNetworkConnectionChannel();
130
131 QAbstractSocket::NetworkLayerProtocol networkLayerPreference;
132
133 void setConnection(QHttpNetworkConnection *c);
134 QPointer<QHttpNetworkConnection> connection;
135
136#ifndef QT_NO_NETWORKPROXY
137 QNetworkProxy proxy;
138 void setProxy(const QNetworkProxy &networkProxy);
139#endif
140
141 void init();
142 void close();
143 void abort();
144
145 void sendRequest();
146 void sendRequestDelayed();
147
148 bool ensureConnection();
149
150 void allDone(); // reply header + body have been read
151 void handleStatus(); // called from allDone()
152
153 bool resetUploadData(); // return true if resetting worked or there is no upload data
154
155 void handleUnexpectedEOF();
156 void closeAndResendCurrentRequest();
157 void resendCurrentRequest();
158
159 void checkAndResumeCommunication();
160
161 bool isSocketBusy() const;
162 bool isSocketWriting() const;
163 bool isSocketWaiting() const;
164 bool isSocketReading() const;
165
166 protected slots:
167 void _q_receiveReply();
168 void _q_bytesWritten(qint64 bytes); // proceed sending
169 void _q_readyRead(); // pending data to read
170 void _q_disconnected(); // disconnected from host
171 void _q_connected_abstract_socket(QAbstractSocket *socket);
172#if QT_CONFIG(localserver)
173 void _q_connected_local_socket(QLocalSocket *socket);
174#endif
175 void _q_connected(); // start sending request
176 void _q_error(QAbstractSocket::SocketError); // error from socket
177#ifndef QT_NO_NETWORKPROXY
178 void _q_proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth); // from transparent proxy
179#endif
180
181 void _q_uploadDataReadyRead();
182
183#ifndef QT_NO_SSL
184 void _q_encrypted(); // start sending request (https)
185 void _q_sslErrors(const QList<QSslError> &errors); // ssl errors from the socket
186 void _q_preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator*); // tls-psk auth necessary
187 void _q_encryptedBytesWritten(qint64 bytes); // proceed sending
188#endif
189
190 friend class QHttpProtocolHandler;
191};
192
193QT_END_NAMESPACE
194
195#endif
196

source code of qtbase/src/network/access/qhttpnetworkconnectionchannel_p.h