tufao  1.3.0
An asynchronous web framework for C++ built on top of Qt
httpserverrequestrouter.h
1 /*
2  Copyright (c) 2012 Vinícius dos Santos Oliveira
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in all
12  copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  SOFTWARE.
21  */
22 
23 #ifndef TUFAO_HTTPSERVERREQUESTROUTER_H
24 #define TUFAO_HTTPSERVERREQUESTROUTER_H
25 
26 #include <functional>
27 #include <initializer_list>
28 
29 #include <QtCore/QRegularExpression>
30 #include <QtCore/QObject>
31 
32 #include "abstracthttpserverrequesthandler.h"
33 
34 namespace Tufao {
35 
63 class TUFAO_EXPORT HttpServerRequestRouter: public QObject,
65 {
66  Q_OBJECT
67 public:
75  typedef std::function<bool(HttpServerRequest&, HttpServerResponse&)>
77 
92  struct Mapping
93  {
98  Mapping(QRegularExpression path, Handler handler) :
99  path(path), handler(handler)
100  {}
105  Mapping(QRegularExpression path, QByteArray method, Handler handler) :
106  path(path), method(method), handler(handler)
107  {}
108 
112  Mapping() = default;
113 
118  QRegularExpression path;
119 
127  QByteArray method;
128  Handler handler;
129  };
130 
134  explicit HttpServerRequestRouter(QObject *parent = 0);
135 
142  explicit HttpServerRequestRouter(std::initializer_list<Mapping> mappings,
143  QObject *parent = 0);
144 
149 
159  int map(Mapping map);
160 
170  int map(std::initializer_list<Mapping> map);
171 
178  void unmap(int index);
179 
183  void clear();
184 
185 public slots:
219  bool handleRequest(Tufao::HttpServerRequest &request,
220  Tufao::HttpServerResponse &response) override;
221 
222 private:
223  struct Priv;
224  Priv *priv;
225 };
226 
227 } // namespace Tufao
228 
229 #endif // TUFAO_HTTPSERVERREQUESTROUTER_H
This class provides an interface for HttpServerRequest handlers.
Definition: abstracthttpserverrequesthandler.h:50
The Tufao::HttpServerResponse is used to respond to a Tufao::HttpServerRequest.
Definition: httpserverresponse.h:142
QRegularExpression path
This attribute is used to filter requests based on the url's path component.
Definition: httpserverrequestrouter.h:118
This class provides a robust and high performance HTTP request router.
Definition: httpserverrequestrouter.h:63
The Tufao::HttpServer represents a HTTP request received by Tufao::HttpServer.
Definition: httpserverrequest.h:54
QByteArray method
This attribute is used to filter requests based on the HTTP method.
Definition: httpserverrequestrouter.h:127
Mapping(QRegularExpression path, QByteArray method, Handler handler)
Constructs a Mapping object using path and method as filters and handler as handler.
Definition: httpserverrequestrouter.h:105
This is the namespace where all Tufão facilities are grouped.
Definition: abstracthttpserverrequesthandler.h:30
This class describes a request handler and a filter.
Definition: httpserverrequestrouter.h:92
std::function< bool(HttpServerRequest &, HttpServerResponse &)> Handler
It's a simple typedef for the type of handler accepted by the HttpServerRequestRouter.
Definition: httpserverrequestrouter.h:76
Mapping(QRegularExpression path, Handler handler)
Constructs a Mapping object using path as filter and handler as handler.
Definition: httpserverrequestrouter.h:98