Fixing quoting of special characters in URLs
[privoxy.git] / loaders.c
index dbfdc62..2ec1ffe 100644 (file)
--- a/loaders.c
+++ b/loaders.c
@@ -1,4 +1,4 @@
-const char loaders_rcs[] = "$Id: loaders.c,v 1.30 2001/10/25 03:40:48 david__schmidt Exp $";
+const char loaders_rcs[] = "$Id: loaders.c,v 1.34 2001/12/30 14:07:32 steudten Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loaders.c,v $
@@ -35,6 +35,27 @@ const char loaders_rcs[] = "$Id: loaders.c,v 1.30 2001/10/25 03:40:48 david__sch
  *
  * Revisions   :
  *    $Log: loaders.c,v $
+ *    Revision 1.34  2001/12/30 14:07:32  steudten
+ *    - Add signal handling (unix)
+ *    - Add SIGHUP handler (unix)
+ *    - Add creation of pidfile (unix)
+ *    - Add action 'top' in rc file (RH)
+ *    - Add entry 'SIGNALS' to manpage
+ *    - Add exit message to logfile (unix)
+ *
+ *    Revision 1.33  2001/11/13 00:16:38  jongfoster
+ *    Replacing references to malloc.h with the standard stdlib.h
+ *    (See ANSI or K&R 2nd Ed)
+ *
+ *    Revision 1.32  2001/11/07 00:02:13  steudten
+ *    Add line number in error output for lineparsing for
+ *    actionsfile and configfile.
+ *    Special handling for CLF added.
+ *
+ *    Revision 1.31  2001/10/26 17:39:01  oes
+ *    Removed csp->referrer
+ *    Moved ijb_isspace and ijb_tolower to project.h
+ *
  *    Revision 1.30  2001/10/25 03:40:48  david__schmidt
  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
  *    threads to call select() simultaneously.  So, it's time to do a real, live,
@@ -192,7 +213,6 @@ const char loaders_rcs[] = "$Id: loaders.c,v 1.30 2001/10/25 03:40:48 david__sch
 #include <stdlib.h>
 #include <sys/types.h>
 #include <string.h>
-#include <malloc.h>
 #include <errno.h>
 #include <sys/stat.h>
 #include <ctype.h>
@@ -211,6 +231,7 @@ const char loaders_rcs[] = "$Id: loaders.c,v 1.30 2001/10/25 03:40:48 david__sch
 #include "miscutil.h"
 #include "errlog.h"
 #include "actions.h"
+#include "urlmatch.h"
 
 const char loaders_h_rcs[] = LOADERS_H_VERSION;
 
@@ -351,174 +372,6 @@ void sweep(void)
 }
 
 
-/*********************************************************************
- *
- * Function    :  create_url_spec
- *
- * Description :  Creates a "url_spec" structure from a string.
- *                When finished, free with unload_url().
- *
- * Parameters  :
- *          1  :  url = Target url_spec to be filled in.  Must be
- *                      zeroed out before the call (e.g. using zalloc).
- *          2  :  buf = Source pattern, null terminated.  NOTE: The
- *                      contents of this buffer are destroyed by this
- *                      function.  If this function succeeds, the
- *                      buffer is copied to url->spec.  If this
- *                      function fails, the contents of the buffer
- *                      are lost forever.
- *
- * Returns     :  JB_ERR_OK - Success
- *                JB_ERR_MEMORY - Out of memory
- *                JB_ERR_PARSE - Cannot parse regex (Detailed message
- *                               written to system log)
- *
- *********************************************************************/
-jb_err create_url_spec(struct url_spec * url, char * buf)
-{
-   char *p;
-   struct url_spec tmp_url[1];
-
-   assert(url);
-   assert(buf);
-
-   /* save a copy of the orignal specification */
-   if ((url->spec = strdup(buf)) == NULL)
-   {
-      return JB_ERR_MEMORY;
-   }
-
-   if ((p = strchr(buf, '/')))
-   {
-      if (NULL == (url->path = strdup(p)))
-      {
-         freez(url->spec);
-         return JB_ERR_MEMORY;
-      }
-      url->pathlen = strlen(url->path);
-      *p = '\0';
-   }
-   else
-   {
-      url->path    = NULL;
-      url->pathlen = 0;
-   }
-#ifdef REGEX
-   if (url->path)
-   {
-      int errcode;
-      char rebuf[BUFFER_SIZE];
-
-      if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
-      {
-         freez(url->spec);
-         freez(url->path);
-         return JB_ERR_MEMORY;
-      }
-
-      sprintf(rebuf, "^(%s)", url->path);
-
-      errcode = regcomp(url->preg, rebuf,
-            (REG_EXTENDED|REG_NOSUB|REG_ICASE));
-      if (errcode)
-      {
-         size_t errlen = regerror(errcode,
-            url->preg, rebuf, sizeof(rebuf));
-
-         if (errlen > (sizeof(rebuf) - (size_t)1))
-         {
-            errlen = sizeof(rebuf) - (size_t)1;
-         }
-         rebuf[errlen] = '\0';
-
-         log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
-            url->spec, rebuf);
-
-         freez(url->spec);
-         freez(url->path);
-         freez(url->preg);
-
-         return JB_ERR_PARSE;
-      }
-   }
-#endif
-   if ((p = strchr(buf, ':')) == NULL)
-   {
-      url->port = 0;
-   }
-   else
-   {
-      *p++ = '\0';
-      url->port = atoi(p);
-   }
-
-   if ((url->domain = strdup(buf)) == NULL)
-   {
-      freez(url->spec);
-      freez(url->path);
-#ifdef REGEX
-      freez(url->preg);
-#endif /* def REGEX */
-      return JB_ERR_MEMORY;
-   }
-
-   /* split domain into components */
-
-   *tmp_url = dsplit(url->domain);
-   if (tmp_url->dbuf == NULL)
-   {
-      freez(url->spec);
-      freez(url->path);
-      freez(url->domain);
-#ifdef REGEX
-      freez(url->preg);
-#endif /* def REGEX */
-      return JB_ERR_MEMORY;
-   }
-
-   url->dbuf = tmp_url->dbuf;
-   url->dcnt = tmp_url->dcnt;
-   url->dvec = tmp_url->dvec;
-   url->unanchored = tmp_url->unanchored;
-
-   return JB_ERR_OK;
-
-}
-
-
-/*********************************************************************
- *
- * Function    :  free_url
- *
- * Description :  Called from the "unloaders".  Freez the url
- *                structure elements.
- *
- * Parameters  :
- *          1  :  url = pointer to a url_spec structure.
- *
- * Returns     :  N/A
- *
- *********************************************************************/
-void free_url(struct url_spec *url)
-{
-   if (url == NULL) return;
-
-   freez(url->spec);
-   freez(url->domain);
-   freez(url->dbuf);
-   freez(url->dvec);
-   freez(url->path);
-#ifdef REGEX
-   if (url->preg)
-   {
-      regfree(url->preg);
-      freez(url->preg);
-   }
-#endif
-
-}
-
-
 /*********************************************************************
  *
  * Function    :  check_file_changed
@@ -564,11 +417,11 @@ int check_file_changed(const struct file_list * current,
        && (current->lastmodified == statbuf->st_mtime)
        && (0 == strcmp(current->filename, filename)))
    {
-      return 0;
+       /* force reload of configfile and all the logs */
+       if ( !MustReload ) return 0;
    }
 
    fs = (struct file_list *)zalloc(sizeof(struct file_list));
