9 #include "response_p.h"
11 #include <QCryptographicHash>
14 #include <QJsonObject>
15 #include <QtCore/QJsonDocument>
20 : d_ptr(new ResponsePrivate(defaultHeaders, engineRequest))
41 if (!(d->engineRequest->status & EngineRequest::FinalizedHeaders)) {
42 if (d->headers.header(
"Transfer-Encoding"_qba).compare(
"chunked") == 0) {
43 d->engineRequest->status |= EngineRequest::IOWrite | EngineRequest::Chunked;
47 d->headers.setHeader(
"Connection"_qba,
"Close"_qba);
48 d->engineRequest->status |= EngineRequest::IOWrite;
50 delete d->bodyIODevice;
51 d->bodyIODevice =
nullptr;
54 d->engineRequest->finalizeHeaders();
57 return d->engineRequest->write(data, len);
62 delete d_ptr->bodyIODevice;
81 return !d->bodyData.isEmpty() || d->bodyIODevice ||
82 d->engineRequest->status & EngineRequest::IOWrite;
88 if (d->bodyIODevice) {
89 delete d->bodyIODevice;
90 d->bodyIODevice =
nullptr;
100 return d->bodyIODevice;
108 if (!(d->engineRequest->status & EngineRequest::IOWrite)) {
110 if (d->bodyIODevice) {
111 delete d->bodyIODevice;
113 d->bodyIODevice =
body;
122 d->setBodyData(
body);
128 d->setBodyData(cbor);
129 d->headers.setContentType(
"application/cbor"_qba);
140 d->setBodyData(json);
141 d->headers.setContentType(
"application/json"_qba);
157 return d->headers.contentEncoding();
163 Q_ASSERT_X(!(d->engineRequest->status & EngineRequest::FinalizedHeaders),
164 "setContentEncoding",
165 "setting a header value after finalize_headers and the response callback has been "
166 "called. Not what you want.");
168 d->headers.setContentEncoding(encoding);
174 return d->headers.contentLength();
180 Q_ASSERT_X(!(d->engineRequest->status & EngineRequest::FinalizedHeaders),
182 "setting a header value after finalize_headers and the response callback has been "
183 "called. Not what you want.");
185 d->headers.setContentLength(length);
191 return d->headers.contentType();
197 return d->headers.contentTypeCharset();
209 return d->cookies.values();
229 return d->cookies.remove(name);
240 qCDebug(CUTELYST_RESPONSE) <<
"Redirecting to" <<
location <<
status;
242 d->headers.setHeader(
"Location"_qba,
location);
243 d->headers.setContentType(
"text/html; charset=utf-8"_qba);
245 const QByteArray buf = R
"V0G0N(<!DOCTYPE html>
246 <html xmlns="http://www.w3.org/1999/xhtml">
251 <p>This item has moved <a href=")V0G0N" +
258 d->headers.removeHeader(
"Location"_qba);
259 qCDebug(CUTELYST_RESPONSE) <<
"Invalid redirect removing header" << url <<
status;
271 if (url.
matches(d->engineRequest->context->req()->uri(),
288 return d->headers.header(field);
294 Q_ASSERT_X(!(d->engineRequest->status & EngineRequest::FinalizedHeaders),
296 "setting a header value after finalize_headers and the response callback has been "
297 "called. Not what you want.");
299 d->headers.setHeader(key, value);
311 return d->engineRequest->status & EngineRequest::FinalizedHeaders;
322 if (d->engineRequest->status & EngineRequest::IOWrite) {
324 }
else if (d->bodyIODevice) {
325 return d->bodyIODevice->size();
327 return d->bodyData.size();
336 return d->engineRequest->webSocketHandshake(key, origin, protocol);
342 return d->engineRequest->webSocketSendTextMessage(message);
348 return d->engineRequest->webSocketSendBinaryMessage(message);
354 return d->engineRequest->webSocketSendPing(payload);
360 return d->engineRequest->webSocketClose(code, reason);
363 void ResponsePrivate::setBodyData(
const QByteArray &body)
365 if (!(engineRequest->status & EngineRequest::IOWrite)) {
368 bodyIODevice =
nullptr;
376 #include "moc_response.cpp"
qint64 size() const noexcept override
QByteArray header(const QByteArray &field) const noexcept
void redirect(const QUrl &url, quint16 status=Found)
QVariant cookie(const QByteArray &name) const
qint64 contentLength() const
bool webSocketHandshake(const QByteArray &key={}, const QByteArray &origin={}, const QByteArray &protocol={})
bool hasBody() const noexcept
bool webSocketTextMessage(const QString &message)
void setStatus(quint16 status) noexcept
bool webSocketPing(const QByteArray &payload={})
void setHeader(const QByteArray &key, const QByteArray &value)
Response(const Headers &defaultHeaders, EngineRequest *conn=nullptr)
Headers & headers() noexcept
QByteArray contentEncoding() const noexcept
QByteArray contentTypeCharset() const
void setBody(QIODevice *body)
void setJsonArrayBody(const QJsonArray &array)
void redirectSafe(const QUrl &url, const QUrl &fallback)
bool isSequential() const noexcept override
void setCookies(const QList< QNetworkCookie > &cookies)
virtual qint64 writeData(const char *data, qint64 len) override
QList< QNetworkCookie > cookies() const
bool webSocketBinaryMessage(const QByteArray &message)
bool isFinalizedHeaders() const noexcept
void setCborValueBody(const QCborValue &value)
void setContentLength(qint64 length)
int removeCookies(const QByteArray &name)
void setJsonObjectBody(const QJsonObject &obj)
virtual qint64 readData(char *data, qint64 maxlen) override
QIODevice * bodyDevice() const noexcept
void setCborBody(const QByteArray &cbor)
void setJsonBody(QStringView json)
bool webSocketClose(quint16 code=Response::CloseCodeNormal, const QString &reason={})
quint16 status() const noexcept
void setCookie(const QNetworkCookie &cookie)
QUrl location() const noexcept
void setContentEncoding(const QByteArray &encoding)
QByteArray contentType() const
virtual ~Response() override
The Cutelyst namespace holds all public Cutelyst API.
virtual bool open(QIODeviceBase::OpenMode mode)
bool isValid() const const
bool matches(const QUrl &url, QUrl::FormattingOptions options) const const
QByteArray toEncoded(QUrl::FormattingOptions options) const const
QVariant fromValue(T &&value)