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 
27 namespace 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. */
37 WebUrlManager::WebUrlManager() : router_(std::make_shared<WebviewRouter<Handler>>())
38 {
39 }
40 
41 /** Destructor. */
42 WebUrlManager::~WebUrlManager()
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  */
53 void
54 WebUrlManager::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  */
69 void
70 WebUrlManager::add_handler(WebRequest::Method method,
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  */
83 void
84 WebUrlManager::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  */
97 WebReply *
98 WebUrlManager::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
fawkes::WebRequest
Definition: request.h:40
fawkes::WebUrlManager::WebUrlManager
WebUrlManager()
Constructor.
Definition: url_manager.cpp:41
fawkes
fawkes::WebRequest::Method
Method
HTTP transfer methods.
Definition: request.h:46
fawkes::WebRequest::set_path_args
void set_path_args(std::map< std::string, std::string > &&args)
Set path arguments.
Definition: request.h:313
fawkes::WebUrlManager::Handler
std::function< WebReply *(const WebRequest *)> Handler
Function type for handling requests.
Definition: url_manager.h:49
fawkes::WebReply
Definition: reply.h:37