summaryrefslogtreecommitdiff
path: root/csvncgi/fatal.c
blob: 27da083daf9cac5e9cbffedd72e860e46282ac65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <stdint.h>
#include <dirent.h>
#include <sys/stat.h> /* chmod(2)    */
#include <sys/file.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>   /* strdup(3)   */
#include <libgen.h>   /* basename(3) */
#include <ctype.h>    /* tolower(3)  */
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <pwd.h>
#include <grp.h>
#include <stdarg.h>
#include <unistd.h>

#include <defs.h>
#include <fatal.h>
#include <http.h>


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 =
    "<!DOCTYPE html>\n"
    "<html lang=\"en-US\">\n"
    "  <head>\n"
    "    <meta charset=\"utf-8\">\n"
    "    <!--[if IE]><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><![endif]-->\n"
    "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
    "    <title>HTTP/%s 500 Internal Server Error</title>\n"
    "    <style>\n"
    "      body {\n"
    "        min-width: 460px;\n"
    "      }\n"
    "      .csvn-internal-server-error {\n"
    "        margin: 48px auto;\n"
    "        padding: 24px 48px 28px 48px;\n"
    "        width: 70%%;\n"
    "        color: #fff;\n"
    "        background: #db2828;\n"
    "        box-shadow: 3px 3px 10px rgba(0,0,0,0.5);\n"
    "        border-radius: 4px;\n"
    "      }\n"
    "      .csvn-internal-server-error h1 {\n"
    "        margin-bottom: 2px;\n"
    "        font-family: sans-serif;\n"
    "        font-size: 28px;\n"
    "      }\n"
    "      .csvn-internal-server-error p.http-date {\n"
    "        margin-top: 2px;\n"
    "        padding-left: 0px 0px 12px 0px;\n"
    "        color: #fc0;\n"
    "        font-family: monospace;\n"
    "        font-size: 18px;\n"
    "      }\n"
    "      .csvn-internal-server-error p {\n"
    "        padding-left: 1px;\n"
    "        font-family: monospace;\n"
    "        font-size: 14px;\n"
    "      }\n"
    "      @media screen and (max-width: 680px) {\n"
    "        .csvn-internal-server-error {\n"
    "          padding: 24px 28px 24px 28px;\n"
    "          width: 80%%;\n"
    "        }\n"
    "        .csvn-internal-server-error h1 {\n"
    "          font-size: 22px;\n"
    "        }\n"
    "        .csvn-internal-server-error p.http-date {\n"
    "          font-size: 16px;\n"
    "        }\n"
    "        .csvn-internal-server-error p {\n"
    "          font-size: 12px;\n"
    "        }\n"
    "      }\n"
    "    </style>\n"
    "  </head>\n"
    "  <body>\n"
    "    <div class=\"csvn-internal-server-error\">\n"
    "      <h1>HTTP/%s 500 Internal Server Error</h1>\n"
    "      <p class=\"http-date\">Date: %s</p>\n"
    "      <p>%s: %s</p>\n"
    "    </div>\n"
    "  </body>\n"
    "</html>\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 );
}