From bfc1508d26c89c9a36d2d9a827fe2c4ed128884d Mon Sep 17 00:00:00 2001 From: kx Date: Fri, 24 Mar 2023 03:55:33 +0300 Subject: Version 0.1.4 --- csvncgi/fatal.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 csvncgi/fatal.c (limited to 'csvncgi/fatal.c') diff --git a/csvncgi/fatal.c b/csvncgi/fatal.c new file mode 100644 index 0000000..27da083 --- /dev/null +++ b/csvncgi/fatal.c @@ -0,0 +1,179 @@ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include /* chmod(2) */ +#include +#include +#include +#include +#include /* strdup(3) */ +#include /* basename(3) */ +#include /* tolower(3) */ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +void fatal( const char *fmt, ... ) +{ + va_list arg_ptr; + char buf[HTTP_ERRMSG_SIZE]; + char msg[HTTP_ERRMSG_SIZE]; + char *format = "%s: %s\n"; + + va_start( arg_ptr, fmt ); + + vsnprintf( msg, HTTP_ERRMSG_SIZE, (const void *)fmt, arg_ptr ); + + va_end( arg_ptr ); /* Reset variable arguments. */ + + snprintf( buf, HTTP_ERRMSG_SIZE, format, PROGRAM_CGI, msg ); + + (void)write( STDERR_FILENO, buf, strlen( buf ) ); + + exit( 1 ); +} + +void fatal_json( const char *fmt, ... ) +{ + va_list arg_ptr; + + char resp[HTTP_ERRMSG_SIZE]; + char json[HTTP_ERRMSG_SIZE]; + char msg[HTTP_ERRMSG_SIZE]; + + char *http_format = "Status: 500 Internal Server Error\n" + "Date: %s\n" + "Cache-Control: no-cache, no-store\n" + "Content-Type: application/json\n" + "Content-Length: %d\n\n" + "%s"; + + va_start( arg_ptr, fmt ); + + vsnprintf( msg, HTTP_ERRMSG_SIZE, (const void *)fmt, arg_ptr ); + + va_end( arg_ptr ); /* Reset variable arguments. */ + + snprintf( json, HTTP_ERRMSG_SIZE, "{\"error\":\"500\",\"description\":\"Internal Server Error\",\"message\":\"%s: %s\"}", PROGRAM_CGI, msg ); + + snprintf( resp, HTTP_ERRMSG_SIZE, http_format, + http_date( time(NULL) ), strlen( json ), json ); + + (void)write( STDOUT_FILENO, resp, strlen( resp ) ); + + exit( 1 ); +} + +void fatal_html( const char *fmt, ... ) +{ + va_list arg_ptr; + + char resp[HTTP_ERRMSG_SIZE]; + char html[HTTP_ERRMSG_SIZE]; + char msg[HTTP_ERRMSG_SIZE]; + + char *http_format = "Status: 500 Internal Server Error\n" + "Date: %s\n" + "Cache-Control: no-cache, no-store\n" + "Content-Type: text/html; charset=utf-8\n" + "Content-Length: %d\n\n" + "%s"; + + char *html_format = + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " HTTP/%s 500 Internal Server Error\n" + " \n" + " \n" + " \n" + "
\n" + "

HTTP/%s 500 Internal Server Error

\n" + "

Date: %s

\n" + "

%s: %s

\n" + "
\n" + " \n" + "\n"; + + va_start( arg_ptr, fmt ); + + vsnprintf( msg, HTTP_ERRMSG_SIZE, (const void *)fmt, arg_ptr ); + + va_end( arg_ptr ); /* Reset variable arguments. */ + + snprintf( html, HTTP_ERRMSG_SIZE, html_format, + HTTP_VERSION, HTTP_VERSION, + http_date( time(NULL) ), PROGRAM_CGI, msg ); + + snprintf( resp, HTTP_ERRMSG_SIZE, http_format, + http_date( time(NULL) ), strlen( html ), html ); + + (void)write( STDOUT_FILENO, resp, strlen( resp ) ); + + exit( 1 ); +} -- cgit v1.2.3