1 const char cgi_rcs[] = "$Id: cgi.c,v 1.15 2001/08/01 21:19:22 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.15 2001/08/01 21:19:22 jongfoster
40 * Moving file version information to a separate CGI page.
42 * Revision 1.14 2001/08/01 00:19:03 jongfoster
43 * New function: map_conditional() for an if-then-else syntax.
44 * Changing to use new version of show_defines()
46 * Revision 1.13 2001/07/30 22:08:36 jongfoster
47 * Tidying up #defines:
48 * - All feature #defines are now of the form FEATURE_xxx
49 * - Permanently turned off WIN_GUI_EDIT
50 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
52 * Revision 1.12 2001/07/29 18:47:05 jongfoster
53 * Adding missing #include "loadcfg.h"
55 * Revision 1.11 2001/07/18 17:24:37 oes
56 * Changed to conform to new pcrs interface
58 * Revision 1.10 2001/07/13 13:53:13 oes
59 * Removed all #ifdef PCRS and related code
61 * Revision 1.9 2001/06/29 21:45:41 oes
62 * Indentation, CRLF->LF, Tab-> Space
64 * Revision 1.8 2001/06/29 13:21:46 oes
65 * - Cosmetics: renamed and reordered functions, variables,
66 * texts, improved comments etc
68 * - Removed ij_untrusted_url() The relevant
69 * info is now part of the "untrusted" page,
70 * which is generated by filters.c:trust_url()
72 * - Generators of content now call finish_http_response()
73 * themselves, making jcc.c:chat() a little less
76 * - Removed obsolete "Pragma: no-cache" from our headers
78 * - http_responses now know their head length
80 * - fill_template now uses the new interface to pcrs, so that
81 * - long jobs (like whole files) no longer have to be assembled
82 * in a fixed size buffer
83 * - the new T (trivial) option is used, and the replacement may
84 * contain Perl syntax backrefs without confusing pcrs
86 * - Introduced default_exports() which generates a set of exports
87 * common to all CGIs and other content generators
89 * - Introduced convenience function map_block_killer()
91 * - Introduced convenience function make_menu()
93 * - Introduced CGI-like function error_response() which generates
94 * the "No such domain" and "Connect failed" messages using the
97 * - cgi_show_url_info:
98 * - adapted to new CGI features
99 * - form and answers now generated from same template
100 * - http:// prefix in URL now OK
103 * - adapted to new CGI features
104 * - no longer uses csp->init_proxy_args
107 * - moved menu generation to make_menu()
109 * - add_stats now writes single export map entries instead
112 * - Moved redirect_url() to filters.c
114 * - Fixed mem leak in free_http_response(), map_block_killer(),
116 * - Removed logentry from cancelled commit
118 * Revision 1.7 2001/06/09 10:51:58 jongfoster
119 * Changing "show URL info" handler to new style.
120 * Changing BUFSIZ ==> BUFFER_SIZE
122 * Revision 1.6 2001/06/07 23:05:19 jongfoster
123 * Removing code related to old forward and ACL files.
125 * Revision 1.5 2001/06/05 19:59:16 jongfoster
126 * Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
128 * Revision 1.4 2001/06/04 10:41:52 swa
129 * show version string of cgi.h and cgi.c
131 * Revision 1.3 2001/06/03 19:12:16 oes
132 * introduced new cgi handling
134 * No revisions before 1.3
136 **********************************************************************/
142 #include <sys/types.h>
148 #define snprintf _snprintf
149 #endif /* def _WIN32 */
160 #include "miscutil.h"
161 #include "showargs.h"
164 const char cgi_h_rcs[] = CGI_H_VERSION;
166 const struct cgi_dispatcher cgi_dispatcher[] = {
169 "Show information about the current configuration" },
171 13, cgi_show_url_info,
172 "Show which actions apply to a URL and why" },
174 12, cgi_show_version,
175 "Show the source code version numbers" },
178 "HIDE Send the transparent or \"Junkbuster\" gif" },
181 "Junkbuster main page" },
182 { NULL, 0, NULL, NULL }
186 /*********************************************************************
188 * Function : dispatch_cgi
190 * Description : Checks if a request URL has either the magical hostname
191 * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses
192 * the (rest of the) path as a cgi name plus query string,
193 * prepares a map that maps CGI parameter names to their values,
194 * initializes the http_response struct, and calls the
195 * relevant CGI handler function.
198 * 1 : csp = Current client state (buffers, headers, etc...)
200 * Returns : http_response if match, NULL if nonmatch or handler fail
202 *********************************************************************/
203 struct http_response *dispatch_cgi(struct client_state *csp)
205 char *argstring = NULL;
206 const struct cgi_dispatcher *d;
207 struct map *param_list;
208 struct http_response *rsp;
211 * Should we intercept ?
214 /* Either the host matches CGI_PREFIX_HOST ..*/
215 if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST))
217 /* ..then the path will all be for us */
218 argstring = csp->http->path;
220 /* Or it's the host part HOME_PAGE_URL, and the path /config ? */
221 else if ( (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 ))
222 && (0 == strncmpic(csp->http->path,"/config", 7))
223 && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
225 /* then it's everything following "/config" */
226 argstring = csp->http->path + 7;
234 * This is a CGI call.
237 /* Get mem for response or fail*/
238 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
243 /* Remove leading slash */
244 if (*argstring == '/')
249 log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
250 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
251 csp->ip_addr_str, csp->http->cmd);
253 /* Find and start the right CGI function*/
254 for (d = cgi_dispatcher; d->handler; d++)
256 if (strncmp(argstring, d->name, d->name_length) == 0)
258 param_list = parse_cgi_parameters(argstring + d->name_length);
259 if ((d->handler)(csp, rsp, param_list))
264 free_map(param_list);
265 return(finish_http_response(rsp));
269 /* Can't get here, since cgi_default will match all requests */
276 /*********************************************************************
278 * Function : parse_cgi_parameters
280 * Description : Parse a URL-encoded argument string into name/value
281 * pairs and store them in a struct map list.
284 * 1 : string = string to be parsed
286 * Returns : poniter to param list, or NULL if failiure
288 *********************************************************************/
289 struct map *parse_cgi_parameters(char *argstring)
292 char *vector[BUFFER_SIZE];
294 struct map *cgi_params = NULL;
296 if(*argstring == '?') argstring++;
297 tmp = strdup(argstring);
299 pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1);
301 for (i = 0; i < pairs; i++)
303 if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
306 cgi_params = map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
316 /*********************************************************************
318 * Function : cgi_default
320 * Description : CGI function that is called if no action was given.
321 * Lists menu of available unhidden CGIs.
324 * 1 : csp = Current client state (buffers, headers, etc...)
325 * 2 : rsp = http_response data structure for output
326 * 3 : parameters = map of cgi parameters
330 *********************************************************************/
331 int cgi_default(struct client_state *csp, struct http_response *rsp,
332 struct map *parameters)
334 char *p, *tmp = NULL;
335 struct map *exports = default_exports(csp, "");
337 /* If there were other parameters, export a dump as "cgi-parameters" */
340 p = dump_map(parameters);
341 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
342 "Anyway, here they are, in case you're interested:</p>\n");
343 tmp = strsav(tmp, p);
344 exports = map(exports, "cgi-parameters", 1, tmp, 0);
349 exports = map(exports, "cgi-parameters", 1, "", 1);
352 rsp->body = fill_template(csp, "default", exports);
359 /*********************************************************************
361 * Function : cgi_send_banner
363 * Description : CGI function that returns a banner.
366 * 1 : csp = Current client state (buffers, headers, etc...)
367 * 2 : rsp = http_response data structure for output
368 * 3 : parameters = map of cgi parameters
371 * type : Selects the type of banner between "trans" and "jb".
372 * Defaults to "jb" if absent or != "trans".
376 *********************************************************************/
377 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
378 struct map *parameters)
380 if(strcmp(lookup(parameters, "type"), "trans"))
382 rsp->body = bindup(JBGIF, sizeof(JBGIF));
383 rsp->content_length = sizeof(JBGIF);
387 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
388 rsp->content_length = sizeof(BLANKGIF);
391 enlist(rsp->headers, "Content-Type: image/gif");
398 /*********************************************************************
400 * Function : cgi_show_version
402 * Description : CGI function that returns a a web page describing the
403 * file versions of IJB.
406 * 1 : csp = Current client state (buffers, headers, etc...)
407 * 2 : rsp = http_response data structure for output
408 * 3 : parameters = map of cgi parameters
411 * type : Selects the type of banner between "trans" and "jb".
412 * Defaults to "jb" if absent or != "trans".
416 *********************************************************************/
417 int cgi_show_version(struct client_state *csp, struct http_response *rsp,
418 struct map *parameters)
420 struct map *exports = default_exports(csp, "show-version");
422 exports = map(exports, "sourceversions", 1, show_rcs(), 0);
424 rsp->body = fill_template(csp, "show-version", exports);
431 /*********************************************************************
433 * Function : cgi_show_status
435 * Description : CGI function that returns a a web page describing the
436 * current status of IJB.
439 * 1 : csp = Current client state (buffers, headers, etc...)
440 * 2 : rsp = http_response data structure for output
441 * 3 : parameters = map of cgi parameters
444 * type : Selects the type of banner between "trans" and "jb".
445 * Defaults to "jb" if absent or != "trans".
449 *********************************************************************/
450 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
451 struct map *parameters)
455 struct map *exports = default_exports(csp, "show-status");
458 char buf[BUFFER_SIZE];
460 const char * filename = NULL;
461 char * file_description = NULL;
464 p = lookup(parameters, "file");
468 if (csp->actions_list)
470 filename = csp->actions_list->filename;
471 file_description = "Actions List";
478 filename = csp->rlist->filename;
479 file_description = "Regex Filter List";
487 filename = csp->tlist->filename;
488 file_description = "Trust List";
491 #endif /* def FEATURE_TRUST */
494 if (NULL != filename)
496 exports = map(exports, "file-description", 1, file_description, 1);
497 exports = map(exports, "filepath", 1, html_encode(filename), 0);
499 if ((fp = fopen(filename, "r")) == NULL)
501 exports = map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
505 while (fgets(buf, sizeof(buf), fp))
507 p = html_encode(buf);
512 s = strsav(s, "<br>");
516 exports = map(exports, "contents", 1, s, 0);
518 rsp->body = fill_template(csp, "show-status-file", exports);
524 exports = map(exports, "redirect-url", 1, REDIRECT_URL, 1);
527 for (i=0; i < Argc; i++)
529 s = strsav(s, Argv[i]);
532 exports = map(exports, "invocation", 1, s, 0);
534 exports = map(exports, "options", 1, csp->config->proxy_args, 1);
535 exports = show_defines(exports);
537 #ifdef FEATURE_STATISTICS
538 exports = add_stats(exports);
539 #else /* ndef FEATURE_STATISTICS */
540 exports = map_block_killer(exports, "statistics");
541 #endif /* ndef FEATURE_STATISTICS */
543 if (csp->actions_list)
545 exports = map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
549 exports = map(exports, "actions-filename", 1, "None specified", 1);
554 exports = map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
558 exports = map(exports, "re-filter-filename", 1, "None specified", 1);
564 exports = map(exports, "trust-filename", 1, csp->tlist->filename, 1);
568 exports = map(exports, "trust-filename", 1, "None specified", 1);
571 exports = map_block_killer(exports, "trust-support");
572 #endif /* ndef FEATURE_TRUST */
574 rsp->body = fill_template(csp, "show-status", exports);
581 /*********************************************************************
583 * Function : cgi_show_url_info
585 * Description : CGI function that determines and shows which actions
586 * junkbuster will perform for a given url, and which
587 * matches starting from the defaults have lead to that.
590 * 1 : csp = Current client state (buffers, headers, etc...)
591 * 2 : rsp = http_response data structure for output
592 * 3 : parameters = map of cgi parameters
595 * url : The url whose actions are to be determined.
596 * If url is unset, the url-given conditional will be
597 * set, so that all but the form can be suppressed in
602 *********************************************************************/
603 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
604 struct map *parameters)
606 struct map *exports = default_exports(csp, "show-url-info");
607 char *url_param, *host = NULL;
609 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
611 exports = map_block_killer(exports, "url-given");
612 exports = map(exports, "url", 1, "", 1);
616 char *matches = NULL;
621 struct file_list *fl;
622 struct url_actions *b;
623 struct url_spec url[1];
624 struct current_action_spec action[1];
627 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
629 exports = map(exports, "url", 1, host, 1);
630 exports = map(exports, "url-html", 1, html_encode(host), 0);
632 init_current_action(action);
634 s = current_action_to_text(action);
635 exports = map(exports, "default", 1, s , 0);
637 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
639 exports = map(exports, "matches", 1, "none" , 1);
640 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
643 free_current_action(action);
645 rsp->body = fill_template(csp, "show-url-info", exports);
651 s = strchr(host, '/');
661 s = strchr(host, ':');
671 /* if splitting the domain fails, punt */
672 if (url->dbuf == NULL)
674 exports = map(exports, "matches", 1, "none" , 1);
675 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
679 free_current_action(action);
681 rsp->body = fill_template(csp, "show-url-info", exports);
687 for (b = b->next; NULL != b; b = b->next)
689 if ((b->url->port == 0) || (b->url->port == port))
691 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
693 if ((b->url->path == NULL) ||
695 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
697 (strncmp(b->url->path, path, b->url->pathlen) == 0)
701 s = actions_to_text(b->action);
702 matches = strsav(matches, "<b>{");
703 matches = strsav(matches, s);
704 matches = strsav(matches, " }</b><br>\n<code>");
705 matches = strsav(matches, b->url->spec);
706 matches = strsav(matches, "</code><br>\n<br>\n");
709 merge_current_action(action, b->action);
718 exports = map(exports, "matches", 1, matches , 0);
722 exports = map(exports, "matches", 1, "none", 1);
732 s = current_action_to_text(action);
733 exports = map(exports, "final", 1, s, 0);
736 free_current_action(action);
739 rsp->body = fill_template(csp, "show-url-info", exports);
746 /*********************************************************************
748 * Function : error_response
750 * Description : returns an http_response that explains the reason
751 * why a request failed.
754 * 1 : csp = Current client state (buffers, headers, etc...)
755 * 2 : template = Which template should be used for the answer
756 * 3 : errno = system error number
758 * Returns : NULL if no memory, else http_response
760 *********************************************************************/
761 struct http_response *error_response(struct client_state *csp, const char *template, int err)
763 struct http_response *rsp;
764 struct map *exports = default_exports(csp, NULL);
766 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
771 exports = map(exports, "host-html", 1, html_encode(csp->http->host), 0);
772 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
773 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
774 exports = map(exports, "path", 1, csp->http->path, 1);
775 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
776 exports = map(exports, "error", 1, safe_strerror(err), 0);
777 exports = map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
779 rsp->body = fill_template(csp, template, exports);
782 if (!strcmp(template, "no-such-domain"))
784 rsp->status = strdup("404 No such domain");
786 else if (!strcmp(template, "connect-failed"))
788 rsp->status = strdup("503 Connect failed");
791 return(finish_http_response(rsp));
795 /*********************************************************************
797 * Function : finish_http_response
799 * Description : Fill in the missing headers in an http response,
800 * and flatten the headers to an http head.
803 * 1 : rsp = pointer to http_response to be processed
805 * Returns : http_response, or NULL on failiure
807 *********************************************************************/
808 struct http_response *finish_http_response(struct http_response *rsp)
810 char buf[BUFFER_SIZE];
813 * Fill in the HTTP Status
815 sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
816 enlist_first(rsp->headers, buf);
819 * Set the Content-Length
821 if (rsp->content_length == 0)
823 rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
825 sprintf(buf, "Content-Length: %d", rsp->content_length);
826 enlist(rsp->headers, buf);
829 * Fill in the default headers FIXME: Are these correct? sequence OK? check rfc!
830 * FIXME: Should have:
831 * "JunkBuster" GIF: Last-Modified: any *fixed* date in the past (as now).
832 * Expires: 5 minutes after the time when reply sent
833 * CGI, "blocked", & all other requests:
834 * Last-Modified: Time when reply sent
835 * Expires: Time when reply sent
836 * "Cache-Control: no-cache"
838 * See http://www.w3.org/Protocols/rfc2068/rfc2068
840 enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
841 enlist_unique(rsp->headers, "Expires: Thu Jul 31, 1997 07:42:22 pm GMT", 8);
842 enlist_unique(rsp->headers, "Content-Type: text/html", 13);
843 enlist(rsp->headers, "");
849 if (NULL == (rsp->head = list_to_text(rsp->headers)))
851 free_http_response(rsp);
854 rsp->head_length = strlen(rsp->head);
861 /*********************************************************************
863 * Function : free_http_response
865 * Description : Free the memory occupied by an http_response
866 * and its depandant structures.
869 * 1 : rsp = pointer to http_response to be freed
873 *********************************************************************/
874 void free_http_response(struct http_response *rsp)
881 destroy_list(rsp->headers);
888 /*********************************************************************
890 * Function : fill_template
892 * Description : CGI support function that loads a given HTML
893 * template from the confdir, and fills it in
894 * by replacing @name@ with value using pcrs,
895 * for each item in the output map.
898 * 1 : csp = Current client state (buffers, headers, etc...)
899 * 3 : template = name of the HTML template to be used
900 * 2 : exports = map with fill in symbol -> name pairs
902 * Returns : char * with filled out form, or NULL if failiure
904 *********************************************************************/
905 char *fill_template(struct client_state *csp, const char *templatename, struct map *exports)
909 char buf[BUFFER_SIZE];
910 char *tmp_out_buffer;
911 char *file_buffer = NULL;
918 * Open template file or fail
920 snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, templatename);
922 if(NULL == (fp = fopen(buf, "r")))
924 log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
930 * Read the file, ignoring comments
932 while (fgets(buf, BUFFER_SIZE, fp))
934 /* skip lines starting with '#' */
935 if(*buf == '#') continue;
937 file_buffer = strsav(file_buffer, buf);
945 size = strlen(file_buffer) + 1;
948 * Assemble pcrs joblist from exports map
950 for (m = exports; m != NULL; m = m->next)
952 /* Enclose name in @@ */
953 snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
955 /* Make and run job */
956 if ( NULL == (job = (pcrs_compile(buf, m->value, "sigTU", &error))) )
958 log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
962 pcrs_execute(job, file_buffer, size, &tmp_out_buffer, &size);
963 if (file_buffer != tmp_out_buffer)
966 file_buffer = tmp_out_buffer;
981 /*********************************************************************
983 * Function : default_exports
985 * Description : returns a struct map list that contains exports
986 * which are common to all CGI functions.
989 * 1 : csp = Current client state (buffers, headers, etc...)
990 * 2 : caller = name of CGI who calls us and which should
991 * be excluded from the generated menu.
992 * Returns : NULL if no memory, else map
994 *********************************************************************/
995 struct map *default_exports(struct client_state *csp, char *caller)
997 struct map *exports = NULL;
1000 exports = map(exports, "version", 1, VERSION, 1);
1001 exports = map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1);
1002 exports = map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1);
1003 exports = map(exports, "admin-address", 1, csp->config->admin_address ? csp->config->admin_address : "fill@me.in.please", 1);
1004 exports = map(exports, "homepage", 1, HOME_PAGE_URL, 1);
1005 exports = map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1);
1006 exports = map(exports, "menu", 1, make_menu(caller), 0);
1007 exports = map(exports, "code-status", 1, CODE_STATUS, 1);
1009 snprintf(buf, 20, "%d", csp->config->hport);
1010 exports = map(exports, "my-port", 1, buf, 1);
1012 if(!strcmp(CODE_STATUS, "stable"))
1014 exports = map_block_killer(exports, "unstable");
1017 if(csp->config->proxy_info_url != NULL)
1019 exports = map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
1023 exports = map_block_killer(exports, "have-proxy-info");
1031 /*********************************************************************
1033 * Function : map_block_killer
1035 * Description : Convenience function.
1036 * Adds a "killer" for the conditional HTML-template
1037 * block <name>, i.e. a substitution of the regex
1038 * "if-<name>-start.*if-<name>-end" to the given
1042 * 1 : exports = map to extend
1043 * 2 : name = name of conditional block
1045 * Returns : extended map
1047 *********************************************************************/
1048 struct map *map_block_killer(struct map *exports, char *name)
1050 char buf[1000]; /* Will do, since the names are hardwired */
1052 snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
1053 exports = map(exports, buf, 1, "", 1);
1060 /*********************************************************************
1062 * Function : map_conditional
1064 * Description : Convenience function.
1065 * Adds an "if-then-else" for the conditional HTML-template
1066 * block <name>, i.e. a substitution of the form:
1073 * The control structure and one of the alternatives
1077 * 1 : exports = map to extend
1078 * 2 : name = name of conditional block
1079 * 3 : choose_first = nonzero for first, zero for second.
1081 * Returns : extended map
1083 *********************************************************************/
1084 struct map *map_conditional(struct map *exports, char *name, int choose_first)
1086 char buf[1000]; /* Will do, since the names are hardwired */
1088 snprintf(buf, 1000, (choose_first
1089 ? "else-not-%s@.*@endif-%s"
1090 : "if-%s-then@.*@else-not-%s"),
1092 exports = map(exports, buf, 1, "", 1);
1094 snprintf(buf, 1000, (choose_first ? "if-%s-then" : "endif-%s"), name);
1095 exports = map(exports, buf, 1, "", 1);
1102 /*********************************************************************
1104 * Function : make_menu
1106 * Description : Returns an HTML-formatted menu of the available
1107 * unhidden CGIs, excluding the one given in <self>.
1109 * Parameters : self = name of CGI to leave out, can be NULL
1111 * Returns : menu string
1113 *********************************************************************/
1114 char *make_menu(const char *self)
1116 const struct cgi_dispatcher *d;
1117 char buf[BUFFER_SIZE], *tmp = NULL;
1119 if (self == NULL) self = "NO-SUCH-CGI!";
1121 /* List available unhidden CGI's and export as "other-cgis" */
1122 for (d = cgi_dispatcher; d->handler; d++)
1124 if (strncmp(d->description, "HIDE", 4) && strcmp(d->name, self))
1126 snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
1127 HOME_PAGE_URL, d->name, d->description);
1128 tmp = strsav(tmp, buf);
1136 /*********************************************************************
1138 * Function : dump_map
1140 * Description : HTML-dump a map for debugging
1143 * 1 : map = map to dump
1145 * Returns : string with HTML
1147 *********************************************************************/
1148 char *dump_map(struct map *map)
1150 struct map *p = map;
1153 ret = strsav(ret, "<table>\n");
1157 ret = strsav(ret, "<tr><td><b>");
1158 ret = strsav(ret, p->name);
1159 ret = strsav(ret, "</b></td><td>");
1160 ret = strsav(ret, p->value);
1161 ret = strsav(ret, "</td></tr>\n");
1165 ret = strsav(ret, "</table>\n");
1171 #ifdef FEATURE_STATISTICS
1172 /*********************************************************************
1174 * Function : add_stats
1176 * Description : Add the blocking statistics to a given map.
1179 * 1 : exports = map to write to.
1181 * Returns : pointer to extended map
1183 *********************************************************************/
1184 struct map *add_stats(struct map *exports)
1186 float perc_rej; /* Percentage of http requests rejected */
1188 int local_urls_read = urls_read;
1189 int local_urls_rejected = urls_rejected;
1192 * Need to alter the stats not to include the fetch of this
1195 * Can't do following thread safely! doh!
1198 * urls_rejected--; * This will be incremented subsequently *
1201 if (local_urls_read == 0)
1203 exports = map_block_killer(exports, "have-stats");
1207 exports = map_block_killer(exports, "have-no-stats");
1209 perc_rej = (float)local_urls_rejected * 100.0F /
1210 (float)local_urls_read;
1212 sprintf(buf, "%d", local_urls_read);
1213 exports = map(exports, "requests-received", 1, buf, 1);
1215 sprintf(buf, "%d", local_urls_rejected);
1216 exports = map(exports, "requests-blocked", 1, buf, 1);
1218 sprintf(buf, "%6.2f", perc_rej);
1219 exports = map(exports, "percent-blocked", 1, buf, 1);
1225 #endif /* def FEATURE_STATISTICS */