cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
view.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2013-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "view.h"
6 
7 #include "common.h"
8 #include "view_p.h"
9 
10 #include <Cutelyst/Context>
11 #include <Cutelyst/Response>
12 
13 #include <QtCore/QLoggingCategory>
14 #include <QtCore/QVariant>
15 
16 using namespace Cutelyst;
17 
18 View::View(QObject *parent, const QString &name)
19  : Component(new ViewPrivate, parent)
20 {
21  setName(name);
22 }
23 
24 View::View(ViewPrivate *d, QObject *parent, const QString &name)
25  : Component(d, parent)
26 {
27  setName(name);
28 }
29 
30 Component::Modifiers View::modifiers() const
31 {
32  return Component::OnlyExecute;
33 }
34 
35 bool View::doExecute(Context *c)
36 {
37  Q_D(const View);
38  Response *response = c->response();
39  if (response->hasBody()) {
40  // Ignore if we already have a body
41  return true;
42  }
43 
44  const QByteArray output = render(c);
45  if (Q_UNLIKELY(c->error())) {
46  const auto errors = c->errors();
47  for (const QString &error : errors) {
48  qCCritical(CUTELYST_VIEW) << error;
49  }
50  }
51  const QByteArray acceptEncoding = c->req()->header("Accept-Encoding");
52  if (d->minimalSizeToDeflate >= 0 && output.count() > d->minimalSizeToDeflate &&
53  acceptEncoding.toLower().contains("deflate")) {
54  QByteArray compressedData = qCompress(output); // Use zlib's default compression
55  compressedData.remove(0, 6); // Remove qCompress and zlib headers
56  compressedData.chop(4); // Remove zlib tailer
57  response->headers().setContentEncoding("deflate");
58  response->setBody(compressedData);
59  } else {
60  response->setBody(output);
61  }
62  return !c->error();
63 }
64 
65 void View::setMinimalSizeToDeflate(qint32 minSize)
66 {
67  Q_D(View);
68  d->minimalSizeToDeflate = minSize;
69 }
70 #include "moc_view.cpp"
The Cutelyst Component base class.
Definition: component.h:30
QString name() const noexcept
Definition: component.cpp:33
void setName(const QString &name)
Definition: component.cpp:39
The Cutelyst Context.
Definition: context.h:42
QStringList errors() const noexcept
Definition: context.cpp:67
Request * req
Definition: context.h:66
bool error() const noexcept
Definition: context.cpp:50
Response * response() const noexcept
Definition: context.cpp:97
void setContentEncoding(const QByteArray &encoding)
Definition: headers.cpp:62
QByteArray header(QByteArrayView key) const noexcept
Definition: request.h:611
A Cutelyst response.
Definition: response.h:29
bool hasBody() const noexcept
Definition: response.cpp:78
Headers & headers() noexcept
void setBody(QIODevice *body)
Definition: response.cpp:103
Abstract View component for Cutelyst.
Definition: view.h:25
virtual QByteArray render(Context *c) const =0
View(QObject *parent, const QString &name)
Definition: view.cpp:18
void setMinimalSizeToDeflate(qint32 minSize=-1)
Definition: view.cpp:65
Modifiers modifiers() const override
Definition: view.cpp:30
The Cutelyst namespace holds all public Cutelyst API.
qsizetype count() const const
void chop(qsizetype n)
bool contains(QByteArrayView bv) const const
QByteArray & remove(qsizetype pos, qsizetype len)
QByteArray toLower() const const