1 const char cgi_rcs[] = "$Id: cgi.c,v 1.13 2001/07/30 22:08:36 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgi.c,v $
6 * Purpose : Declares functions to intercept request, generate
7 * html or gif answers, and to compose HTTP resonses.
9 * Functions declared include:
12 * Copyright : Written by and Copyright (C) 2001 the SourceForge
13 * IJBSWA team. http://ijbswa.sourceforge.net
15 * Based on the Internet Junkbuster originally written
16 * by and Copyright (C) 1997 Anonymous Coders and
17 * Junkbusters Corporation. http://www.junkbusters.com
19 * This program is free software; you can redistribute it
20 * and/or modify it under the terms of the GNU General
21 * Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at
23 * your option) any later version.
25 * This program is distributed in the hope that it will
26 * be useful, but WITHOUT ANY WARRANTY; without even the
27 * implied warranty of MERCHANTABILITY or FITNESS FOR A
28 * PARTICULAR PURPOSE. See the GNU General Public
29 * License for more details.
31 * The GNU General Public License should be included with
32 * this file. If not, you can view it at
33 * http://www.gnu.org/copyleft/gpl.html
34 * or write to the Free Software Foundation, Inc., 59
35 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 * Revision 1.13 2001/07/30 22:08:36 jongfoster
40 * Tidying up #defines:
41 * - All feature #defines are now of the form FEATURE_xxx
42 * - Permanently turned off WIN_GUI_EDIT
43 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
45 * Revision 1.12 2001/07/29 18:47:05 jongfoster
46 * Adding missing #include "loadcfg.h"
48 * Revision 1.11 2001/07/18 17:24:37 oes
49 * Changed to conform to new pcrs interface
51 * Revision 1.10 2001/07/13 13:53:13 oes
52 * Removed all #ifdef PCRS and related code
54 * Revision 1.9 2001/06/29 21:45:41 oes
55 * Indentation, CRLF->LF, Tab-> Space
57 * Revision 1.8 2001/06/29 13:21:46 oes
58 * - Cosmetics: renamed and reordered functions, variables,
59 * texts, improved comments etc
61 * - Removed ij_untrusted_url() The relevant
62 * info is now part of the "untrusted" page,
63 * which is generated by filters.c:trust_url()
65 * - Generators of content now call finish_http_response()
66 * themselves, making jcc.c:chat() a little less
69 * - Removed obsolete "Pragma: no-cache" from our headers
71 * - http_responses now know their head length
73 * - fill_template now uses the new interface to pcrs, so that
74 * - long jobs (like whole files) no longer have to be assembled
75 * in a fixed size buffer
76 * - the new T (trivial) option is used, and the replacement may
77 * contain Perl syntax backrefs without confusing pcrs
79 * - Introduced default_exports() which generates a set of exports
80 * common to all CGIs and other content generators
82 * - Introduced convenience function map_block_killer()
84 * - Introduced convenience function make_menu()
86 * - Introduced CGI-like function error_response() which generates
87 * the "No such domain" and "Connect failed" messages using the
90 * - cgi_show_url_info:
91 * - adapted to new CGI features
92 * - form and answers now generated from same template
93 * - http:// prefix in URL now OK
96 * - adapted to new CGI features
97 * - no longer uses csp->init_proxy_args
100 * - moved menu generation to make_menu()
102 * - add_stats now writes single export map entries instead
105 * - Moved redirect_url() to filters.c
107 * - Fixed mem leak in free_http_response(), map_block_killer(),
109 * - Removed logentry from cancelled commit
111 * Revision 1.7 2001/06/09 10:51:58 jongfoster
112 * Changing "show URL info" handler to new style.
113 * Changing BUFSIZ ==> BUFFER_SIZE
115 * Revision 1.6 2001/06/07 23:05:19 jongfoster
116 * Removing code related to old forward and ACL files.
118 * Revision 1.5 2001/06/05 19:59:16 jongfoster
119 * Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
121 * Revision 1.4 2001/06/04 10:41:52 swa
122 * show version string of cgi.h and cgi.c
124 * Revision 1.3 2001/06/03 19:12:16 oes
125 * introduced new cgi handling
127 * No revisions before 1.3
129 **********************************************************************/
135 #include <sys/types.h>
141 #define snprintf _snprintf
142 #endif /* def _WIN32 */
153 #include "miscutil.h"
154 #include "showargs.h"
157 const char cgi_h_rcs[] = CGI_H_VERSION;
159 const struct cgi_dispatcher cgi_dispatcher[] = {
162 "Show information about the version and configuration" },
164 13, cgi_show_url_info,
165 "Show which actions apply to a URL and why" },
168 "HIDE Send the transparent or \"Junkbuster\" gif" },
171 "Junkbuster main page" },
172 { NULL, 0, NULL, NULL }
176 /*********************************************************************
178 * Function : dispatch_cgi
180 * Description : Checks if a request URL has either the magical hostname
181 * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses
182 * the (rest of the) path as a cgi name plus query string,
183 * prepares a map that maps CGI parameter names to their values,
184 * initializes the http_response struct, and calls the
185 * relevant CGI handler function.
188 * 1 : csp = Current client state (buffers, headers, etc...)
190 * Returns : http_response if match, NULL if nonmatch or handler fail
192 *********************************************************************/
193 struct http_response *dispatch_cgi(struct client_state *csp)
195 char *argstring = NULL;
196 const struct cgi_dispatcher *d;
197 struct map *param_list;
198 struct http_response *rsp;
201 * Should we intercept ?
204 /* Either the host matches CGI_PREFIX_HOST ..*/
205 if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST))
207 /* ..then the path will all be for us */
208 argstring = csp->http->path;
210 /* Or it's the host part HOME_PAGE_URL, and the path /config ? */
211 else if ( (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 ))
212 && (0 == strncmpic(csp->http->path,"/config", 7))
213 && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
215 /* then it's everything following "/config" */
216 argstring = csp->http->path + 7;
224 * This is a CGI call.
227 /* Get mem for response or fail*/
228 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
233 /* Remove leading slash */
234 if (*argstring == '/')
239 log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
240 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
241 csp->ip_addr_str, csp->http->cmd);
243 /* Find and start the right CGI function*/
244 for (d = cgi_dispatcher; d->handler; d++)
246 if (strncmp(argstring, d->name, d->name_length) == 0)
248 param_list = parse_cgi_parameters(argstring + d->name_length);
249 if ((d->handler)(csp, rsp, param_list))
254 free_map(param_list);
255 return(finish_http_response(rsp));
259 /* Can't get here, since cgi_default will match all requests */
266 /*********************************************************************
268 * Function : parse_cgi_parameters
270 * Description : Parse a URL-encoded argument string into name/value
271 * pairs and store them in a struct map list.
274 * 1 : string = string to be parsed
276 * Returns : poniter to param list, or NULL if failiure
278 *********************************************************************/
279 struct map *parse_cgi_parameters(char *argstring)
282 char *vector[BUFFER_SIZE];
284 struct map *cgi_params = NULL;
286 if(*argstring == '?') argstring++;
287 tmp = strdup(argstring);
289 pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1);
291 for (i = 0; i < pairs; i++)
293 if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
296 cgi_params = map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
306 /*********************************************************************
308 * Function : cgi_default
310 * Description : CGI function that is called if no action was given.
311 * Lists menu of available unhidden CGIs.
314 * 1 : csp = Current client state (buffers, headers, etc...)
315 * 2 : rsp = http_response data structure for output
316 * 3 : parameters = map of cgi parameters
320 *********************************************************************/
321 int cgi_default(struct client_state *csp, struct http_response *rsp,
322 struct map *parameters)
324 char *p, *tmp = NULL;
325 struct map *exports = default_exports(csp, "");
327 /* If there were other parameters, export a dump as "cgi-parameters" */
330 p = dump_map(parameters);
331 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
332 "Anyway, here they are, in case you're interested:</p>\n");
333 tmp = strsav(tmp, p);
334 exports = map(exports, "cgi-parameters", 1, tmp, 0);
339 exports = map(exports, "cgi-parameters", 1, "", 1);
342 rsp->body = fill_template(csp, "default", exports);
349 /*********************************************************************
351 * Function : cgi_send_banner
353 * Description : CGI function that returns a banner.
356 * 1 : csp = Current client state (buffers, headers, etc...)
357 * 2 : rsp = http_response data structure for output
358 * 3 : parameters = map of cgi parameters
361 * type : Selects the type of banner between "trans" and "jb".
362 * Defaults to "jb" if absent or != "trans".
366 *********************************************************************/
367 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
368 struct map *parameters)
370 if(strcmp(lookup(parameters, "type"), "trans"))
372 rsp->body = bindup(JBGIF, sizeof(JBGIF));
373 rsp->content_length = sizeof(JBGIF);
377 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
378 rsp->content_length = sizeof(BLANKGIF);
381 enlist(rsp->headers, "Content-Type: image/gif");
388 /*********************************************************************
390 * Function : cgi_show_status
392 * Description : CGI function that returns a a web page describing the
393 * current status of IJB.
396 * 1 : csp = Current client state (buffers, headers, etc...)
397 * 2 : rsp = http_response data structure for output
398 * 3 : parameters = map of cgi parameters
401 * type : Selects the type of banner between "trans" and "jb".
402 * Defaults to "jb" if absent or != "trans".
406 *********************************************************************/
407 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
408 struct map *parameters)
412 struct map *exports = default_exports(csp, "show-status");
415 char buf[BUFFER_SIZE];
417 const char * filename = NULL;
418 char * file_description = NULL;
421 p = lookup(parameters, "file");
425 if (csp->actions_list)
427 filename = csp->actions_list->filename;
428 file_description = "Actions List";
435 filename = csp->rlist->filename;
436 file_description = "Regex Filter List";
444 filename = csp->tlist->filename;
445 file_description = "Trust List";
448 #endif /* def FEATURE_TRUST */
451 if (NULL != filename)
453 exports = map(exports, "file-description", 1, file_description, 1);
454 exports = map(exports, "filepath", 1, html_encode(filename), 0);
456 if ((fp = fopen(filename, "r")) == NULL)
458 exports = map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
462 while (fgets(buf, sizeof(buf), fp))
464 p = html_encode(buf);
469 s = strsav(s, "<br>");
473 exports = map(exports, "contents", 1, s, 0);
475 rsp->body = fill_template(csp, "show-status-file", exports);
481 exports = map(exports, "redirect-url", 1, REDIRECT_URL, 1);
484 for (i=0; i < Argc; i++)
486 s = strsav(s, Argv[i]);
489 exports = map(exports, "invocation", 1, s, 0);
491 exports = map(exports, "options", 1, csp->config->proxy_args, 1);
493 exports = map(exports, "sourceversions", 1, s, 0);
494 exports = show_defines(exports);
496 #ifdef FEATURE_STATISTICS
497 exports = add_stats(exports);
498 #else /* ndef FEATURE_STATISTICS */
499 exports = map_block_killer(exports, "statistics");
500 #endif /* ndef FEATURE_STATISTICS */
502 if (csp->actions_list)
504 exports = map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
508 exports = map(exports, "actions-filename", 1, "None specified", 1);
513 exports = map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
517 exports = map(exports, "re-filter-filename", 1, "None specified", 1);
523 exports = map(exports, "trust-filename", 1, csp->tlist->filename, 1);
527 exports = map(exports, "trust-filename", 1, "None specified", 1);
530 exports = map_block_killer(exports, "trust-support");
531 #endif /* ndef FEATURE_TRUST */
533 rsp->body = fill_template(csp, "show-status", exports);
540 /*********************************************************************
542 * Function : cgi_show_url_info
544 * Description : CGI function that determines and shows which actions
545 * junkbuster will perform for a given url, and which
546 * matches starting from the defaults have lead to that.
549 * 1 : csp = Current client state (buffers, headers, etc...)
550 * 2 : rsp = http_response data structure for output
551 * 3 : parameters = map of cgi parameters
554 * url : The url whose actions are to be determined.
555 * If url is unset, the url-given conditional will be
556 * set, so that all but the form can be suppressed in
561 *********************************************************************/
562 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
563 struct map *parameters)
565 struct map *exports = default_exports(csp, "show-url-info");
566 char *url_param, *host = NULL;
568 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
570 exports = map_block_killer(exports, "url-given");
571 exports = map(exports, "url", 1, "", 1);
575 char *matches = NULL;
580 struct file_list *fl;
581 struct url_actions *b;
582 struct url_spec url[1];
583 struct current_action_spec action[1];
586 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
588 exports = map(exports, "url", 1, host, 1);
589 exports = map(exports, "url-html", 1, html_encode(host), 0);
591 init_current_action(action);
593 s = current_action_to_text(action);
594 exports = map(exports, "default", 1, s , 0);
596 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
598 exports = map(exports, "matches", 1, "none" , 1);
599 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
602 free_current_action(action);
604 rsp->body = fill_template(csp, "show-url-info", exports);
610 s = strchr(host, '/');
620 s = strchr(host, ':');
630 /* if splitting the domain fails, punt */
631 if (url->dbuf == NULL)
633 exports = map(exports, "matches", 1, "none" , 1);
634 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
638 free_current_action(action);
640 rsp->body = fill_template(csp, "show-url-info", exports);
646 for (b = b->next; NULL != b; b = b->next)
648 if ((b->url->port == 0) || (b->url->port == port))
650 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
652 if ((b->url->path == NULL) ||
654 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
656 (strncmp(b->url->path, path, b->url->pathlen) == 0)
660 s = actions_to_text(b->action);
661 matches = strsav(matches, "<b>{");
662 matches = strsav(matches, s);
663 matches = strsav(matches, " }</b><br>\n<code>");
664 matches = strsav(matches, b->url->spec);
665 matches = strsav(matches, "</code><br>\n<br>\n");
668 merge_current_action(action, b->action);
677 exports = map(exports, "matches", 1, matches , 0);
681 exports = map(exports, "matches", 1, "none", 1);
691 s = current_action_to_text(action);
692 exports = map(exports, "final", 1, s, 0);
695 free_current_action(action);
698 rsp->body = fill_template(csp, "show-url-info", exports);
705 /*********************************************************************
707 * Function : error_response
709 * Description : returns an http_response that explains the reason
710 * why a request failed.
713 * 1 : csp = Current client state (buffers, headers, etc...)
714 * 2 : template = Which template should be used for the answer
715 * 3 : errno = system error number
717 * Returns : NULL if no memory, else http_response
719 *********************************************************************/
720 struct http_response *error_response(struct client_state *csp, const char *template, int err)
722 struct http_response *rsp;
723 struct map *exports = default_exports(csp, NULL);
725 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
730 exports = map(exports, "host-html", 1, html_encode(csp->http->host), 0);
731 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
732 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
733 exports = map(exports, "path", 1, csp->http->path, 1);
734 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
735 exports = map(exports, "error", 1, safe_strerror(err), 0);
736 exports = map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
738 rsp->body = fill_template(csp, template, exports);
741 if (!strcmp(template, "no-such-domain"))
743 rsp->status = strdup("404 No such domain");
745 else if (!strcmp(template, "connect-failed"))
747 rsp->status = strdup("503 Connect failed");
750 return(finish_http_response(rsp));
754 /*********************************************************************
756 * Function : finish_http_response
758 * Description : Fill in the missing headers in an http response,
759 * and flatten the headers to an http head.
762 * 1 : rsp = pointer to http_response to be processed
764 * Returns : http_response, or NULL on failiure
766 *********************************************************************/
767 struct http_response *finish_http_response(struct http_response *rsp)
769 char buf[BUFFER_SIZE];
772 * Fill in the HTTP Status
774 sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
775 enlist_first(rsp->headers, buf);
778 * Set the Content-Length
780 if (rsp->content_length == 0)
782 rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
784 sprintf(buf, "Content-Length: %d", rsp->content_length);
785 enlist(rsp->headers, buf);
788 * Fill in the default headers FIXME: Are these correct? sequence OK? check rfc!
790 enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
791 enlist_unique(rsp->headers, "Expires: Thu Jul 31, 1997 07:42:22 pm GMT", 8);
792 enlist_unique(rsp->headers, "Content-Type: text/html", 13);
793 enlist(rsp->headers, "");
799 if (NULL == (rsp->head = list_to_text(rsp->headers)))
801 free_http_response(rsp);
804 rsp->head_length = strlen(rsp->head);
811 /*********************************************************************
813 * Function : free_http_response
815 * Description : Free the memory occupied by an http_response
816 * and its depandant structures.
819 * 1 : rsp = pointer to http_response to be freed
823 *********************************************************************/
824 void free_http_response(struct http_response *rsp)
831 destroy_list(rsp->headers);
838 /*********************************************************************
840 * Function : fill_template
842 * Description : CGI support function that loads a given HTML
843 * template from the confdir, and fills it in
844 * by replacing @name@ with value using pcrs,
845 * for each item in the output map.
848 * 1 : csp = Current client state (buffers, headers, etc...)
849 * 3 : template = name of the HTML template to be used
850 * 2 : exports = map with fill in symbol -> name pairs
852 * Returns : char * with filled out form, or NULL if failiure
854 *********************************************************************/
855 char *fill_template(struct client_state *csp, const char *template, struct map *exports)
858 pcrs_job *job, *joblist = NULL;
859 char buf[BUFFER_SIZE];
860 char *new, *old = NULL;
866 * Open template file or fail
868 snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, template);
870 if(NULL == (fp = fopen(buf, "r")))
872 log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
878 * Assemble pcrs joblist from exports map
880 for (m = exports; m; m = m->next)
884 /* Enclose name in @@ */
885 snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
887 /* Make and chain job */
888 if ( NULL == (job = (pcrs_compile(buf, m->value, "sigTU", &error))) )
890 log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
901 * Read the file, ignoring comments
903 while (fgets(buf, BUFFER_SIZE, fp))
905 /* skip lines starting with '#' */
906 if(*buf == '#') continue;
908 old = strsav(old, buf);
916 size = strlen(old) + 1;
919 for (job = joblist; NULL != job; job = job->next)
921 pcrs_execute(job, old, size, &new, &size);
922 if (old != buf) free(old);
928 * Free the jobs & return
930 pcrs_free_joblist(joblist);
936 /*********************************************************************
938 * Function : default_exports
940 * Description : returns a struct map list that contains exports
941 * which are common to all CGI functions.
944 * 1 : csp = Current client state (buffers, headers, etc...)
945 * 2 : caller = name of CGI who calls us and which should
946 * be excluded from the generated menu.
947 * Returns : NULL if no memory, else map
949 *********************************************************************/
950 struct map *default_exports(struct client_state *csp, char *caller)
952 struct map *exports = NULL;
955 exports = map(exports, "version", 1, VERSION, 1);
956 exports = map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1);
957 exports = map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1);
958 exports = map(exports, "admin-address", 1, csp->config->admin_address ? csp->config->admin_address : "fill@me.in.please", 1);
959 exports = map(exports, "homepage", 1, HOME_PAGE_URL, 1);
960 exports = map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1);
961 exports = map(exports, "menu", 1, make_menu(caller), 0);
962 exports = map(exports, "code-status", 1, CODE_STATUS, 1);
964 snprintf(buf, 20, "%d", csp->config->hport);
965 exports = map(exports, "my-port", 1, buf, 1);
967 if(!strcmp(CODE_STATUS, "stable"))
969 exports = map_block_killer(exports, "unstable");
972 if(csp->config->proxy_info_url != NULL)
974 exports = map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
978 exports = map_block_killer(exports, "have-proxy-info");
986 /*********************************************************************
988 * Function : map_block_killer
990 * Description : Convenience function.
991 * Adds a "killer" for the conditional HTML-template
992 * block <name>, i.e. a substitution of the regex
993 * "if-<name>-start.*if-<name>-end" to the given
997 * 1 : exports = map to extend
998 * 2 : name = name of conditional block
1000 * Returns : extended map
1002 *********************************************************************/
1003 struct map *map_block_killer(struct map *exports, char *name)
1005 char buf[1000]; /* Will do, since the names are hardwired */
1007 snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
1008 exports = map(exports, buf, 1, "", 1);
1015 /*********************************************************************
1017 * Function : map_conditional
1019 * Description : Convenience function.
1020 * Adds an "if-then-else" for the conditional HTML-template
1021 * block <name>, i.e. a substitution of the form:
1028 * The control structure and one of the alternatives
1032 * 1 : exports = map to extend
1033 * 2 : name = name of conditional block
1034 * 3 : choose_first = nonzero for first, zero for second.
1036 * Returns : extended map
1038 *********************************************************************/
1039 struct map *map_conditional(struct map *exports, char *name, int choose_first)
1041 char buf[1000]; /* Will do, since the names are hardwired */
1043 snprintf(buf, 1000, (choose_first
1044 ? "else-not-%s@.*@endif-%s"
1045 : "if-%s-then@.*@else-not-%s"),
1047 exports = map(exports, buf, 1, "", 1);
1049 snprintf(buf, 1000, (choose_first ? "if-%s-then" : "endif-%s"), name);
1050 exports = map(exports, buf, 1, "", 1);
1057 /*********************************************************************
1059 * Function : make_menu
1061 * Description : Returns an HTML-formatted menu of the available
1062 * unhidden CGIs, excluding the one given in <self>.
1064 * Parameters : self = name of CGI to leave out, can be NULL
1066 * Returns : menu string
1068 *********************************************************************/
1069 char *make_menu(const char *self)
1071 const struct cgi_dispatcher *d;
1072 char buf[BUFFER_SIZE], *tmp = NULL;
1074 if (self == NULL) self = "NO-SUCH-CGI!";
1076 /* List available unhidden CGI's and export as "other-cgis" */
1077 for (d = cgi_dispatcher; d->handler; d++)
1079 if (strncmp(d->description, "HIDE", 4) && strcmp(d->name, self))
1081 snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
1082 HOME_PAGE_URL, d->name, d->description);
1083 tmp = strsav(tmp, buf);
1091 /*********************************************************************
1093 * Function : dump_map
1095 * Description : HTML-dump a map for debugging
1098 * 1 : map = map to dump
1100 * Returns : string with HTML
1102 *********************************************************************/
1103 char *dump_map(struct map *map)
1105 struct map *p = map;
1108 ret = strsav(ret, "<table>\n");
1112 ret = strsav(ret, "<tr><td><b>");
1113 ret = strsav(ret, p->name);
1114 ret = strsav(ret, "</b></td><td>");
1115 ret = strsav(ret, p->value);
1116 ret = strsav(ret, "</td></tr>\n");
1120 ret = strsav(ret, "</table>\n");
1126 #ifdef FEATURE_STATISTICS
1127 /*********************************************************************
1129 * Function : add_stats
1131 * Description : Add the blocking statistics to a given map.
1134 * 1 : exports = map to write to.
1136 * Returns : pointer to extended map
1138 *********************************************************************/
1139 struct map *add_stats(struct map *exports)
1141 float perc_rej; /* Percentage of http requests rejected */
1143 int local_urls_read = urls_read;
1144 int local_urls_rejected = urls_rejected;
1147 * Need to alter the stats not to include the fetch of this
1150 * Can't do following thread safely! doh!
1153 * urls_rejected--; * This will be incremented subsequently *
1156 if (local_urls_read == 0)
1158 exports = map_block_killer(exports, "have-stats");
1162 exports = map_block_killer(exports, "have-no-stats");
1164 perc_rej = (float)local_urls_rejected * 100.0F /
1165 (float)local_urls_read;
1167 sprintf(buf, "%d", local_urls_read);
1168 exports = map(exports, "requests-received", 1, buf, 1);
1170 sprintf(buf, "%d", local_urls_rejected);
1171 exports = map(exports, "requests-blocked", 1, buf, 1);
1173 sprintf(buf, "%6.2f", perc_rej);
1174 exports = map(exports, "percent-blocked", 1, buf, 1);
1180 #endif /* def FEATURE_STATISTICS */