5 kx
5 kx #ifdef HAVE_CONFIG_H
5 kx #include <config.h>
5 kx #endif
5 kx
5 kx #include <stdlib.h>
5 kx #include <stdio.h>
5 kx #include <sys/sysinfo.h>
5 kx #include <sys/types.h>
5 kx #include <stdint.h>
5 kx #include <dirent.h>
5 kx #include <sys/stat.h> /* chmod(2) */
5 kx #include <sys/file.h>
5 kx #include <sys/mman.h>
5 kx #include <fcntl.h>
5 kx #include <limits.h>
5 kx #include <string.h> /* strdup(3) */
5 kx #include <libgen.h> /* basename(3) */
5 kx #include <ctype.h> /* tolower(3) */
5 kx #include <errno.h>
5 kx #include <time.h>
5 kx #include <sys/time.h>
5 kx #include <pwd.h>
5 kx #include <grp.h>
5 kx #include <stdarg.h>
5 kx #include <unistd.h>
5 kx
5 kx #include <defs.h>
5 kx #include <fatal.h>
5 kx #include <http.h>
5 kx
5 kx
5 kx void fatal( const char *fmt, ... )
5 kx {
5 kx va_list arg_ptr;
5 kx char buf[HTTP_ERRMSG_SIZE];
5 kx char msg[HTTP_ERRMSG_SIZE];
5 kx char *format = "%s: %s\n";
5 kx
5 kx va_start( arg_ptr, fmt );
5 kx
5 kx vsnprintf( msg, HTTP_ERRMSG_SIZE, (const void *)fmt, arg_ptr );
5 kx
5 kx va_end( arg_ptr ); /* Reset variable arguments. */
5 kx
5 kx snprintf( buf, HTTP_ERRMSG_SIZE, format, PROGRAM_CGI, msg );
5 kx
5 kx (void)write( STDERR_FILENO, buf, strlen( buf ) );
5 kx
5 kx exit( 1 );
5 kx }
5 kx
5 kx void fatal_json( const char *fmt, ... )
5 kx {
5 kx va_list arg_ptr;
5 kx
5 kx char resp[HTTP_ERRMSG_SIZE];
5 kx char json[HTTP_ERRMSG_SIZE];
5 kx char msg[HTTP_ERRMSG_SIZE];
5 kx
5 kx char *http_format = "Status: 500 Internal Server Error\n"
5 kx "Date: %s\n"
5 kx "Cache-Control: no-cache, no-store\n"
5 kx "Content-Type: application/json\n"
5 kx "Content-Length: %d\n\n"
5 kx "%s";
5 kx
5 kx va_start( arg_ptr, fmt );
5 kx
5 kx vsnprintf( msg, HTTP_ERRMSG_SIZE, (const void *)fmt, arg_ptr );
5 kx
5 kx va_end( arg_ptr ); /* Reset variable arguments. */
5 kx
5 kx snprintf( json, HTTP_ERRMSG_SIZE, "{\"error\":\"500\",\"description\":\"Internal Server Error\",\"message\":\"%s: %s\"}", PROGRAM_CGI, msg );
5 kx
5 kx snprintf( resp, HTTP_ERRMSG_SIZE, http_format,
5 kx http_date( time(NULL) ), strlen( json ), json );
5 kx
5 kx (void)write( STDOUT_FILENO, resp, strlen( resp ) );
5 kx
5 kx exit( 1 );
5 kx }
5 kx
5 kx void fatal_html( const char *fmt, ... )
5 kx {
5 kx va_list arg_ptr;
5 kx
5 kx char resp[HTTP_ERRMSG_SIZE];
5 kx char html[HTTP_ERRMSG_SIZE];
5 kx char msg[HTTP_ERRMSG_SIZE];
5 kx
5 kx char *http_format = "Status: 500 Internal Server Error\n"
5 kx "Date: %s\n"
5 kx "Cache-Control: no-cache, no-store\n"
5 kx "Content-Type: text/html; charset=utf-8\n"
5 kx "Content-Length: %d\n\n"
5 kx "%s";
5 kx
5 kx char *html_format =
5 kx "<!DOCTYPE html>\n"
5 kx "<html lang=\"en-US\">\n"
5 kx " <head>\n"
5 kx " <meta charset=\"utf-8\">\n"
5 kx " <!--[if IE]><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><![endif]-->\n"
5 kx " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
5 kx " <title>HTTP/%s 500 Internal Server Error</title>\n"
5 kx " <style>\n"
5 kx " body {\n"
5 kx " min-width: 460px;\n"
5 kx " }\n"
5 kx " .cgit-internal-server-error {\n"
5 kx " margin: 48px auto;\n"
5 kx " padding: 24px 48px 28px 48px;\n"
5 kx " width: 70%%;\n"
5 kx " color: #fff;\n"
5 kx " background: #db2828;\n"
5 kx " box-shadow: 3px 3px 10px rgba(0,0,0,0.5);\n"
5 kx " border-radius: 4px;\n"
5 kx " }\n"
5 kx " .cgit-internal-server-error h1 {\n"
5 kx " margin-bottom: 2px;\n"
5 kx " font-family: sans-serif;\n"
5 kx " font-size: 28px;\n"
5 kx " }\n"
5 kx " .cgit-internal-server-error p.http-date {\n"
5 kx " margin-top: 2px;\n"
5 kx " padding-left: 0px 0px 12px 0px;\n"
5 kx " color: #fc0;\n"
5 kx " font-family: monospace;\n"
5 kx " font-size: 18px;\n"
5 kx " }\n"
5 kx " .cgit-internal-server-error p {\n"
5 kx " padding-left: 1px;\n"
5 kx " font-family: monospace;\n"
5 kx " font-size: 14px;\n"
5 kx " }\n"
5 kx " @media screen and (max-width: 680px) {\n"
5 kx " .cgit-internal-server-error {\n"
5 kx " padding: 24px 28px 24px 28px;\n"
5 kx " width: 80%%;\n"
5 kx " }\n"
5 kx " .cgit-internal-server-error h1 {\n"
5 kx " font-size: 22px;\n"
5 kx " }\n"
5 kx " .cgit-internal-server-error p.http-date {\n"
5 kx " font-size: 16px;\n"
5 kx " }\n"
5 kx " .cgit-internal-server-error p {\n"
5 kx " font-size: 12px;\n"
5 kx " }\n"
5 kx " }\n"
5 kx " </style>\n"
5 kx " </head>\n"
5 kx " <body>\n"
5 kx " <div class=\"cgit-internal-server-error\">\n"
5 kx " <h1>HTTP/%s 500 Internal Server Error</h1>\n"
5 kx " <p class=\"http-date\">Date: %s</p>\n"
5 kx " <p>%s: %s</p>\n"
5 kx " </div>\n"
5 kx " </body>\n"
5 kx "</html>\n";
5 kx
5 kx va_start( arg_ptr, fmt );
5 kx
5 kx vsnprintf( msg, HTTP_ERRMSG_SIZE, (const void *)fmt, arg_ptr );
5 kx
5 kx va_end( arg_ptr ); /* Reset variable arguments. */
5 kx
5 kx snprintf( html, HTTP_ERRMSG_SIZE, html_format,
5 kx HTTP_VERSION, HTTP_VERSION,
5 kx http_date( time(NULL) ), PROGRAM_CGI, msg );
5 kx
5 kx snprintf( resp, HTTP_ERRMSG_SIZE, http_format,
5 kx http_date( time(NULL) ), strlen( html ), html );
5 kx
5 kx (void)write( STDOUT_FILENO, resp, strlen( resp ) );
5 kx
5 kx exit( 1 );
5 kx }