9#include <Cutelyst/Context>
10#include <Cutelyst/response_p.h>
12#include <QLoggingCategory>
13Q_LOGGING_CATEGORY(CUTELYST_ENGINEREQUEST,
"cutelyst.engine_request", QtWarningMsg)
17EngineRequest::EngineRequest()
21EngineRequest::~EngineRequest()
28 if (!(
status & EngineRequest::Chunked)) {
33 if (!
body->isSequential()) {
37 char block[64 * 1024];
38 while (!
body->atEnd()) {
39 qint64 in =
body->read(block,
sizeof(block));
44 if (
write(block, in) != in) {
45 qCWarning(CUTELYST_ENGINEREQUEST) <<
"Failed to write body";
53 }
else if (!(
status & EngineRequest::ChunkedDone)) {
76 res->
setStatus(Response::InternalServerError);
89 status |= EngineRequest::Finalized;
97 const auto cookies = res->
cookies();
101#if (QT_VERSION < QT_VERSION_CHECK(6, 1, 0))
102 const auto cuteCookies = res->cuteCookies();
103 for (
const Cookie &cookie : cuteCookies) {
115 if (
headers.contentLength() < 0) {
116 qint64 size = response->
size();
118 headers.setContentLength(size);
125 status |= EngineRequest::FinalizedHeaders;
131 if (!(
status & EngineRequest::Chunked)) {
133 }
else if (!(
status & EngineRequest::ChunkedDone)) {
143 status |= EngineRequest::ChunkedDone;
146 return retWrite == chunk.
size() ? len : -1;
151bool EngineRequest::webSocketHandshake(
const QString &key,
155 if (
status & EngineRequest::FinalizedHeaders) {
159 if (webSocketHandshakeDo(key, origin,
protocol)) {
160 status |= EngineRequest::FinalizedHeaders | EngineRequest::Async | EngineRequest::IOWrite;
170bool EngineRequest::webSocketSendTextMessage(
const QString &message)
176bool EngineRequest::webSocketSendBinaryMessage(
const QByteArray &message)
182bool EngineRequest::webSocketSendPing(
const QByteArray &payload)
188bool EngineRequest::webSocketClose(quint16 code,
const QString &reason)
199bool EngineRequest::webSocketHandshakeDo(
const QString &key,
216 char *data = rawPath;
217 const char *inputPtr = data;
219 bool skipUtf8 =
true;
221 for (
int i = 0; i < len; ++i, ++outlen) {
222 const char c = inputPtr[i];
223 if (c ==
'%' && i + 2 < len) {
224 int a = inputPtr[++i];
225 int b = inputPtr[++i];
227 if (a >=
'0' && a <=
'9')
229 else if (a >=
'a' && a <=
'f')
231 else if (a >=
'A' && a <=
'F')
234 if (b >=
'0' && b <=
'9')
236 else if (b >=
'a' && b <=
'f')
238 else if (b >=
'A' && b <=
'F')
241 *data++ = char((a << 4) | b);
243 }
else if (c ==
'+') {
257#include "moc_enginerequest.cpp"
void finalize()
finalize the request right away this is automatically called at the end of the actions chain
virtual qint64 doWrite(const char *data, qint64 len)=0
Reimplement this to do the RAW writing to the client.
Context * context
The Cutelyst::Context of this request.
virtual void finalizeBody()
Engines must reimplement this to write the response body back to the caller.
virtual void finalizeError()
Engines should overwrite this if they want to to make custom error messages.
QString path
Call setPath() instead.
Status status
Connection status.
void finalize()
Called by Application to deal with finalizing cookies, headers and body.
qint64 write(const char *data, qint64 len)
Called by Response to manually write data.
void setPath(char *rawPath, const int len)
This method sets the path and already does the decoding so that it is done a single time.
Headers headers
The request headers.
virtual bool writeHeaders(quint16 status, const Headers &headers)=0
Reimplement this to write the headers back to the client.
virtual bool finalizeHeaders()
Finalize the headers, and call doWriteHeader(), reimplemententions must call this first.
virtual void finalizeCookies()
Reimplement if you need a custom way to Set-Cookie, the default implementation writes them to c->res(...
QIODevice * body
The QIODevice containing the body (if any) of the request.
QString protocol
The protocol requested by the user agent 'HTTP1/1'.
virtual void processingFinished()
This is called when the Application chain is finished processing this request, here the request can s...
Headers & headers() noexcept
void setStatus(quint16 status) noexcept
void setBody(QIODevice *body)
QIODevice * bodyDevice() const
QList< QNetworkCookie > cookies() const
Q_REQUIRED_RESULT QByteArray & body()
virtual qint64 size() const noexcept override
quint16 status() const noexcept
void setContentType(const QString &type)
The Cutelyst namespace holds all public Cutelyst API.
QByteArray & append(char ch)
const char * constData() const const
QByteArray number(int n, int base)
QByteArray toUpper() const const
QString fromLatin1(const char *str, int size)
QString fromUtf8(const char *str, int size)