5#include "actionrest_p.h"
54 :
Action(new ActionRESTPrivate(this), parent)
66 return d->dispatchRestMethod(c, c->
request()->method());
69ActionRESTPrivate::ActionRESTPrivate(
ActionREST *q)
74bool ActionRESTPrivate::dispatchRestMethod(
Context *c,
const QByteArray &httpMethod)
const
77 const QString restMethod = q->name() + u
'_' + QString::fromLatin1(httpMethod);
84 for (
Action *controllerAction : actions) {
85 if (controllerAction->name() == restMethod) {
86 action = controllerAction;
97 if (httpMethod.compare(
"OPTIONS") == 0) {
98 ret = returnOptions(c, q->name());
99 }
else if (httpMethod.compare(
"HEAD") == 0) {
101 ret = dispatchRestMethod(c,
"GET"_qba);
102 }
else if (httpMethod.compare(
"not_implemented") != 0) {
104 ret = dispatchRestMethod(c,
"not_implemented"_qba);
107 ret = returnNotImplemented(c, q->name());
113bool ActionRESTPrivate::returnOptions(
Context *c,
const QString &methodName)
const
119 response->
body().clear();
123bool ActionRESTPrivate::returnNotImplemented(
Context *c,
const QString &methodName)
const
126 response->
setStatus(Response::MethodNotAllowed);
129 const QByteArray body =
"Method " + c->
req()->method() +
" not implemented for " +
130 c->
request()->uri().toString(QUrl::FullyEncoded).toLatin1();
135QByteArray Cutelyst::ActionRESTPrivate::getAllowedMethods(
Controller *controller,
136 const QString &methodName)
const
139 const QString name = methodName + u
'_';
141 for (
Action *action : actions) {
142 const QString method = action->
name();
143 if (method.startsWith(name)) {
144 methods.append(method.mid(name.size()));
148 if (methods.contains(u
"GET")) {
149 methods.append(QStringLiteral(
"HEAD"));
152 methods.removeAll(QStringLiteral(
"not_implemented"));
154 methods.removeDuplicates();
156 return methods.join(u
", ").toLatin1();
159#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
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 setHeader(const QByteArray &key, const QByteArray &value)
void setBody(QIODevice *body)
The Cutelyst namespace holds all public Cutelyst API.
QVector< Action * > ActionList