#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 ); }