Indentation, CRLF->LF, Tab-> Space
[privoxy.git] / cgi.c
diff --git a/cgi.c b/cgi.c
index 00cbc3b..55935c2 100644 (file)
--- a/cgi.c
+++ b/cgi.c
@@ -1,4 +1,4 @@
-const char cgi_rcs[] = "$Id: cgi.c,v 1.7 2001/06/09 10:51:58 jongfoster Exp $";
+const char cgi_rcs[] = "$Id: cgi.c,v 1.8 2001/06/29 13:21:46 oes Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgi.c,v $
@@ -36,6 +36,60 @@ const char cgi_rcs[] = "$Id: cgi.c,v 1.7 2001/06/09 10:51:58 jongfoster Exp $";
  *
  * Revisions   :
  *    $Log: cgi.c,v $
+ *    Revision 1.8  2001/06/29 13:21:46  oes
+ *    - Cosmetics: renamed and reordered functions, variables,
+ *      texts, improved comments  etc
+ *
+ *    - Removed ij_untrusted_url() The relevant
+ *      info is now part of the "untrusted" page,
+ *      which is generated by filters.c:trust_url()
+ *
+ *    - Generators of content now call finish_http_response()
+ *      themselves, making jcc.c:chat() a little less
+ *      cluttered
+ *
+ *    - Removed obsolete "Pragma: no-cache" from our headers
+ *
+ *    - http_responses now know their head length
+ *
+ *    - fill_template now uses the new interface to pcrs, so that
+ *       - long jobs (like whole files) no longer have to be assembled
+ *         in a fixed size buffer
+ *       - the new T (trivial) option is used, and the replacement may
+ *         contain Perl syntax backrefs without confusing pcrs
+ *
+ *    - Introduced default_exports() which generates a set of exports
+ *      common to all CGIs and other content generators
+ *
+ *    - Introduced convenience function map_block_killer()
+ *
+ *    - Introduced convenience function make_menu()
+ *
+ *    - Introduced CGI-like function error_response() which generates
+ *      the "No such domain" and "Connect failed" messages using the
+ *      CGI platform
+ *
+ *    - cgi_show_url_info:
+ *      - adapted to new CGI features
+ *      - form and answers now generated from same template
+ *      - http:// prefix in URL now OK
+ *
+ *    - cgi_show_status:
+ *      - adapted to new CGI features
+ *      - no longer uses csp->init_proxy_args
+ *
+ *    - cgi_default:
+ *      - moved menu generation to make_menu()
+ *
+ *    - add_stats now writes single export map entries instead
+ *      of a fixed string
+ *
+ *    - Moved redirect_url() to filters.c
+ *
+ *    - Fixed mem leak in free_http_response(), map_block_killer(),
+ *
+ *    - Removed logentry from cancelled commit
+ *
  *    Revision 1.7  2001/06/09 10:51:58  jongfoster
  *    Changing "show URL info" handler to new style.
  *    Changing BUFSIZ ==> BUFFER_SIZE
@@ -175,12 +229,12 @@ struct http_response *dispatch_cgi(struct client_state *csp)
       {
          param_list = parse_cgi_parameters(argstring + d->name_length);
          if ((d->handler)(csp, rsp, param_list))
-        {
-           freez(rsp);
-        }
+         {
+            freez(rsp);
+         }
 
-        free_map(param_list);
-        return(finish_http_response(rsp));
+         free_map(param_list);
+         return(finish_http_response(rsp));
       }
    }
 
@@ -297,18 +351,19 @@ int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
 {
    if(strcmp(lookup(parameters, "type"), "trans"))
    {
-     rsp->body = bindup(JBGIF, sizeof(JBGIF));
-     rsp->content_length = sizeof(JBGIF);
+      rsp->body = bindup(JBGIF, sizeof(JBGIF));
+      rsp->content_length = sizeof(JBGIF);
    }
    else
    {
-     rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
-     rsp->content_length = sizeof(BLANKGIF);
+      rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
+      rsp->content_length = sizeof(BLANKGIF);
    }   
 
    enlist(rsp->headers, "Content-Type: image/gif");
 
    return(0);
+
 }
 
 
@@ -462,11 +517,11 @@ int cgi_show_status(struct client_state *csp, struct http_response *rsp,
    if (csp->tlist)
    {
       exports = map(exports, "trust-filename", 1,  csp->tlist->filename, 1);
-       }
+   }
    else
