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 <webview/url_manager.h>
24 #include <webview/request_processor.h>
25 #include <core/threading/mutex.h>
26 #include <core/threading/mutex_locker.h>
27 #include <core/exception.h>
28 
29 namespace fawkes {
30 #if 0 /* just to make Emacs auto-indent happy */
31 }
32 #endif
33 
34 
35 /** @class WebUrlManager <webview/url_manager.h>
36  * Manage URL mappings.
37  * This class maps (base) URLs to web request processors which handle all
38  * requests for the given URL.
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor. */
44 {
45  __mutex = new Mutex();
46  __startpage_processor = NULL;
47 }
48 
49 
50 /** Destructor. */
52 {
53  delete __mutex;
54 }
55 
56 
57 /** Add a request processor.
58  * @param url_prefix baseurl this processor should handle
59  * @param processor processor for baseurl
60  * @exception Exception thrown if a processor has already been registered
61  * for the given URL prefix.
62  */
63 void
64 WebUrlManager::register_baseurl(const char *url_prefix,
65  WebRequestProcessor *processor)
66 {
67  MutexLocker lock(__mutex);
68  if (std::string(url_prefix) == "/") {
69  if (__startpage_processor) {
70  throw Exception("Start page processor has already been registered");
71  }
72  __startpage_processor = processor;
73  } else {
74  if (__processors.find(url_prefix) != __processors.end()) {
75  throw Exception("A processor for %s has already been registered",
76  url_prefix);
77  }
78  __processors[url_prefix] = processor;
79  }
80 }
81 
82 
83 /** Remove a request processor.
84  * @param url_prefix baseurl the processor handled
85  */
86 void
87 WebUrlManager::unregister_baseurl(const char *url_prefix)
88 {
89  MutexLocker lock(__mutex);
90  if (std::string(url_prefix) == "/") {
91  __startpage_processor = NULL;
92  } else {
93  __processors.erase(url_prefix);
94  }
95 }
96 
97 /** Lock mutex and find processor.
98  * This method determines if a processor has been registered for the URL.
99  * It is the callers duty to ensure that the mutex has been locked while
100  * searching and while using the found processor.
101  * @param url url to get the processor for
102  * @return request processor if found, NULL otherwise
103  */
105 WebUrlManager::find_processor(std::string &url) const
106 {
107  if ( url == "/" && __startpage_processor ) {
108  return __startpage_processor;
109  }
110 
111  WebRequestProcessor *proc = NULL;
112  std::map<std::string, WebRequestProcessor *>::const_iterator pit;
113  for (pit = __processors.begin();
114  (proc == NULL) && (pit != __processors.end());
115  ++pit)
116  {
117  if (url.find(pit->first) == 0) {
118  url = pit->first;
119  return pit->second;
120  }
121  }
122 
123  return NULL;
124 }
125 
126 
127 /** Get internal mutex.
128  * Use this mutex to guard find_processor() and a following invocation of
129  * a found processor against changes due to registering/unregistering of
130  * processors.
131  * @return internal mutex
132  */
133 Mutex *
135 {
136  return __mutex;
137 }
138 
139 } // end namespace fawkes
WebUrlManager()
Constructor.
Definition: url_manager.cpp:43
Fawkes library namespace.
Mutex locking helper.
Definition: mutex_locker.h:33
Abstract web request processor.
Mutex * mutex()
Get internal mutex.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void unregister_baseurl(const char *url_prefix)
Remove a request processor.
Definition: url_manager.cpp:87
WebRequestProcessor * find_processor(std::string &url) const
Lock mutex and find processor.
~WebUrlManager()
Destructor.
Definition: url_manager.cpp:51
Mutex mutual exclusion lock.
Definition: mutex.h:32
void register_baseurl(const char *url_prefix, WebRequestProcessor *processor)
Add a request processor.
Definition: url_manager.cpp:64