diff options
author | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-08-27 03:45:34 +0000 |
---|---|---|
committer | Robert James Kaes <rjkaes@users.sourceforge.net> | 2001-08-27 03:45:34 +0000 |
commit | 3b5e17d57913d3f196b7e05efe27508d10603476 (patch) | |
tree | 7e07b800a67ff3e567e2449d1893be919a620394 /src | |
parent | 771425700d849ac1fdacf82ea366cf5eeb02a30a (diff) | |
download | tinyproxy-3b5e17d57913d3f196b7e05efe27508d10603476.tar.gz tinyproxy-3b5e17d57913d3f196b7e05efe27508d10603476.zip |
Split the headers from the message body. Also now include proper headers
like Date, Server, Content-Length, etc. Also, fixed the type for an error
message to be const char*.
Diffstat (limited to '')
-rw-r--r-- | src/utils.c | 55 | ||||
-rw-r--r-- | src/utils.h | 4 |
2 files changed, 50 insertions, 9 deletions
diff --git a/src/utils.c b/src/utils.c index 50110ab..15ef2f7 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,4 @@ -/* $Id: utils.c,v 1.4 2001-05-27 02:38:46 rjkaes Exp $ +/* $Id: utils.c,v 1.5 2001-08-27 03:45:34 rjkaes Exp $ * * Misc. routines which are used by the various functions to handle strings * and memory allocation and pretty much anything else we can think of. Also, @@ -61,25 +61,66 @@ char *xstrstr(char *haystack, char *needle, size_t length, /* * Display an error to the client. */ -int httperr(struct conn_s *connptr, int err, char *msg) +#define HEADER_SIZE (1024 * 8) +int httperr(struct conn_s *connptr, int err, const char *msg) { - static char *premsg = "HTTP/1.0 %d %s\r\n" \ - "Content-type: text/html\r\n\r\n" \ + static char *headers = \ + "HTTP/1.0 %d %s\r\n" \ + "Server: %s/%s\r\n" \ + "Date: %s\r\n" \ + "Content-Type: text/mime\r\n" \ + "Content-Length: %d\r\n" \ + "Connection: close\r\n" \ + "\r\n"; + + static char *message = \ "<html><head><title>%s</title></head>\r\n" \ "<body>\r\n" \ "<font size=\"+2\">Cache Error!</font><br>\r\n" \ "An error of type %d occurred: %s\r\n" \ "<hr>\r\n" \ - "<font size=\"-1\"><em>Generated by %s</em></font>\r\n" \ + "<font size=\"-1\"><em>Generated by %s/%s</em></font>\r\n" \ "</body></html>\r\n"; - connptr->output_message = malloc(MAXBUFFSIZE); + char *header_buffer; + char *message_buffer; + int output_size; + char timebuf[30]; + time_t global_time; + + header_buffer = malloc(HEADER_SIZE); + if (!header_buffer) { + log_message(LOG_CRIT, "Out of memory!"); + return -1; + } + + message_buffer = malloc(MAXBUFFSIZE); + if (!message_buffer) { + log_message(LOG_CRIT, "Out of memory!"); + safefree(header_buffer); + return -1; + } + + global_time = time(NULL); + strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&global_time)); + + snprintf(message_buffer, MAXBUFFSIZE - 1, message, msg, err, msg, PACKAGE, VERSION); + snprintf(header_buffer, HEADER_SIZE - 1, headers, err, msg, PACKAGE, VERSION, timebuf, strlen(message_buffer)); + + output_size = strlen(message_buffer) + strlen(header_buffer); + connptr->output_message = malloc(output_size + 1); if (!connptr->output_message) { log_message(LOG_CRIT, "Out of memory!"); + safefree(header_buffer); + safefree(message_buffer); return -1; } - snprintf(connptr->output_message, MAXBUFFSIZE, premsg, err, msg, msg, err, msg, VERSION); + strlcpy(connptr->output_message, header_buffer, output_size); + strlcat(connptr->output_message, message_buffer, output_size); + + safefree(header_buffer); + safefree(message_buffer); return 0; } diff --git a/src/utils.h b/src/utils.h index cd53fae..da4dd2f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,4 +1,4 @@ -/* $Id: utils.h,v 1.4 2001-05-27 02:38:46 rjkaes Exp $ +/* $Id: utils.h,v 1.5 2001-08-27 03:45:34 rjkaes Exp $ * * See 'utils.h' for a detailed description. * @@ -24,7 +24,7 @@ extern char *xstrstr(char *haystack, char *needle, size_t length, bool_t case_sensitive); -extern int httperr(struct conn_s *connptr, int err, char *msg); +extern int httperr(struct conn_s *connptr, int err, const char *msg); extern void makedaemon(void); extern void pidfile_create(const char *path); |