-
    if (fs == NULL)
    {
       /* Out of memory error */
@@ -584,11 +437,8 @@ int check_file_changed(const struct file_list * current,
       freez (fs);
       return 1;
    }
-
-
    *newfl = fs;
    return 1;
-
 }
 
 
@@ -604,12 +454,13 @@ int check_file_changed(const struct file_list * current,
  *          1  :  buf = Buffer to use.
  *          2  :  buflen = Size of buffer in bytes.
  *          3  :  fp = File to read from
+ *         4  :  linenum = linenumber in file
  *
  * Returns     :  NULL on EOF or error
  *                Otherwise, returns buf.
  *
  *********************************************************************/
-char *read_config_line(char *buf, int buflen, FILE *fp)
+char *read_config_line(char *buf, int buflen, FILE *fp, unsigned long *linenum)
 {
    char *p;
    char *src;
@@ -621,6 +472,7 @@ char *read_config_line(char *buf, int buflen, FILE *fp)
 
    while (fgets(linebuf, sizeof(linebuf), fp))
    {
+       (*linenum)++;
       /* Trim off newline */
       if ((p = strpbrk(linebuf, "\r\n")) != NULL)
       {
@@ -708,7 +560,7 @@ static void unload_trustfile(void *f)
 
    unload_trustfile(b->next); /* Stack is cheap, isn't it? */
 
-   free_url(b->url);
+   free_url_spec(b->url);
 
    freez(b);
 
@@ -737,6 +589,7 @@ int load_trustfile(struct client_state *csp)
    char  buf[BUFFER_SIZE], *p, *q;
    int reject, trusted;
    struct file_list *fs;
+   unsigned long linenum = 0;
 
    if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
    {
@@ -765,7 +618,7 @@ int load_trustfile(struct client_state *csp)
 
    tl = csp->config->trust_list;
 
-   while (read_config_line(buf, sizeof(buf), fp) != NULL)
+   while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
    {
       trusted = 0;
       reject  = 1;
@@ -819,6 +672,7 @@ int load_trustfile(struct client_state *csp)
       if (trusted)
       {
          *tl++ = b->url;
+         /* FIXME BUFFER OVERFLOW if >=64 entries */
       }
    }
 
@@ -903,6 +757,7 @@ int load_re_filterfile(struct client_state *csp)
 
    char  buf[BUFFER_SIZE];
    int error;
+   unsigned long linenum = 0;
    pcrs_job *dummy;
 
    if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
@@ -932,7 +787,7 @@ int load_re_filterfile(struct client_state *csp)
    }
 
    /* Read line by line */
-   while (read_config_line(buf, sizeof(buf), fp) != NULL)
+   while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
    {
       enlist( bl->patterns, buf );