aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/mcpserver/httpresponse.h
blob: af5b185b95ce1f4e51609d3ea38aa03d36c06067 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2025 David M. Cotter
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once

#include <QByteArray>
#include <QMap>
#include <QObject>
#include <QString>

namespace Mcp::Internal {

/**
 * @brief HTTP Response Builder
 *
 * Builds HTTP/1.1 responses with proper headers and formatting.
 * Designed to work with the existing TCP MCP server to provide
 * HTTP compatibility without requiring the HttpServer module.
 */
class HttpResponse : public QObject
{
    Q_OBJECT

public:
    /**
     * @brief HTTP status codes
     */
    enum StatusCode {
        OK = 200,
        CREATED = 201,
        NO_CONTENT = 204,
        BAD_REQUEST = 400,
        UNAUTHORIZED = 401,
        FORBIDDEN = 403,
        NOT_FOUND = 404,
        METHOD_NOT_ALLOWED = 405,
        INTERNAL_SERVER_ERROR = 500,
        NOT_IMPLEMENTED = 501,
        BAD_GATEWAY = 502,
        SERVICE_UNAVAILABLE = 503
    };

    /**
     * @brief HTTP response structure
     */
    struct ResponseData
    {
        StatusCode statusCode;
        QString statusMessage;
        QMap<QString, QString> headers;
        QByteArray body;
        QString version;
    };

    explicit HttpResponse(QObject *parent = nullptr);

    /**
     * @brief Create a successful response with JSON body
     * @param jsonBody JSON content to send
     * @param statusCode HTTP status code (default: 200 OK)
     * @return Formatted HTTP response
     */
    static QByteArray createJsonResponse(const QByteArray &jsonBody, StatusCode statusCode = OK);

    /**
     * @brief Create a simple text response
     * @param textBody Text content to send
     * @param statusCode HTTP status code (default: 200 OK)
     * @return Formatted HTTP response
     */
    static QByteArray createTextResponse(const QString &textBody, StatusCode statusCode = OK);

    /**
     * @brief Create an error response
     * @param statusCode HTTP status code
     * @param errorMessage Error message to include
     * @return Formatted HTTP response
     */
    static QByteArray createErrorResponse(StatusCode statusCode, const QString &errorMessage);

    /**
     * @brief Create a CORS-enabled response
     * @param jsonBody JSON content to send
     * @param statusCode HTTP status code (default: 200 OK)
     * @return Formatted HTTP response with CORS headers
     */
    static QByteArray createCorsResponse(const QByteArray &jsonBody, StatusCode statusCode = OK);

    /**
     * @brief Build HTTP response from response data
     * @param response Response data structure
     * @return Formatted HTTP response
     */
    static QByteArray buildResponse(const ResponseData &response);

private:
    /**
     * @brief Get status message for status code
     * @param statusCode HTTP status code
     * @return Status message string
     */
    static QString getStatusMessage(StatusCode statusCode);

    /**
     * @brief Format HTTP response line
     * @param version HTTP version
     * @param statusCode HTTP status code
     * @param statusMessage Status message
     * @return Formatted status line
     */
    static QString formatStatusLine(
        const QString &version, StatusCode statusCode, const QString &statusMessage);

    /**
     * @brief Format HTTP headers
     * @param headers Header map
     * @return Formatted header string
     */
    static QString formatHeaders(const QMap<QString, QString> &headers);
};

} // namespace Mcp::Internal