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*.
This commit is contained in:
Robert James Kaes 2001-08-27 03:45:34 +00:00
parent 771425700d
commit 3b5e17d579
2 changed files with 51 additions and 10 deletions

View File

@ -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);
if (!connptr->output_message) {
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;
}
snprintf(connptr->output_message, MAXBUFFSIZE, premsg, err, msg, msg, err, msg, VERSION);
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;
}
strlcpy(connptr->output_message, header_buffer, output_size);
strlcat(connptr->output_message, message_buffer, output_size);
safefree(header_buffer);
safefree(message_buffer);
return 0;
}

View File

@ -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);