dispatcher.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2005 Tommi Maekitalo
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  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_DISPATCHER_H
31 #define TNT_DISPATCHER_H
32 
33 #include <cxxtools/mutex.h>
34 #include <tnt/urlmapper.h>
35 #include <tnt/maptarget.h>
36 #include <tnt/tntconfig.h>
37 #include <vector>
38 #include <map>
39 #include <cxxtools/regex.h>
40 
41 namespace tnt
42 {
43  class HttpRequest;
44 
45  class Mapping
46  {
47  private:
48  std::string vhost;
49  std::string url;
50  std::string method;
51  int ssl;
52 
53  cxxtools::Regex r_vhost;
54  cxxtools::Regex r_url;
55  cxxtools::Regex r_method;
56 
57  Maptarget target;
58 
59  public:
61 
62  Mapping() { }
63 
64  Mapping(const std::string& vhost_, const std::string& url_,
65  const std::string& method_, int ssl_, const Maptarget& target_);
66 
67  const std::string& getVHost() const { return vhost; }
68  const std::string& getUrl() const { return url; }
69  const std::string& getMethod() const { return method; }
70  int getSsl() const { return ssl; }
71 
72  const Maptarget& getTarget() const { return target; }
73 
74  Mapping& setPathInfo(const std::string& p)
75  { target.setPathInfo(p); return *this; }
76 
78  { target.setArgs(a); return *this; }
79 
80  Mapping& setVHost(const std::string& vhost_)
81  { vhost = vhost_; r_vhost = cxxtools::Regex(vhost_); return *this; }
82 
83  Mapping& setUrl(const std::string& url_)
84  { url = url_; r_url = cxxtools::Regex(url_); return *this; }
85 
86  Mapping& setMethod(const std::string& method_)
87  { method = method_; r_method = cxxtools::Regex(method_); return *this; }
88 
89  Mapping& setSsl(bool sw)
90  { ssl = (sw ? SSL_YES : SSL_NO); return *this; }
91 
93  { ssl = SSL_ALL; return *this; }
94 
95  bool match(const HttpRequest& request, cxxtools::RegexSMatch& smatch) const;
96 
97  };
98 
99  // Dispatcher - one per host
100  class Dispatcher : public Urlmapper
101  {
102  typedef std::vector<Mapping> urlmap_type;
103  urlmap_type urlmap; // map url to soname/compname
104  mutable cxxtools::ReadWriteMutex mutex;
105 
106  class UrlMapCacheKey
107  {
108  std::string vhost;
109  std::string url;
110  std::string method;
111  bool ssl;
112  urlmap_type::size_type pos;
113 
114  public:
115  UrlMapCacheKey() { }
116  UrlMapCacheKey(const HttpRequest& request, urlmap_type::size_type pos_);
117 
118  bool operator< (const UrlMapCacheKey& other) const;
119 
120  const std::string& getHost() const { return vhost; }
121  const std::string& getUrl() const { return url; }
122  const std::string& getMethod() const { return method; }
123  bool getSsl() const { return ssl; }
124  urlmap_type::size_type getPos() const { return pos; }
125  };
126 
127  struct UrlMapCacheValue
128  {
129  Maptarget ci;
130  urlmap_type::size_type pos;
131 
132  UrlMapCacheValue() { }
133  UrlMapCacheValue(const Maptarget& ci_, urlmap_type::size_type pos_)
134  : ci(ci_),
135  pos(pos_)
136  { }
137  };
138 
139  typedef std::map<UrlMapCacheKey, UrlMapCacheValue> urlMapCacheType;
140  mutable cxxtools::ReadWriteMutex urlMapCacheMutex;
141  mutable urlMapCacheType urlMapCache;
142 
143  Maptarget mapCompNext(const HttpRequest& request, urlmap_type::size_type& pos) const;
144 
145  public:
146  virtual ~Dispatcher() { }
147 
148  Mapping& addUrlMapEntry(const std::string& vhost, const std::string& url,
149  const std::string& method, int ssl, const Maptarget& ci);
150 
151  Mapping& addUrlMapEntry(const std::string& vhost, const std::string& url,
152  const Maptarget& ci)
153  { return addUrlMapEntry(vhost, url, std::string(), SSL_ALL, ci); }
154 
155  friend class PosType;
156 
157  class PosType
158  {
159  const Dispatcher& dis;
160  cxxtools::ReadLock lock;
161  urlmap_type::size_type pos;
162  const HttpRequest& request;
163  bool first;
164 
165  public:
166  PosType(const Dispatcher& d, const HttpRequest& r)
167  : dis(d),
168  lock(dis.mutex),
169  pos(0),
170  request(r),
171  first(true)
172  { }
173 
174  Maptarget getNext();
175  };
176  };
177 
178 }
179 
180 #endif // TNT_DISPATCHER_H
181