Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #ifndef _URL_MAPPING_PATH_INDEX_H
00024 #define _URL_MAPPING_PATH_INDEX_H
00025 
00026 #include "libts.h"
00027 #undef std  // FIXME: remove dependancy on the STL
00028 #include <map>
00029 
00030 #include "URL.h"
00031 #include "UrlMapping.h"
00032 #include "Trie.h"
00033 
00034 class UrlMappingPathIndex
00035 {
00036 public:
00037   UrlMappingPathIndex()
00038   { }
00039 
00040   virtual ~UrlMappingPathIndex();
00041   bool Insert(url_mapping *mapping);
00042   url_mapping* Search(URL *request_url, int request_port, bool normal_search = true) const;
00043   void Print();
00044 
00045 private:
00046   typedef Trie<url_mapping> UrlMappingTrie;
00047 
00048   struct UrlMappingTrieKey {
00049     int scheme_wks_idx;
00050     int port;
00051 
00052     UrlMappingTrieKey(int idx, int p)
00053       : scheme_wks_idx(idx), port(p)
00054     { }
00055     
00056     bool operator <(const UrlMappingTrieKey &rhs) const {
00057       if (scheme_wks_idx == rhs.scheme_wks_idx) {
00058         return (port < rhs.port);
00059       }
00060       return (scheme_wks_idx < rhs.scheme_wks_idx);
00061     }
00062   };
00063 
00064   typedef std::map<UrlMappingTrieKey, UrlMappingTrie *> UrlMappingGroup;
00065   UrlMappingGroup m_tries;
00066 
00067   
00068   
00069   UrlMappingPathIndex(const UrlMappingPathIndex & ) { };
00070   UrlMappingPathIndex &operator =(const UrlMappingPathIndex & ) { return *this; }
00071 
00072   inline UrlMappingTrie *
00073   _GetTrie(URL *url, int &idx, int port, bool search = true) const {
00074     idx = url->scheme_get_wksidx();
00075     
00076     
00077     if (idx == -1) {
00078         if (port == 80) {
00079             idx = URL_WKSIDX_HTTP;
00080         } else {
00081             idx = URL_WKSIDX_HTTPS;
00082         }
00083     }
00084     UrlMappingGroup::const_iterator group_iter;
00085     if (search) { 
00086       group_iter = m_tries.find(UrlMappingTrieKey(idx, port));
00087     } else { 
00088       Debug("UrlMappingPathIndex::_GetTrie", "Not performing search; will return first available trie");
00089       group_iter = m_tries.begin();
00090     }
00091     if (group_iter != m_tries.end()) {
00092       return group_iter->second;
00093     }
00094     return 0;
00095   }
00096 };
00097 
00098 #endif // _URL_MAPPING_PATH_INDEX_H