summaryrefslogtreecommitdiffstats
path: root/src/httpserver/qhttpserverhttp1protocolhandler_p.h
blob: 8afd8f97c8b0871ed26206706deb3acdf03ef827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QHttpServerHttp1ProtocolHandler_H
#define QHttpServerHttp1ProtocolHandler_H

#include <QtHttpServer/qthttpserverglobal.h>
#include <QtHttpServer/qhttpserverrequest.h>
#include <QtHttpServer/private/qhttpserverparser_p.h>
#include <QtHttpServer/private/qhttpserverstream_p.h>
#include <QtHttpServer/private/qhttpserverrequestfilter_p.h>

#include <QtCore/qelapsedtimer.h>

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of QHttpServer. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.

QT_BEGIN_NAMESPACE

class QTcpSocket;
class QAbstractHttpServer;
#if QT_CONFIG(localserver)
class QLocalSocket;
#endif

class QHttpServerHttp1ProtocolHandler : public QHttpServerStream
{
    Q_OBJECT

    friend class QAbstractHttpServerPrivate;
    friend class QHttpServerResponder;

private:
    QHttpServerHttp1ProtocolHandler(QAbstractHttpServer *server,
                                    QIODevice *socket,
                                    QHttpServerRequestFilter *filter);

    void responderDestroyed() final;
    void startHandlingRequest() final;
    void socketDisconnected() final;

    void handleReadyRead();

    void write(const QByteArray &body, const QHttpHeaders &headers,
               QHttpServerResponder::StatusCode status, quint32 streamId) final;
    void write(QHttpServerResponder::StatusCode status, quint32 streamId) final;
    void write(QIODevice *data, const QHttpHeaders &headers,
               QHttpServerResponder::StatusCode status, quint32 streamId) final;
    void writeBeginChunked(const QHttpHeaders &headers,
                           QHttpServerResponder::StatusCode status,
                           quint32 streamId) final;
    void writeChunk(const QByteArray &body, quint32 streamId) final;
    void writeEndChunked(const QByteArray &data,
                         const QHttpHeaders &trailers,
                         quint32 streamId) final;

    void writeStatusAndHeaders(QHttpServerResponder::StatusCode status,
                               const QHttpHeaders &headers);
    void writeHeader(const QByteArray &key, const QByteArray &value);
    void write(const QByteArray &data);
    void write(const char *body, qint64 size);

    void checkKeepAliveTimeout();

    QAbstractHttpServer *server;
    QIODevice *socket;
    QTcpSocket *tcpSocket;
#if QT_CONFIG(localserver)
    QLocalSocket *localSocket;
#endif
    QHttpServerRequestFilter *m_filter;

    enum class TransferState {
        Ready,
        HeadersSent,
        ChunkedTransferBegun
    } state = TransferState::Ready;

   // To avoid destroying the object when socket object is destroyed while
   // a request is still being handled.
    bool handlingRequest = false;
    bool protocolChanged = false;
    QElapsedTimer lastActiveTimer;
};

QT_END_NAMESPACE

#endif // QHttpServerHttp1ProtocolHandler_H