1 const char cgi_rcs[] = "$Id: cgi.c,v 1.14 2001/08/01 00:19:03 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.14 2001/08/01 00:19:03 jongfoster
40 * New function: map_conditional() for an if-then-else syntax.
41 * Changing to use new version of show_defines()
43 * Revision 1.13 2001/07/30 22:08:36 jongfoster
44 * Tidying up #defines:
45 * - All feature #defines are now of the form FEATURE_xxx
46 * - Permanently turned off WIN_GUI_EDIT
47 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
49 * Revision 1.12 2001/07/29 18:47:05 jongfoster
50 * Adding missing #include "loadcfg.h"
52 * Revision 1.11 2001/07/18 17:24:37 oes
53 * Changed to conform to new pcrs interface
55 * Revision 1.10 2001/07/13 13:53:13 oes
56 * Removed all #ifdef PCRS and related code
58 * Revision 1.9 2001/06/29 21:45:41 oes
59 * Indentation, CRLF->LF, Tab-> Space
61 * Revision 1.8 2001/06/29 13:21:46 oes
62 * - Cosmetics: renamed and reordered functions, variables,
63 * texts, improved comments etc
65 * - Removed ij_untrusted_url() The relevant
66 * info is now part of the "untrusted" page,
67 * which is generated by filters.c:trust_url()
69 * - Generators of content now call finish_http_response()
70 * themselves, making jcc.c:chat() a little less
73 * - Removed obsolete "Pragma: no-cache" from our headers
75 * - http_responses now know their head length
77 * - fill_template now uses the new interface to pcrs, so that
78 * - long jobs (like whole files) no longer have to be assembled
79 * in a fixed size buffer
80 * - the new T (trivial) option is used, and the replacement may
81 * contain Perl syntax backrefs without confusing pcrs
83 * - Introduced default_exports() which generates a set of exports
84 * common to all CGIs and other content generators
86 * - Introduced convenience function map_block_killer()
88 * - Introduced convenience function make_menu()
90 * - Introduced CGI-like function error_response() which generates
91 * the "No such domain" and "Connect failed" messages using the
94 * - cgi_show_url_info:
95 * - adapted to new CGI features
96 * - form and answers now generated from same template
97 * - http:// prefix in URL now OK
100 * - adapted to new CGI features
101 * - no longer uses csp->init_proxy_args
104 * - moved menu generation to make_menu()
106 * - add_stats now writes single export map entries instead
109 * - Moved redirect_url() to filters.c
111 * - Fixed mem leak in free_http_response(), map_block_killer(),
113 * - Removed logentry from cancelled commit
115 * Revision 1.7 2001/06/09 10:51:58 jongfoster
116 * Changing "show URL info" handler to new style.
117 * Changing BUFSIZ ==> BUFFER_SIZE
119 * Revision 1.6 2001/06/07 23:05:19 jongfoster
120 * Removing code related to old forward and ACL files.
122 * Revision 1.5 2001/06/05 19:59:16 jongfoster
123 * Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
125 * Revision 1.4 2001/06/04 10:41:52 swa
126 * show version string of cgi.h and cgi.c
128 * Revision 1.3 2001/06/03 19:12:16 oes
129 * introduced new cgi handling
131 * No revisions before 1.3
133 **********************************************************************/
139 #include <sys/types.h>
145 #define snprintf _snprintf
146 #endif /* def _WIN32 */
157 #include "miscutil.h"
158 #include "showargs.h"
161 const char cgi_h_rcs[] = CGI_H_VERSION;
163 const struct cgi_dispatcher cgi_dispatcher[] = {
166 "Show information about the current configuration" },
168 13, cgi_show_url_info,
169 "Show which actions apply to a URL and why" },
171 12, cgi_show_version,
172 "Show the source code version numbers" },
175 "HIDE Send the transparent or \"Junkbuster\" gif" },
178 "Junkbuster main page" },
179 { NULL, 0, NULL, NULL }
183 /*********************************************************************
185 * Function : dispatch_cgi
187 * Description : Checks if a request URL has either the magical hostname
188 * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses
189 * the (rest of the) path as a cgi name plus query string,
190 * prepares a map that maps CGI parameter names to their values,
191 * initializes the http_response struct, and calls the
192 * relevant CGI handler function.
195 * 1 : csp = Current client state (buffers, headers, etc...)
197 * Returns : http_response if match, NULL if nonmatch or handler fail
199 *********************************************************************/
200 struct http_response *dispatch_cgi(struct client_state *csp)
202 char *argstring = NULL;
203 const struct cgi_dispatcher *d;
204 struct map *param_list;
205 struct http_response *rsp;
208 * Should we intercept ?
211 /* Either the host matches CGI_PREFIX_HOST ..*/
212 if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST))
214 /* ..then the path will all be for us */
215 argstring = csp->http->path;
217 /* Or it's the host part HOME_PAGE_URL, and the path /config ? */
218 else if ( (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 ))
219 && (0 == strncmpic(csp->http->path,"/config", 7))
220 && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
222 /* then it's everything following "/config" */
223 argstring = csp->http->path + 7;
231 * This is a CGI call.
234 /* Get mem for response or fail*/
235 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
240 /* Remove leading slash */
241 if (*argstring == '/')
246 log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
247 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
248 csp->ip_addr_str, csp->http->cmd);
250 /* Find and start the right CGI function*/
251 for (d = cgi_dispatcher; d->handler; d++)
253 if (strncmp(argstring, d->name, d->name_length) == 0)
255 param_list = parse_cgi_parameters(argstring + d->name_length);
256 if ((d->handler)(csp, rsp, param_list))
261 free_map(param_list);
262 return(finish_http_response(rsp));
266 /* Can't get here, since cgi_default will match all requests */
273 /*********************************************************************
275 * Function : parse_cgi_parameters
277 * Description : Parse a URL-encoded argument string into name/value
278 * pairs and store them in a struct map list.
281 * 1 : string = string to be parsed
283 * Returns : poniter to param list, or NULL if failiure
285 *********************************************************************/
286 struct map *parse_cgi_parameters(char *argstring)
289 char *vector[BUFFER_SIZE];
291 struct map *cgi_params = NULL;
293 if(*argstring == '?') argstring++;
294 tmp = strdup(argstring);
296 pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1);
298 for (i = 0; i < pairs; i++)
300 if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
303 cgi_params = map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
313 /*********************************************************************
315 * Function : cgi_default
317 * Description : CGI function that is called if no action was given.
318 * Lists menu of available unhidden CGIs.
321 * 1 : csp = Current client state (buffers, headers, etc...)
322 * 2 : rsp = http_response data structure for output
323 * 3 : parameters = map of cgi parameters
327 *********************************************************************/
328 int cgi_default(struct client_state *csp, struct http_response *rsp,
329 struct map *parameters)
331 char *p, *tmp = NULL;
332 struct map *exports = default_exports(csp, "");
334 /* If there were other parameters, export a dump as "cgi-parameters" */
337 p = dump_map(parameters);
338 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
339 "Anyway, here they are, in case you're interested:</p>\n");
340 tmp = strsav(tmp, p);
341 exports = map(exports, "cgi-parameters", 1, tmp, 0);
346 exports = map(exports, "cgi-parameters", 1, "", 1);
349 rsp->body = fill_template(csp, "default", exports);
356 /*********************************************************************
358 * Function : cgi_send_banner
360 * Description : CGI function that returns a banner.
363 * 1 : csp = Current client state (buffers, headers, etc...)
364 * 2 : rsp = http_response data structure for output
365 * 3 : parameters = map of cgi parameters
368 * type : Selects the type of banner between "trans" and "jb".
369 * Defaults to "jb" if absent or != "trans".
373 *********************************************************************/
374 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
375 struct map *parameters)
377 if(strcmp(lookup(parameters, "type"), "trans"))
379 rsp->body = bindup(JBGIF, sizeof(JBGIF));
380 rsp->content_length = sizeof(JBGIF);
384 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
385 rsp->content_length = sizeof(BLANKGIF);
388 enlist(rsp->headers, "Content-Type: image/gif");
395 /*********************************************************************
397 * Function : cgi_show_version
399 * Description : CGI function that returns a a web page describing the
400 * file versions of IJB.
403 * 1 : csp = Current client state (buffers, headers, etc...)
404 * 2 : rsp = http_response data structure for output
405 * 3 : parameters = map of cgi parameters
408 * type : Selects the type of banner between "trans" and "jb".
409 * Defaults to "jb" if absent or != "trans".
413 *********************************************************************/
414 int cgi_show_version(struct client_state *csp, struct http_response *rsp,
415 struct map *parameters)
417 struct map *exports = default_exports(csp, "show-version");
419 exports = map(exports, "sourceversions", 1, show_rcs(), 0);
421 rsp->body = fill_template(csp, "show-version", exports);
428 /*********************************************************************
430 * Function : cgi_show_status
432 * Description : CGI function that returns a a web page describing the
433 * current status of IJB.
436 * 1 : csp = Current client state (buffers, headers, etc...)
437 * 2 : rsp = http_response data structure for output
438 * 3 : parameters = map of cgi parameters
441 * type : Selects the type of banner between "trans" and "jb".
442 * Defaults to "jb" if absent or != "trans".
446 *********************************************************************/
447 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
448 struct map *parameters)
452 struct map *exports = default_exports(csp, "show-status");
455 char buf[BUFFER_SIZE];
457 const char * filename = NULL;
458 char * file_description = NULL;
461 p = lookup(parameters, "file");
465 if (csp->actions_list)
467 filename = csp->actions_list->filename;
468 file_description = "Actions List";
475 filename = csp->rlist->filename;
476 file_description = "Regex Filter List";
484 filename = csp->tlist->filename;
485 file_description = "Trust List";
488 #endif /* def FEATURE_TRUST */
491 if (NULL != filename)
493 exports = map(exports, "file-description", 1, file_description, 1);
494 exports = map(exports, "filepath", 1, html_encode(filename), 0);
496 if ((fp = fopen(filename, "r")) == NULL)
498 exports = map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
502 while (fgets(buf, sizeof(buf), fp))
504 p = html_encode(buf);
509 s = strsav(s, "<br>");
513 exports = map(exports, "contents", 1, s, 0);
515 rsp->body = fill_template(csp, "show-status-file", exports);
521 exports = map(exports, "redirect-url", 1, REDIRECT_URL, 1);
524 for (i=0; i < Argc; i++)
526 s = strsav(s, Argv[i]);
529 exports = map(exports, "invocation", 1, s, 0);
531 exports = map(exports, "options", 1, csp->config->proxy_args, 1);
532 exports = show_defines(exports);
534 #ifdef FEATURE_STATISTICS
535 exports = add_stats(exports);
536 #else /* ndef FEATURE_STATISTICS */
537 exports = map_block_killer(exports, "statistics");
538 #endif /* ndef FEATURE_STATISTICS */
540 if (csp->actions_list)
542 exports = map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
546 exports = map(exports, "actions-filename", 1, "None specified", 1);
551 exports = map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
555 exports = map(exports, "re-filter-filename", 1, "None specified", 1);
561 exports = map(exports, "trust-filename", 1, csp->tlist->filename, 1);
565 exports = map(exports, "trust-filename", 1, "None specified", 1);
568 exports = map_block_killer(exports, "trust-support");
569 #endif /* ndef FEATURE_TRUST */
571 rsp->body = fill_template(csp, "show-status", exports);
578 /*********************************************************************
580 * Function : cgi_show_url_info
582 * Description : CGI function that determines and shows which actions
583 * junkbuster will perform for a given url, and which
584 * matches starting from the defaults have lead to that.
587 * 1 : csp = Current client state (buffers, headers, etc...)
588 * 2 : rsp = http_response data structure for output
589 * 3 : parameters = map of cgi parameters
592 * url : The url whose actions are to be determined.
593 * If url is unset, the url-given conditional will be
594 * set, so that all but the form can be suppressed in
599 *********************************************************************/
600 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
601 struct map *parameters)
603 struct map *exports = default_exports(csp, "show-url-info");
604 char *url_param, *host = NULL;
606 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
608 exports = map_block_killer(exports, "url-given");
609 exports = map(exports, "url", 1, "", 1);
613 char *matches = NULL;
618 struct file_list *fl;
619 struct url_actions *b;
620 struct url_spec url[1];
621 struct current_action_spec action[1];
624 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
626 exports = map(exports, "url", 1, host, 1);
627 exports = map(exports, "url-html", 1, html_encode(host), 0);
629 init_current_action(action);
631 s = current_action_to_text(action);
632 exports = map(exports, "default", 1, s , 0);
634 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
636 exports = map(exports, "matches", 1, "none" , 1);
637 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
640 free_current_action(action);
642 rsp->body = fill_template(csp, "show-url-info", exports);
648 s = strchr(host, '/');
658 s = strchr(host, ':');
668 /* if splitting the domain fails, punt */
669 if (url->dbuf == NULL)
671 exports = map(exports, "matches", 1, "none" , 1);
672 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
676 free_current_action(action);
678 rsp->body = fill_template(csp, "show-url-info", exports);
684 for (b = b->next; NULL != b; b = b->next)
686 if ((b->url->port == 0) || (b->url->port == port))
688 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
690 if ((b->url->path == NULL) ||
692 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
694 (strncmp(b->url->path, path, b->url->pathlen) == 0)
698 s = actions_to_text(b->action);
699 matches = strsav(matches, "<b>{");
700 matches = strsav(matches, s);
701 matches = strsav(matches, " }</b><br>\n<code>");
702 matches = strsav(matches, b->url->spec);
703 matches = strsav(matches, "</code><br>\n<br>\n");
706 merge_current_action(action, b->action);
715 exports = map(exports, "matches", 1, matches , 0);
719 exports = map(exports, "matches", 1, "none", 1);
729 s = current_action_to_text(action);
730 exports = map(exports, "final", 1, s, 0);
733 free_current_action(action);
736 rsp->body = fill_template(csp, "show-url-info", exports);
743 /*********************************************************************
745 * Function : error_response
747 * Description : returns an http_response that explains the reason
748 * why a request failed.
751 * 1 : csp = Current client state (buffers, headers, etc...)
752 * 2 : template = Which template should be used for the answer
753 * 3 : errno = system error number
755 * Returns : NULL if no memory, else http_response
757 *********************************************************************/
758 struct http_response *error_response(struct client_state *csp, const char *template, int err)
760 struct http_response *rsp;
761 struct map *exports = default_exports(csp, NULL);
763 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
768 exports = map(exports, "host-html", 1, html_encode(csp->http->host), 0);
769 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
770 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
771 exports = map(exports, "path", 1, csp->http->path, 1);
772 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
773 exports = map(exports, "error", 1, safe_strerror(err), 0);
774 exports = map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
776 rsp->body = fill_template(csp, template, exports);
779 if (!strcmp(template, "no-such-domain"))
781 rsp->status = strdup("404 No such domain");
783 else if (!strcmp(template, "connect-failed"))
785 rsp->status = strdup("503 Connect failed");
788 return(finish_http_response(rsp));
792 /*********************************************************************
794 * Function : finish_http_response
796 * Description : Fill in the missing headers in an http response,
797 * and flatten the headers to an http head.
800 * 1 : rsp = pointer to http_response to be processed
802 * Returns : http_response, or NULL on failiure
804 *********************************************************************/
805 struct http_response *finish_http_response(struct http_response *rsp)
807 char buf[BUFFER_SIZE];
810 * Fill in the HTTP Status
812 sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
813 enlist_first(rsp->headers, buf);
816 * Set the Content-Length
818 if (rsp->content_length == 0)
820 rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
822 sprintf(buf, "Content-Length: %d", rsp->content_length);
823 enlist(rsp->headers, buf);
826 * Fill in the default headers FIXME: Are these correct? sequence OK? check rfc!
827 * FIXME: Should have:
828 * "JunkBuster" GIF: Last-Modified: any *fixed* date in the past (as now).
829 * Expires: 5 minutes after the time when reply sent
830 * CGI, "blocked", & all other requests:
831 * Last-Modified: Time when reply sent
832 * Expires: Time when reply sent
833 * "Cache-Control: no-cache"
835 * See http://www.w3.org/Protocols/rfc2068/rfc2068
837 enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
838 enlist_unique(rsp->headers, "Expires: Thu Jul 31, 1997 07:42:22 pm GMT", 8);
839 enlist_unique(rsp->headers, "Content-Type: text/html", 13);
840 enlist(rsp->headers, "");
846 if (NULL == (rsp->head = list_to_text(rsp->headers)))
848 free_http_response(rsp);
851 rsp->head_length = strlen(rsp->head);
858 /*********************************************************************
860 * Function : free_http_response
862 * Description : Free the memory occupied by an http_response
863 * and its depandant structures.
866 * 1 : rsp = pointer to http_response to be freed
870 *********************************************************************/
871 void free_http_response(struct http_response *rsp)
878 destroy_list(rsp->headers);
885 /*********************************************************************
887 * Function : fill_template
889 * Description : CGI support function that loads a given HTML
890 * template from the confdir, and fills it in
891 * by replacing @name@ with value using pcrs,
892 * for each item in the output map.
895 * 1 : csp = Current client state (buffers, headers, etc...)
896 * 3 : template = name of the HTML template to be used
897 * 2 : exports = map with fill in symbol -> name pairs
899 * Returns : char * with filled out form, or NULL if failiure
901 *********************************************************************/
902 char *fill_template(struct client_state *csp, const char *template, struct map *exports)
905 pcrs_job *job, *joblist = NULL;
906 char buf[BUFFER_SIZE];
907 char *new, *old = NULL;
913 * Open template file or fail
915 snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, template);
917 if(NULL == (fp = fopen(buf, "r")))
919 log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
925 * Assemble pcrs joblist from exports map
927 for (m = exports; m; m = m->next)
931 /* Enclose name in @@ */
932 snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
934 /* Make and chain job */
935 if ( NULL == (job = (pcrs_compile(buf, m->value, "sigTU", &error))) )
937 log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
948 * Read the file, ignoring comments
950 while (fgets(buf, BUFFER_SIZE, fp))
952 /* skip lines starting with '#' */
953 if(*buf == '#') continue;
955 old = strsav(old, buf);
963 size = strlen(old) + 1;
966 for (job = joblist; NULL != job; job = job->next)
968 pcrs_execute(job, old, size, &new, &size);
969 if (old != buf) free(old);
975 * Free the jobs & return
977 pcrs_free_joblist(joblist);
983 /*********************************************************************
985 * Function : default_exports
987 * Description : returns a struct map list that contains exports
988 * which are common to all CGI functions.
991 * 1 : csp = Current client state (buffers, headers, etc...)
992 * 2 : caller = name of CGI who calls us and which should
993 * be excluded from the generated menu.
994 * Returns : NULL if no memory, else map
996 *********************************************************************/
997 struct map *default_exports(struct client_state *csp, char *caller)
999 struct map *exports = NULL;
1002 exports = map(exports, "version", 1, VERSION, 1);
1003 exports = map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1);
1004 exports = map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1);
1005 exports = map(exports, "admin-address", 1, csp->config->admin_address ? csp->config->admin_address : "fill@me.in.please", 1);
1006 exports = map(exports, "homepage", 1, HOME_PAGE_URL, 1);
1007 exports = map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1);
1008 exports = map(exports, "menu", 1, make_menu(caller), 0);
1009 exports = map(exports, "code-status", 1, CODE_STATUS, 1);
1011 snprintf(buf, 20, "%d", csp->config->hport);
1012 exports = map(exports, "my-port", 1, buf, 1);
1014 if(!strcmp(CODE_STATUS, "stable"))
1016 exports = map_block_killer(exports, "unstable");
1019 if(csp->config->proxy_info_url != NULL)
1021 exports = map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
1025 exports = map_block_killer(exports, "have-proxy-info");
1033 /*********************************************************************
1035 * Function : map_block_killer
1037 * Description : Convenience function.
1038 * Adds a "killer" for the conditional HTML-template
1039 * block <name>, i.e. a substitution of the regex
1040 * "if-<name>-start.*if-<name>-end" to the given
1044 * 1 : exports = map to extend
1045 * 2 : name = name of conditional block
1047 * Returns : extended map
1049 *********************************************************************/
1050 struct map *map_block_killer(struct map *exports, char *name)
1052 char buf[1000]; /* Will do, since the names are hardwired */
1054 snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
1055 exports = map(exports, buf, 1, "", 1);
1062 /*********************************************************************
1064 * Function : map_conditional
1066 * Description : Convenience function.
1067 * Adds an "if-then-else" for the conditional HTML-template
1068 * block <name>, i.e. a substitution of the form:
1075 * The control structure and one of the alternatives
1079 * 1 : exports = map to extend
1080 * 2 : name = name of conditional block
1081 * 3 : choose_first = nonzero for first, zero for second.
1083 * Returns : extended map
1085 *********************************************************************/
1086 struct map *map_conditional(struct map *exports, char *name, int choose_first)
1088 char buf[1000]; /* Will do, since the names are hardwired */
1090 snprintf(buf, 1000, (choose_first
1091 ? "else-not-%s@.*@endif-%s"
1092 : "if-%s-then@.*@else-not-%s"),
1094 exports = map(exports, buf, 1, "", 1);
1096 snprintf(buf, 1000, (choose_first ? "if-%s-then" : "endif-%s"), name);
1097 exports = map(exports, buf, 1, "", 1);
1104 /*********************************************************************
1106 * Function : make_menu
1108 * Description : Returns an HTML-formatted menu of the available
1109 * unhidden CGIs, excluding the one given in <self>.
1111 * Parameters : self = name of CGI to leave out, can be NULL
1113 * Returns : menu string
1115 *********************************************************************/
1116 char *make_menu(const char *self)
1118 const struct cgi_dispatcher *d;
1119 char buf[BUFFER_SIZE], *tmp = NULL;
1121 if (self == NULL) self = "NO-SUCH-CGI!";
1123 /* List available unhidden CGI's and export as "other-cgis" */
1124 for (d = cgi_dispatcher; d->handler; d++)
1126 if (strncmp(d->description, "HIDE", 4) && strcmp(d->name, self))
1128 snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
1129 HOME_PAGE_URL, d->name, d->description);
1130 tmp = strsav(tmp, buf);
1138 /*********************************************************************
1140 * Function : dump_map
1142 * Description : HTML-dump a map for debugging
1145 * 1 : map = map to dump
1147 * Returns : string with HTML
1149 *********************************************************************/
1150 char *dump_map(struct map *map)
1152 struct map *p = map;
1155 ret = strsav(ret, "<table>\n");
1159 ret = strsav(ret, "<tr><td><b>");
1160 ret = strsav(ret, p->name);
1161 ret = strsav(ret, "</b></td><td>");
1162 ret = strsav(ret, p->value);
1163 ret = strsav(ret, "</td></tr>\n");
1167 ret = strsav(ret, "</table>\n");
1173 #ifdef FEATURE_STATISTICS
1174 /*********************************************************************
1176 * Function : add_stats
1178 * Description : Add the blocking statistics to a given map.
1181 * 1 : exports = map to write to.
1183 * Returns : pointer to extended map
1185 *********************************************************************/
1186 struct map *add_stats(struct map *exports)
1188 float perc_rej; /* Percentage of http requests rejected */
1190 int local_urls_read = urls_read;
1191 int local_urls_rejected = urls_rejected;
1194 * Need to alter the stats not to include the fetch of this
1197 * Can't do following thread safely! doh!
1200 * urls_rejected--; * This will be incremented subsequently *
1203 if (local_urls_read == 0)
1205 exports = map_block_killer(exports, "have-stats");
1209 exports = map_block_killer(exports, "have-no-stats");
1211 perc_rej = (float)local_urls_rejected * 100.0F /
1212 (float)local_urls_read;
1214 sprintf(buf, "%d", local_urls_read);
1215 exports = map(exports, "requests-received", 1, buf, 1);
1217 sprintf(buf, "%d", local_urls_rejected);
1218 exports = map(exports, "requests-blocked", 1, buf, 1);
1220 sprintf(buf, "%6.2f", perc_rej);
1221 exports = map(exports, "percent-blocked", 1, buf, 1);
1227 #endif /* def FEATURE_STATISTICS */