Changed send_http_message() to use the write_message() function.

Changed httperr() to use the same concept as the write_message() function.
Still haven't figured out how to combine the code.
This commit is contained in:
Robert James Kaes 2001-12-24 00:02:32 +00:00
parent b10221fa07
commit 9520866ab3

View File

@ -1,4 +1,4 @@
/* $Id: utils.c,v 1.20 2001-12-20 04:48:52 rjkaes Exp $ /* $Id: utils.c,v 1.21 2001-12-24 00:02:32 rjkaes Exp $
* *
* Misc. routines which are used by the various functions to handle strings * 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, * and memory allocation and pretty much anything else we can think of. Also,
@ -77,33 +77,29 @@ int
send_http_message(struct conn_s *connptr, int http_code, send_http_message(struct conn_s *connptr, int http_code,
const char *error_title, const char *message) const char *error_title, const char *message)
{ {
static char *headers = static char *headers = \
"HTTP/1.0 %d %s\r\n" "HTTP/1.0 %d %s\r\n" \
"Server: %s/%s\r\n" "Server: %s/%s\r\n" \
"Date: %s\r\n" "Date: %s\r\n" \
"Content-Type: text/html\r\n" "Content-Type: text/html\r\n" \
"Content-Length: %d\r\n" "Connection: close\r\n" "\r\n"; "Content-Length: %d\r\n" \
"Connection: close\r\n" \
"\r\n";
char *header_buffer;
char timebuf[30]; char timebuf[30];
time_t global_time; time_t global_time;
header_buffer = safemalloc(HEADER_SIZE);
if (!header_buffer)
return -1;
global_time = time(NULL); global_time = time(NULL);
strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT",
gmtime(&global_time)); gmtime(&global_time));
snprintf(header_buffer, HEADER_SIZE - 1, headers, http_code, write_message(connptr->client_fd,
error_title, PACKAGE, VERSION, timebuf, strlen(message)); headers,
http_code, error_title, PACKAGE, VERSION, timebuf,
strlen(message));
safe_write(connptr->client_fd, header_buffer, strlen(header_buffer));
safe_write(connptr->client_fd, message, strlen(message)); safe_write(connptr->client_fd, message, strlen(message));
safefree(header_buffer);
connptr->send_response_message = TRUE; connptr->send_response_message = TRUE;
return 0; return 0;
@ -115,31 +111,50 @@ send_http_message(struct conn_s *connptr, int http_code,
int int
httperr(struct conn_s *connptr, int err, const char *msg) httperr(struct conn_s *connptr, int err, const char *msg)
{ {
static char *message = static char *message = \
"<html><head><title>%s</title></head>\r\n" "<html><head><title>%s</title></head>\r\n" \
"<body>\r\n" "<body>\r\n" \
"<font size=\"+2\">Cache Error!</font><br>\r\n" "<font size=\"+2\">Cache Error!</font><br>\r\n" \
"An error of type %d occurred: %s\r\n" "An error of type %d occurred: %s\r\n" \
"<hr>\r\n" "<hr>\r\n" \
"<font size=\"-1\"><em>Generated by %s (%s)</em></font>\r\n" "<font size=\"-1\"><em>Generated by %s (%s)</em></font>\r\n" \
"</body></html>\r\n\r\n"; "</body></html>\r\n\r\n";
char *message_buffer; char *message_buffer;
char *tmpbuf;
size_t size = (1024 * 8); /* start with 8 KB */
ssize_t n;
int ret;
message_buffer = safemalloc(MAXBUFFSIZE); message_buffer = safemalloc(size);
if (!message_buffer) if (!message_buffer)
return -1; return -1;
snprintf(message_buffer, MAXBUFFSIZE - 1, message, msg, err, msg, /*
PACKAGE, VERSION); * Build a new line. Keep increasing the size until the line fits.
* See the write_message() function in sock.c for more information.
*/
while (1) {
n = snprintf(message_buffer, size, message, msg, err, msg, PACKAGE, VERSION);
if (send_http_message(connptr, err, msg, message_buffer) < 0) { if (n > -1 && n < size)
safefree(message_buffer); break;
return -1;
if (n > - 1)
size = n + 1;
else
size *= 2;
if ((tmpbuf = saferealloc(message_buffer, size)) == NULL) {
safefree(message_buffer);
return -1;
} else
message_buffer = tmpbuf;
} }
ret = send_http_message(connptr, err, msg, message_buffer);
safefree(message_buffer); safefree(message_buffer);
return 0; return ret;
} }
void void