Fawkes API Fawkes Development Version
url_manager.cpp
1
2/***************************************************************************
3 * url_manager.cpp - Web URL manager
4 *
5 * Created: Thu Nov 25 21:56:19 2010
6 * Copyright 2006-2010 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#include <core/exception.h>
24#include <core/threading/mutex.h>
25#include <webview/url_manager.h>
26
27namespace fawkes {
28
29/** @class WebUrlManager <webview/url_manager.h>
30 * Manage URL mappings.
31 * This class maps (base) URLs to web request processors which handle all
32 * requests for the given URL.
33 * @author Tim Niemueller
34 */
35
36/** Constructor. */
37WebUrlManager::WebUrlManager() : router_(std::make_shared<WebviewRouter<Handler>>())
38{
39}
40
41/** Destructor. */
43{
44}
45
46/** Add a request processor.
47 * @param method HTTP method to register for
48 * @param path path pattern to register for, may contain {var}, {var*}, and {var+} elements
49 * @param handler handler function
50 * @exception Exception thrown if a processor has already been registered
51 * for the given URL prefix.
52 */
53void
54WebUrlManager::add_handler(WebRequest::Method method, const std::string &path, Handler handler)
55{
56 std::lock_guard<std::mutex> lock(mutex_);
57 router_->add(method, path, handler, 0);
58}
59
60/** Add a request processor with weight.
61 * This one should mostly be necessary to implement "catch-all" handlers.
62 * @param method HTTP method to register for
63 * @param path path pattern to register for, may contain {var}, {var*}, and {var+} elements
64 * @param handler handler function
65 * @param weight the higher the weight the later the handler will be tried.
66 * @exception Exception thrown if a processor has already been registered
67 * for the given URL prefix.
68 */
69void
71 const std::string &path,
72 Handler handler,
73 int weight)
74{
75 std::lock_guard<std::mutex> lock(mutex_);
76 router_->add(method, path, handler, weight);
77}
78
79/** Remove a request processor.
80 * @param method HTTP method to unregister from
81 * @param path path pattern to unregister from
82 */
83void
84WebUrlManager::remove_handler(WebRequest::Method method, const std::string &path)
85{
86 std::lock_guard<std::mutex> lock(mutex_);
87 router_->remove(method, path);
88}
89
90/** Lock mutex and find processor.
91 * This method determines if a processor has been registered for the URL.
92 * It is the callers duty to ensure that the mutex has been locked while
93 * searching and while using the found processor.
94 * @param url url to get the processor for
95 * @return request processor if found, NULL otherwise
96 */
98WebUrlManager::process_request(WebRequest *request)
99{
100 std::lock_guard<std::mutex> lock(mutex_);
101 try {
102 std::map<std::string, std::string> path_args;
103 Handler handler = router_->find_handler(request, path_args);
104 request->set_path_args(std::move(path_args));
105 return handler(request);
106 } catch (NullPointerException &e) {
107 return NULL;
108 }
109}
110
111} // end namespace fawkes
Basic web reply.
Definition: reply.h:34
Web request meta data carrier.
Definition: request.h:42
Method
HTTP transfer methods.
Definition: request.h:47
void set_path_args(std::map< std::string, std::string > &&args)
Set path arguments.
Definition: request.h:314
std::function< WebReply *(const WebRequest *)> Handler
Function type for handling requests.
Definition: url_manager.h:45
void remove_handler(WebRequest::Method method, const std::string &path)
Remove a request processor.
Definition: url_manager.cpp:84
~WebUrlManager()
Destructor.
Definition: url_manager.cpp:42
WebUrlManager()
Constructor.
Definition: url_manager.cpp:37
void add_handler(WebRequest::Method method, const std::string &path, Handler handler)
Add a request processor.
Definition: url_manager.cpp:54
URL path router.
Definition: router.h:44
Fawkes library namespace.