- Cosmetics: renamed and reordered functions, variables,
[privoxy.git] / cgi.c
diff --git a/cgi.c b/cgi.c
index f461bb3..00cbc3b 100644 (file)
--- a/cgi.c
+++ b/cgi.c
@@ -1,4 +1,4 @@
-const char cgi_rcs[] = "$Id: cgi.c,v 1.3 2001/06/03 19:12:16 oes Exp $";
+const char cgi_rcs[] = "$Id: cgi.c,v 1.7 2001/06/09 10:51:58 jongfoster Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgi.c,v $
@@ -36,64 +36,23 @@ const char cgi_rcs[] = "$Id: cgi.c,v 1.3 2001/06/03 19:12:16 oes Exp $";
  *
  * Revisions   :
  *    $Log: cgi.c,v $
- *    Revision 1.3  2001/06/03 19:12:16  oes
- *    introduced new cgi handling
- *
- *    Revision 1.1  2001/06/03 11:03:48  oes
- *    Makefile/in
- *
- *    introduced cgi.c
- *
- *    actions.c:
- *
- *    adapted to new enlist_unique arg format
- *
- *    conf loadcfg.c
- *
- *    introduced confdir option
- *
- *    filters.c filtrers.h
- *
- *     extracted-CGI relevant stuff
- *
- *    jbsockets.c
- *
- *     filled comment
- *
- *    jcc.c
- *
- *     support for new cgi mechansim
- *
- *    list.c list.h
- *
- *    functions for new list type: "map"
- *    extended enlist_unique
+ *    Revision 1.7  2001/06/09 10:51:58  jongfoster
+ *    Changing "show URL info" handler to new style.
+ *    Changing BUFSIZ ==> BUFFER_SIZE
  *
- *    miscutil.c .h
- *    introduced bindup()
+ *    Revision 1.6  2001/06/07 23:05:19  jongfoster
+ *    Removing code related to old forward and ACL files.
  *
- *    parsers.c parsers.h
+ *    Revision 1.5  2001/06/05 19:59:16  jongfoster
+ *    Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
  *
- *    deleted const struct interceptors
- *
- *    pcrs.c
- *    added FIXME
- *
- *    project.h
- *
- *    added struct map
- *    added struct http_response
- *    changes struct interceptors to struct cgi_dispatcher
- *    moved HTML stuff to cgi.h
- *
- *    re_filterfile:
- *
- *    changed
- *
- *    showargs.c
- *    NO TIME LEFT
+ *    Revision 1.4  2001/06/04 10:41:52  swa
+ *    show version string of cgi.h and cgi.c
  *
+ *    Revision 1.3  2001/06/03 19:12:16  oes
+ *    introduced new cgi handling
  *
+ *    No revisions before 1.3
  *
  **********************************************************************/
 \f
@@ -106,6 +65,10 @@ const char cgi_rcs[] = "$Id: cgi.c,v 1.3 2001/06/03 19:12:16 oes Exp $";
 #include <ctype.h>
 #include <string.h>
 
+#ifdef _WIN32
+#define snprintf _snprintf
+#endif /* def _WIN32 */
+
 #include "project.h"
 #include "cgi.h"
 #include "list.h"
@@ -121,24 +84,19 @@ const char cgi_rcs[] = "$Id: cgi.c,v 1.3 2001/06/03 19:12:16 oes Exp $";
 
 const char cgi_h_rcs[] = CGI_H_VERSION;
 
-const struct cgi_dispatcher cgi_dispatchers[] = {
+const struct cgi_dispatcher cgi_dispatcher[] = {
    { "show-status", 
          11, cgi_show_status,  
          "Show information about the version and configuration" }, 
-/* { "show-url-info",
+   { "show-url-info",
          13, cgi_show_url_info, 
-         "Show which actions apply to a URL and why"  },*/
+         "Show which actions apply to a URL and why"  },
    { "send-banner",
          11, cgi_send_banner, 
          "HIDE Send the transparent or \"Junkbuster\" gif" },
-#ifdef TRUST_FILES
-/* { "untrusted-url",
-         15, ij_untrusted_url,
-             "HIDE Show why a URL was not trusted" }, */
-#endif /* def TRUST_FILES */
    { "",
          0, cgi_default,
-         "HIDE Send a page linking to all unhidden CGIs" },
+         "Junkbuster main page" },
    { NULL, 0, NULL, NULL }
 };
 
