5#include "actionrest_p.h"
15using namespace Qt::StringLiterals;
56 :
Action(new ActionRESTPrivate(this), parent)
64 const int &actionRefCount = c->d_ptr->actionRefCount;
74 c->d_ptr->pendingAsync.enqueue(action);
84ActionRESTPrivate::ActionRESTPrivate(
ActionREST *q)
89Action *ActionRESTPrivate::getRestAction(
Context *c,
const QByteArray &httpMethod)
const
92 const QString restMethod = q->name() + u
'_' + QString::fromLatin1(httpMethod);
94 const Controller *controller = q->controller();
99 auto it = std::ranges::find_if(actions, [&restMethod](
const Action *controllerAction) {
100 return controllerAction->
name() == restMethod;
102 if (it != actions.end()) {
108 if (httpMethod ==
"HEAD") {
110 action = getRestAction(c,
"GET"_ba);
111 }
else if (httpMethod ==
"OPTIONS") {
112 returnOptions(c, q->name());
113 }
else if (httpMethod ==
"not_implemented") {
115 returnNotImplemented(c, q->name());
118 action = getRestAction(c,
"not_implemented"_ba);
125void ActionRESTPrivate::returnOptions(
Context *c,
const QString &methodName)
const
131 response->
body().clear();
134void ActionRESTPrivate::returnNotImplemented(
Context *c,
const QString &methodName)
const
137 response->
setStatus(Response::MethodNotAllowed);
140 const QByteArray body =
"Method " + c->
req()->method() +
" not implemented for " +
141 c->
request()->uri().toString(QUrl::FullyEncoded).toLatin1();
145QByteArray Cutelyst::ActionRESTPrivate::getAllowedMethods(
const Controller *controller,
146 const QString &methodName)
const
149 const QString name = methodName + u
'_';
151 for (
const Action *action : actions) {
152 const QString method = action->
name();
153 if (method.startsWith(name)) {
154 methods.append(method.mid(name.size()));
158 if (methods.contains(u
"GET")) {
159 methods.append(u
"HEAD"_s);
162 methods.removeDuplicates();
163 methods.removeOne(u
"not_implemented"_s);
166 return methods.join(u
", ").toLatin1();
169#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 setHeader(const QByteArray &key, const QByteArray &value)
void setBody(QIODevice *body)
The Cutelyst namespace holds all public Cutelyst API.
QVector< Action * > ActionList