Don't free p which is still in use and will be later
[privoxy.git] / parsers.c
index e2c3211..d6a7701 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -1,4 +1,4 @@
-const char parsers_rcs[] = "$Id: parsers.c,v 1.61 2006/08/14 08:25:19 fabiankeil Exp $";
+const char parsers_rcs[] = "$Id: parsers.c,v 1.64 2006/08/17 17:15:10 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
@@ -40,6 +40,28 @@ const char parsers_rcs[] = "$Id: parsers.c,v 1.61 2006/08/14 08:25:19 fabiankeil
  *
  * Revisions   :
  *    $Log: parsers.c,v $
+ *    Revision 1.64  2006/08/17 17:15:10  fabiankeil
+ *    - Back to timegm() using GnuPG's replacement if necessary.
+ *      Using mktime() and localtime() could add a on hour offset if
+ *      the randomize factor was big enough to lead to a summer/wintertime
+ *      switch.
+ *
+ *    - Removed now-useless Privoxy 3.0.3 compatibility glue.
+ *
+ *    - Moved randomization code into pick_from_range().
+ *
+ *    - Changed parse_header_time definition.
+ *      time_t isn't guaranteed to be signed and
+ *      if it isn't, -1 isn't available as error code.
+ *      Changed some variable types in client_if_modified_since()
+ *      because of the same reason.
+ *
+ *    Revision 1.63  2006/08/14 13:18:08  david__schmidt
+ *    OS/2 compilation compatibility fixups
+ *
+ *    Revision 1.62  2006/08/14 08:58:42  fabiankeil
+ *    Changed include from strptime.c to strptime.h
+ *
  *    Revision 1.61  2006/08/14 08:25:19  fabiankeil
  *    Split filter-headers{} into filter-client-headers{}
  *    and filter-server-headers{}.
@@ -967,20 +989,13 @@ jb_err filter_header(struct client_state *csp, char **header)
 
    int i, found_filters = 0;
 