@@ -160,12 +118,12 @@ const struct cgi_dispatcher cgi_dispatchers[] = {
  * Returns     :  http_response if match, NULL if nonmatch or handler fail
  *
  *********************************************************************/
-struct http_response *cgi_dispatch(struct client_state *csp)
+struct http_response *dispatch_cgi(struct client_state *csp)
 {
    char *argstring = NULL;
    const struct cgi_dispatcher *d;
    struct map *param_list;
-   struct http_response *response;
+   struct http_response *rsp;
 
    /*
     * Should we intercept ?
@@ -177,8 +135,8 @@ struct http_response *cgi_dispatch(struct client_state *csp)
       /* ..then the path will all be for us */
       argstring = csp->http->path;
    }
-   /* Or it's the host part of HOME_PAGE_URL ? */
-   else if (   (0 == strcmpic(csp->http->host, *&HOME_PAGE_URL + 7 ))
+   /* Or it's the host part HOME_PAGE_URL, and the path /config ? */
+   else if (   (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 ))
             && (0 == strncmpic(csp->http->path,"/config", 7))
             && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
    {
@@ -191,16 +149,16 @@ struct http_response *cgi_dispatch(struct client_state *csp)
    }
 
    /* 
-    * We have intercepted it.
+    * This is a CGI call.
     */
 
-   /* Get mem for response */
-   if (NULL == ( response = zalloc(sizeof(*response))))
+   /* Get mem for response or fail*/
+   if (NULL == ( rsp = zalloc(sizeof(*rsp))))
    {
       return NULL;
    }
 
-   /* remove any leading slash */
+   /* Remove leading slash */
    if (*argstring == '/')
    {
       argstring++;
@@ -210,22 +168,24 @@ struct http_response *cgi_dispatch(struct client_state *csp)
    log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3", 
                             csp->ip_addr_str, csp->http->cmd); 
 
-   for (d = cgi_dispatchers; d->handler; d++)
+   /* Find and start the right CGI function*/
+   for (d = cgi_dispatcher; d->handler; d++)
    {
       if (strncmp(argstring, d->name, d->name_length) == 0)
       {
-         param_list = parse_cgi(argstring + d->name_length);
-         if ((d->handler)(csp, response, param_list))
-             {
-                freez(response);
-             }
-
-             free_map(param_list);
-             return(response);
+         param_list = parse_cgi_parameters(argstring + d->name_length);
+         if ((d->handler)(csp, rsp, param_list))
+        {
+           freez(rsp);
+        }
+
+        free_map(param_list);
+        return(finish_http_response(rsp));
       }
    }
 
-   freez(response);
+   /* Can't get here, since cgi_default will match all requests */
+   freez(rsp);
    return(NULL);
 
 }
@@ -233,7 +193,7 @@ struct http_response *cgi_dispatch(struct client_state *csp)
 
 /*********************************************************************
  *
- * Function    :  parse_cgi
+ * Function    :  parse_cgi_parameters
  *
  * Description :  Parse a URL-encoded argument string into name/value
  *                pairs and store them in a struct map list.
@@ -244,10 +204,10 @@ struct http_response *cgi_dispatch(struct client_state *csp)
  * Returns     :  poniter to param list, or NULL if failiure
  *
  *********************************************************************/
-struct map *parse_cgi(char *argstring)
+struct map *parse_cgi_parameters(char *argstring)
 {
    char *tmp, *p;
-   char *vector[BUFSIZ];
+   char *vector[BUFFER_SIZE];
    int pairs, i;
    struct map *cgi_params = NULL;
 
@@ -271,217 +231,12 @@ struct map *parse_cgi(char *argstring)
 }
 
 
-/*********************************************************************
- *
- * Function    :  make_http_response
- *
- * Description :  Fill in the missing headers in an http response,
- *                and flatten the headers to an http head.
- *
- * Parameters  :
- *          1  :  rsp = pointer to http_response to be processed
- *
- * Returns     :  length of http head, or 0 on failiure
- *
- *********************************************************************/
-int make_http_response(struct http_response *rsp)
-{
-  char buf[BUFSIZ];
-
-  /* Fill in the HTTP Status */
-  sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
-  enlist_first(rsp->headers, buf);
-
-  /* Set the Content-Length */
-  if (rsp->content_length == 0)
-  {
-     rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
-  }
-
-
-  sprintf(buf, "Content-Length: %d", rsp->content_length);
-  enlist(rsp->headers, buf);
-
-  /* Fill in the default headers FIXME: Are these correct? sequence OK? check rfc! */
-  enlist_unique(rsp->headers, "Pragma: no-cache", 7);
-  enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
-  enlist_unique(rsp->headers, "Expires:       Thu Jul 31, 1997 07:42:22 pm GMT", 8);
-  enlist_unique(rsp->headers, "Content-Type: text/html", 13);
-  enlist(rsp->headers, "");
-  
-
-  /* Write the head */
-  if (NULL == (rsp->head = list_to_text(rsp->headers)))
-  {
-    free_http_response(rsp);
-    return(0);
-  }
-  return(strlen(rsp->head));
-}
-  
-
-/*********************************************************************
- *
- * Function    :  free_http_response
- *
- * Description :  Free the memory occupied by an http_response
- *                and its depandant structures.
- *
- * Parameters  :
- *          1  :  rsp = pointer to http_response to be freed
- *
- * Returns     :  N/A
- *
- *********************************************************************/
-void free_http_response(struct http_response *rsp)
-{
-   if(rsp)
-   {
-      freez(rsp->status);
-      freez(rsp->head);
-      freez(rsp->body);
-      destroy_list(rsp->headers);
-   }
-}
-
-
-/*********************************************************************
- *
- * Function    :  fill_template
- *
- * Description :  CGI support function that loads a given HTML
- *                template from the confdir, and fills it in
- *                by replacing @name@ with value using pcrs,
- *                for each item in the output map.
- *
- * Parameters  :
- *           1 :  csp = Current client state (buffers, headers, etc...)
- *           3 :  template = name of the HTML template to be used
- *           2 :  answers = map with fill in symbol -> name pairs
- *                FIXME: needs better name!
- *
- * Returns     :  char * with filled out form, or NULL if failiure
- *
- *********************************************************************/
-char *fill_template(struct client_state *csp, char *template, struct map *answers)
-{
-   struct map *m;
-   pcrs_job *job, *joblist = NULL;
-   char buf[BUFSIZ];
-   char *new, *old = NULL;
-   int error, size;
-   FILE *fp;
-
-   /*
-    * Open template file or fail
-    */
-   snprintf(buf, BUFSIZ, "%s/templates/%s", csp->config->confdir, template);
-
-   if(NULL == (fp = fopen(buf, "r")))
-       {
-          log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
-      return NULL;
-       }
-       
-   /* 
-    * Assemble pcrs joblist from answers map
-    */
-   for (m = answers; m; m = m->next)
-       {
-          int error;
-
-          snprintf(buf, BUFSIZ, "s°@%s@°%s°ig", m->name, m->value);
-
-          if(NULL == (job = pcrs_make_job(buf, &error)))
-               {
-                 log_error(LOG_LEVEL_ERROR, "Adding template fill job %s failed with error %d",
-                                               buf, error);
-                 while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
-                 return NULL;
-               }
-               else
-               {
-                  job->next = joblist;
-                       joblist = job;
-               }
-       }
-
-   /* 
-    * Read the file, ignoring comments
-    */
-       while (fgets(buf, BUFSIZ, fp))
-       {
-      /* skip lines starting with '#' */
-          if(*buf == '#') continue;
-       
-      old = strsav(old, buf);
-       }
-       fclose(fp);
-
-   /*
-    * Execute the jobs
-    */
-       size = strlen(old) + 1;
-   new = old;
-
-   for (job = joblist; NULL != job; job = job->next)
-   {
-          pcrs_exec_substitution(job, old, size, &new, &size);
-      if (old != buf) free(old);
-      old=new;
-       }
-
-   /*
-    * Free the jobs & return
-    */
-   while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
-   return(new);
-
-}
-
-
-/*********************************************************************
- *
- * Function    :  dump_map
- *
- * Description :  HTML-dump a map for debugging
- *
- * Parameters  :
- *          1  :  map = map to dump
- *
- * Returns     :  string with HTML
- *
- *********************************************************************/
-char *dump_map(struct map *map)
-{
-   struct map *p = map;
-   char *ret = NULL;
-
-
-   ret = strsav(ret, "<table>\n");
-
-   while (p)
-   {
-      ret = strsav(ret, "<tr><td><b>");
-      ret = strsav(ret, p->name);
-      ret = strsav(ret, "</b></td><td>");
-      ret = strsav(ret, p->value);
-      ret = strsav(ret, "</td></tr>\n");
-      p = p->next;
-   }
-
-   ret = strsav(ret, "</table>\n");
-   return(ret);
-}
-
-
 /*********************************************************************
  *
  * Function    :  cgi_default
  *
- * Description :  CGI function that is called if no action was given
- *                lists menu of available unhidden CGIs.
+ * Description :  CGI function that is called if no action was given.
+ *                Lists menu of available unhidden CGIs.
  *               
  * Parameters  :
  *           1 :  csp = Current client state (buffers, headers, etc...)
@@ -495,39 +250,24 @@ int cgi_default(struct client_state *csp, struct http_response *rsp,
                 struct map *parameters)
 {
    char *p, *tmp = NULL;
-   char buf[BUFSIZ];
-   const struct cgi_dispatcher *d;
-       struct map *exports = NULL;
-
-   /* List available unhidden CGI's and export as "other-cgis" */
-   for (d = cgi_dispatchers; d->handler; d++)
-   {
-      if (strncmp(d->description, "HIDE", 4))
-          {
-         snprintf(buf, BUFSIZ, "<li><a href=%s/config/%s>%s</a></li>",
-                                 HOME_PAGE_URL, d->name, d->description);
-         tmp = strsav(tmp, buf);
-      }
-       }
-       exports = map(exports, "other-cgis", 1, tmp, 0);
+   struct map *exports = default_exports(csp, "");
 
    /* If there were other parameters, export a dump as "cgi-parameters" */
    if(parameters)
-       {
+   {
       p = dump_map(parameters);
-          tmp = strsav(tmp, "<p>What made you think this cgi takes options?\n
-                         Anyway, here they are, in case you're interested:</p>\n");
-               tmp = strsav(tmp, p);
-               exports = map(exports, "cgi-parameters", 1, tmp, 0);
+      tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
+                        "Anyway, here they are, in case you're interested:</p>\n");
+      tmp = strsav(tmp, p);
+      exports = map(exports, "cgi-parameters", 1, tmp, 0);
       free(p);
-       }
-       else
-       {
-          exports = map(exports, "cgi-parameters", 1, "", 1);
-       }
+   }
+   else
+   {
+      exports = map(exports, "cgi-parameters", 1, "", 1);
+   }
 
    rsp->body = fill_template(csp, "default", exports);
-
    free_map(exports);
    return(0);
 
@@ -557,13 +297,13 @@ int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
 {
    if(strcmp(lookup(parameters, "type"), "trans"))
    {
-     rsp->body = bindup(CJBGIF, sizeof(CJBGIF));
-     rsp->content_length = sizeof(CJBGIF);
+     rsp->body = bindup(JBGIF, sizeof(JBGIF));
+     rsp->content_length = sizeof(JBGIF);
    }
    else
    {
-     rsp->body = bindup(CBLANKGIF, sizeof(CBLANKGIF));
-     rsp->content_length = sizeof(CBLANKGIF);
+     rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
+     rsp->content_length = sizeof(BLANKGIF);
    }   
 
    enlist(rsp->headers, "Content-Type: image/gif");
@@ -572,53 +312,6 @@ int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
 }
 
 
-#ifdef FAST_REDIRECTS
-/*********************************************************************
- *
- * Function    :  redirect_url
- *
- * Description :  Checks for redirection URLs and returns a HTTP redirect
- *                to the destination URL.
- *
- * Parameters  :
- *          1  :  http = http_request request, check `basename's of blocklist
- *          2  :  csp = Current client state (buffers, headers, etc...)
- *
- * Returns     :  NULL if URL was clean, HTTP redirect otherwise.
- *
- *********************************************************************/
-char *redirect_url(struct http_request *http, struct client_state *csp)
-{
-   char *p, *q;
-
-   p = q = csp->http->path;
-   log_error(LOG_LEVEL_REDIRECTS, "checking path: %s", p);
-
-   /* find the last URL encoded in the request */
-   while (p = strstr(p, "http://"))
-   {
-      q = p++;
-   }
-
-   /* if there was any, generate and return a HTTP redirect */
-   if (q != csp->http->path)
-   {
-      log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
-
-      p = (char *)malloc(strlen(HTTP_REDIRECT_TEMPLATE) + strlen(q));
-      sprintf(p, HTTP_REDIRECT_TEMPLATE, q);
-      return(p);
-   }
-   else
-   {
-      return(NULL);
-   }
-
-}
-#endif /* def FAST_REDIRECTS */
-
-
-
 /*********************************************************************
  *
  * Function    :  cgi_show_status
@@ -642,14 +335,12 @@ int cgi_show_status(struct client_state *csp, struct http_response *rsp,
                     struct map *parameters)
 {
    char *s = NULL;
-   const struct gateway *g;
    int i;
-
-   struct map *exports = NULL;
+   struct map *exports = default_exports(csp, "show-status");
 
 #ifdef SPLIT_PROXY_ARGS
    FILE * fp;
-   char buf[BUFSIZ];
+   char buf[BUFFER_SIZE];
    char * p;
    const char * filename = NULL;
    char * file_description = NULL;
@@ -665,23 +356,6 @@ int cgi_show_status(struct client_state *csp, struct http_response *rsp,
          file_description = "Actions List";
       }
       break;
-   case 'f':
-      if (csp->flist)
-      {
-         filename = csp->flist->filename;
-         file_description = "Forward List";
-      }
-      break;
-
-#ifdef ACL_FILES
-   case 'a':
-      if (csp->alist)
-      {
-         filename = csp->alist->filename;
-         file_description = "Access Control List";
-      }
-      break;
-#endif /* def ACL_FILES */
 
 #ifdef PCRS
    case 'r':
@@ -706,12 +380,12 @@ int cgi_show_status(struct client_state *csp, struct http_response *rsp,
 
    if (NULL != filename)
    {
-          exports = map(exports, "filename", 1, file_description, 1);
+      exports = map(exports, "file-description", 1, file_description, 1);
       exports = map(exports, "filepath", 1, html_encode(filename), 0);
 
       if ((fp = fopen(filename, "r")) == NULL)
       {
-         exports = map(exports, "content", 1, "</pre><h1>ERROR OPENING FILE!</h1><pre>", 1);
+         exports = map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
       }
       else
       {
@@ -727,72 +401,61 @@ int cgi_show_status(struct client_state *csp, struct http_response *rsp,
          }
          fclose(fp);
          exports = map(exports, "contents", 1, s, 0);
-     }
-         rsp->body = fill_template(csp, "show-status-file", exports);;
-         free_map(exports);
-         return(0);
+      }
+      rsp->body = fill_template(csp, "show-status-file", exports);
+      free_map(exports);
+      return(0);
 
    }
 
 #endif /* def SPLIT_PROXY_ARGS */
 
    exports = map(exports, "redirect-url", 1, REDIRECT_URL, 1);
-   exports = map(exports, "version", 1, VERSION, 1);
-   exports = map(exports, "home-page", 1, HOME_PAGE_URL, 1);
-   exports = map(exports, "invocation-args", 1, csp->config->proxy_args_header, 1);
-   exports = map(exports, "gateways", 1, csp->config->proxy_args_gateways, 1);
-   exports = map(exports, "gateway-protocols", 1, s, 0);
+   
+   s = NULL;
+   for (i=0; i < Argc; i++)
+   {
+      s = strsav(s, Argv[i]);
+      s = strsav(s, " ");
+   }
+   exports = map(exports, "invocation", 1, s, 0);
 
+   exports = map(exports, "options", 1, csp->config->proxy_args, 1);
+   s =   show_rcs();
+   exports = map(exports, "sourceversions", 1, s, 0);  
+   s =   show_defines();
+   exports = map(exports, "defines", 1, s, 0); 
 
 #ifdef STATISTICS
-   exports = map(exports, "statistics", 1, add_stats(NULL), 0);
+   exports = add_stats(exports);
 #else
-   exports = map(exports, "statistics", 1, "", 1);
+   exports = map_block_killer(exports, "statistics");
 #endif /* ndef STATISTICS */
 
 #ifdef SPLIT_PROXY_ARGS
+
+   exports = map_block_killer(exports, "no-split-args");
+
    if (csp->actions_list)
    {
       exports = map(exports, "actions-filename", 1,  csp->actions_list->filename, 1);
-       }
+   }
    else
