1 const char cgi_rcs[] = "$Id: cgi.c,v 1.12 2001/07/29 18:47:05 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.12 2001/07/29 18:47:05 jongfoster
40 * Adding missing #include "loadcfg.h"
42 * Revision 1.11 2001/07/18 17:24:37 oes
43 * Changed to conform to new pcrs interface
45 * Revision 1.10 2001/07/13 13:53:13 oes
46 * Removed all #ifdef PCRS and related code
48 * Revision 1.9 2001/06/29 21:45:41 oes
49 * Indentation, CRLF->LF, Tab-> Space
51 * Revision 1.8 2001/06/29 13:21:46 oes
52 * - Cosmetics: renamed and reordered functions, variables,
53 * texts, improved comments etc
55 * - Removed ij_untrusted_url() The relevant
56 * info is now part of the "untrusted" page,
57 * which is generated by filters.c:trust_url()
59 * - Generators of content now call finish_http_response()
60 * themselves, making jcc.c:chat() a little less
63 * - Removed obsolete "Pragma: no-cache" from our headers
65 * - http_responses now know their head length
67 * - fill_template now uses the new interface to pcrs, so that
68 * - long jobs (like whole files) no longer have to be assembled
69 * in a fixed size buffer
70 * - the new T (trivial) option is used, and the replacement may
71 * contain Perl syntax backrefs without confusing pcrs
73 * - Introduced default_exports() which generates a set of exports
74 * common to all CGIs and other content generators
76 * - Introduced convenience function map_block_killer()
78 * - Introduced convenience function make_menu()
80 * - Introduced CGI-like function error_response() which generates
81 * the "No such domain" and "Connect failed" messages using the
84 * - cgi_show_url_info:
85 * - adapted to new CGI features
86 * - form and answers now generated from same template
87 * - http:// prefix in URL now OK
90 * - adapted to new CGI features
91 * - no longer uses csp->init_proxy_args
94 * - moved menu generation to make_menu()
96 * - add_stats now writes single export map entries instead
99 * - Moved redirect_url() to filters.c
101 * - Fixed mem leak in free_http_response(), map_block_killer(),
103 * - Removed logentry from cancelled commit
105 * Revision 1.7 2001/06/09 10:51:58 jongfoster
106 * Changing "show URL info" handler to new style.
107 * Changing BUFSIZ ==> BUFFER_SIZE
109 * Revision 1.6 2001/06/07 23:05:19 jongfoster
110 * Removing code related to old forward and ACL files.
112 * Revision 1.5 2001/06/05 19:59:16 jongfoster
113 * Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
115 * Revision 1.4 2001/06/04 10:41:52 swa
116 * show version string of cgi.h and cgi.c
118 * Revision 1.3 2001/06/03 19:12:16 oes
119 * introduced new cgi handling
121 * No revisions before 1.3
123 **********************************************************************/
129 #include <sys/types.h>
135 #define snprintf _snprintf
136 #endif /* def _WIN32 */
147 #include "miscutil.h"
148 #include "showargs.h"
151 const char cgi_h_rcs[] = CGI_H_VERSION;
153 const struct cgi_dispatcher cgi_dispatcher[] = {
156 "Show information about the version and configuration" },
158 13, cgi_show_url_info,
159 "Show which actions apply to a URL and why" },
162 "HIDE Send the transparent or \"Junkbuster\" gif" },
165 "Junkbuster main page" },
166 { NULL, 0, NULL, NULL }
170 /*********************************************************************
172 * Function : dispatch_cgi
174 * Description : Checks if a request URL has either the magical hostname
175 * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses
176 * the (rest of the) path as a cgi name plus query string,
177 * prepares a map that maps CGI parameter names to their values,
178 * initializes the http_response struct, and calls the
179 * relevant CGI handler function.
182 * 1 : csp = Current client state (buffers, headers, etc...)
184 * Returns : http_response if match, NULL if nonmatch or handler fail
186 *********************************************************************/
187 struct http_response *dispatch_cgi(struct client_state *csp)
189 char *argstring = NULL;
190 const struct cgi_dispatcher *d;
191 struct map *param_list;
192 struct http_response *rsp;
195 * Should we intercept ?
198 /* Either the host matches CGI_PREFIX_HOST ..*/
199 if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST))
201 /* ..then the path will all be for us */
202 argstring = csp->http->path;
204 /* Or it's the host part HOME_PAGE_URL, and the path /config ? */
205 else if ( (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 ))
206 && (0 == strncmpic(csp->http->path,"/config", 7))
207 && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
209 /* then it's everything following "/config" */
210 argstring = csp->http->path + 7;
218 * This is a CGI call.
221 /* Get mem for response or fail*/
222 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
227 /* Remove leading slash */
228 if (*argstring == '/')
233 log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
234 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
235 csp->ip_addr_str, csp->http->cmd);
237 /* Find and start the right CGI function*/
238 for (d = cgi_dispatcher; d->handler; d++)
240 if (strncmp(argstring, d->name, d->name_length) == 0)
242 param_list = parse_cgi_parameters(argstring + d->name_length);
243 if ((d->handler)(csp, rsp, param_list))
248 free_map(param_list);
249 return(finish_http_response(rsp));
253 /* Can't get here, since cgi_default will match all requests */
260 /*********************************************************************
262 * Function : parse_cgi_parameters
264 * Description : Parse a URL-encoded argument string into name/value
265 * pairs and store them in a struct map list.
268 * 1 : string = string to be parsed
270 * Returns : poniter to param list, or NULL if failiure
272 *********************************************************************/
273 struct map *parse_cgi_parameters(char *argstring)
276 char *vector[BUFFER_SIZE];
278 struct map *cgi_params = NULL;
280 if(*argstring == '?') argstring++;
281 tmp = strdup(argstring);
283 pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1);
285 for (i = 0; i < pairs; i++)
287 if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
290 cgi_params = map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
300 /*********************************************************************
302 * Function : cgi_default
304 * Description : CGI function that is called if no action was given.
305 * Lists menu of available unhidden CGIs.
308 * 1 : csp = Current client state (buffers, headers, etc...)
309 * 2 : rsp = http_response data structure for output
310 * 3 : parameters = map of cgi parameters
314 *********************************************************************/
315 int cgi_default(struct client_state *csp, struct http_response *rsp,
316 struct map *parameters)
318 char *p, *tmp = NULL;
319 struct map *exports = default_exports(csp, "");
321 /* If there were other parameters, export a dump as "cgi-parameters" */
324 p = dump_map(parameters);
325 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
326 "Anyway, here they are, in case you're interested:</p>\n");
327 tmp = strsav(tmp, p);
328 exports = map(exports, "cgi-parameters", 1, tmp, 0);
333 exports = map(exports, "cgi-parameters", 1, "", 1);
336 rsp->body = fill_template(csp, "default", exports);
343 /*********************************************************************
345 * Function : cgi_send_banner
347 * Description : CGI function that returns a banner.
350 * 1 : csp = Current client state (buffers, headers, etc...)
351 * 2 : rsp = http_response data structure for output
352 * 3 : parameters = map of cgi parameters
355 * type : Selects the type of banner between "trans" and "jb".
356 * Defaults to "jb" if absent or != "trans".
360 *********************************************************************/
361 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
362 struct map *parameters)
364 if(strcmp(lookup(parameters, "type"), "trans"))
366 rsp->body = bindup(JBGIF, sizeof(JBGIF));
367 rsp->content_length = sizeof(JBGIF);
371 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
372 rsp->content_length = sizeof(BLANKGIF);
375 enlist(rsp->headers, "Content-Type: image/gif");
382 /*********************************************************************
384 * Function : cgi_show_status
386 * Description : CGI function that returns a a web page describing the
387 * current status of IJB.
390 * 1 : csp = Current client state (buffers, headers, etc...)
391 * 2 : rsp = http_response data structure for output
392 * 3 : parameters = map of cgi parameters
395 * type : Selects the type of banner between "trans" and "jb".
396 * Defaults to "jb" if absent or != "trans".
400 *********************************************************************/
401 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
402 struct map *parameters)
406 struct map *exports = default_exports(csp, "show-status");
409 char buf[BUFFER_SIZE];
411 const char * filename = NULL;
412 char * file_description = NULL;
415 p = lookup(parameters, "file");
419 if (csp->actions_list)
421 filename = csp->actions_list->filename;
422 file_description = "Actions List";
429 filename = csp->rlist->filename;
430 file_description = "Regex Filter List";
438 filename = csp->tlist->filename;
439 file_description = "Trust List";
442 #endif /* def FEATURE_TRUST */
445 if (NULL != filename)
447 exports = map(exports, "file-description", 1, file_description, 1);
448 exports = map(exports, "filepath", 1, html_encode(filename), 0);
450 if ((fp = fopen(filename, "r")) == NULL)
452 exports = map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
456 while (fgets(buf, sizeof(buf), fp))
458 p = html_encode(buf);
463 s = strsav(s, "<br>");
467 exports = map(exports, "contents", 1, s, 0);
469 rsp->body = fill_template(csp, "show-status-file", exports);
475 exports = map(exports, "redirect-url", 1, REDIRECT_URL, 1);
478 for (i=0; i < Argc; i++)
480 s = strsav(s, Argv[i]);
483 exports = map(exports, "invocation", 1, s, 0);
485 exports = map(exports, "options", 1, csp->config->proxy_args, 1);
487 exports = map(exports, "sourceversions", 1, s, 0);
489 exports = map(exports, "defines", 1, s, 0);
491 #ifdef FEATURE_STATISTICS
492 exports = add_stats(exports);
493 #else /* ndef FEATURE_STATISTICS */
494 exports = map_block_killer(exports, "statistics");
495 #endif /* ndef FEATURE_STATISTICS */
497 exports = map_block_killer(exports, "no-split-args");
499 if (csp->actions_list)
501 exports = map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
505 exports = map(exports, "actions-filename", 1, "None specified", 1);
510 exports = map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
514 exports = map(exports, "re-filter-filename", 1, "None specified", 1);
520 exports = map(exports, "trust-filename", 1, csp->tlist->filename, 1);
524 exports = map(exports, "trust-filename", 1, "None specified", 1);
527 exports = map_block_killer(exports, "trust-support");
528 #endif /* ndef FEATURE_TRUST */
530 rsp->body = fill_template(csp, "show-status", exports);
537 /*********************************************************************
539 * Function : cgi_show_url_info
541 * Description : CGI function that determines and shows which actions
542 * junkbuster will perform for a given url, and which
543 * matches starting from the defaults have lead to that.
546 * 1 : csp = Current client state (buffers, headers, etc...)
547 * 2 : rsp = http_response data structure for output
548 * 3 : parameters = map of cgi parameters
551 * url : The url whose actions are to be determined.
552 * If url is unset, the url-given conditional will be
553 * set, so that all but the form can be suppressed in
558 *********************************************************************/
559 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
560 struct map *parameters)
562 struct map *exports = default_exports(csp, "show-url-info");
563 char *url_param, *host = NULL;
565 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
567 exports = map_block_killer(exports, "url-given");
568 exports = map(exports, "url", 1, "", 1);
572 char *matches = NULL;
577 struct file_list *fl;
578 struct url_actions *b;
579 struct url_spec url[1];
580 struct current_action_spec action[1];
583 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
585 exports = map(exports, "url", 1, host, 1);
586 exports = map(exports, "url-html", 1, html_encode(host), 0);
588 init_current_action(action);
590 s = current_action_to_text(action);
591 exports = map(exports, "default", 1, s , 0);
593 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
595 exports = map(exports, "matches", 1, "none" , 1);
596 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
599 free_current_action(action);
601 rsp->body = fill_template(csp, "show-url-info", exports);
607 s = strchr(host, '/');
617 s = strchr(host, ':');
627 /* if splitting the domain fails, punt */
628 if (url->dbuf == NULL)
630 exports = map(exports, "matches", 1, "none" , 1);
631 exports = map(exports, "final", 1, lookup(exports, "default"), 1);
635 free_current_action(action);
637 rsp->body = fill_template(csp, "show-url-info", exports);
643 for (b = b->next; NULL != b; b = b->next)
645 if ((b->url->port == 0) || (b->url->port == port))
647 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
649 if ((b->url->path == NULL) ||
651 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
653 (strncmp(b->url->path, path, b->url->pathlen) == 0)
657 s = actions_to_text(b->action);
658 matches = strsav(matches, "<b>{");
659 matches = strsav(matches, s);
660 matches = strsav(matches, " }</b><br>\n<code>");
661 matches = strsav(matches, b->url->spec);
662 matches = strsav(matches, "</code><br>\n<br>\n");
665 merge_current_action(action, b->action);
674 exports = map(exports, "matches", 1, matches , 0);
678 exports = map(exports, "matches", 1, "none", 1);
688 s = current_action_to_text(action);
689 exports = map(exports, "final", 1, s, 0);
692 free_current_action(action);
695 rsp->body = fill_template(csp, "show-url-info", exports);
702 /*********************************************************************
704 * Function : error_response
706 * Description : returns an http_response that explains the reason
707 * why a request failed.
710 * 1 : csp = Current client state (buffers, headers, etc...)
711 * 2 : template = Which template should be used for the answer
712 * 3 : errno = system error number
714 * Returns : NULL if no memory, else http_response
716 *********************************************************************/
717 struct http_response *error_response(struct client_state *csp, const char *template, int err)
719 struct http_response *rsp;
720 struct map *exports = default_exports(csp, NULL);
722 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
727 exports = map(exports, "host-html", 1, html_encode(csp->http->host), 0);
728 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
729 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
730 exports = map(exports, "path", 1, csp->http->path, 1);
731 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
732 exports = map(exports, "error", 1, safe_strerror(err), 0);
733 exports = map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
735 rsp->body = fill_template(csp, template, exports);
738 if (!strcmp(template, "no-such-domain"))
740 rsp->status = strdup("404 No such domain");
742 else if (!strcmp(template, "connect-failed"))
744 rsp->status = strdup("503 Connect failed");
747 return(finish_http_response(rsp));
751 /*********************************************************************
753 * Function : finish_http_response
755 * Description : Fill in the missing headers in an http response,
756 * and flatten the headers to an http head.
759 * 1 : rsp = pointer to http_response to be processed
761 * Returns : http_response, or NULL on failiure
763 *********************************************************************/
764 struct http_response *finish_http_response(struct http_response *rsp)
766 char buf[BUFFER_SIZE];
769 * Fill in the HTTP Status
771 sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
772 enlist_first(rsp->headers, buf);
775 * Set the Content-Length
777 if (rsp->content_length == 0)
779 rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
781 sprintf(buf, "Content-Length: %d", rsp->content_length);
782 enlist(rsp->headers, buf);
785 * Fill in the default headers FIXME: Are these correct? sequence OK? check rfc!
787 enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
788 enlist_unique(rsp->headers, "Expires: Thu Jul 31, 1997 07:42:22 pm GMT", 8);
789 enlist_unique(rsp->headers, "Content-Type: text/html", 13);
790 enlist(rsp->headers, "");
796 if (NULL == (rsp->head = list_to_text(rsp->headers)))
798 free_http_response(rsp);
801 rsp->head_length = strlen(rsp->head);
808 /*********************************************************************
810 * Function : free_http_response
812 * Description : Free the memory occupied by an http_response
813 * and its depandant structures.
816 * 1 : rsp = pointer to http_response to be freed
820 *********************************************************************/
821 void free_http_response(struct http_response *rsp)
828 destroy_list(rsp->headers);
835 /*********************************************************************
837 * Function : fill_template
839 * Description : CGI support function that loads a given HTML
840 * template from the confdir, and fills it in
841 * by replacing @name@ with value using pcrs,
842 * for each item in the output map.
845 * 1 : csp = Current client state (buffers, headers, etc...)
846 * 3 : template = name of the HTML template to be used
847 * 2 : exports = map with fill in symbol -> name pairs
849 * Returns : char * with filled out form, or NULL if failiure
851 *********************************************************************/
852 char *fill_template(struct client_state *csp, const char *template, struct map *exports)
855 pcrs_job *job, *joblist = NULL;
856 char buf[BUFFER_SIZE];
857 char *new, *old = NULL;
863 * Open template file or fail
865 snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, template);
867 if(NULL == (fp = fopen(buf, "r")))
869 log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
875 * Assemble pcrs joblist from exports map
877 for (m = exports; m; m = m->next)
881 /* Enclose name in @@ */
882 snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
884 /* Make and chain job */
885 if ( NULL == (job = (pcrs_compile(buf, m->value, "sigTU", &error))) )
887 log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
898 * Read the file, ignoring comments
900 while (fgets(buf, BUFFER_SIZE, fp))
902 /* skip lines starting with '#' */
903 if(*buf == '#') continue;
905 old = strsav(old, buf);
913 size = strlen(old) + 1;
916 for (job = joblist; NULL != job; job = job->next)
918 pcrs_execute(job, old, size, &new, &size);
919 if (old != buf) free(old);
925 * Free the jobs & return
927 pcrs_free_joblist(joblist);
933 /*********************************************************************
935 * Function : default_exports
937 * Description : returns a struct map list that contains exports
938 * which are common to all CGI functions.
941 * 1 : csp = Current client state (buffers, headers, etc...)
942 * 2 : caller = name of CGI who calls us and which should
943 * be excluded from the generated menu.
944 * Returns : NULL if no memory, else map
946 *********************************************************************/
947 struct map *default_exports(struct client_state *csp, char *caller)
949 struct map *exports = NULL;
952 exports = map(exports, "version", 1, VERSION, 1);
953 exports = map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1);
954 exports = map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1);
955 exports = map(exports, "admin-address", 1, csp->config->admin_address ? csp->config->admin_address : "fill@me.in.please", 1);
956 exports = map(exports, "homepage", 1, HOME_PAGE_URL, 1);
957 exports = map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1);
958 exports = map(exports, "menu", 1, make_menu(caller), 0);
959 exports = map(exports, "code-status", 1, CODE_STATUS, 1);
961 snprintf(buf, 20, "%d", csp->config->hport);
962 exports = map(exports, "my-port", 1, buf, 1);
964 if(!strcmp(CODE_STATUS, "stable"))
966 exports = map_block_killer(exports, "unstable");
969 if(csp->config->proxy_info_url != NULL)
971 exports = map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
975 exports = map_block_killer(exports, "have-proxy-info");
983 /*********************************************************************
985 * Function : map_block_killer
987 * Description : Convenience function.
988 * Adds a "killer" for the conditional HTML-template
989 * block <name>, i.e. a substitution of the regex
990 * "if-<name>-start.*if-<name>-end" to the given
994 * 1 : exports = map to extend
995 * 2 : name = name of conditional block
997 * Returns : extended map
999 *********************************************************************/
1000 struct map *map_block_killer(struct map *exports, char *name)
1002 char buf[1000]; /* Will do, since the names are hardwired */
1004 snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
1005 exports = map(exports, buf, 1, "", 1);
1012 /*********************************************************************
1014 * Function : make_menu
1016 * Description : Returns an HTML-formatted menu of the available
1017 * unhidden CGIs, excluding the one given in <self>.
1019 * Parameters : self = name of CGI to leave out, can be NULL
1021 * Returns : menu string
1023 *********************************************************************/
1024 char *make_menu(const char *self)
1026 const struct cgi_dispatcher *d;
1027 char buf[BUFFER_SIZE], *tmp = NULL;
1029 if (self == NULL) self = "NO-SUCH-CGI!";
1031 /* List available unhidden CGI's and export as "other-cgis" */
1032 for (d = cgi_dispatcher; d->handler; d++)
1034 if (strncmp(d->description, "HIDE", 4) && strcmp(d->name, self))
1036 snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
1037 HOME_PAGE_URL, d->name, d->description);
1038 tmp = strsav(tmp, buf);
1046 /*********************************************************************
1048 * Function : dump_map
1050 * Description : HTML-dump a map for debugging
1053 * 1 : map = map to dump
1055 * Returns : string with HTML
1057 *********************************************************************/
1058 char *dump_map(struct map *map)
1060 struct map *p = map;
1063 ret = strsav(ret, "<table>\n");
1067 ret = strsav(ret, "<tr><td><b>");
1068 ret = strsav(ret, p->name);
1069 ret = strsav(ret, "</b></td><td>");
1070 ret = strsav(ret, p->value);
1071 ret = strsav(ret, "</td></tr>\n");
1075 ret = strsav(ret, "</table>\n");
1081 #ifdef FEATURE_STATISTICS
1082 /*********************************************************************
1084 * Function : add_stats
1086 * Description : Add the blocking statistics to a given map.
1089 * 1 : exports = map to write to.
1091 * Returns : pointer to extended map
1093 *********************************************************************/
1094 struct map *add_stats(struct map *exports)
1096 float perc_rej; /* Percentage of http requests rejected */
1098 int local_urls_read = urls_read;
1099 int local_urls_rejected = urls_rejected;
1102 * Need to alter the stats not to include the fetch of this
1105 * Can't do following thread safely! doh!
1108 * urls_rejected--; * This will be incremented subsequently *
1111 if (local_urls_read == 0)
1113 exports = map_block_killer(exports, "have-stats");
1117 exports = map_block_killer(exports, "have-no-stats");
1119 perc_rej = (float)local_urls_rejected * 100.0F /
1120 (float)local_urls_read;
1122 sprintf(buf, "%d", local_urls_read);
1123 exports = map(exports, "requests-received", 1, buf, 1);
1125 sprintf(buf, "%d", local_urls_rejected);
1126 exports = map(exports, "requests-blocked", 1, buf, 1);
1128 sprintf(buf, "%6.2f", perc_rej);
1129 exports = map(exports, "percent-blocked", 1, buf, 1);
1135 #endif /* def FEATURE_STATISTICS */