diff options
Diffstat (limited to '')
-rw-r--r-- | src/utils.c | 55 |
1 files changed, 48 insertions, 7 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; } |