-       {
-          exports = map(exports, "actions-filename", 1, "None specified", 1);
-       }
+   {
+      exports = map(exports, "actions-filename", 1, "None specified", 1);
+   }
 
-   if (csp->flist)
-   {
-      exports = map(exports, "forward-filename", 1,  csp->flist->filename, 1);
-       }
-   else
-       {
-          exports = map(exports, "forward-filename", 1, "None specified", 1);
-       }
-
-#ifdef ACL_FILES
-   if (csp->alist)
-   {
-      exports = map(exports, "acl-filename", 1,  csp->alist->filename, 1);
-       }
-   else
-       {
-          exports = map(exports, "acl-filename", 1, "None specified", 1);
-       }
-#else
-   exports = map(exports, "acl-killer-start.*acl-killer-end", 1, "", 1);
-#endif /* ndef ACL_FILES */
-
-#ifdef PCRS
-   if (csp->rlist)
+#ifdef PCRS
+   if (csp->rlist)
    {
       exports = map(exports, "re-filter-filename", 1,  csp->rlist->filename, 1);
-       }
+   }
    else
-       {
-          exports = map(exports, "re-filter-filename", 1, "None specified", 1);
-       }
+   {
+      exports = map(exports, "re-filter-filename", 1, "None specified", 1);
+   }
 #else
