| 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 | #include <QtNetwork/qhttpheaders.h> |
| 9 | |
| 10 | #include <QtCore/qstringfwd.h> |
| 11 | #include <QtCore/qmetatype.h> |
| 12 | |
| 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | #include <initializer_list> |
| 16 | |
| 17 | QT_BEGIN_NAMESPACE |
| 18 | |
| 19 | class QHttpServerStream; |
| 20 | class QHttpServerRequest; |
| 21 | class QHttpServerResponse; |
| 22 | |
| 23 | class QHttpServerResponderPrivate; |
| 24 | class QHttpServerResponder final |
| 25 | { |
| 26 | Q_GADGET_EXPORT(Q_HTTPSERVER_EXPORT) |
| 27 | Q_DECLARE_PRIVATE(QHttpServerResponder) |
| 28 | |
| 29 | friend class QHttpServerHttp1ProtocolHandler; |
| 30 | friend class QHttpServerHttp2ProtocolHandler; |
| 31 | |
| 32 | public: |
| 33 | enum class StatusCode { |
| 34 | // 1xx: Informational |
| 35 | Continue = 100, |
| 36 | SwitchingProtocols, |
| 37 | Processing, |
| 38 | |
| 39 | // 2xx: Success |
| 40 | Ok = 200, |
| 41 | Created, |
| 42 | Accepted, |
| 43 | NonAuthoritativeInformation, |
| 44 | NoContent, |
| 45 | ResetContent, |
| 46 | PartialContent, |
| 47 | MultiStatus, |
| 48 | AlreadyReported, |
| 49 | IMUsed = 226, |
| 50 | |
| 51 | // 3xx: Redirection |
| 52 | MultipleChoices = 300, |
| 53 | MovedPermanently, |
| 54 | Found, |
| 55 | SeeOther, |
| 56 | NotModified, |
| 57 | UseProxy, |
| 58 | // 306: not used, was proposed as "Switch Proxy" but never standardized |
| 59 | TemporaryRedirect = 307, |
| 60 | PermanentRedirect, |
| 61 | |
| 62 | // 4xx: Client Error |
| 63 | BadRequest = 400, |
| 64 | Unauthorized, |
| 65 | PaymentRequired, |
| 66 | Forbidden, |
| 67 | NotFound, |
| 68 | MethodNotAllowed, |
| 69 | NotAcceptable, |
| 70 | ProxyAuthenticationRequired, |
| 71 | RequestTimeout, |
| 72 | Conflict, |
| 73 | Gone, |
| 74 | LengthRequired, |
| 75 | PreconditionFailed, |
| 76 | PayloadTooLarge, |
| 77 | UriTooLong, |
| 78 | UnsupportedMediaType, |
| 79 | RequestRangeNotSatisfiable, |
| 80 | ExpectationFailed, |
| 81 | ImATeapot, |
| 82 | MisdirectedRequest = 421, |
| 83 | UnprocessableEntity, |
| 84 | Locked, |
| 85 | FailedDependency, |
| 86 | UpgradeRequired = 426, |
| 87 | PreconditionRequired = 428, |
| 88 | TooManyRequests, |
| 89 | = 431, |
| 90 | UnavailableForLegalReasons = 451, |
| 91 | |
| 92 | // 5xx: Server Error |
| 93 | InternalServerError = 500, |
| 94 | NotImplemented, |
| 95 | BadGateway, |
| 96 | ServiceUnavailable, |
| 97 | GatewayTimeout, |
| 98 | HttpVersionNotSupported, |
| 99 | VariantAlsoNegotiates, |
| 100 | InsufficientStorage, |
| 101 | LoopDetected, |
| 102 | NotExtended = 510, |
| 103 | NetworkAuthenticationRequired, |
| 104 | NetworkConnectTimeoutError = 599, |
| 105 | }; |
| 106 | Q_ENUM(StatusCode) |
| 107 | |
| 108 | QHttpServerResponder(QHttpServerResponder &&other) noexcept |
| 109 | : d_ptr(std::exchange(obj&: other.d_ptr, new_val: nullptr)) |
| 110 | { |
| 111 | } |
| 112 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QHttpServerResponder) |
| 113 | |
| 114 | Q_HTTPSERVER_EXPORT ~QHttpServerResponder(); |
| 115 | |
| 116 | void swap(QHttpServerResponder &other) noexcept { qt_ptr_swap(lhs&: d_ptr, rhs&: other.d_ptr); } |
| 117 | |
| 118 | Q_HTTPSERVER_EXPORT void (QIODevice *data, const QHttpHeaders &, |
| 119 | StatusCode status = StatusCode::Ok); |
| 120 | |
| 121 | Q_HTTPSERVER_EXPORT void write(QIODevice *data, const QByteArray &mimeType, |
| 122 | StatusCode status = StatusCode::Ok); |
| 123 | |
| 124 | Q_HTTPSERVER_EXPORT void (const QJsonDocument &document, const QHttpHeaders &, |
| 125 | StatusCode status = StatusCode::Ok); |
| 126 | |
| 127 | Q_HTTPSERVER_EXPORT void write(const QJsonDocument &document, |
| 128 | StatusCode status = StatusCode::Ok); |
| 129 | |
| 130 | Q_HTTPSERVER_EXPORT void (const QByteArray &data, const QHttpHeaders &, |
| 131 | StatusCode status = StatusCode::Ok); |
| 132 | |
| 133 | Q_HTTPSERVER_EXPORT void write(const QByteArray &data, const QByteArray &mimeType, |
| 134 | StatusCode status = StatusCode::Ok); |
| 135 | |
| 136 | Q_HTTPSERVER_EXPORT void (const QHttpHeaders &, StatusCode status = StatusCode::Ok); |
| 137 | |
| 138 | Q_HTTPSERVER_EXPORT void write(StatusCode status = StatusCode::Ok); |
| 139 | |
| 140 | Q_HTTPSERVER_EXPORT void sendResponse(const QHttpServerResponse &response); |
| 141 | |
| 142 | Q_HTTPSERVER_EXPORT void (const QHttpHeaders &, |
| 143 | StatusCode status = StatusCode::Ok); |
| 144 | |
| 145 | Q_HTTPSERVER_EXPORT void writeBeginChunked(const QByteArray &mimeType, |
| 146 | StatusCode status = StatusCode::Ok); |
| 147 | |
| 148 | Q_HTTPSERVER_EXPORT void (const QHttpHeaders &, |
| 149 | QList<QHttpHeaders::WellKnownHeader> trailerNames, |
| 150 | StatusCode status = StatusCode::Ok); |
| 151 | |
| 152 | Q_HTTPSERVER_EXPORT void writeChunk(const QByteArray &data); |
| 153 | |
| 154 | Q_HTTPSERVER_EXPORT void (const QByteArray &data, const QHttpHeaders &trailers); |
| 155 | |
| 156 | Q_HTTPSERVER_EXPORT void writeEndChunked(const QByteArray &data); |
| 157 | |
| 158 | private: |
| 159 | Q_HTTPSERVER_EXPORT QHttpServerResponder(QHttpServerStream *stream); |
| 160 | Q_DISABLE_COPY(QHttpServerResponder) |
| 161 | |
| 162 | QHttpServerResponderPrivate *d_ptr; |
| 163 | }; |
| 164 | |
| 165 | QT_END_NAMESPACE |
| 166 | |
| 167 | #endif // QHTTPSERVERRESPONDER_H |
| 168 | |