-       {
-          exports = map(exports, "trust-filename", 1, "None specified", 1);
-       }
+   {
+       exports = map(exports, "trust-filename", 1, "None specified", 1);
+   }
 #else
    exports = map_block_killer(exports, "trust-support");
 #endif /* ndef TRUST_FILES */
@@ -524,8 +579,8 @@ int cgi_show_status(struct client_state *csp, struct http_response *rsp,
  * Returns     :  0
  *
  *********************************************************************/
-int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,\r
-                      struct map *parameters)\r
+int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
+                      struct map *parameters)
 {
    struct map *exports = default_exports(csp, "show-url-info");
    char *url_param, *host = NULL;
@@ -550,28 +605,28 @@ int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
       host = url_param;
       host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
 
-      exports = map(exports, "url", 1, host, 1);\r
-      exports = map(exports, "url-html", 1, html_encode(host), 0);\r\r
+      exports = map(exports, "url", 1, host, 1);
+      exports = map(exports, "url-html", 1, html_encode(host), 0);
 
       init_current_action(action);
 
       s = current_action_to_text(action);
-      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
+      exports = map(exports, "default", 1, s , 0);
+
+      if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
+      {
+         exports = map(exports, "matches", 1, "none" , 1);
+         exports = map(exports, "final", 1, lookup(exports, "default"), 1);
+
+         freez(url_param);
+         free_current_action(action);
+
+         rsp->body = fill_template(csp, "show-url-info", exports);
+         free_map(exports);
+
+         return 0;
+      }
+
       s = strchr(host, '/');
       if (s != NULL)
       {
@@ -587,7 +642,7 @@ int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
       {
          *s++ = '\0';
          port = atoi(s);
-         s = NULL;\r
+         s = NULL;
       }
 
       *url = dsplit(host);
@@ -595,17 +650,17 @@ int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
       /* if splitting the domain fails, punt */
       if (url->dbuf == NULL)
       {
-         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
+         exports = map(exports, "matches", 1, "none" , 1);
+         exports = map(exports, "final", 1, lookup(exports, "default"), 1);
+
+         freez(url_param);
+         freez(path);
+         free_current_action(action);
+
+         rsp->body = fill_template(csp, "show-url-info", exports);
+         free_map(exports);
+
+         return 0;
       }
 
       for (b = b->next; NULL != b; b = b->next)
@@ -645,23 +700,23 @@ int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
       {
          exports = map(exports, "matches", 1, "none", 1);
       }
-      matches = NULL;\r
-\r
+      matches = NULL;
+
       freez(url->dbuf);
       freez(url->dvec);
 
       freez(url_param);
       freez(path);
-\r
+
       s = current_action_to_text(action);
-      exports = map(exports, "final", 1, s, 0);\r
-      s = NULL;\r
+      exports = map(exports, "final", 1, s, 0);
+      s = NULL;
 
       free_current_action(action);
    }
 
-   rsp->body = fill_template(csp, "show-url-info", exports);\r
-   free_map(exports);\r
+   rsp->body = fill_template(csp, "show-url-info", exports);
+   free_map(exports);
    return 0;
 
 }
@@ -731,44 +786,44 @@ struct http_response *error_response(struct client_state *csp, const char *templ
  *********************************************************************/
 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, "");
+   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);
+   /* 
+    * 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);
+   return(rsp);
 
 }
   
@@ -837,7 +892,7 @@ char *fill_template(struct client_state *csp, const char *template, struct map *
       log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
       return NULL;
    }
-       
+   
 
    /* 
     * Assemble pcrs joblist from exports map
@@ -869,7 +924,7 @@ char *fill_template(struct client_state *csp, const char *template, struct map *
    {
       /* skip lines starting with '#' */
       if(*buf == '#') continue;
-       
+   
       old = strsav(old, buf);
    }
    fclose(fp);
@@ -1002,7 +1057,7 @@ char *make_menu(const char *self)
       if (strncmp(d->description, "HIDE", 4) && strcmp(d->name, self))
       {
          snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
-                      HOME_PAGE_URL, d->name, d->description);
+              HOME_PAGE_URL, d->name, d->description);
          tmp = strsav(tmp, buf);
       }
    }
@@ -1028,7 +1083,6 @@ char *dump_map(struct map *map)
    struct map *p = map;
    char *ret = NULL;
 
-
    ret = strsav(ret, "<table>\n");
 
    while (p)