-   exports = map(exports, "re-filter-killer-start.*re-filter-killer-end", 1, "", 1);
+   exports = map_block_killer(exports, "pcrs-support");
 #endif /* ndef PCRS */
 
 #ifdef TRUST_FILES
@@ -805,51 +468,33 @@ int cgi_show_status(struct client_state *csp, struct http_response *rsp,
           exports = map(exports, "trust-filename", 1, "None specified", 1);
        }
 #else
-   exports = map(exports, "acl-killer-start.*acl-killer-end", 1, "", 1);
+   exports = map_block_killer(exports, "trust-support");
 #endif /* ndef TRUST_FILES */
 
-   exports = map(exports, ".list", 1, "" , 1);
-
 #else /* ifndef SPLIT_PROXY_ARGS */
-   exports = map(exports, "magic-eliminator-start.*magic-eliminator-end", 1, "", 1);
+   exports = map_block_killer(exports, "split-args");
 
    if (csp->clist)
    {
       map(exports, "clist", 1, csp->clist->proxy_args , 1);
    }
 
-   if (csp->flist)
-   {
-      map(exports, "flist", 1, csp->flist->proxy_args , 1);
-       }
-
-#ifdef ACL_FILES
-   if (csp->alist)
-   {
-      map(exports, "alist", 1, csp->alist->proxy_args , 1);
-       }
-#endif /* def ACL_FILES */
-
 #ifdef PCRS
    if (csp->rlist)
    {
       map(exports, "rlist", 1, csp->rlist->proxy_args , 1);
-       }
+   }
 #endif /* def PCRS */
 
 #ifdef TRUST_FILES
     if (csp->tlist)
    {
       map(exports, "tlist", 1, csp->tlist->proxy_args , 1);
-       }
+   }
 #endif /* def TRUST_FILES */
 
 #endif /* ndef SPLIT_PROXY_ARGS */
 
