Remove a TODO comment about expiring cookies before the end of the session
[privoxy.git] / parsers.c
index 5489288..c520d0a 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -1,4 +1,4 @@
-const char parsers_rcs[] = "$Id: parsers.c,v 1.255 2012/10/17 18:19:29 fabiankeil Exp $";
+const char parsers_rcs[] = "$Id: parsers.c,v 1.263 2012/11/11 12:38:42 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
@@ -280,21 +280,21 @@ long flush_socket(jb_socket fd, struct iob *iob)
  *
  * Function    :  add_to_iob
  *
- * Description :  Add content to the buffered page, expanding the
+ * Description :  Add content to the buffer, expanding the
  *                buffer if necessary.
  *
  * Parameters  :
- *          1  :  csp = Current client state (buffers, headers, etc...)
- *          2  :  buf = holds the content to be added to the page
- *          3  :  n = number of bytes to be added
+ *          1  :  iob = Destination buffer.
+ *          2  :  buffer_limit = Limit to which the destination may grow
+ *          3  :  src = holds the content to be added
+ *          4  :  n = number of bytes to be added
  *
  * Returns     :  JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
  *                or buffer limit reached.
  *
  *********************************************************************/
-jb_err add_to_iob(struct client_state *csp, char *buf, long n)
+jb_err add_to_iob(struct iob *iob, const size_t buffer_limit, char *src, long n)
 {
-   struct iob *iob = csp->iob;
    size_t used, offset, need;
    char *p;
 
@@ -308,24 +308,24 @@ jb_err add_to_iob(struct client_state *csp, char *buf, long n)
     * If the buffer can't hold the new data, extend it first.
     * Use the next power of two if possible, else use the actual need.
     */
-   if (need > csp->config->buffer_limit)
+   if (need > buffer_limit)
    {
       log_error(LOG_LEVEL_INFO,
          "Buffer limit reached while extending the buffer (iob). Needed: %d. Limit: %d",
-         need, csp->config->buffer_limit);
+         need, buffer_limit);
       return JB_ERR_MEMORY;
    }
 
    if (need > iob->size)
    {
-      size_t want = csp->iob->size ? csp->iob->size : 512;
+      size_t want = iob->size ? iob->size : 512;
 
       while (want <= need)
       {
          want *= 2;
       }
 
-      if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
+      if (want <= buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
       {
          iob->size = want;
       }
@@ -346,7 +346,7 @@ jb_err add_to_iob(struct client_state *csp, char *buf, long n)
    }
 
    /* copy the new data into the iob buffer */
-   memcpy(iob->eod, buf, (size_t)n);
+   memcpy(iob->eod, src, (size_t)n);
 
    /* point to the end of the data */
    iob->eod += n;
@@ -359,6 +359,27 @@ jb_err add_to_iob(struct client_state *csp, char *buf, long n)
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  clear_iob
+ *
+ * Description :  Frees the memory allocated for an I/O buffer and
+ *                resets the structure.
+ *
+ * Parameters  :
+ *          1  :  iob = I/O buffer to clear.
+ *
+ * Returns     :  JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
+ *                or buffer limit reached.
+ *
+ *********************************************************************/
+void clear_iob(struct iob *iob)
+{
+   free(iob->buf);
+   memset(iob, '\0', sizeof(*iob));;
+}
+
+
 #ifdef FEATURE_ZLIB
 /*********************************************************************
  *
@@ -510,11 +531,6 @@ jb_err decompress_iob(struct client_state *csp)
    }
    else if (csp->content_type & CT_DEFLATE)
    {
-      /*
-       * XXX: The debug level should be lowered
-       * before the next stable release.
-       */
-      log_error(LOG_LEVEL_INFO, "Decompressing deflated iob: %d", *cur);
       /*
        * In theory (that is, according to RFC 1950), deflate-compressed
        * data should begin with a two-byte zlib header and have an
@@ -3683,7 +3699,9 @@ static jb_err server_proxy_connection_adder(struct client_state *csp)
 
    if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
     && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
-    && !(csp->flags & CSP_FLAG_SERVER_PROXY_CONNECTION_HEADER_SET))
+    && !(csp->flags & CSP_FLAG_SERVER_PROXY_CONNECTION_HEADER_SET)
+    && ((csp->flags & CSP_FLAG_SERVER_CONTENT_LENGTH_SET)
+       || (csp->flags & CSP_FLAG_CHUNKED)))
    {
       log_error(LOG_LEVEL_HEADER, "Adding: %s", proxy_connection_header);
       err = enlist(csp->headers, proxy_connection_header);
@@ -3786,6 +3804,50 @@ static jb_err server_http(struct client_state *csp, char **header)
    return JB_ERR_OK;
 }
 
+/*********************************************************************
+ *
+ * Function    :  add_cooky_expiry_date
+ *
+ * Description :  Adds a cookie expiry date to a string.
+ *
+ * Parameters  :
+ *          1  :  cookie = On input, pointer to cookie to modify.
+ *                         On output, pointer to the modified header.
+ *                         The original string is freed.
+ *          2  :  lifetime = Seconds the cookie should be valid
+ *
+ * Returns     :  N/A
+ *
+ *********************************************************************/
+static void add_cookie_expiry_date(char **cookie, time_t lifetime)
+{
+   char tmp[50];
+   struct tm *timeptr = NULL;
+   time_t expiry_date = time(NULL) + lifetime;
+#ifdef HAVE_GMTIME_R
+   struct tm gmt;
+
+   timeptr = gmtime_r(&expiry_date, &gmt);
+#elif defined(MUTEX_LOCKS_AVAILABLE)
+   privoxy_mutex_lock(&gmtime_mutex);
+   timeptr = gmtime(&expiry_date);
+   privoxy_mutex_unlock(&gmtime_mutex);
+#else
+   timeptr = gmtime(&expiry_date);
+#endif
+
+   if (NULL == timeptr)
+   {
+      log_error(LOG_LEVEL_FATAL,
+         "Failed to get the time in add_cooky_expiry_date()");
+   }
+   strftime(tmp, sizeof(tmp), "; expires=%a, %d-%b-%Y %H:%M:%S GMT", timeptr);
+   if (JB_ERR_OK != string_append(cookie, tmp))
+   {
+      log_error(LOG_LEVEL_FATAL, "Out of memory in add_cooky_expiry()");
+   }
+}
+
 
 /*********************************************************************
  *
@@ -3795,10 +3857,6 @@ static jb_err server_http(struct client_state *csp, char **header)
  *                Crunch, accept or rewrite it to a session cookie.
  *                Called from `sed'.
  *
- *                TODO: Allow the user to specify a new expiration
- *                time to cause the cookie to expire even before the
- *                browser is closed.
- *
  * Parameters  :
  *          1  :  csp = Current client state (buffers, headers, etc...)
  *          2  :  header = On input, pointer to header to modify.
@@ -3812,20 +3870,18 @@ static jb_err server_http(struct client_state *csp, char **header)
  *********************************************************************/
 static jb_err server_set_cookie(struct client_state *csp, char **header)
 {
-   time_t now;
-   time_t cookie_time;
-
-   time(&now);
-
    if ((csp->action->flags & ACTION_CRUNCH_INCOMING_COOKIES) != 0)
    {
       log_error(LOG_LEVEL_HEADER, "Crunching incoming cookie: %s", *header);
       freez(*header);
    }
-   else if ((csp->action->flags & ACTION_SESSION_COOKIES_ONLY) != 0)
+   else if ((0 != (csp->action->flags & ACTION_SESSION_COOKIES_ONLY))
+         || (0 != (csp->action->flags & ACTION_LIMIT_COOKIE_LIFETIME)))
    {
-      /* Flag whether or not to log a message */
-      int changed = 0;
+      time_t now;
+      time_t cookie_time;
+      long cookie_lifetime = 0;
+      int expiry_date_acceptable = 0;
 
       /* A variable to store the tag we're working on */
       char *cur_tag;
@@ -3839,6 +3895,20 @@ static jb_err server_set_cookie(struct client_state *csp, char **header)
          cur_tag++;
       }
 
+      time(&now);
+
+      if ((csp->action->flags & ACTION_LIMIT_COOKIE_LIFETIME) != 0)
+      {
+         const char *param = csp->action->string[ACTION_STRING_LIMIT_COOKIE_LIFETIME];
+
+         cookie_lifetime = strtol(param, NULL, 0);
+         if (cookie_lifetime < 0)
+         {
+            log_error(LOG_LEVEL_FATAL, "Invalid cookie lifetime limit: %s", param);
+         }
+         cookie_lifetime *= 60U;
+      }
+
       /* Loop through each tag in the cookie */
       while (*cur_tag)
       {
@@ -3892,7 +3962,7 @@ static jb_err server_set_cookie(struct client_state *csp, char **header)
                log_error(LOG_LEVEL_ERROR,
                   "Can't parse \'%s\', send by %s. Unsupported time format?", cur_tag, csp->http->url);
                string_move(cur_tag, next_tag);
-               changed = 1;
+               expiry_date_acceptable = 0;
             }
             else
             {
@@ -3930,12 +4000,21 @@ static jb_err server_set_cookie(struct client_state *csp, char **header)
                 *   anyway, which in many cases will be shorter
                 *   than a browser session.
                 */
-               if (cookie_time - now < 0)
+               if (cookie_time < now)
                {
                   log_error(LOG_LEVEL_HEADER,
                      "Cookie \'%s\' is already expired and can pass unmodified.", *header);
                   /* Just in case some clown sets more then one expiration date */
                   cur_tag = next_tag;
+                  expiry_date_acceptable = 1;
+               }
+               else if ((cookie_lifetime != 0) && (cookie_time < (now + cookie_lifetime)))
+               {
+                  log_error(LOG_LEVEL_HEADER, "Cookie \'%s\' can pass unmodified. "
+                     "Its lifetime is below the limit.", *header);
+                  /* Just in case some clown sets more then one expiration date */
+                  cur_tag = next_tag;
+                  expiry_date_acceptable = 1;
                }
                else
                {
@@ -3946,7 +4025,7 @@ static jb_err server_set_cookie(struct client_state *csp, char **header)
                   string_move(cur_tag, next_tag);
 
                   /* That changed the header, need to issue a log message */
-                  changed = 1;
+                  expiry_date_acceptable = 0;
 
                   /*
                    * Note that the next tag has now been moved to *cur_tag,
@@ -3963,11 +4042,19 @@ static jb_err server_set_cookie(struct client_state *csp, char **header)
          }
       }
 
-      if (changed)
+      if (!expiry_date_acceptable)
       {
          assert(NULL != *header);
-         log_error(LOG_LEVEL_HEADER, "Cookie rewritten to a temporary one: %s",
-            *header);
+         if (cookie_lifetime == 0)
+         {
+            log_error(LOG_LEVEL_HEADER, "Cookie rewritten to a temporary one: %s",
+               *header);
+         }
+         else
+         {
+            add_cookie_expiry_date(header, cookie_lifetime);
+            log_error(LOG_LEVEL_HEADER, "Cookie rewritten to: %s", *header);
+         }
       }
    }
 
@@ -4307,6 +4394,7 @@ static void create_content_length_header(unsigned long long content_length,
 }
 
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
 /*********************************************************************
  *
  * Function    :  get_expected_content_length
@@ -4338,7 +4426,7 @@ unsigned long long get_expected_content_length(struct list *headers)
 
    return content_length;
 }
-
+#endif
 
 /*
   Local Variables: