1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QHTTPSERVERRESPONDER_H
5#define QHTTPSERVERRESPONDER_H
6
7#include <QtHttpServer/qthttpserverglobal.h>
8
9#include <QtCore/qstringfwd.h>
10#include <QtCore/qmetatype.h>
11
12#include <memory>
13#include <utility>
14#include <initializer_list>
15
16QT_BEGIN_NAMESPACE
17
18class QHttpServerStream;
19class QHttpServerRequest;
20class QHttpServerResponse;
21
22class QHttpServerResponderPrivate;
23class Q_HTTPSERVER_EXPORT QHttpServerResponder final
24{
25 Q_GADGET
26 Q_DECLARE_PRIVATE(QHttpServerResponder)
27
28 friend class QHttpServerStream;
29
30public:
31 enum class StatusCode {
32 // 1xx: Informational
33 Continue = 100,
34 SwitchingProtocols,
35 Processing,
36
37 // 2xx: Success
38 Ok = 200,
39 Created,
40 Accepted,
41 NonAuthoritativeInformation,
42 NoContent,
43 ResetContent,
44 PartialContent,
45 MultiStatus,
46 AlreadyReported,
47 IMUsed = 226,
48
49 // 3xx: Redirection
50 MultipleChoices = 300,
51 MovedPermanently,
52 Found,
53 SeeOther,
54 NotModified,
55 UseProxy,
56 // 306: not used, was proposed as "Switch Proxy" but never standardized
57 TemporaryRedirect = 307,
58 PermanentRedirect,
59
60 // 4xx: Client Error
61 BadRequest = 400,
62 Unauthorized,
63 PaymentRequired,
64 Forbidden,
65 NotFound,
66 MethodNotAllowed,
67 NotAcceptable,
68 ProxyAuthenticationRequired,
69 RequestTimeout,
70 Conflict,
71 Gone,
72 LengthRequired,
73 PreconditionFailed,
74 PayloadTooLarge,
75 UriTooLong,
76 UnsupportedMediaType,
77 RequestRangeNotSatisfiable,
78 ExpectationFailed,
79 ImATeapot,
80 MisdirectedRequest = 421,
81 UnprocessableEntity,
82 Locked,
83 FailedDependency,
84 UpgradeRequired = 426,
85 PreconditionRequired = 428,
86 TooManyRequests,
87 RequestHeaderFieldsTooLarge = 431,
88 UnavailableForLegalReasons = 451,
89
90 // 5xx: Server Error
91 InternalServerError = 500,
92 NotImplemented,
93 BadGateway,
94 ServiceUnavailable,
95 GatewayTimeout,
96 HttpVersionNotSupported,
97 VariantAlsoNegotiates,
98 InsufficientStorage,
99 LoopDetected,
100 NotExtended = 510,
101 NetworkAuthenticationRequired,
102 NetworkConnectTimeoutError = 599,
103 };
104 Q_ENUM(StatusCode)
105
106 using HeaderList = std::initializer_list<std::pair<QByteArray, QByteArray>>;
107
108 QHttpServerResponder(QHttpServerResponder &&other);
109 ~QHttpServerResponder();
110
111 void write(QIODevice *data, HeaderList headers, StatusCode status = StatusCode::Ok);
112
113 void write(QIODevice *data, const QByteArray &mimeType, StatusCode status = StatusCode::Ok);
114
115 void write(const QJsonDocument &document,
116 HeaderList headers,
117 StatusCode status = StatusCode::Ok);
118
119 void write(const QJsonDocument &document,
120 StatusCode status = StatusCode::Ok);
121
122 void write(const QByteArray &data,
123 HeaderList headers,
124 StatusCode status = StatusCode::Ok);
125
126 void write(const QByteArray &data,
127 const QByteArray &mimeType,
128 StatusCode status = StatusCode::Ok);
129
130 void write(HeaderList headers, StatusCode status = StatusCode::Ok);
131 void write(StatusCode status = StatusCode::Ok);
132
133
134 void writeStatusLine(StatusCode status = StatusCode::Ok);
135
136 void writeHeader(const QByteArray &key, const QByteArray &value);
137 void writeHeaders(HeaderList headers);
138
139 void writeBody(const char *body, qint64 size);
140 void writeBody(const char *body);
141 void writeBody(const QByteArray &body);
142
143 void sendResponse(const QHttpServerResponse &response);
144
145private:
146 QHttpServerResponder(QHttpServerStream *stream);
147
148 std::unique_ptr<QHttpServerResponderPrivate> d_ptr;
149};
150
151QT_END_NAMESPACE
152
153QT_DECL_METATYPE_EXTERN_TAGGED(QHttpServerResponder::StatusCode, QHttpServerResponder__StatusCode,
154 Q_HTTPSERVER_EXPORT)
155
156#endif // QHTTPSERVERRESPONDER_H
157

source code of qthttpserver/src/httpserver/qhttpserverresponder.h