Added the send_http_message() function to handle sending messages back to
the client. It's used by httperr() and showstats().
This commit is contained in:
parent
d3213f193c
commit
9860222979
77
src/utils.c
77
src/utils.c
@ -1,4 +1,4 @@
|
||||
/* $Id: utils.c,v 1.11 2001-09-11 19:27:27 rjkaes Exp $
|
||||
/* $Id: utils.c,v 1.12 2001-09-15 21:29:59 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,
|
||||
@ -65,11 +65,12 @@ void debugging_free(void *ptr, const char *file, unsigned long line)
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Display an error to the client.
|
||||
*/
|
||||
#define HEADER_SIZE (1024 * 8)
|
||||
int httperr(struct conn_s *connptr, int err, const char *msg)
|
||||
/*
|
||||
* Build the data for a complete HTTP & HTML message for the client.
|
||||
*/
|
||||
int send_http_message(struct conn_s* connptr, int http_code,
|
||||
const char *error_title, const char *message)
|
||||
{
|
||||
static char *headers = \
|
||||
"HTTP/1.0 %d %s\r\n" \
|
||||
@ -80,6 +81,40 @@ int httperr(struct conn_s *connptr, int err, const char *msg)
|
||||
"Connection: close\r\n" \
|
||||
"\r\n";
|
||||
|
||||
char *header_buffer;
|
||||
char timebuf[30];
|
||||
time_t global_time;
|
||||
int output_size;
|
||||
|
||||
header_buffer = safemalloc(HEADER_SIZE);
|
||||
if (!header_buffer)
|
||||
return -1;
|
||||
|
||||
global_time = time(NULL);
|
||||
strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&global_time));
|
||||
|
||||
snprintf(header_buffer, HEADER_SIZE - 1, headers, http_code, error_title, PACKAGE, VERSION, timebuf, strlen(message));
|
||||
|
||||
output_size = strlen(message) + strlen(header_buffer);
|
||||
connptr->output_message = safemalloc(output_size + 1);
|
||||
if (!connptr->output_message) {
|
||||
safefree(header_buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
strlcpy(connptr->output_message, header_buffer, output_size);
|
||||
strlcat(connptr->output_message, message, output_size);
|
||||
|
||||
safefree(header_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Display an error to the client.
|
||||
*/
|
||||
int httperr(struct conn_s *connptr, int err, const char *msg)
|
||||
{
|
||||
static char *message = \
|
||||
"<html><head><title>%s</title></head>\r\n" \
|
||||
"<body>\r\n" \
|
||||
@ -89,46 +124,20 @@ int httperr(struct conn_s *connptr, int err, const char *msg)
|
||||
"<font size=\"-1\"><em>Generated by %s (%s)</em></font>\r\n" \
|
||||
"</body></html>\r\n\r\n";
|
||||
|
||||
char *header_buffer;
|
||||
char *message_buffer;
|
||||
int output_size;
|
||||
char timebuf[30];
|
||||
time_t global_time;
|
||||
|
||||
header_buffer = safemalloc(HEADER_SIZE);
|
||||
if (!header_buffer) {
|
||||
log_message(LOG_ERR, "Could not allocate memory.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
message_buffer = safemalloc(MAXBUFFSIZE);
|
||||
if (!message_buffer) {
|
||||
log_message(LOG_ERR, "Could not allocate memory.");
|
||||
safefree(header_buffer);
|
||||
if (!message_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 = safemalloc(output_size + 1);
|
||||
if (!connptr->output_message) {
|
||||
log_message(LOG_ERR, "Could not allocate memory.");
|
||||
safefree(header_buffer);
|
||||
if (send_http_message(connptr, err, msg, message_buffer) < 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -154,7 +163,7 @@ void makedaemon(void)
|
||||
/*
|
||||
* Safely creates filename and returns the low-level file descriptor.
|
||||
*/
|
||||
static int create_file_safely(const char *filename)
|
||||
int create_file_safely(const char *filename)
|
||||
{
|
||||
struct stat lstatinfo;
|
||||
int fildes;
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $Id: utils.h,v 1.8 2001-09-11 19:27:27 rjkaes Exp $
|
||||
/* $Id: utils.h,v 1.9 2001-09-15 21:29:59 rjkaes Exp $
|
||||
*
|
||||
* See 'utils.h' for a detailed description.
|
||||
*
|
||||
@ -21,11 +21,15 @@
|
||||
|
||||
#include "tinyproxy.h"
|
||||
|
||||
extern int send_http_message(struct conn_s* connptr, int http_code,
|
||||
const char *error_title, const char *message);
|
||||
extern int httperr(struct conn_s *connptr, int err, const char *msg);
|
||||
|
||||
extern void makedaemon(void);
|
||||
extern void pidfile_create(const char *path);
|
||||
|
||||
extern int create_file_safely(const char *filename);
|
||||
|
||||
#ifndef HAVE_STRLCAT
|
||||
extern size_t strlcat(char *dst, const char *src, size_t size);
|
||||
#endif /* HAVE_STRLCAT */
|
||||
|
Loading…
Reference in New Issue
Block a user