-       s = end_proxy_args(csp->config);
-   exports = map(exports, "rcs-and-defines", 1, s , 0);
-
-
    rsp->body = fill_template(csp, "show-status", exports);
    free_map(exports);
    return(0);
@@ -861,51 +506,72 @@ int cgi_show_status(struct client_state *csp, struct http_response *rsp,
  *
  * Function    :  cgi_show_url_info
  *
- * Description :  (please fill me in)
+ * Description :  CGI function that determines and shows which actions
+ *                junkbuster will perform for a given url, and which
+ *                matches starting from the defaults have lead to that.
  *
  * Parameters  :
- *          1  :  http = http_request request for crunched URL
- *          2  :  csp = Current client state (buffers, headers, etc...)
+ *           1 :  csp = Current client state (buffers, headers, etc...)
+ *           2 :  rsp = http_response data structure for output
+ *           3 :  parameters = map of cgi parameters
  *
- * Returns     :  ???FIXME
+ * CGI Parameters :
+ *            url : The url whose actions are to be determined.
+ *                  If url is unset, the url-given conditional will be
+ *                  set, so that all but the form can be suppressed in
+ *                  the template.
+ *
+ * Returns     :  0
  *
  *********************************************************************/
-char *cgi_show_url_info(struct http_request *http, struct client_state *csp)
+int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,\r
+                      struct map *parameters)\r
 {
-   char * query_string = strchr(http->path, '?');
-   char * host = NULL;
-   
-   if (query_string != NULL)
+   struct map *exports = default_exports(csp, "show-url-info");
+   char *url_param, *host = NULL;
+
+   if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
    {
-      query_string = url_decode(query_string + 1);
-      if (strncmpic(query_string, "url=", 4) == 0)
-      {
-         host = strdup(query_string + 4);
-      }
-      freez(query_string);
+      exports = map_block_killer(exports, "url-given");
+      exports = map(exports, "url", 1, "", 1);
    }
-   if (host != NULL)
+   else
    {
-      char * result;
-      char * path;
-      char * s;
+      char *matches = NULL;
+      char *path;
+      char *s;
       int port = 80;
+      int hits = 0;
       struct file_list *fl;
       struct url_actions *b;
       struct url_spec url[1];
       struct current_action_spec action[1];
+      
+      host = url_param;
+      host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
 
-      init_current_action(action);
+      exports = map(exports, "url", 1, host, 1);\r
+      exports = map(exports, "url-html", 1, html_encode(host), 0);\r\r
 
-      result = (char *)malloc(sizeof(C_URL_INFO_HEADER) + 2 * strlen(host));
-      sprintf(result, C_URL_INFO_HEADER, host, host);
+      init_current_action(action);
 
       s = current_action_to_text(action);
-      result = strsav(result, "<h3>Defaults:</h3>\n<p><b>{");
-      result = strsav(result, s);
-      result = strsav(result, " }</b></p>\n<h3>Patterns affecting the URL:</h3>\n<p>\n");
-      freez(s);
-
+      exports = map(exports, "default", 1, s , 0);\r
+
+      if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))\r
+      {\r
+         exports = map(exports, "matches", 1, "none" , 1);\r
+         exports = map(exports, "final", 1, lookup(exports, "default"), 1);\r
+\r
+         freez(url_param);\r
+         free_current_action(action);\r
+\r
+         rsp->body = fill_template(csp, "show-url-info", exports);\r
+         free_map(exports);\r
+\r
+         return 0;\r
+      }\r
+\r
       s = strchr(host, '/');
       if (s != NULL)
       {
@@ -921,14 +587,7 @@ char *cgi_show_url_info(struct http_request *http, struct client_state *csp)
       {
          *s++ = '\0';
          port = atoi(s);
-      }
-
-      if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
-      {
-         freez(host);
-         freez(path);
-         result = strsav(result, C_URL_INFO_FOOTER);
-         return result;
+         s = NULL;\r
       }
 
       *url = dsplit(host);
@@ -936,10 +595,17 @@ char *cgi_show_url_info(struct http_request *http, struct client_state *csp)
       /* if splitting the domain fails, punt */
       if (url->dbuf == NULL)
       {
-         freez(host);
-         freez(path);
-         result = strsav(result, C_URL_INFO_FOOTER);
-         return result;
+         exports = map(exports, "matches", 1, "none" , 1);\r
+         exports = map(exports, "final", 1, lookup(exports, "default"), 1);\r
+\r
+         freez(url_param);\r
+         freez(path);\r
+         free_current_action(action);\r
+\r
+         rsp->body = fill_template(csp, "show-url-info", exports);\r
+         free_map(exports);\r
+\r
+         return 0;\r
       }
 
       for (b = b->next; NULL != b; b = b->next)
@@ -957,157 +623,428 @@ char *cgi_show_url_info(struct http_request *http, struct client_state *csp)
                )
                {
                   s = actions_to_text(b->action);
-                  result = strsav(result, "<b>{");
-                  result = strsav(result, s);
-                  result = strsav(result, " }</b><br>\n<code>");
-                  result = strsav(result, b->url->spec);
-                  result = strsav(result, "</code><br>\n<br>\n");
+                  matches = strsav(matches, "<b>{");
+                  matches = strsav(matches, s);
+                  matches = strsav(matches, " }</b><br>\n<code>");
+                  matches = strsav(matches, b->url->spec);
+                  matches = strsav(matches, "</code><br>\n<br>\n");
                   freez(s);
 
                   merge_current_action(action, b->action);
+                  hits++;
                }
             }
          }
       }
 
