Cutelyst  2.3.0
actionrest.cpp
1 /*
2  * Copyright (C) 2013-2018 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "actionrest_p.h"
19 #include "context.h"
20 #include "controller.h"
21 #include "dispatcher.h"
22 
23 #include <QUrl>
24 #include <QDebug>
25 
26 using namespace Cutelyst;
27 
62 ActionREST::ActionREST(QObject *parent) : Action(parent)
63  , d_ptr(new ActionRESTPrivate)
64 {
65  d_ptr->q_ptr = this;
66 }
67 
68 ActionREST::~ActionREST()
69 {
70  delete d_ptr;
71 }
72 
74 {
75  Q_D(const ActionREST);
76 
77  if (!Action::doExecute(c)) {
78  return false;
79  }
80 
81  return d->dispatchRestMethod(c, c->request()->method());
82 }
83 
84 bool ActionRESTPrivate::dispatchRestMethod(Context *c, const QString &httpMethod) const
85 {
86  Q_Q(const ActionREST);
87  const QString restMethod = q->name() + QLatin1Char('_') + httpMethod;
88 
89  Controller *controller = c->controller();
90  Action *action = controller->actionFor(restMethod);
91  if (!action) {
92  // Look for non registered actions in this controller
93  const ActionList actions = controller->actions();
94  for (Action *controllerAction : actions) {
95  if (controllerAction->name() == restMethod) {
96  action = controllerAction;
97  break;
98  }
99  }
100  }
101 
102  if (action) {
103  return c->execute(action);
104  }
105 
106  bool ret = false;
107  if (httpMethod == QLatin1String("OPTIONS")) {
108  ret = returnOptions(c, q->name());
109  } else if (httpMethod == QLatin1String("HEAD")) {
110  // redispatch to GET
111  ret = dispatchRestMethod(c, QStringLiteral("GET"));
112  } else if (httpMethod != QLatin1String("not_implemented")) {
113  // try dispatching to foo_not_implemented
114  ret = dispatchRestMethod(c, QStringLiteral("not_implemented"));
115  } else {
116  // not_implemented
117  ret = returnNotImplemented(c, q->name());
118  }
119 
120  return ret;
121 }
122 
123 bool ActionRESTPrivate::returnOptions(Context *c, const QString &methodName) const
124 {
125  Response *response = c->response();
126  response->setContentType(QStringLiteral("text/plain"));
127  response->setStatus(Response::OK); // 200
128  response->setHeader(QStringLiteral("ALLOW"),
129  getAllowedMethods(c->controller(), methodName));
130  response->body().clear();
131  return true;
132 }
133 
134 bool ActionRESTPrivate::returnNotImplemented(Context *c, const QString &methodName) const
135 {
136  Response *response = c->response();
137  response->setStatus(Response::MethodNotAllowed); // 405
138  response->setHeader(QStringLiteral("ALLOW"),
139  getAllowedMethods(c->controller(), methodName));
140  const QString body = QLatin1String("Method ") + c->req()->method()
141  + QLatin1String(" not implemented for ") + c->uriFor(methodName).toString();
142  response->setBody(body);
143  return true;
144 }
145 
146 QString Cutelyst::ActionRESTPrivate::getAllowedMethods(Controller *controller, const QString &methodName) const
147 {
148  QStringList methods;
149  const QString name = methodName + QLatin1Char('_');
150  const ActionList actions = controller->actions();
151  for (Action *action : actions) {
152  const QString method = action->name();
153  if (method.startsWith(name)) {
154  methods.append(method.mid(name.size()));
155  }
156  }
157 
158  if (methods.contains(QStringLiteral("GET"))) {
159  methods.append(QStringLiteral("HEAD"));
160  }
161 
162  methods.removeAll(QStringLiteral("not_implemented"));
163  methods.sort();
164  methods.removeDuplicates();
165 
166  return methods.join(QStringLiteral(", "));
167 }
168 
169 #include "moc_actionrest.cpp"
QVector< Action * > ActionList
Definition: action.h:162
void setContentType(const QString &type)
Definition: response.h:205
virtual bool doExecute(Context *c) override
Definition: action.cpp:149
void setHeader(const QString &field, const QString &value)
Definition: response.cpp:280
Action * actionFor(const QString &name) const
Definition: controller.cpp:48
void setStatus(quint16 status)
Definition: response.cpp:85
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:225
This class represents a Cutelyst Action.
Definition: action.h:47
The Cutelyst Context.
Definition: context.h:50
Cutelyst Controller base class
Definition: controller.h:102
Automated REST Method Dispatching.
Definition: actionrest.h:28
QString name() const
Definition: component.cpp:39
bool doExecute(Context *c) override
Definition: actionrest.cpp:73
Response * response() const
Definition: context.cpp:110
Controller * controller() const
Definition: action.cpp:105
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
bool execute(Component *code)
Definition: context.cpp:356
ActionREST(QObject *parent=nullptr)
Definition: actionrest.cpp:62
Q_REQUIRED_RESULT QByteArray & body()
Definition: response.cpp:97
void setBody(QIODevice *body)
Definition: response.cpp:114
ActionList actions() const
Definition: controller.cpp:58