-#ifndef  MAX_AF_FILES
-# define MAX_AF_FILES 1
-# define INDEX_OR_NOT
-#else
-# define INDEX_OR_NOT [i] 
-#endif
-
    log_error(LOG_LEVEL_RE_FILTER, "Entered filter_headers");
    /*
     * Need to check the set of re_filterfiles...
     */
    for (i = 0; i < MAX_AF_FILES; i++)
    {
-      fl = csp->rlist INDEX_OR_NOT;
+      fl = csp->rlist[i];
       if (NULL != fl)
       {
          if (NULL != fl->f)
@@ -999,12 +1014,12 @@ jb_err filter_header(struct client_state *csp, char **header)
 
    for (i = 0; i < MAX_AF_FILES; i++)
    {
-      fl = csp->rlist INDEX_OR_NOT;
+      fl = csp->rlist[i];
       if ((NULL == fl) || (NULL == fl->f))
          break;
       /*
        * For all applying +filter actions, look if a filter by that
-       * name exists and if yes, execute it's pcrs_joblist on the
+       * name exists and if yes, execute its pcrs_joblist on the
        * buffer.
        */
       for (b = fl->f; b; b = b->next)
@@ -1516,23 +1531,19 @@ jb_err server_last_modified(struct client_state *csp, char **header)
       log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
       now = time(NULL);
       timeptr = gmtime(&now);
-      if ( (last_modified = parse_header_time(*header, timeptr)) < 0 )
+      if ((timeptr = parse_header_time(*header, &last_modified)) == NULL)
       {
-          log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
-          freez(*header);
+         log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
+         freez(*header);
       }
       else
       {
          rtime = difftime(now, last_modified);
          if (rtime)
          {
-#if !defined(_WIN32) && !defined(__OS2__)
-            rtime = random() % rtime + 1; 
-#else
-            rtime = rand() % rtime + 1; 
-#endif /* (ifndef _WIN32 || __OS2__) */
+            rtime = pick_from_range(rtime);
             last_modified += rtime;
-            timeptr = localtime(&last_modified);
+            timeptr = gmtime(&last_modified);
             strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
             freez(*header);
             *header = strdup("Last-Modified: ");
@@ -1540,7 +1551,7 @@ jb_err server_last_modified(struct client_state *csp, char **header)
 
             if (*header == NULL)
             {
-               log_error(LOG_LEVEL_ERROR, " Insufficent memory, header crunched without replacement.");
+               log_error(LOG_LEVEL_ERROR, "Insufficent memory, header crunched without replacement.");
                return JB_ERR_MEMORY;  
             }
 
@@ -1664,7 +1675,7 @@ jb_err client_referrer(struct client_state *csp, char **header)
    const char *newval;
    const char *host;
    char *referer;
-   int hostlenght;
+   size_t hostlenght;
  
 #ifdef FEATURE_FORCE_LOAD
    /* Since the referrer can include the prefix even
@@ -1722,7 +1733,7 @@ jb_err client_referrer(struct client_state *csp, char **header)
           *if www.example.org/www.example.com-shall-see-the-referer/
           *links to www.example.com/
           */
-         referer[hostlenght+17] = '\n';
+         referer[hostlenght+17] = '\0';
       }
       if ( 0 == strstr(referer, host)) /*Host has changed*/
       {
@@ -2216,8 +2227,8 @@ jb_err client_if_modified_since(struct client_state *csp, char **header)
    struct tm *timeptr = NULL;
    time_t tm = 0;                  
    const char *newval;
-   time_t rtime;
-   time_t hours, minutes, seconds;
+   long int rtime;
+   long int hours, minutes, seconds;
    int negative = 0;
    char * endptr;
    
@@ -2244,28 +2255,25 @@ jb_err client_if_modified_since(struct client_state *csp, char **header)
       }
       else /* add random value */
       {
-         if ( (tm = parse_header_time(*header, timeptr)) < 0 )
+         if ((timeptr = parse_header_time(*header, &tm)) == NULL)
          {
             log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
             freez(*header);
          }
          else
          {
-            rtime = (time_t) strtol(newval, &endptr, 0);
+            rtime = strtol(newval, &endptr, 0);
+            if(rtime < 0)
+            {
+                rtime *= -1; 
+                negative = 1;
+            }
             if(rtime)
             {
                log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d hou%s)",
                   *header, rtime, (rtime == 1 || rtime == -1) ? "r": "rs");
                rtime *= 3600;
-#if !defined(_WIN32) && !defined(__OS2__)
-               rtime = random() % rtime + 1; 
-#else
-               rtime = rand() % rtime + 1; 
-#endif /* (_WIN32 || __OS2__) */
-               if(newval[0] == '-')
-               {
-                  rtime *= -1;      
-               }
+               rtime = pick_from_range(rtime);
             }
             else
             {
@@ -2273,7 +2281,7 @@ jb_err client_if_modified_since(struct client_state *csp, char **header)
                   *header);
             }
             tm += rtime;
-            timeptr = localtime(&tm);
+            timeptr = gmtime(&tm);
             strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
 
             freez(*header);
@@ -2282,18 +2290,13 @@ jb_err client_if_modified_since(struct client_state *csp, char **header)
 
             if (*header == NULL)
             {
-               log_error(LOG_LEVEL_HEADER, " Insufficent memory, header crunched without replacement.");
+               log_error(LOG_LEVEL_HEADER, "Insufficent memory, header crunched without replacement.");
                return JB_ERR_MEMORY;  
             }
 
             if(LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
             {
-               if(rtime < 0)
-               {
-                  rtime *= -1; 
-                  negative = 1;
-               }
-               hours   = rtime / 3600 % 24;
+               hours   = rtime / 3600;
                minutes = rtime / 60 % 60;
                seconds = rtime % 60;            
 
@@ -2829,26 +2832,30 @@ int strclean(const char *string, const char *substring)
  *
  * Parameters  :
  *          1  :  header = header to parse
- *          2  :  timeptr = storage for the resulting time structure 
+ *          2  :  tm = storage for the resulting time in seconds 
  *
- * Returns     :  Time in seconds since Unix epoch or -1 for failure.
+ * Returns     :  Time struct containing the header time, or
+ *                NULL in case of a parsing problem.
  *
  *********************************************************************/
-time_t parse_header_time(char *header, struct tm *timeptr) {
+struct tm *parse_header_time(char *header, time_t *tm) {
 
    char * timestring;
-   time_t tm;
-
-   tm = time(NULL);
-   timeptr = localtime(&tm);
+   struct tm * timeptr;
+   
+   *tm = 0;
+   timeptr = localtime(tm);
    /* Skipping header name */
    timestring = strstr(header, ": ");
    if (strptime(timestring, ": %a, %d %b %Y %H:%M:%S", timeptr) == NULL)
    {
-      return(-1);
+      timeptr = NULL;
+   }
+   else
+   {
+      *tm = timegm(timeptr);
    }
-   tm = mktime(timeptr);
-   return(tm);
+   return(timeptr);
 }