+      if (hits)
+      {
+         exports = map(exports, "matches", 1, matches , 0);
+      }
+      else
+      {
+         exports = map(exports, "matches", 1, "none", 1);
+      }
+      matches = NULL;\r
+\r
       freez(url->dbuf);
       freez(url->dvec);
 
-      freez(host);
+      freez(url_param);
       freez(path);
-
+\r
       s = current_action_to_text(action);
-      result = strsav(result, "</p>\n<h2>Final Results:</h2>\n<p><b>{");
-      result = strsav(result, s);
-      result = strsav(result, " }</b><br>\n<br>\n");
-      freez(s);
+      exports = map(exports, "final", 1, s, 0);\r
+      s = NULL;\r
 
       free_current_action(action);
-
-      result = strsav(result, C_URL_INFO_FOOTER);
-      return result;
    }
-   else
+
+   rsp->body = fill_template(csp, "show-url-info", exports);\r
+   free_map(exports);\r
+   return 0;
+
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  error_response
+ *
+ * Description :  returns an http_response that explains the reason
+ *                why a request failed.
+ *
+ * Parameters  :
+ *          1  :  csp = Current client state (buffers, headers, etc...)
+ *          2  :  template = Which template should be used for the answer
+ *          3  :  errno = system error number
+ *
+ * Returns     :  NULL if no memory, else http_response
+ *
+ *********************************************************************/
+struct http_response *error_response(struct client_state *csp, const char *template, int err)
+{
+   struct http_response *rsp;
+   struct map *exports = default_exports(csp, NULL);
+
+   if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
    {
-      return strdup(C_URL_INFO_FORM);
-   }
+      return NULL;
+   }  
+
+      exports = map(exports, "host-html", 1, html_encode(csp->http->host), 0);
+      exports = map(exports, "hostport", 1, csp->http->hostport, 1);
+      exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
+      exports = map(exports, "path", 1, csp->http->path, 1);
+      exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
+      exports = map(exports, "error", 1, safe_strerror(err), 0);
+      exports = map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
+
+      rsp->body = fill_template(csp, template, exports);
+      free_map(exports);
+      
+      if (!strcmp(template, "no-such-domain"))
+      {
+         rsp->status = strdup("404 No such domain"); 
+      }
+      else if (!strcmp(template, "connect-failed"))
+      {
+         rsp->status = strdup("503 Connect failed");
+      }
+
+      return(finish_http_response(rsp));
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  finish_http_response
+ *
+ * Description :  Fill in the missing headers in an http response,
+ *                and flatten the headers to an http head.
+ *
+ * Parameters  :
+ *          1  :  rsp = pointer to http_response to be processed
+ *
+ * Returns     :  http_response, or NULL on failiure
+ *
+ *********************************************************************/
+struct http_response *finish_http_response(struct http_response *rsp)
+{
+  char buf[BUFFER_SIZE];
+
+  /* 
+   * Fill in the HTTP Status
+   */
+  sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
+  enlist_first(rsp->headers, buf);
+
+  /* 
+   * Set the Content-Length
+   */
+  if (rsp->content_length == 0)
+  {
+     rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
+  }
+  sprintf(buf, "Content-Length: %d", rsp->content_length);
+  enlist(rsp->headers, buf);
+
+  /* 
+   * Fill in the default headers FIXME: Are these correct? sequence OK? check rfc!
+   */
+  enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
+  enlist_unique(rsp->headers, "Expires:       Thu Jul 31, 1997 07:42:22 pm GMT", 8);
+  enlist_unique(rsp->headers, "Content-Type: text/html", 13);
+  enlist(rsp->headers, "");
+  
+
+  /* 
+   * Write the head
+   */
+  if (NULL == (rsp->head = list_to_text(rsp->headers)))
+  {
+    free_http_response(rsp);
+    return(NULL);
+  }
+  rsp->head_length = strlen(rsp->head);
+
+  return(rsp);
+
+}
+  
 
-#ifdef TRUST_FILES
 /*********************************************************************
  *
- * Function    :  ij_untrusted_url
+ * Function    :  free_http_response
  *
- * Description :  This "crunch"es "http:/any.thing/ij-untrusted-url" and
- *                returns a web page describing why it was untrusted.
+ * Description :  Free the memory occupied by an http_response
+ *                and its depandant structures.
  *
  * Parameters  :
- *          1  :  http = http_request request for crunched URL
- *          2  :  csp = Current client state (buffers, headers, etc...)
+ *          1  :  rsp = pointer to http_response to be freed
  *
- * Returns     :  A string that contains why this was untrusted.
+ * Returns     :  N/A
  *
  *********************************************************************/
