00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #include "ink_defs.h"
00025 #include "UrlMapping.h"
00026 #include "I_RecCore.h"
00027 #include "ink_cap.h"
00028 
00029 
00030 
00031 url_mapping::url_mapping(int rank )
00032   : from_path_len(0), fromURL(), toUrl(), homePageRedirect(false), unique(false), default_redirect_url(false),
00033     optional_referer(false), negative_referer(false), wildcard_from_scheme(false),
00034     tag(NULL), filter_redirect_url(NULL), referer_list(0),
00035     redir_chunk_list(0), filter(NULL), _plugin_count(0), _rank(rank)
00036 {
00037   memset(_plugin_list, 0, sizeof(_plugin_list));
00038   memset(_instance_data, 0, sizeof(_instance_data));
00039 }
00040 
00041 
00042 
00043 
00044 
00045 bool
00046 url_mapping::add_plugin(remap_plugin_info* i, void* ih)
00047 {
00048   if (_plugin_count >= MAX_REMAP_PLUGIN_CHAIN)
00049     return false;
00050 
00051   _plugin_list[_plugin_count] = i;
00052   _instance_data[_plugin_count] = ih;
00053   ++_plugin_count;
00054 
00055   return true;
00056 }
00057 
00058 
00059 
00060 
00061 
00062 remap_plugin_info*
00063 url_mapping::get_plugin(unsigned int index) const
00064 {
00065   Debug("url_rewrite", "get_plugin says we have %d plugins and asking for plugin %d", _plugin_count, index);
00066   if ((_plugin_count == 0) || unlikely(index > _plugin_count))
00067     return NULL;
00068 
00069   return _plugin_list[index];
00070 }
00071 
00072 
00073 
00074 
00075 void
00076 url_mapping::delete_instance(unsigned int index)
00077 {
00078   void *ih = get_instance(index);
00079   remap_plugin_info* p = get_plugin(index);
00080 
00081   if (ih && p && p->fp_tsremap_delete_instance) {
00082     p->fp_tsremap_delete_instance(ih);
00083   }
00084 }
00085 
00086 
00087 
00088 
00089 
00090 url_mapping::~url_mapping()
00091 {
00092   referer_info *r;
00093   redirect_tag_str *rc;
00094   acl_filter_rule *afr;
00095 
00096   tag = (char *)ats_free_null(tag);
00097   filter_redirect_url = (char *)ats_free_null(filter_redirect_url);
00098 
00099   while ((r = referer_list) != 0) {
00100     referer_list = r->next;
00101     delete r;
00102   }
00103 
00104   while ((rc = redir_chunk_list) != 0) {
00105     redir_chunk_list = rc->next;
00106     delete rc;
00107   }
00108 
00109   
00110   for (unsigned int i = 0; i < _plugin_count; ++i)
00111     delete_instance(i);
00112 
00113   
00114   while ((afr = filter) != NULL) {
00115     filter = afr->next;
00116     delete afr;
00117   }
00118 
00119   
00120   fromURL.destroy();
00121   toUrl.destroy();
00122 }
00123 
00124 void
00125 url_mapping::Print()
00126 {
00127   char from_url_buf[131072], to_url_buf[131072];
00128 
00129   fromURL.string_get_buf(from_url_buf, (int) sizeof(from_url_buf));
00130   toUrl.string_get_buf(to_url_buf, (int) sizeof(to_url_buf));
00131   printf("\t %s %s=> %s %s <%s> [plugins %s enabled; running with %u plugins]\n", from_url_buf,
00132          unique ? "(unique)" : "", to_url_buf,
00133          homePageRedirect ? "(R)" : "", tag ? tag : "",
00134          _plugin_count > 0 ? "are" : "not", _plugin_count);
00135 }
00136 
00137 
00138 
00139 
00140 redirect_tag_str *
00141 redirect_tag_str::parse_format_redirect_url(char *url)
00142 {
00143   char *c;
00144   redirect_tag_str *r, **rr;
00145   redirect_tag_str *list = 0;
00146   char type = 0;
00147 
00148   if (url && *url) {
00149     for (rr = &list; *(c = url) != 0;) {
00150       for (type = 's'; *c; c++) {
00151         if (c[0] == '%') {
00152           char tmp_type = (char) tolower((int) c[1]);
00153           if (tmp_type == 'r' || tmp_type == 'f' || tmp_type == 't' || tmp_type == 'o') {
00154             if (url == c)
00155               type = tmp_type;
00156             break;
00157           }
00158         }
00159       }
00160       r = new redirect_tag_str();
00161       if (likely(r)) {
00162         if ((r->type = type) == 's') {
00163           char svd = *c;
00164           *c = 0;
00165           r->chunk_str = ats_strdup(url);
00166           *c = svd;
00167           url = c;
00168         } else
00169           url += 2;
00170         (*rr = r)->next = 0;
00171         rr = &(r->next);
00172         
00173       } else
00174         break;                  
00175     }
00176   }
00177   return list;
00178 }
00179 
00180 
00181 
00182 
00183 
00184 referer_info::referer_info(char *_ref, bool *error_flag, char *errmsgbuf, int errmsgbuf_size)
00185   : next(0),
00186     referer(0),
00187     referer_size(0),
00188     any(false),
00189     negative(false),
00190     regx_valid(false)
00191 {
00192   const char *error;
00193   int erroffset;
00194 
00195   if (error_flag)
00196     *error_flag = false;
00197   regx = NULL;
00198 
00199   if (_ref) {
00200     if (*_ref == '~') {
00201       negative = true;
00202       _ref++;
00203     }
00204     if ((referer = ats_strdup(_ref)) != 0) {
00205       referer_size = strlen(referer);
00206       if (!strcmp(referer, "*"))
00207         any = true;
00208       else {
00209         regx = pcre_compile(referer, PCRE_CASELESS, &error, &erroffset, NULL);
00210         if (!regx) {
00211           if (errmsgbuf && (errmsgbuf_size - 1) > 0)
00212             ink_strlcpy(errmsgbuf, error, errmsgbuf_size);
00213           if (error_flag)
00214             *error_flag = true;
00215         } else
00216           regx_valid = true;
00217       }
00218     }
00219   }
00220 }
00221 
00222 
00223 
00224 
00225 
00226 referer_info::~referer_info()
00227 {
00228   ats_free(referer);
00229   referer = 0;
00230   referer_size = 0;
00231 
00232   if (regx_valid) {
00233     pcre_free(regx);
00234     regx = NULL;
00235     regx_valid = false;
00236   }
00237 }