• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

UrlRewrite.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #ifndef _URL_REWRITE_H_
00025 #define _URL_REWRITE_H_
00026 
00027 #include "UrlMapping.h"
00028 #include "HttpTransact.h"
00029 
00030 #ifdef HAVE_PCRE_PCRE_H
00031 #include <pcre/pcre.h>
00032 #else
00033 #include <pcre.h>
00034 #endif
00035 
00036 #define URL_REMAP_FILTER_NONE         0x00000000
00037 #define URL_REMAP_FILTER_REFERER      0x00000001        /* enable "referer" header validation */
00038 #define URL_REMAP_FILTER_REDIRECT_FMT 0x00010000        /* enable redirect URL formatting */
00039 
00040 struct BUILD_TABLE_INFO ;
00041 
00042 /**
00043  * used for redirection, mapping, and reverse mapping
00044 **/
00045 enum mapping_type
00046 { FORWARD_MAP, REVERSE_MAP, PERMANENT_REDIRECT, TEMPORARY_REDIRECT, FORWARD_MAP_REFERER,
00047   FORWARD_MAP_WITH_RECV_PORT, NONE };
00048 
00049 /**
00050  *
00051 **/
00052 class UrlRewrite
00053 {
00054 public:
00055   UrlRewrite();
00056   ~UrlRewrite();
00057 
00058   int BuildTable(const char * path);
00059   mapping_type Remap_redirect(HTTPHdr * request_header, URL *redirect_url);
00060   bool ReverseMap(HTTPHdr *response_header);
00061   void SetReverseFlag(int flag);
00062   void Print();
00063   bool is_valid() const { return _valid; };
00064 //  private:
00065 
00066   static const int MAX_REGEX_SUBS = 10;
00067 
00068   struct RegexMapping
00069   {
00070     url_mapping *url_map;
00071     pcre *re;
00072     pcre_extra *re_extra;
00073 
00074     // we store the host-string-to-substitute here; if a match is found,
00075     // the substitutions are made and the resulting url is stored
00076     // directly in toURL's host field
00077     char *to_url_host_template;
00078     int to_url_host_template_len;
00079 
00080     // stores the number of substitutions
00081     int n_substitutions;
00082 
00083     // these two together point to template string places where
00084     // substitutions need to be made and the matching substring
00085     // to use
00086     int substitution_markers[MAX_REGEX_SUBS];
00087     int substitution_ids[MAX_REGEX_SUBS];
00088 
00089     LINK(RegexMapping, link);
00090   };
00091 
00092   typedef Queue<RegexMapping> RegexMappingList;
00093 
00094   struct MappingsStore
00095   {
00096     InkHashTable *hash_lookup;
00097     RegexMappingList regex_list;
00098     bool empty() { return ((hash_lookup == NULL) && regex_list.empty()); }
00099   };
00100 
00101   void PerformACLFiltering(HttpTransact::State * s, url_mapping * mapping);
00102   url_mapping *SetupPacMapping();       // manager proxy-autconfig mapping
00103   url_mapping *SetupBackdoorMapping();
00104   void PrintStore(MappingsStore &store);
00105 
00106   void DestroyStore(MappingsStore &store)
00107   {
00108     _destroyTable(store.hash_lookup);
00109     _destroyList(store.regex_list);
00110   }
00111 
00112   bool InsertForwardMapping(mapping_type maptype, url_mapping * mapping, const char * src_host);
00113   bool InsertMapping(mapping_type maptype, url_mapping *new_mapping, RegexMapping *reg_map,
00114                         const char * src_host, bool is_cur_mapping_regex);
00115 
00116   bool TableInsert(InkHashTable *h_table, url_mapping *mapping, const char *src_host);
00117 
00118   MappingsStore forward_mappings;
00119   MappingsStore reverse_mappings;
00120   MappingsStore permanent_redirects;
00121   MappingsStore temporary_redirects;
00122   MappingsStore forward_mappings_with_recv_port;
00123 
00124   bool forwardMappingLookup(URL *request_url, int request_port, const char *request_host,
00125                             int request_host_len, UrlMappingContainer &mapping_container)
00126   {
00127     return _mappingLookup(forward_mappings, request_url, request_port, request_host, request_host_len,
00128                           mapping_container);
00129   }
00130   bool reverseMappingLookup(URL *request_url, int request_port, const char *request_host,
00131                             int request_host_len, UrlMappingContainer &mapping_container)
00132   {
00133     return _mappingLookup(reverse_mappings, request_url, request_port, request_host, request_host_len,
00134                           mapping_container);
00135   }
00136   bool permanentRedirectLookup(URL *request_url, int request_port, const char *request_host,
00137                                int request_host_len, UrlMappingContainer &mapping_container)
00138   {
00139     return _mappingLookup(permanent_redirects, request_url, request_port, request_host, request_host_len,
00140                           mapping_container);
00141   }
00142   bool temporaryRedirectLookup(URL *request_url, int request_port, const char *request_host,
00143                                int request_host_len, UrlMappingContainer &mapping_container)
00144   {
00145     return _mappingLookup(temporary_redirects, request_url, request_port, request_host, request_host_len,
00146                           mapping_container);
00147   }
00148   bool forwardMappingWithRecvPortLookup(URL *request_url, int recv_port, const char *request_host,
00149                                         int request_host_len, UrlMappingContainer &mapping_container)
00150   {
00151     return _mappingLookup(forward_mappings_with_recv_port, request_url, recv_port, request_host,
00152                           request_host_len, mapping_container);
00153   }
00154 
00155   int nohost_rules;
00156   int reverse_proxy;
00157   int backdoor_enabled;
00158 
00159   // Vars for PAC mapping
00160   int mgmt_autoconf_port;
00161   int default_to_pac;
00162   int default_to_pac_port;
00163 
00164   char *ts_name;                // Used to send redirects when no host info
00165 
00166   char *http_default_redirect_url;      // Used if redirect in "referer" filtering was not defined properly
00167   int num_rules_forward;
00168   int num_rules_reverse;
00169   int num_rules_redirect_permanent;
00170   int num_rules_redirect_temporary;
00171   int num_rules_forward_with_recv_port;
00172 
00173 private:
00174   bool _valid;
00175 
00176   bool _mappingLookup(MappingsStore &mappings, URL *request_url, int request_port, const char *request_host,
00177                       int request_host_len, UrlMappingContainer &mapping_container);
00178   url_mapping *_tableLookup(InkHashTable * h_table, URL * request_url, int request_port, char *request_host,
00179                             int request_host_len);
00180   bool _regexMappingLookup(RegexMappingList &regex_mappings, URL * request_url, int request_port, const char *request_host,
00181                            int request_host_len, int rank_ceiling,
00182                            UrlMappingContainer &mapping_container);
00183   int _expandSubstitutions(int *matches_info, const RegexMapping *reg_map, const char *matched_string, char *dest_buf,
00184                            int dest_buf_size);
00185   void _destroyTable(InkHashTable *h_table);
00186   void _destroyList(RegexMappingList &regexes);
00187   inline bool _addToStore(MappingsStore &store, url_mapping *new_mapping, RegexMapping *reg_map, const char *src_host,
00188                           bool is_cur_mapping_regex, int &count);
00189 };
00190 
00191 void url_rewrite_remap_request(const UrlMappingContainer& mapping_container, URL * request_url);
00192 
00193 #endif

Generated by  doxygen 1.7.1