Cutelyst
2.13.0
Cutelyst
view.cpp
1
/*
2
* Copyright (C) 2013-2019 Daniel Nicoletti <dantti12@gmail.com>
3
*
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
8
*
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
13
*
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
*/
18
19
#include "view.h"
20
#include "view_p.h"
21
22
#include "common.h"
23
24
#include <Cutelyst/Context>
25
#include <Cutelyst/Response>
26
27
#include <QtCore/QVariant>
28
#include <QtCore/QLoggingCategory>
29
30
using namespace
Cutelyst
;
31
32
View::View
(QObject *parent,
const
QString &name) :
Component
(new ViewPrivate, parent)
33
{
34
setName
(
name
);
35
}
36
37
View::View
(ViewPrivate *d, QObject *parent,
const
QString &name) :
Component
(d, parent)
38
{
39
setName
(
name
);
40
}
41
42
Component::Modifiers
View::modifiers
()
const
43
{
44
return
Component::OnlyExecute;
45
}
46
47
bool
View::doExecute(
Context
*c)
48
{
49
Q_D(
const
View
);
50
Response
*response = c->
response
();
51
if
(response->
hasBody
()) {
52
// Ignore if we already have a body
53
return
true
;
54
}
55
56
const
QByteArray output =
render
(c);
57
if
(Q_UNLIKELY(c->
error
())) {
58
const
auto
errors = c->
errors
();
59
for
(
const
QString &error : errors) {
60
qCCritical(CUTELYST_VIEW) << error;
61
}
62
}
63
const
QString acceptEncoding = c->req()->
header
(QStringLiteral(
"ACCEPT_ENCODING"
));
64
if
(d->minimalSizeToDeflate >= 0 && output.count() > d->minimalSizeToDeflate &&
65
acceptEncoding.contains(QLatin1String(
"deflate"
), Qt::CaseInsensitive)) {
66
QByteArray compressedData = qCompress(output);
// Use zlib's default compression
67
compressedData.remove(0, 6);
// Remove qCompress and zlib headers
68
compressedData.chop(4);
// Remove zlib tailer
69
response->
headers
().
setContentEncoding
(QStringLiteral(
"deflate"
));
70
response->
setBody
(compressedData);
71
}
else
{
72
response->
setBody
(output);
73
}
74
return
!c->
error
();
75
}
76
77
void
View::setMinimalSizeToDeflate
(qint32 minSize) {
78
Q_D(
View
);
79
d->minimalSizeToDeflate = minSize;
80
}
81
#include "moc_view.cpp"
Cutelyst::Request::header
QString header(const QString &key) const
Definition:
request.h:568
Cutelyst::Context
The Cutelyst Context.
Definition:
context.h:50
Cutelyst::View::modifiers
virtual Modifiers modifiers() const override
Definition:
view.cpp:42
Cutelyst::Context::response
Response * response() const
Definition:
context.cpp:110
Cutelyst::Component
The Cutelyst Component base class.
Definition:
component.h:38
Cutelyst::View::setMinimalSizeToDeflate
void setMinimalSizeToDeflate(qint32 minSize=-1)
Definition:
view.cpp:77
Cutelyst::Headers::setContentEncoding
void setContentEncoding(const QString &encoding)
Definition:
headers.cpp:65
Cutelyst::Response::setBody
void setBody(QIODevice *body)
Definition:
response.cpp:114
Cutelyst::Context::error
bool error() const
Returns true if an error was set.
Definition:
context.cpp:63
Cutelyst::Response::hasBody
bool hasBody() const
Definition:
response.cpp:91
Cutelyst::View::render
virtual QByteArray render(Context *c) const =0
Cutelyst::View
Cutelyst View abstract view component
Definition:
view.h:34
Cutelyst::Context::errors
QStringList errors() const
Returns a list of errors that were defined.
Definition:
context.cpp:80
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition:
Mainpage.dox:7
Cutelyst::Component::name
QString name() const
Definition:
component.cpp:44
Cutelyst::View::View
View(QObject *parent, const QString &name)
Definition:
view.cpp:32
Cutelyst::Component::setName
void setName(const QString &name)
Definition:
component.cpp:50
Cutelyst::Response::headers
Headers & headers()
Definition:
response.cpp:316
Cutelyst::Response
Definition:
response.h:34
Generated on Fri Sep 11 2020 14:33:06 for Cutelyst by
1.8.17