-char *ij_untrusted_url(struct http_request *http, struct client_state *csp)
+void free_http_response(struct http_response *rsp)
 {
-   int n;
-   char *hostport, *path, *refer, *p, *v[9];
-   char buf[BUFSIZ];
-   struct url_spec **tl, *t;
-
-
-   static const char format[] =
-      "HTTP/1.0 200 OK\r\n"
-      "Pragma: no-cache\n"
-      "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
-      "Expires:       Thu Jul 31, 1997 07:42:22 pm GMT\n"
-      "Content-Type: text/html\n\n"
-      "<html>\n"
-      "<head>\n"
-      "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
-      "</head>\n"
-      BODY
-      "<center><h1>"
-      BANNER
-      "</h1></center>"
-      "The " BANNER " Proxy "
-      "<A href=\"" HOME_PAGE_URL "\">"
-      "(" HOME_PAGE_URL ") </A>"
-      "intercepted the request for %s%s\n"
-      "because the URL is not trusted.\n"
-      "<br><br>\n";
-
-   if ((n = ssplit(http->path, "?+", v, SZ(v), 0, 0)) == 4)
+   if(rsp)
    {
-      hostport = url_decode(v[1]);
-      path     = url_decode(v[2]);
-      refer    = url_decode(v[3]);
+      freez(rsp->status);
+      freez(rsp->head);
+      freez(rsp->body);
+      destroy_list(rsp->headers);
+      freez(rsp);
    }
-   else
+
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  fill_template
+ *
+ * Description :  CGI support function that loads a given HTML
+ *                template from the confdir, and fills it in
+ *                by replacing @name@ with value using pcrs,
+ *                for each item in the output map.
+ *
+ * Parameters  :
+ *           1 :  csp = Current client state (buffers, headers, etc...)
+ *           3 :  template = name of the HTML template to be used
+ *           2 :  exports = map with fill in symbol -> name pairs
+ *
+ * Returns     :  char * with filled out form, or NULL if failiure
+ *
+ *********************************************************************/
+char *fill_template(struct client_state *csp, const char *template, struct map *exports)
+{
+   struct map *m;
+   pcrs_job *job, *joblist = NULL;
+   char buf[BUFFER_SIZE];
+   char *new, *old = NULL;
+   int size;
+   FILE *fp;
+
+
+   /*
+    * Open template file or fail
+    */
+   snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, template);
+
+   if(NULL == (fp = fopen(buf, "r")))
    {
-      hostport = strdup("undefined_host");
-      path     = strdup("/undefined_path");
-      refer    = strdup("undefined");
+      log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
+      return NULL;
+   }
+       
+
+   /* 
+    * Assemble pcrs joblist from exports map
+    */
+   for (m = exports; m; m = m->next)
+   {
+      int error;
+
+      /* Enclose name in @@ */
+      snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
+
+      /* Make and chain in job */
+      if ( NULL == (job = (pcrs_make_job(buf, m->value, "sigTU", &error))) ) 
+      {
+         log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
+      }
+      else
+      {
+         job->next = joblist;
+         joblist = job;
+      }
    }
 
-   n  = sizeof(format);
-   n += strlen(hostport);
-   n += strlen(path    );
 
-   if ((p = (char *)malloc(n)))
+   /* 
+    * Read the file, ignoring comments
+    */
+   while (fgets(buf, BUFFER_SIZE, fp))
    {
-      sprintf(p, format, hostport, path);
+      /* skip lines starting with '#' */
+      if(*buf == '#') continue;
+       
+      old = strsav(old, buf);
+   }
+   fclose(fp);
+
+
+   /*
+    * Execute the jobs
+    */
+   size = strlen(old) + 1;
+   new = old;
+
+   for (job = joblist; NULL != job; job = job->next)
+   {
+      pcrs_execute(job, old, size, &new, &size);
+      if (old != buf) free(old);
+      old = new;
    }
 
-   strsav(p, "The referrer in this request was <strong>");
-   strsav(p, refer);
-   strsav(p, "</strong><br>\n");
 
-   freez(hostport);
-   freez(path    );
-   freez(refer   );
+   /*
+    * Free the jobs & return
+    */
+   pcrs_free_joblist(joblist);
+   return(new);
+
+}
+
 
-   p = strsav(p, "<h3>The following referrers are trusted</h3>\n");
+/*********************************************************************
+ *
+ * Function    :  default_exports
+ *
+ * Description :  returns a struct map list that contains exports
+ *                which are common to all CGI functions.
+ *
+ * Parameters  :
+ *          1  :  csp = Current client state (buffers, headers, etc...)
+ *          2  :  caller = name of CGI who calls us and which should
+ *                         be excluded from the generated menu.
+ * Returns     :  NULL if no memory, else map
+ *
+ *********************************************************************/
+struct map *default_exports(struct client_state *csp, char *caller)
+{
+   struct map *exports = NULL;
+   char buf[20];
 
-   for (tl = csp->config->trust_list; (t = *tl) ; tl++)
+   exports = map(exports, "version", 1, VERSION, 1);
+   exports = map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1);
+   exports = map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1);
+   exports = map(exports, "admin-address", 1, csp->config->admin_address ? csp->config->admin_address : "fill@me.in.please", 1);
+   exports = map(exports, "homepage", 1, HOME_PAGE_URL, 1);
+   exports = map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1);
+   exports = map(exports, "menu", 1, make_menu(caller), 0);
+   exports = map(exports, "code-status", 1, CODE_STATUS, 1);
+
+   snprintf(buf, 20, "%d", csp->config->hport);
+   exports = map(exports, "my-port", 1, buf, 1);
+
+   if(!strcmp(CODE_STATUS, "stable"))
    {
-      sprintf(buf, "%s<br>\n", t->spec);
-      p = strsav(p, buf);
+      exports = map_block_killer(exports, "unstable");
    }
 
