5#include "actionrest_p.h"
64 return d->dispatchRestMethod(c, c->request()->method());
67ActionRESTPrivate::ActionRESTPrivate(
ActionREST *q)
72bool ActionRESTPrivate::dispatchRestMethod(
Context *c,
const QString &httpMethod)
const
75 const QString restMethod = q->name() + u
'_' + httpMethod;
82 for (
Action *controllerAction : actions) {
83 if (controllerAction->name() == restMethod) {
84 action = controllerAction;
95 if (httpMethod.compare(u
"OPTIONS") == 0) {
96 ret = returnOptions(c, q->name());
97 }
else if (httpMethod.compare(u
"HEAD") == 0) {
99 ret = dispatchRestMethod(c, QStringLiteral(
"GET"));
100 }
else if (httpMethod.compare(u
"not_implemented") != 0) {
102 ret = dispatchRestMethod(c, QStringLiteral(
"not_implemented"));
105 ret = returnNotImplemented(c, q->name());
111bool ActionRESTPrivate::returnOptions(
Context *c,
const QString &methodName)
const
116 response->
setHeader(QStringLiteral(
"ALLOW"), getAllowedMethods(c->controller(), methodName));
121bool ActionRESTPrivate::returnNotImplemented(
Context *c,
const QString &methodName)
const
124 response->
setStatus(Response::MethodNotAllowed);
125 response->
setHeader(QStringLiteral(
"ALLOW"), getAllowedMethods(c->controller(), methodName));
126 const QString body = QLatin1String(
"Method ") + c->req()->method() +
127 QLatin1String(
" not implemented for ") + c->
uriFor(methodName).
toString();
132QString Cutelyst::ActionRESTPrivate::getAllowedMethods(
Controller *controller,
133 const QString &methodName)
const
136 const QString name = methodName + u
'_';
138 for (
Action *action : actions) {
139 const QString method = action->
name();
146 methods.
append(QStringLiteral(
"HEAD"));
149 methods.
removeAll(QStringLiteral(
"not_implemented"));
153 return methods.
join(u
", ");
156#include "moc_actionrest.cpp"
Automated REST Method Dispatching.
bool doExecute(Context *c) override
ActionREST(QObject *parent=nullptr)
This class represents a Cutelyst Action.
virtual bool doExecute(Context *c) override
Action(QObject *parent=nullptr)
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
bool execute(Component *code)
Response * response() const noexcept
Cutelyst Controller base class
Action * actionFor(const QString &name) const
ActionList actions() const
void setStatus(quint16 status) noexcept
void setBody(QIODevice *body)
Q_REQUIRED_RESULT QByteArray & body()
void setHeader(const QString &field, const QString &value)
void setContentType(const QString &type)
The Cutelyst namespace holds all public Cutelyst API.
QVector< Action * > ActionList
void append(const T &value)
int removeAll(const T &value)
QObject * parent() const const
QString mid(int position, int n) const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QString join(const QString &separator) const const
void sort(Qt::CaseSensitivity cs)
QString toString(FormattingOptions options) const const