5#include "actionrest_p.h"
14using namespace Qt::StringLiterals;
55 :
Action(new ActionRESTPrivate(this), parent)
67 return d->dispatchRestMethod(c, c->
request()->method());
70ActionRESTPrivate::ActionRESTPrivate(
ActionREST *q)
75bool ActionRESTPrivate::dispatchRestMethod(
Context *c,
const QByteArray &httpMethod)
const
78 const QString restMethod = q->name() + u
'_' + QString::fromLatin1(httpMethod);
85 for (
Action *controllerAction : actions) {
86 if (controllerAction->name() == restMethod) {
87 action = controllerAction;
98 if (httpMethod.compare(
"OPTIONS") == 0) {
99 ret = returnOptions(c, q->name());
100 }
else if (httpMethod.compare(
"HEAD") == 0) {
102 ret = dispatchRestMethod(c,
"GET"_ba);
103 }
else if (httpMethod.compare(
"not_implemented") != 0) {
105 ret = dispatchRestMethod(c,
"not_implemented"_ba);
108 ret = returnNotImplemented(c, q->name());
114bool ActionRESTPrivate::returnOptions(
Context *c,
const QString &methodName)
const
120 response->
body().clear();
124bool ActionRESTPrivate::returnNotImplemented(
Context *c,
const QString &methodName)
const
127 response->
setStatus(Response::MethodNotAllowed);
130 const QByteArray body =
"Method " + c->
req()->method() +
" not implemented for " +
131 c->
request()->uri().toString(QUrl::FullyEncoded).toLatin1();
136QByteArray Cutelyst::ActionRESTPrivate::getAllowedMethods(
Controller *controller,
137 const QString &methodName)
const
140 const QString name = methodName + u
'_';
142 for (
Action *action : actions) {
143 const QString method = action->
name();
144 if (method.startsWith(name)) {
145 methods.append(method.mid(name.size()));
149 if (methods.contains(u
"GET")) {
150 methods.append(QStringLiteral(
"HEAD"));
153 methods.removeDuplicates();
154 methods.removeOne(u
"not_implemented"_s);
157 return methods.join(u
", ").toLatin1();
160#include "moc_actionrest.cpp"
Automated REST method dispatching.
bool doExecute(Context *c) override
ActionREST(QObject *parent=nullptr)
This class represents a Cutelyst Action.
bool doExecute(Context *c) override
Action(QObject *parent=nullptr)
QString name() const noexcept
bool execute(Component *code)
Response * response() const noexcept
Cutelyst Controller base class.
ActionList actions() const noexcept
Action * actionFor(QStringView name) const
void setContentType(const QByteArray &type)
void setStatus(quint16 status) noexcept
void setBody(QIODevice *body)
void setHeader(const QByteArray &key, const QByteArray &value)
The Cutelyst namespace holds all public Cutelyst API.
QVector< Action * > ActionList