Insert a refresh tag into the /client-tags cgi page
authorFabian Keil <fk@fabiankeil.de>
Mon, 20 Feb 2017 13:47:04 +0000 (13:47 +0000)
committerFabian Keil <fk@fabiankeil.de>
Mon, 20 Feb 2017 13:47:04 +0000 (13:47 +0000)
... when serving it while a client-specific tag
is temporarily enabled.

This makes it less likely that the user ends up
looking at tag state that is out of date.

cgisimple.c
client-tags.c
client-tags.h
templates/client-tags

index c2983e3..078a001 100644 (file)
@@ -1,4 +1,4 @@
-const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.145 2017/01/23 13:05:12 fabiankeil Exp $";
+const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.146 2017/02/20 13:45:14 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
@@ -345,6 +345,7 @@ jb_err cgi_show_client_tags(struct client_state *csp,
    jb_err err = JB_ERR_OK;
    char *client_tag_status;
    char buf[1000];
+   time_t refresh_delay;
 
    assert(csp);
    assert(rsp);
@@ -397,6 +398,24 @@ jb_err cgi_show_client_tags(struct client_state *csp,
       }
       if (!err) err = string_append(&client_tag_status, "</table>\n");
    }
+   refresh_delay = get_next_tag_timeout_for_client(csp->client_address);
+   if (refresh_delay != 0)
+   {
+      snprintf(buf, sizeof(buf), "%d", csp->config->client_tag_lifetime);
+      if (map(exports, "refresh-delay", 1, buf, 1))
+      {
+         free_map(exports);
+         return JB_ERR_MEMORY;
+      }
+   }
+   else
+   {
+      err = map_block_killer(exports, "tags-expire");
+      if (err != JB_ERR_OK)
+      {
+         return err;
+      }
+   }
 
    if (map(exports, "client-tags", 1, client_tag_status, 0))
    {
index fda1f88..f9b38c9 100644 (file)
@@ -252,6 +252,54 @@ void get_tag_list_for_client(struct list *tag_list,
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  get_next_tag_timeout_for_client
+ *
+ * Description :  Figures out when the next temporarily enabled tag
+ *                for the client will have timed out.
+ *
+ * Parameters  :
+ *          1  :  client_address = Address of the client
+ *
+ * Returns     :  Lowest timeout in seconds
+ *
+ *********************************************************************/
+time_t get_next_tag_timeout_for_client(const char *client_address)
+{
+   struct client_specific_tag *enabled_tags;
+   time_t next_timeout = 0;
+   const time_t now = time(NULL);
+
+   privoxy_mutex_lock(&client_tags_mutex);
+
+   enabled_tags = get_tags_for_client(client_address);
+   while (enabled_tags != NULL)
+   {
+      log_error(LOG_LEVEL_CGI, "Evaluating tag '%s' for client %s. End of life %d",
+         enabled_tags->name, client_address, enabled_tags->end_of_life);
+      if (enabled_tags->end_of_life)
+      {
+          time_t time_left = enabled_tags->end_of_life - now;
+          /* Add a second to make sure the tag will have expired */
+          time_left++;
+          log_error(LOG_LEVEL_CGI, "%d > %d?", next_timeout, time_left);
+          if (next_timeout == 0 || next_timeout > time_left)
+          {
+             next_timeout = time_left;
+          }
+       }
+       enabled_tags = enabled_tags->next;
+   }
+
+   privoxy_mutex_unlock(&client_tags_mutex);
+
+   log_error(LOG_LEVEL_CGI, "Next timeout in %d seconds", next_timeout);
+
+   return next_timeout;
+
+}
+
 /*********************************************************************
  *
  * Function    :  add_tag_for_client
index 6f8398c..24b45c1 100644 (file)
@@ -1,6 +1,6 @@
 #ifndef CLIENT_TAGS_H_INCLUDED
 #define CLIENT_TAGS_H_INCLUDED
-#define CLIENT_TAGS_H_VERSION "$Id: client-tags.h,v 1.2 2016/05/03 13:21:42 fabiankeil Exp $"
+#define CLIENT_TAGS_H_VERSION "$Id: client-tags.h,v 1.3 2016/05/22 12:43:07 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/client-tags.h,v $
@@ -33,6 +33,7 @@ extern int client_tag_match(const struct pattern_spec *pattern,
                             const struct list *tags);
 extern void get_tag_list_for_client(struct list *tag_list,
                                     const char *client_address);
+extern time_t get_next_tag_timeout_for_client(const char *client_address);
 extern jb_err disable_client_specific_tag(struct client_state *csp,
                                           const char *tag_name);
 extern jb_err enable_client_specific_tag(struct client_state *csp,
index a5291c1..6520a1a 100644 (file)
@@ -80,6 +80,9 @@
   <meta name="robots" content="noindex,nofollow">
   <link rel="stylesheet" type="text/css" href="@default-cgi@send-stylesheet">
   <link rel="shortcut icon" href="@default-cgi@favicon.ico" type="image/x-icon">
+<!-- @if-tags-expire-start -->
+  <meta http-equiv="refresh" content="@refresh-delay@">
+<!-- if-tags-expire-end@ -->
 </head>
 
 <body>