-   if (csp->config->trust_info->next)
+   if(csp->config->proxy_info_url != NULL)
    {
-      struct list *l;
+      exports = map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
+   }
+   else
+   {
+      exports = map_block_killer(exports, "have-proxy-info");
+   }   
 
-      strcpy(buf,
-         "<p>"
-         "You can learn more about what this means "
-         "and what you may be able to do about it by "
-         "reading the following documents:<br>\n"
-         "<ol>\n"
-      );
+   return(exports);
+
+}
 
-      p = strsav(p, buf);
 
-      for (l = csp->config->trust_info->next; l ; l = l->next)
+/*********************************************************************
+ *
+ * Function    :  map_block_killer
+ *
+ * Description :  Convenience function.
+ *                Adds a "killer" for the conditional HTML-template
+ *                block <name>, i.e. a substitution of the regex
+ *                "if-<name>-start.*if-<name>-end" to the given
+ *                export list.
+ *
+ * Parameters  :  
+ *          1  :  exports = map to extend
+ *          2  :  name = name of conditional block
+ *
+ * Returns     :  extended map
+ *
+ *********************************************************************/
+struct map *map_block_killer(struct map *exports, char *name)
+{
+   char buf[1000]; /* Will do, since the names are hardwired */
+
+   snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
+   exports = map(exports, buf, 1, "", 1);
+
+   return(exports);
+
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  make_menu
+ *
+ * Description :  Returns an HTML-formatted menu of the available 
+ *                unhidden CGIs, excluding the one given in <self>.
+ *
+ * Parameters  :  self = name of CGI to leave out, can be NULL
+ *
+ * Returns     :  menu string
+ *
+ *********************************************************************/
+char *make_menu(const char *self)
+{
+   const struct cgi_dispatcher *d;
+   char buf[BUFFER_SIZE], *tmp = NULL;
+
+   if (self == NULL) self = "NO-SUCH-CGI!";
+
+   /* List available unhidden CGI's and export as "other-cgis" */
+   for (d = cgi_dispatcher; d->handler; d++)
+   {
+      if (strncmp(d->description, "HIDE", 4) && strcmp(d->name, self))
       {
-         sprintf(buf,
-            "<li> <a href=%s>%s</a><br>\n",
-               l->str, l->str);
-         p = strsav(p, buf);
+         snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
+                      HOME_PAGE_URL, d->name, d->description);
+         tmp = strsav(tmp, buf);
       }
-
-      p = strsav(p, "</ol>\n");
    }
+   return(tmp);
+
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  dump_map
+ *
+ * Description :  HTML-dump a map for debugging
+ *
+ * Parameters  :
+ *          1  :  map = map to dump
+ *
+ * Returns     :  string with HTML
+ *
+ *********************************************************************/
+char *dump_map(struct map *map)
+{
+   struct map *p = map;
+   char *ret = NULL;
+
 
-   p = strsav(p, "</body>\n" "</html>\n");
+   ret = strsav(ret, "<table>\n");
+
+   while (p)
+   {
+      ret = strsav(ret, "<tr><td><b>");
+      ret = strsav(ret, p->name);
+      ret = strsav(ret, "</b></td><td>");
+      ret = strsav(ret, p->value);
+      ret = strsav(ret, "</td></tr>\n");
+      p = p->next;
+   }
 
-   return(p);
+   ret = strsav(ret, "</table>\n");
+   return(ret);
 
 }
-#endif /* def TRUST_FILES */
 
 
 #ifdef STATISTICS
@@ -1115,24 +1052,18 @@ char *ij_untrusted_url(struct http_request *http, struct client_state *csp)
  *
  * Function    :  add_stats
  *
- * Description :  Statistics function of JB.  Called by `show_proxy_args'.
+ * Description :  Add the blocking statistics to a given map.
  *
  * Parameters  :
- *          1  :  s = string that holds the proxy args description page
+ *          1  :  exports = map to write to.
  *
- * Returns     :  A pointer to the descriptive status web page.
+ * Returns     :  pointer to extended map
  *
  *********************************************************************/
-char *add_stats(char *s)
+struct map *add_stats(struct map *exports)
 {
-   /*
-    * Output details of the number of requests rejected and
-    * accepted. This is switchable in the junkbuster config.
-    * Does nothing if this option is not enabled.
-    */
-
    float perc_rej;   /* Percentage of http requests rejected */
-   char out_str[81];
+   char buf[1000];
    int local_urls_read     = urls_read;
    int local_urls_rejected = urls_rejected;
 
@@ -1146,30 +1077,29 @@ char *add_stats(char *s)
     * urls_rejected--; * This will be incremented subsequently *
     */
 
-   s = strsav(s,"<h2>Statistics for this " BANNER ":</h2>\n");
-
    if (local_urls_read == 0)
    {
-
-      s = strsav(s,"No activity so far!\n");
-
+      exports = map_block_killer(exports, "have-stats");
    }
    else
    {
+      exports = map_block_killer(exports, "have-no-stats");
 
       perc_rej = (float)local_urls_rejected * 100.0F /
             (float)local_urls_read;
 
-      sprintf(out_str,
-         "%d requests received, %d filtered "
-         "(%6.2f %%).",
-         local_urls_read, 
-         local_urls_rejected, perc_rej);
+      sprintf(buf, "%d", local_urls_read);
+      exports = map(exports, "requests-received", 1, buf, 1);
 
-      s = strsav(s,out_str);
+      sprintf(buf, "%d", local_urls_rejected);
+      exports = map(exports, "requests-blocked", 1, buf, 1);
+
+      sprintf(buf, "%6.2f", perc_rej);
+      exports = map(exports, "percent-blocked", 1, buf, 1);
    }
 
-   return(s);
+   return(exports);
+
 }
 #endif /* def STATISTICS */