18 #include "actionrest_p.h"
20 #include "controller.h"
21 #include "dispatcher.h"
74 return d->dispatchRestMethod(c, c->request()->method());
77 ActionRESTPrivate::ActionRESTPrivate(
ActionREST* q) : q_ptr(q)
81 bool ActionRESTPrivate::dispatchRestMethod(
Context *c,
const QString &httpMethod)
const
84 const QString restMethod = q->name() + QLatin1Char(
'_') + httpMethod;
91 for (
Action *controllerAction : actions) {
92 if (controllerAction->name() == restMethod) {
93 action = controllerAction;
104 if (httpMethod == QLatin1String(
"OPTIONS")) {
105 ret = returnOptions(c, q->name());
106 }
else if (httpMethod == QLatin1String(
"HEAD")) {
108 ret = dispatchRestMethod(c, QStringLiteral(
"GET"));
109 }
else if (httpMethod != QLatin1String(
"not_implemented")) {
111 ret = dispatchRestMethod(c, QStringLiteral(
"not_implemented"));
114 ret = returnNotImplemented(c, q->name());
120 bool ActionRESTPrivate::returnOptions(
Context *c,
const QString &methodName)
const
125 response->
setHeader(QStringLiteral(
"ALLOW"),
126 getAllowedMethods(c->controller(), methodName));
127 response->
body().clear();
131 bool ActionRESTPrivate::returnNotImplemented(
Context *c,
const QString &methodName)
const
134 response->
setStatus(Response::MethodNotAllowed);
135 response->
setHeader(QStringLiteral(
"ALLOW"),
136 getAllowedMethods(c->controller(), methodName));
137 const QString body = QLatin1String(
"Method ") + c->req()->method()
138 + QLatin1String(
" not implemented for ") + c->
uriFor(methodName).toString();
143 QString Cutelyst::ActionRESTPrivate::getAllowedMethods(
Controller *controller,
const QString &methodName)
const
146 const QString name = methodName + QLatin1Char(
'_');
148 for (
Action *action : actions) {
149 const QString method = action->
name();
150 if (method.startsWith(name)) {
151 methods.append(method.mid(name.size()));
155 if (methods.contains(QStringLiteral(
"GET"))) {
156 methods.append(QStringLiteral(
"HEAD"));
159 methods.removeAll(QStringLiteral(
"not_implemented"));
161 methods.removeDuplicates();
163 return methods.join(QStringLiteral(
", "));
166 #include "moc_actionrest.cpp"