1 const char cgi_rcs[] = "$Id: cgi.c,v 1.9 2001/06/29 21:45:41 oes 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.9 2001/06/29 21:45:41 oes
40 * Indentation, CRLF->LF, Tab-> Space
42 * Revision 1.8 2001/06/29 13:21:46 oes
43 * - Cosmetics: renamed and reordered functions, variables,
44 * texts, improved comments etc
46 * - Removed ij_untrusted_url() The relevant
47 * info is now part of the "untrusted" page,
48 * which is generated by filters.c:trust_url()
50 * - Generators of content now call finish_http_response()
51 * themselves, making jcc.c:chat() a little less
54 * - Removed obsolete "Pragma: no-cache" from our headers
56 * - http_responses now know their head length
58 * - fill_template now uses the new interface to pcrs, so that
59 * - long jobs (like whole files) no longer have to be assembled
60 * in a fixed size buffer
61 * - the new T (trivial) option is used, and the replacement may
62 * contain Perl syntax backrefs without confusing pcrs
64 * - Introduced default_exports() which generates a set of exports
65 * common to all CGIs and other content generators
67 * - Introduced convenience function map_block_killer()
69 * - Introduced convenience function make_menu()
71 * - Introduced CGI-like function error_response() which generates
72 * the "No such domain" and "Connect failed" messages using the
75 * - cgi_show_url_info:
76 * - adapted to new CGI features
77 * - form and answers now generated from same template
78 * - http:// prefix in URL now OK
81 * - adapted to new CGI features
82 * - no longer uses csp->init_proxy_args
85 * - moved menu generation to make_menu()
87 * - add_stats now writes single export map entries instead
90 * - Moved redirect_url() to filters.c
92 * - Fixed mem leak in free_http_response(), map_block_killer(),
94 * - Removed logentry from cancelled commit
96 * Revision 1.7 2001/06/09 10:51:58 jongfoster
97 * Changing "show URL info" handler to new style.
98 * Changing BUFSIZ ==> BUFFER_SIZE
100 * Revision 1.6 2001/06/07 23:05:19 jongfoster
101 * Removing code related to old forward and ACL files.
103 * Revision 1.5 2001/06/05 19:59:16 jongfoster
104 * Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
106 * Revision 1.4 2001/06/04 10:41:52 swa
107 * show version string of cgi.h and cgi.c
109 * Revision 1.3 2001/06/03 19:12:16 oes
110 * introduced new cgi handling
112 * No revisions before 1.3
114 **********************************************************************/
120 #include <sys/types.h>
126 #define snprintf _snprintf
127 #endif /* def _WIN32 */
139 #include "miscutil.h"
140 #include "showargs.h"
142 const char cgi_h_rcs[] = CGI_H_VERSION;
144 const struct cgi_dispatcher cgi_dispatcher[] = {
147 "Show information about the version and configuration" },
149 13, cgi_show_url_info,
150 "Show which actions apply to a URL and why" },
153 "HIDE Send the transparent or \"Junkbuster\" gif" },
156 "Junkbuster main page" },
157 { NULL, 0, NULL, NULL }
161 /*********************************************************************
163 * Function : dispatch_cgi
165 * Description : Checks if a request URL has either the magical hostname
166 * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses
167 * the (rest of the) path as a cgi name plus query string,
168 * prepares a map that maps CGI parameter names to their values,
169 * initializes the http_response struct, and calls the
170 * relevant CGI handler function.
173 * 1 : csp = Current client state (buffers, headers, etc...)
175 * Returns : http_response if match, NULL if nonmatch or handler fail
177 *********************************************************************/
178 struct http_response *dispatch_cgi(struct client_state *csp)
180 char *argstring = NULL;
181 const struct cgi_dispatcher *d;
182 struct map *param_list;
183 struct http_response *rsp;
186 * Should we intercept ?
189 /* Either the host matches CGI_PREFIX_HOST ..*/
190 if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST))
192 /* ..then the path will all be for us */
193 argstring = csp->http->path;
195 /* Or it's the host part HOME_PAGE_URL, and the path /config ? */
196 else if ( (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 ))
197 && (0 == strncmpic(csp->http->path,"/config", 7))
198 && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
200 /* then it's everything following "/config" */
201 argstring = csp->http->path + 7;
209 * This is a CGI call.
212 /* Get mem for response or fail*/
213 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
218 /* Remove leading slash */
219 if (*argstring == '/')
224 log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
225 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
226 csp->ip_addr_str, csp->http->cmd);
228 /* Find and start the right CGI function*/
229 for (d = cgi_dispatcher; d->handler; d++)
231 if (strncmp(argstring, d->name, d->name_length) == 0)
233 param_list = parse_cgi_parameters(argstring + d->name_length);
234 if ((d->handler)(csp, rsp, param_list))
239 free_map(param_list);
240 return(finish_http_response(rsp));
244 /* Can't get here, since cgi_default will match all requests */
251 /*********************************************************************
253 * Function : parse_cgi_parameters
255 * Description : Parse a URL-encoded argument string into name/value
256 * pairs and store them in a struct map list.
259 * 1 : string = string to be parsed
261 * Returns : poniter to param list, or NULL if failiure
263 *********************************************************************/
264 struct map *parse_cgi_parameters(char *argstring)
267 char *vector[BUFFER_SIZE];
269 struct map *cgi_params = NULL;
271 if(*argstring == '?') argstring++;
272 tmp = strdup(argstring);
274 pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1);
276 for (i = 0; i < pairs; i++)
278 if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
281 cgi_params = map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
291 /*********************************************************************
293 * Function : cgi_default
295 * Description : CGI function that is called if no action was given.
296 * Lists menu of available unhidden CGIs.
299 * 1 : csp = Current client state (buffers, headers, etc...)
300 * 2 : rsp = http_response data structure for output
301 * 3 : parameters = map of cgi parameters
305 *********************************************************************/
306 int cgi_default(struct client_state *csp, struct http_response *rsp,
307 struct map *parameters)
309 char *p, *tmp = NULL;
310 struct map *exports = default_exports(csp, "");
312 /* If there were other parameters, export a dump as "cgi-parameters" */
315 p = dump_map(parameters);
316 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
317 "Anyway, here they are, in case you're interested:</p>\n");
318 tmp = strsav(tmp, p);
319 exports = map(exports, "cgi-parameters", 1, tmp, 0);
324 exports = map(exports, "cgi-parameters", 1, "", 1);
327 rsp->body = fill_template(csp, "default", exports);
334 /*********************************************************************
336 * Function : cgi_send_banner
338 * Description : CGI function that returns a banner.
341 * 1 : csp = Current client state (buffers, headers, etc...)
342 * 2 : rsp = http_response data structure for output
343 * 3 : parameters = map of cgi parameters
346 * type : Selects the type of banner between "trans" and "jb".
347 * Defaults to "jb" if absent or != "trans".
351 *********************************************************************/
352 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
353 struct map *parameters)
355 if(strcmp(lookup(parameters, "type"), "trans"))
357 rsp->body = bindup(JBGIF, sizeof(JBGIF));
358 rsp->content_length = sizeof(JBGIF);
362 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
363 rsp->content_length = sizeof(BLANKGIF);
366 enlist(rsp->headers, "Content-Type: image/gif");
373 /*********************************************************************
375 * Function : cgi_show_status
377 * Description : CGI function that returns a a web page describing the
378 * current status of IJB.
381 * 1 : csp = Current client state (buffers, headers, etc...)
382 * 2 : rsp = http_response data structure for output
383 * 3 : parameters = map of cgi parameters
386 * type : Selects the type of banner between "trans" and "jb".
387 * Defaults to "jb" if absent or != "trans".
391 *********************************************************************/
392 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
393 struct map *parameters)
397 struct map *exports = default_exports(csp, "show-status");
399 #ifdef SPLIT_PROXY_ARGS
401 char buf[BUFFER_SIZE];
403 const char * filename = NULL;
404 char * file_description = NULL;
407 p = lookup(parameters, "file");
411 if (csp->actions_list)
413 filename = csp->actions_list->filename;
414 file_description = "Actions List";
421 filename = csp->rlist->filename;
422 file_description = "Regex Filter List";
430 filename = csp->tlist->filename;
431 file_description = "Trust List";
434 #endif /* def TRUST_FILES */
437 if (NULL != filename)
439 exports = map(exports, "file-description", 1, file_description, 1);
440 exports = map(exports, "filepath", 1, html_encode(filename), 0);
442 if ((fp = fopen(filename, "r")) == NULL)
444 exports = map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
448 while (fgets(buf, sizeof(buf), fp))
450 p = html_encode(buf);
455 s = strsav(s, "<br>");
459 exports = map(exports, "contents", 1, s, 0);
461 rsp->body = fill_template(csp, "show-status-file", exports);
467 #endif /* def SPLIT_PROXY_ARGS */
469 exports = map(exports, "redirect-url", 1, REDIRECT_URL, 1);
472 for (i=0; i < Argc; i++)
474 s = strsav(s, Argv[i]);
477 exports = map(exports, "invocation", 1, s, 0);
479 exports = map(exports, "options", 1, csp->config->proxy_args, 1);
481 exports = map(exports, "sourceversions", 1, s, 0);
483 exports = map(exports, "defines", 1, s, 0);
486 exports = add_stats(exports);
488 exports = map_block_killer(exports, "statistics");
489 #endif /* ndef STATISTICS */
491 #ifdef SPLIT_PROXY_ARGS
493 exports = map_block_killer(exports, "no-split-args");
495 if (csp->actions_list)
497 exports = map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
501 exports = map(exports, "actions-filename", 1, "None specified", 1);
506 exports = map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
510 exports = map(exports, "re-filter-filename", 1, "None specified", 1);
516 exports = map(exports, "trust-filename", 1, csp->tlist->filename, 1);
520 exports = map(exports, "trust-filename", 1, "None specified", 1);
523 exports = map_block_killer(exports, "trust-support");
524 #endif /* ndef TRUST_FILES */
526 #else /* ifndef SPLIT_PROXY_ARGS */
527 exports = map_block_killer(exports, "split-args");
531 map(exports, "clist", 1, csp->clist->proxy_args , 1);
536 map(exports, "rlist", 1, csp->rlist->proxy_args , 1);
542 map(exports, "tlist", 1, csp->tlist->proxy_args , 1);
544 #endif /* def TRUST_FILES */
546 #endif /* ndef SPLIT_PROXY_ARGS */
548 rsp->body = fill_template(csp, "show-status", exports);
555 /*********************************************************************
557 * Function : cgi_show_url_info
559 * Description : CGI function that determines and shows which actions
560 * junkbuster will perform for a given url, and which
561 * matches starting from the defaults have lead to that.
564 * 1 : csp = Current client state (buffers, headers, etc...)
565 * 2 : rsp = http_response data structure for output
566 * 3 : parameters = map of cgi parameters
569 * url : The url whose actions are to be determined.
570 * If url is unset, the url-given conditional will be
571 * set, so that all but the form can be suppressed in
576 *********************************************************************/
577 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
578 struct map *parameters)
580 struct map *exports = default_exports(csp, "show-url-info");
581 char *url_param, *host = NULL;
583 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
585 exports = map_block_killer(exports, "url-given");
586 exports = map(exports, "url", 1, "", 1);
590 char *matches = NULL;
595 struct file_list *fl;
596 struct url_actions *b;
597 struct url_spec url[1];
598 struct current_action_spec action[1];
601 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
603 exports = map(exports, "url", 1, host, 1);
604 exports = map(exports, "url-html", 1, html_encode(host), 0);
606 init_current_action(action);
608 s = current_action_to_text(action);
609 exports = map(exports, "default", 1, s , 0);
611 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
613 exports = map(exports, "matches", 1, "none" , 1);
614 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
617 free_current_action(action);
619 rsp->body = fill_template(csp, "show-url-info", exports);
625 s = strchr(host, '/');
635 s = strchr(host, ':');
645 /* if splitting the domain fails, punt */
646 if (url->dbuf == NULL)
648 exports = map(exports, "matches", 1, "none" , 1);
649 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
653 free_current_action(action);
655 rsp->body = fill_template(csp, "show-url-info", exports);
661 for (b = b->next; NULL != b; b = b->next)
663 if ((b->url->port == 0) || (b->url->port == port))
665 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
667 if ((b->url->path == NULL) ||
669 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
671 (strncmp(b->url->path, path, b->url->pathlen) == 0)
675 s = actions_to_text(b->action);
676 matches = strsav(matches, "<b>{");
677 matches = strsav(matches, s);
678 matches = strsav(matches, " }</b><br>\n<code>");
679 matches = strsav(matches, b->url->spec);
680 matches = strsav(matches, "</code><br>\n<br>\n");
683 merge_current_action(action, b->action);
692 exports = map(exports, "matches", 1, matches , 0);
696 exports = map(exports, "matches", 1, "none", 1);
706 s = current_action_to_text(action);
707 exports = map(exports, "final", 1, s, 0);
710 free_current_action(action);
713 rsp->body = fill_template(csp, "show-url-info", exports);
720 /*********************************************************************
722 * Function : error_response
724 * Description : returns an http_response that explains the reason
725 * why a request failed.
728 * 1 : csp = Current client state (buffers, headers, etc...)
729 * 2 : template = Which template should be used for the answer
730 * 3 : errno = system error number
732 * Returns : NULL if no memory, else http_response
734 *********************************************************************/
735 struct http_response *error_response(struct client_state *csp, const char *template, int err)
737 struct http_response *rsp;
738 struct map *exports = default_exports(csp, NULL);
740 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
745 exports = map(exports, "host-html", 1, html_encode(csp->http->host), 0);
746 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
747 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
748 exports = map(exports, "path", 1, csp->http->path, 1);
749 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
750 exports = map(exports, "error", 1, safe_strerror(err), 0);
751 exports = map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
753 rsp->body = fill_template(csp, template, exports);
756 if (!strcmp(template, "no-such-domain"))
758 rsp->status = strdup("404 No such domain");
760 else if (!strcmp(template, "connect-failed"))
762 rsp->status = strdup("503 Connect failed");
765 return(finish_http_response(rsp));
769 /*********************************************************************
771 * Function : finish_http_response
773 * Description : Fill in the missing headers in an http response,
774 * and flatten the headers to an http head.
777 * 1 : rsp = pointer to http_response to be processed
779 * Returns : http_response, or NULL on failiure
781 *********************************************************************/
782 struct http_response *finish_http_response(struct http_response *rsp)
784 char buf[BUFFER_SIZE];
787 * Fill in the HTTP Status
789 sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
790 enlist_first(rsp->headers, buf);
793 * Set the Content-Length
795 if (rsp->content_length == 0)
797 rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
799 sprintf(buf, "Content-Length: %d", rsp->content_length);
800 enlist(rsp->headers, buf);
803 * Fill in the default headers FIXME: Are these correct? sequence OK? check rfc!
805 enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
806 enlist_unique(rsp->headers, "Expires: Thu Jul 31, 1997 07:42:22 pm GMT", 8);
807 enlist_unique(rsp->headers, "Content-Type: text/html", 13);
808 enlist(rsp->headers, "");
814 if (NULL == (rsp->head = list_to_text(rsp->headers)))
816 free_http_response(rsp);
819 rsp->head_length = strlen(rsp->head);
826 /*********************************************************************
828 * Function : free_http_response
830 * Description : Free the memory occupied by an http_response
831 * and its depandant structures.
834 * 1 : rsp = pointer to http_response to be freed
838 *********************************************************************/
839 void free_http_response(struct http_response *rsp)
846 destroy_list(rsp->headers);
853 /*********************************************************************
855 * Function : fill_template
857 * Description : CGI support function that loads a given HTML
858 * template from the confdir, and fills it in
859 * by replacing @name@ with value using pcrs,
860 * for each item in the output map.
863 * 1 : csp = Current client state (buffers, headers, etc...)
864 * 3 : template = name of the HTML template to be used
865 * 2 : exports = map with fill in symbol -> name pairs
867 * Returns : char * with filled out form, or NULL if failiure
869 *********************************************************************/
870 char *fill_template(struct client_state *csp, const char *template, struct map *exports)
873 pcrs_job *job, *joblist = NULL;
874 char buf[BUFFER_SIZE];
875 char *new, *old = NULL;
881 * Open template file or fail
883 snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, template);
885 if(NULL == (fp = fopen(buf, "r")))
887 log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
893 * Assemble pcrs joblist from exports map
895 for (m = exports; m; m = m->next)
899 /* Enclose name in @@ */
900 snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
902 /* Make and chain in job */
903 if ( NULL == (job = (pcrs_make_job(buf, m->value, "sigTU", &error))) )
905 log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
916 * Read the file, ignoring comments
918 while (fgets(buf, BUFFER_SIZE, fp))
920 /* skip lines starting with '#' */
921 if(*buf == '#') continue;
923 old = strsav(old, buf);
931 size = strlen(old) + 1;
934 for (job = joblist; NULL != job; job = job->next)
936 pcrs_execute(job, old, size, &new, &size);
937 if (old != buf) free(old);
943 * Free the jobs & return
945 pcrs_free_joblist(joblist);
951 /*********************************************************************
953 * Function : default_exports
955 * Description : returns a struct map list that contains exports
956 * which are common to all CGI functions.
959 * 1 : csp = Current client state (buffers, headers, etc...)
960 * 2 : caller = name of CGI who calls us and which should
961 * be excluded from the generated menu.
962 * Returns : NULL if no memory, else map
964 *********************************************************************/
965 struct map *default_exports(struct client_state *csp, char *caller)
967 struct map *exports = NULL;
970 exports = map(exports, "version", 1, VERSION, 1);
971 exports = map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1);
972 exports = map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1);
973 exports = map(exports, "admin-address", 1, csp->config->admin_address ? csp->config->admin_address : "fill@me.in.please", 1);
974 exports = map(exports, "homepage", 1, HOME_PAGE_URL, 1);
975 exports = map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1);
976 exports = map(exports, "menu", 1, make_menu(caller), 0);
977 exports = map(exports, "code-status", 1, CODE_STATUS, 1);
979 snprintf(buf, 20, "%d", csp->config->hport);
980 exports = map(exports, "my-port", 1, buf, 1);
982 if(!strcmp(CODE_STATUS, "stable"))
984 exports = map_block_killer(exports, "unstable");
987 if(csp->config->proxy_info_url != NULL)
989 exports = map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
993 exports = map_block_killer(exports, "have-proxy-info");
1001 /*********************************************************************
1003 * Function : map_block_killer
1005 * Description : Convenience function.
1006 * Adds a "killer" for the conditional HTML-template
1007 * block <name>, i.e. a substitution of the regex
1008 * "if-<name>-start.*if-<name>-end" to the given
1012 * 1 : exports = map to extend
1013 * 2 : name = name of conditional block
1015 * Returns : extended map
1017 *********************************************************************/
1018 struct map *map_block_killer(struct map *exports, char *name)
1020 char buf[1000]; /* Will do, since the names are hardwired */
1022 snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
1023 exports = map(exports, buf, 1, "", 1);
1030 /*********************************************************************
1032 * Function : make_menu
1034 * Description : Returns an HTML-formatted menu of the available
1035 * unhidden CGIs, excluding the one given in <self>.
1037 * Parameters : self = name of CGI to leave out, can be NULL
1039 * Returns : menu string
1041 *********************************************************************/
1042 char *make_menu(const char *self)
1044 const struct cgi_dispatcher *d;
1045 char buf[BUFFER_SIZE], *tmp = NULL;
1047 if (self == NULL) self = "NO-SUCH-CGI!";
1049 /* List available unhidden CGI's and export as "other-cgis" */
1050 for (d = cgi_dispatcher; d->handler; d++)
1052 if (strncmp(d->description, "HIDE", 4) && strcmp(d->name, self))
1054 snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
1055 HOME_PAGE_URL, d->name, d->description);
1056 tmp = strsav(tmp, buf);
1064 /*********************************************************************
1066 * Function : dump_map
1068 * Description : HTML-dump a map for debugging
1071 * 1 : map = map to dump
1073 * Returns : string with HTML
1075 *********************************************************************/
1076 char *dump_map(struct map *map)
1078 struct map *p = map;
1081 ret = strsav(ret, "<table>\n");
1085 ret = strsav(ret, "<tr><td><b>");
1086 ret = strsav(ret, p->name);
1087 ret = strsav(ret, "</b></td><td>");
1088 ret = strsav(ret, p->value);
1089 ret = strsav(ret, "</td></tr>\n");
1093 ret = strsav(ret, "</table>\n");
1100 /*********************************************************************
1102 * Function : add_stats
1104 * Description : Add the blocking statistics to a given map.
1107 * 1 : exports = map to write to.
1109 * Returns : pointer to extended map
1111 *********************************************************************/
1112 struct map *add_stats(struct map *exports)
1114 float perc_rej; /* Percentage of http requests rejected */
1116 int local_urls_read = urls_read;
1117 int local_urls_rejected = urls_rejected;
1120 * Need to alter the stats not to include the fetch of this
1123 * Can't do following thread safely! doh!
1126 * urls_rejected--; * This will be incremented subsequently *
1129 if (local_urls_read == 0)
1131 exports = map_block_killer(exports, "have-stats");
1135 exports = map_block_killer(exports, "have-no-stats");
1137 perc_rej = (float)local_urls_rejected * 100.0F /
1138 (float)local_urls_read;
1140 sprintf(buf, "%d", local_urls_read);
1141 exports = map(exports, "requests-received", 1, buf, 1);
1143 sprintf(buf, "%d", local_urls_rejected);
1144 exports = map(exports, "requests-blocked", 1, buf, 1);
1146 sprintf(buf, "%6.2f", perc_rej);
1147 exports = map(exports, "percent-blocked", 1, buf, 1);
1153 #endif /* def STATISTICS */