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/mapping.h>
36 #include <tnt/maptarget.h>
37 #include <vector>
38 #include <map>
39 
40 namespace tnt
41 {
42  class HttpRequest;
43 
45  class Dispatcher : public Urlmapper // one per host
46  {
47  friend class PosType;
48 
49  typedef std::vector<Mapping> urlmap_type;
50 
51  urlmap_type _urlmap; // map url to soname/compname
52  mutable cxxtools::ReadWriteMutex _mutex;
53 
54  class UrlMapCacheKey
55  {
56  std::string _vhost;
57  std::string _url;
58  std::string _method;
59  bool _ssl;
60  urlmap_type::size_type _pos;
61 
62  public:
63  UrlMapCacheKey() { }
64  UrlMapCacheKey(const HttpRequest& request, urlmap_type::size_type pos);
65 
66  bool operator< (const UrlMapCacheKey& other) const;
67 
68  const std::string& getHost() const { return _vhost; }
69  const std::string& getUrl() const { return _url; }
70  const std::string& getMethod() const { return _method; }
71  bool getSsl() const { return _ssl; }
72  urlmap_type::size_type getPos() const { return _pos; }
73  };
74 
75  struct UrlMapCacheValue
76  {
77  Maptarget ci;
78  urlmap_type::size_type pos;
79 
80  UrlMapCacheValue() { }
81  UrlMapCacheValue(const Maptarget& ci_, urlmap_type::size_type pos_)
82  : ci(ci_),
83  pos(pos_)
84  { }
85  };
86 
87  typedef std::map<UrlMapCacheKey, UrlMapCacheValue> urlMapCacheType;
88 
89  mutable cxxtools::ReadWriteMutex _urlMapCacheMutex;
90  mutable urlMapCacheType _urlMapCache;
91 
92  Maptarget mapCompNext(const HttpRequest& request, urlmap_type::size_type& pos) const;
93 
94  public:
95  virtual ~Dispatcher() { }
96 
97  Mapping& addUrlMapEntry(const std::string& vhost, const std::string& url, const std::string& method, int ssl, const Maptarget& ci);
98 
99  Mapping& addUrlMapEntry(const std::string& vhost, const std::string& url, const Maptarget& ci)
100  { return addUrlMapEntry(vhost, url, std::string(), SSL_ALL, ci); }
101 
102  class PosType
103  {
104  const Dispatcher& _dis;
105  cxxtools::ReadLock _lock;
106  urlmap_type::size_type _pos;
107  const HttpRequest& _request;
108  bool _first;
109 
110  public:
111  PosType(const Dispatcher& d, const HttpRequest& r)
112  : _dis(d),
113  _lock(d._mutex),
114  _pos(0),
115  _request(r),
116  _first(true)
117  { }
118 
119  Maptarget getNext();
120  };
121  };
123 }
124 
125 #endif // TNT_DISPATCHER_H