summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/utils.c75
-rw-r--r--src/utils.h6
2 files changed, 47 insertions, 34 deletions
diff --git a/src/utils.c b/src/utils.c
index d9361d6..8786702 100644
--- a/src/utils.c
+++ b/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
+#define HEADER_SIZE (1024 * 8)
/*
- * Display an error to the client.
+ * Build the data for a complete HTTP & HTML message for the client.
*/
-#define HEADER_SIZE (1024 * 8)
-int httperr(struct conn_s *connptr, int err, const char *msg)
+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,58 +81,66 @@ int httperr(struct conn_s *connptr, int err, const char *msg)
"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 (%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;
+ int output_size;
header_buffer = safemalloc(HEADER_SIZE);
- if (!header_buffer) {
- log_message(LOG_ERR, "Could not allocate memory.");
+ if (!header_buffer)
return -1;
- }
-
- message_buffer = safemalloc(MAXBUFFSIZE);
- if (!message_buffer) {
- log_message(LOG_ERR, "Could not allocate 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));
+ snprintf(header_buffer, HEADER_SIZE - 1, headers, http_code, error_title, PACKAGE, VERSION, timebuf, strlen(message));
- output_size = strlen(message_buffer) + strlen(header_buffer);
+ output_size = strlen(message) + 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);
- safefree(message_buffer);
return -1;
}
strlcpy(connptr->output_message, header_buffer, output_size);
- strlcat(connptr->output_message, message_buffer, output_size);
+ strlcat(connptr->output_message, message, output_size);
safefree(header_buffer);
- safefree(message_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" \
+ "<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 (%s)</em></font>\r\n" \
+ "</body></html>\r\n\r\n";
+
+ char *message_buffer;
+
+ message_buffer = safemalloc(MAXBUFFSIZE);
+ if (!message_buffer)
+ return -1;
+
+ snprintf(message_buffer, MAXBUFFSIZE - 1, message, msg, err, msg, PACKAGE, VERSION);
+
+ if (send_http_message(connptr, err, msg, message_buffer) < 0) {
+ safefree(message_buffer);
+ return -1;
+ }
+
+ safefree(message_buffer);
+ return 0;
+}
+
void makedaemon(void)
{
if (fork() != 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;
diff --git a/src/utils.h b/src/utils.h
index 409def9..d0fa57e 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -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 */