Skip empty filter files in pcrs_filter_response,
[privoxy.git] / filters.c
index be0f39c..831fcb0 100644 (file)
--- a/filters.c
+++ b/filters.c
@@ -1,4 +1,4 @@
-const char filters_rcs[] = "$Id: filters.c,v 1.66 2006/09/23 13:26:38 roro Exp $";
+const char filters_rcs[] = "$Id: filters.c,v 1.70 2006/12/09 13:33:15 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/filters.c,v $
@@ -40,6 +40,22 @@ const char filters_rcs[] = "$Id: filters.c,v 1.66 2006/09/23 13:26:38 roro Exp $
  *
  * Revisions   :
  *    $Log: filters.c,v $
+ *    Revision 1.70  2006/12/09 13:33:15  fabiankeil
+ *    Added some sanity checks for get_last_url().
+ *    Fixed possible segfault caused by my last commit.
+ *
+ *    Revision 1.69  2006/12/08 12:39:13  fabiankeil
+ *    Let get_last_url() catch https URLs as well.
+ *
+ *    Revision 1.68  2006/12/05 14:45:48  fabiankeil
+ *    Make sure get_last_url() behaves like advertised
+ *    and fast-redirects{} can be combined with redirect{}.
+ *
+ *    Revision 1.67  2006/11/28 15:19:43  fabiankeil
+ *    Implemented +redirect{s@foo@bar@} to generate
+ *    a redirect based on a rewritten version of the
+ *    original URL.
+ *
  *    Revision 1.66  2006/09/23 13:26:38  roro
  *    Replace TABs by spaces in source code.
  *
@@ -1249,23 +1265,35 @@ char *rewrite_url(char *old_url, const char *pcrs_command)
  *********************************************************************/
 char *get_last_url(char *subject, const char *redirect_mode)
 {
-   char *new_url;
+   char *new_url = NULL;
    char *tmp;
 
    assert(subject);
    assert(redirect_mode);
 
    subject = strdup(subject);
+   if (subject == NULL)
+   {
+      log_error(LOG_LEVEL_ERROR, "Out of memory while searching for redirects.");
+      return NULL;
+   }
 
    if (0 == strcmpic(redirect_mode, "check-decoded-url"))
    {  
       log_error(LOG_LEVEL_REDIRECTS, "Decoding \"%s\" if necessary.", subject);
       new_url = url_decode(subject);
-      freez(subject);
-      subject = new_url;
+      if (new_url != NULL)
+      {
+         freez(subject);
+         subject = new_url;
+      }
+      else
+      {
+         log_error(LOG_LEVEL_ERROR, "Unable to decode \"%s\".", subject);
+      }
    }
 
-   log_error(LOG_LEVEL_REDIRECTS, "Checking \"%s\" for redirects", subject);
+   log_error(LOG_LEVEL_REDIRECTS, "Checking \"%s\" for redirects.", subject);
 
    /*
     * Find the last URL encoded in the request
@@ -1275,9 +1303,26 @@ char *get_last_url(char *subject, const char *redirect_mode)
    {
       new_url = tmp++;
    }
+   tmp = (new_url != NULL) ? new_url : subject;
+   while ((tmp = strstr(tmp, "https://")) != NULL)
+   {
+      new_url = tmp++;
+   }
 
-   if (new_url != subject)
+   if ((new_url != NULL)
+      && (  (new_url != subject)
+         || (0 == strncmpic(subject, "http://", 7))
+         || (0 == strncmpic(subject, "https://", 8))
+         ))
    {
+      /*
+       * Return new URL if we found a redirect 
+       * or if the subject already was a URL.
+       *
+       * The second case makes sure that we can
+       * chain get_last_url after another redirection check
+       * (like rewrite_url) without losing earlier redirects.
+       */
       new_url = strdup(new_url);
       freez(subject);
       return new_url;
@@ -1653,7 +1698,18 @@ char *pcrs_filter_response(struct client_state *csp)
    {
      fl = csp->rlist[i];
      if ((NULL == fl) || (NULL == fl->f))
-       break;
+     {
+        /*
+         * Either there are no filter files
+         * left, or this filter file just
+         * contains no valid filters.
+         *
+         * Continue to be sure we don't miss
+         * valid filter files that are chained
+         * after empty or invalid ones.
+         */
+        continue;
+     }
    /*
     * For all applying +filter actions, look if a filter by that
     * name exists and if yes, execute it's pcrs_joblist on the