| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QHttpServerHttp1ProtocolHandler_H |
| 6 | #define QHttpServerHttp1ProtocolHandler_H |
| 7 | |
| 8 | #include <QtHttpServer/qthttpserverglobal.h> |
| 9 | #include <QtHttpServer/qhttpserverrequest.h> |
| 10 | #include <QtHttpServer/private/qhttpserverparser_p.h> |
| 11 | #include <QtHttpServer/private/qhttpserverstream_p.h> |
| 12 | #include <QtHttpServer/private/qhttpserverrequestfilter_p.h> |
| 13 | |
| 14 | #include <QtCore/qelapsedtimer.h> |
| 15 | |
| 16 | // |
| 17 | // W A R N I N G |
| 18 | // ------------- |
| 19 | // |
| 20 | // This file is not part of the Qt API. It exists for the convenience |
| 21 | // of QHttpServer. This header file may change from version to |
| 22 | // version without notice, or even be removed. |
| 23 | // |
| 24 | // We mean it. |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QTcpSocket; |
| 29 | class QAbstractHttpServer; |
| 30 | #if QT_CONFIG(localserver) |
| 31 | class QLocalSocket; |
| 32 | #endif |
| 33 | template <qint64 BUFFERSIZE> |
| 34 | struct QHttpServerHttp1IOChunkedTransfer; |
| 35 | |
| 36 | class QHttpServerHttp1ProtocolHandler : public QHttpServerStream |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | |
| 40 | friend class QAbstractHttpServerPrivate; |
| 41 | friend class QHttpServerResponder; |
| 42 | |
| 43 | private: |
| 44 | QHttpServerHttp1ProtocolHandler(QAbstractHttpServer *server, |
| 45 | QIODevice *socket, |
| 46 | QHttpServerRequestFilter *filter); |
| 47 | |
| 48 | void responderDestroyed() final; |
| 49 | void startHandlingRequest() final; |
| 50 | void socketDisconnected() final; |
| 51 | |
| 52 | void handleReadyRead(); |
| 53 | |
| 54 | void write(const QByteArray &body, const QHttpHeaders &, |
| 55 | QHttpServerResponder::StatusCode status, quint32 streamId) final; |
| 56 | void write(QHttpServerResponder::StatusCode status, quint32 streamId) final; |
| 57 | void write(QIODevice *data, const QHttpHeaders &, |
| 58 | QHttpServerResponder::StatusCode status, quint32 streamId) final; |
| 59 | void writeBeginChunked(const QHttpHeaders &, |
| 60 | QHttpServerResponder::StatusCode status, |
| 61 | quint32 streamId) final; |
| 62 | void writeChunk(const QByteArray &body, quint32 streamId) final; |
| 63 | void writeEndChunked(const QByteArray &data, |
| 64 | const QHttpHeaders &trailers, |
| 65 | quint32 streamId) final; |
| 66 | |
| 67 | void writeStatusAndHeaders(QHttpServerResponder::StatusCode status, |
| 68 | const QHttpHeaders &); |
| 69 | void writeHeader(const QByteArray &key, const QByteArray &value); |
| 70 | void write(const QByteArray &data); |
| 71 | void write(const char *body, qint64 size); |
| 72 | |
| 73 | void checkKeepAliveTimeout(); |
| 74 | void resumeListening(); |
| 75 | |
| 76 | QAbstractHttpServer *server; |
| 77 | QIODevice *socket; |
| 78 | QTcpSocket *tcpSocket; |
| 79 | #if QT_CONFIG(localserver) |
| 80 | QLocalSocket *localSocket; |
| 81 | #endif |
| 82 | QHttpServerRequestFilter *m_filter; |
| 83 | |
| 84 | enum class TransferState { |
| 85 | Ready, |
| 86 | HeadersSent, |
| 87 | ChunkedTransferBegun, |
| 88 | IODeviceTransferBegun, |
| 89 | } state = TransferState::Ready; |
| 90 | |
| 91 | // To avoid destroying the object when socket object is destroyed while |
| 92 | // a request is still being handled. |
| 93 | bool handlingRequest = false; |
| 94 | bool protocolChanged = false; |
| 95 | QElapsedTimer lastActiveTimer; |
| 96 | bool useHttp1_1 = false; |
| 97 | void completeWriting(); |
| 98 | |
| 99 | template <qint64 BUFFERSIZE> |
| 100 | friend struct QHttpServerHttp1IOChunkedTransfer; |
| 101 | }; |
| 102 | |
| 103 | QT_END_NAMESPACE |
| 104 | |
| 105 | #endif // QHttpServerHttp1ProtocolHandler_H |
| 106 | |