summaryrefslogtreecommitdiffstats
path: root/tests/auto/network/access/qrestaccessmanager/httptestserver_p.h
blob: ead6590a55c25cdf06aff5e9d05658ba6342da59 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QRESTACCESSSMANAGER_HTTPTESTSERVER_P_H
#define QRESTACCESSSMANAGER_HTTPTESTSERVER_P_H

#include <QtNetwork/qtcpserver.h>
#include <QtNetwork/qhttpheaders.h>

#include <QtCore/qmap.h>
#include <QtCore/qurl.h>

// This struct is used for parsing the incoming network request data into, as well
// as getting the response data from the testcase
struct HttpData {
    QUrl url;
    int status = 0;
    QByteArray body;
    QByteArray method;
    quint16 port = 0;
    QPair<quint8, quint8> version;
    QHttpHeaders headers;
};

struct ResponseControl
{
    bool respond = true;
    qsizetype responseChunkSize = -1;
    bool readyForNextChunk = true;
};

// Simple HTTP server. Currently supports only one concurrent connection
class HttpTestServer : public QTcpServer
{
    Q_OBJECT

public:
    explicit HttpTestServer(QObject *parent = nullptr);
    ~HttpTestServer() override;

    // Returns this server's URL for the testcase to send requests to
    QUrl url();

    enum class State {
        ReadingMethod,
        ReadingUrl,
        ReadingStatus,
        ReadingHeader,
        ReadingBody,
        AllDone
    } state = State::ReadingMethod;

    enum class Method {
        Unknown,
        Head,
        Get,
        Put,
        Patch,
        Post,
        Delete,
        Custom,
    } method = Method::Unknown;

    // Parsing helpers for incoming data => HttpData
    bool readMethod(QTcpSocket *socket);
    bool readUrl(QTcpSocket *socket);
    bool readStatus(QTcpSocket *socket);
    bool readHeaders(QTcpSocket *socket);
    bool readBody(QTcpSocket *socket);
    // Parsing-time buffer in case data is received a small chunk at a time (readyRead())
    QByteArray fragment;

    // Settable callback for testcase. Gives the received request data, and takes in response data
    using Handler = std::function<void(const HttpData &request, HttpData &response,
                                       ResponseControl &control)>;
    void setHandler(const Handler &handler);

private slots:
    void handleConnected();
    void handleDataAvailable();

private:
    QTcpSocket *m_socket = nullptr;
    HttpData m_request;
    Handler m_handler = nullptr;
};

#endif // QRESTACCESSSMANAGER_HTTPTESTSERVER_P_H