2008-05-24 16:05:49 +08:00
|
|
|
/* tinyproxy - A fast light-weight HTTP proxy
|
|
|
|
* Copyright (C) 2003 Steven Young <sdyoung@miranda.org>
|
2003-03-15 06:45:59 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2003-03-15 06:45:59 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2003-03-15 06:45:59 +08:00
|
|
|
*
|
2008-05-24 16:05:49 +08:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This file contains source code for the handling and display of
|
|
|
|
* HTML error pages with variable substitution.
|
2003-03-14 05:25:06 +08:00
|
|
|
*/
|
2003-03-15 06:45:59 +08:00
|
|
|
|
2003-03-14 05:25:06 +08:00
|
|
|
#include "tinyproxy.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "conns.h"
|
|
|
|
#include "heap.h"
|
|
|
|
#include "htmlerror.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2003-03-15 06:45:59 +08:00
|
|
|
/*
|
|
|
|
* Add an error number -> filename mapping to the errorpages list.
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
#define ERRORNUM_BUFSIZE 8 /* this is more than required */
|
2003-08-01 08:14:34 +08:00
|
|
|
#define ERRPAGES_BUCKETCOUNT 16
|
|
|
|
|
2003-03-15 06:45:59 +08:00
|
|
|
int
|
2005-08-15 11:54:31 +08:00
|
|
|
add_new_errorpage(char *filepath, unsigned int errornum)
|
|
|
|
{
|
|
|
|
char errornbuf[ERRORNUM_BUFSIZE];
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
config.errorpages = hashmap_create(ERRPAGES_BUCKETCOUNT);
|
|
|
|
if (!config.errorpages)
|
|
|
|
return (-1);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
snprintf(errornbuf, ERRORNUM_BUFSIZE, "%u", errornum);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (hashmap_insert(config.errorpages, errornbuf,
|
|
|
|
filepath, strlen(filepath) + 1) < 0)
|
|
|
|
return (-1);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
return (0);
|
2003-03-14 05:25:06 +08:00
|
|
|
}
|
|
|
|
|
2003-03-15 06:45:59 +08:00
|
|
|
/*
|
|
|
|
* Get the file appropriate for a given error.
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
static char *
|
|
|
|
get_html_file(unsigned int errornum)
|
|
|
|
{
|
|
|
|
hashmap_iter result_iter;
|
|
|
|
char errornbuf[ERRORNUM_BUFSIZE];
|
|
|
|
char *key;
|
|
|
|
static char *val;
|
2003-08-01 08:14:34 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
assert(errornum >= 100 && errornum < 1000);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (!config.errorpages)
|
|
|
|
return (config.errorpage_undef);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
snprintf(errornbuf, ERRORNUM_BUFSIZE, "%u", errornum);
|
2003-08-01 08:14:34 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
result_iter = hashmap_find(config.errorpages, errornbuf);
|
2003-08-01 08:14:34 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (hashmap_is_end(config.errorpages, result_iter))
|
|
|
|
return (config.errorpage_undef);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (hashmap_return_entry(config.errorpages, result_iter,
|
|
|
|
&key, (void **)&val) < 0)
|
|
|
|
return (config.errorpage_undef);
|
2003-08-01 08:14:34 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
return (val);
|
2003-03-14 05:25:06 +08:00
|
|
|
}
|
|
|
|
|
2003-03-15 06:45:59 +08:00
|
|
|
/*
|
|
|
|
* Look up the value for a variable.
|
|
|
|
*/
|
2005-08-15 11:54:31 +08:00
|
|
|
static char *
|
|
|
|
lookup_variable(struct conn_s *connptr, char *varname)
|
|
|
|
{
|
|
|
|
hashmap_iter result_iter;
|
|
|
|
char *key;
|
|
|
|
static char *data;
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
result_iter = hashmap_find(connptr->error_variables, varname);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (hashmap_is_end(connptr->error_variables, result_iter))
|
|
|
|
return (NULL);
|
2003-08-01 08:14:34 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (hashmap_return_entry(connptr->error_variables, result_iter,
|
|
|
|
&key, (void **)&data) < 0)
|
|
|
|
return (NULL);
|
2003-08-01 08:14:34 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
return (data);
|
2003-03-14 05:25:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define HTML_BUFSIZE 4096
|
|
|
|
|
2003-03-15 06:45:59 +08:00
|
|
|
/*
|
|
|
|
* Send an already-opened file to the client with variable substitution.
|
|
|
|
*/
|
|
|
|
int
|
2005-08-15 11:54:31 +08:00
|
|
|
send_html_file(FILE * infile, struct conn_s *connptr)
|
|
|
|
{
|
|
|
|
char inbuf[HTML_BUFSIZE], *varstart = NULL, *p;
|
|
|
|
char *varval;
|
|
|
|
int in_variable = 0, writeret;
|
|
|
|
|
|
|
|
while (fgets(inbuf, HTML_BUFSIZE, infile) != NULL) {
|
|
|
|
for (p = inbuf; *p; p++) {
|
|
|
|
switch (*p) {
|
|
|
|
case '}':
|
|
|
|
if (in_variable) {
|
|
|
|
*p = '\0';
|
|
|
|
if (!
|
|
|
|
(varval =
|
|
|
|
lookup_variable(connptr,
|
|
|
|
varstart)))
|
|
|
|
varval = "(unknown)";
|
|
|
|
writeret =
|
|
|
|
write_message(connptr->client_fd,
|
|
|
|
"%s", varval);
|
|
|
|
if (writeret)
|
|
|
|
return (writeret);
|
|
|
|
in_variable = 0;
|
|
|
|
} else {
|
|
|
|
writeret =
|
|
|
|
write_message(connptr->client_fd,
|
|
|
|
"%c", *p);
|
|
|
|
if (writeret)
|
|
|
|
return (writeret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
/* a {{ will print a single {. If we are NOT
|
|
|
|
* already in a { variable, then proceed with
|
|
|
|
* setup. If we ARE already in a { variable,
|
|
|
|
* this code will fallthrough to the code that
|
|
|
|
* just dumps a character to the client fd.
|
|
|
|
*/
|
|
|
|
if (!in_variable) {
|
|
|
|
varstart = p + 1;
|
|
|
|
in_variable++;
|
|
|
|
} else
|
|
|
|
in_variable = 0;
|
|
|
|
default:
|
|
|
|
if (!in_variable) {
|
|
|
|
writeret =
|
|
|
|
write_message(connptr->client_fd,
|
|
|
|
"%c", *p);
|
|
|
|
if (writeret)
|
|
|
|
return (writeret);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
in_variable = 0;
|
|
|
|
}
|
|
|
|
return (0);
|
2003-03-14 05:25:06 +08:00
|
|
|
}
|
|
|
|
|
2003-03-15 06:45:59 +08:00
|
|
|
int
|
2005-08-15 11:54:31 +08:00
|
|
|
send_http_headers(struct conn_s *connptr, int code, char *message)
|
|
|
|
{
|
|
|
|
char *headers =
|
|
|
|
"HTTP/1.0 %d %s\r\n"
|
|
|
|
"Server: %s/%s\r\n"
|
|
|
|
"Content-Type: text/html\r\n" "Connection: close\r\n" "\r\n";
|
|
|
|
|
|
|
|
return (write_message(connptr->client_fd, headers,
|
|
|
|
code, message, PACKAGE, VERSION));
|
2003-03-14 05:25:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display an error to the client.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
send_http_error_message(struct conn_s *connptr)
|
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
char *error_file;
|
|
|
|
FILE *infile;
|
|
|
|
int ret;
|
|
|
|
char *fallback_error =
|
|
|
|
"<html><head><title>%s</title></head>"
|
|
|
|
"<body><blockquote><i>%s %s</i><br>"
|
|
|
|
"The page you requested was unavailable. The error code is listed "
|
|
|
|
"below. In addition, the HTML file which has been configured as the "
|
|
|
|
"page to be displayed when an error of this type was unavailable, "
|
|
|
|
"with the error code %d (%s). Please contact your administrator."
|
|
|
|
"<center>%s</center>" "</body></html>" "\r\n";
|
|
|
|
|
|
|
|
send_http_headers(connptr, connptr->error_number,
|
|
|
|
connptr->error_string);
|
|
|
|
|
|
|
|
error_file = get_html_file(connptr->error_number);
|
|
|
|
if (!(infile = fopen(error_file, "r")))
|
|
|
|
return (write_message(connptr->client_fd, fallback_error,
|
|
|
|
connptr->error_string,
|
|
|
|
PACKAGE, VERSION,
|
|
|
|
errno, strerror(errno),
|
|
|
|
connptr->error_string));
|
|
|
|
|
|
|
|
ret = send_html_file(infile, connptr);
|
|
|
|
fclose(infile);
|
|
|
|
return (ret);
|
2003-03-14 05:25:06 +08:00
|
|
|
}
|
|
|
|
|
2008-05-24 16:05:49 +08:00
|
|
|
/*
|
2003-03-14 05:25:06 +08:00
|
|
|
* Add a key -> value mapping for HTML file substitution.
|
|
|
|
*/
|
2003-08-01 08:14:34 +08:00
|
|
|
|
|
|
|
#define ERRVAR_BUCKETCOUNT 16
|
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
int
|
|
|
|
add_error_variable(struct conn_s *connptr, char *key, char *val)
|
2003-03-14 05:25:06 +08:00
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
if (!connptr->error_variables)
|
|
|
|
if (!
|
|
|
|
(connptr->error_variables =
|
|
|
|
hashmap_create(ERRVAR_BUCKETCOUNT)))
|
|
|
|
return (-1);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (hashmap_insert(connptr->error_variables, key, val,
|
|
|
|
strlen(val) + 1) < 0)
|
|
|
|
return (-1);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
return (0);
|
2003-03-14 05:25:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ADD_VAR_RET(x, y) if(y) { if(add_error_variable(connptr, x, y) == -1) return(-1); }
|
|
|
|
|
2003-03-15 06:45:59 +08:00
|
|
|
/*
|
|
|
|
* Set some standard variables used by all HTML pages
|
|
|
|
*/
|
|
|
|
int
|
2005-08-15 11:54:31 +08:00
|
|
|
add_standard_vars(struct conn_s *connptr)
|
|
|
|
{
|
|
|
|
char timebuf[30];
|
|
|
|
time_t global_time = time(NULL);
|
|
|
|
|
|
|
|
strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT",
|
|
|
|
gmtime(&global_time));
|
|
|
|
|
|
|
|
ADD_VAR_RET("request", connptr->request_line);
|
|
|
|
ADD_VAR_RET("cause", connptr->error_string);
|
|
|
|
ADD_VAR_RET("clientip", connptr->client_ip_addr);
|
|
|
|
ADD_VAR_RET("clienthost", connptr->client_string_addr);
|
|
|
|
ADD_VAR_RET("version", VERSION);
|
|
|
|
ADD_VAR_RET("package", PACKAGE);
|
|
|
|
ADD_VAR_RET("date", timebuf);
|
|
|
|
return (0);
|
2003-03-14 05:25:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the error information to the conn structure.
|
|
|
|
*/
|
|
|
|
int
|
2005-08-15 11:54:31 +08:00
|
|
|
indicate_http_error(struct conn_s *connptr, int number, char *message, ...)
|
2003-03-14 05:25:06 +08:00
|
|
|
{
|
2005-08-15 11:54:31 +08:00
|
|
|
va_list ap;
|
|
|
|
char *key, *val;
|
|
|
|
|
|
|
|
va_start(ap, message);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
while ((key = va_arg(ap, char *))) {
|
|
|
|
val = va_arg(ap, char *);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
if (add_error_variable(connptr, key, val) == -1) {
|
|
|
|
va_end(ap);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
connptr->error_number = number;
|
|
|
|
connptr->error_string = safestrdup(message);
|
2003-03-14 05:25:06 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
va_end(ap);
|
2003-07-15 01:42:43 +08:00
|
|
|
|
2005-08-15 11:54:31 +08:00
|
|
|
return (add_standard_vars(connptr));
|
2003-03-14 05:25:06 +08:00
|
|
|
}
|