1 #ifndef EX_HTTPD_ERR_CODE_H
2 #define EX_HTTPD_ERR_CODE_H
3 
4 #define CONF_MSG_RET_BEG "\
5 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\r\n\
6 <html>\r\n\
7   <head>\r\n\
8 "
9 
10 #define CONF_MSG_RET_END "\
11   <hr>\r\n\
12   Generated using the <a href=\"http://www.and.org/vstr/httpd\">Vstr httpd example Web Server</a>.\r\n\
13   </body>\r\n\
14 </html>\r\n\
15 "
16 
17 #define CONF_MSG__MAKE(code, sum, msg) \
18     CONF_MSG_RET_BEG \
19 "    <title>" code " " sum "</title>\r\n\
20   </head>\r\n\
21   <body>\r\n\
22     <h1>" code " " sum "</h1>\r\n\
23     <p>" msg ".</p>\r\n" \
24     CONF_MSG_RET_END
25 
26 #define CONF_MSG__FMT_30x_END "\
27 \">here</a>.</p>\r\n" \
28     CONF_MSG_RET_END
29 
30 #define CONF_LINE_RET_301 "Moved Permanently"
31 #define CONF_MSG_FMT_301 "%s${vstr:%p%zu%zu%u}%s"
32 #define CONF_MSG__FMT_301_BEG CONF_MSG_RET_BEG "\
33     <title>301 Moved Permanently</title>\r\n\
34   </head>\r\n\
35   <body>\r\n\
36     <h1>301 Moved Permanently</h1>\r\n\
37     <p>The document has moved <a href=\"\
38 "
39 #define CONF_MSG__FMT_301_END CONF_MSG__FMT_30x_END
40 
41 #define CONF_MSG_LEN_301(s1) (((s1)->len) +                             \
42                               strlen(CONF_MSG__FMT_301_BEG) +           \
43                               strlen(CONF_MSG__FMT_301_END))
44 
45 #define CONF_LINE_RET_302 "Found"
46 #define CONF_MSG_FMT_302 "%s${vstr:%p%zu%zu%u}%s"
47 #define CONF_MSG__FMT_302_BEG CONF_MSG_RET_BEG "\
48     <title>302 Found</title>\r\n\
49   </head>\r\n\
50   <body>\r\n\
51     <h1>302 Found</h1>\r\n\
52     <p>The document can be found <a href=\"\
53 "
54 #define CONF_MSG__FMT_302_END CONF_MSG__FMT_30x_END
55 
56 #define CONF_MSG_LEN_302(s1) (((s1)->len) +                             \
57                               strlen(CONF_MSG__FMT_302_BEG) +           \
58                               strlen(CONF_MSG__FMT_302_END))
59 
60 #define CONF_LINE_RET_303 "See Other"
61 #define CONF_MSG_FMT_303 "%s${vstr:%p%zu%zu%u}%s"
62 #define CONF_MSG__FMT_303_BEG CONF_MSG_RET_BEG "\
63     <title>303 See Other</title>\r\n\
64   </head>\r\n\
65   <body>\r\n\
66     <h1>303 See Other</h1>\r\n\
67     <p>The document should be seen <a href=\"\
68 "
69 #define CONF_MSG__FMT_303_END CONF_MSG__FMT_30x_END
70 
71 #define CONF_MSG_LEN_303(s1) (((s1)->len) +                             \
72                               strlen(CONF_MSG__FMT_303_BEG) +           \
73                               strlen(CONF_MSG__FMT_303_END))
74 
75 #define CONF_LINE_RET_307 "Temporary Redirect"
76 #define CONF_MSG_FMT_307 "%s${vstr:%p%zu%zu%u}%s"
77 #define CONF_MSG__FMT_307_BEG CONF_MSG_RET_BEG "\
78     <title>307 Temporary Redirect</title>\r\n\
79   </head>\r\n\
80   <body>\r\n\
81     <h1>307 Temporary Redirect</h1>\r\n\
82     <p>The document has temporarily moved <a href=\"\
83 "
84 #define CONF_MSG__FMT_307_END CONF_MSG__FMT_30x_END
85 
86 #define CONF_MSG_LEN_307(s1) (((s1)->len) +                             \
87                               strlen(CONF_MSG__FMT_307_BEG) +           \
88                               strlen(CONF_MSG__FMT_307_END))
89 
90 #define CONF_LINE_RET_400 "Bad Request"
91 #define CONF_MSG_RET_400 \
92     CONF_MSG__MAKE("400", "Bad Request", "The request could not be understood")
93 
94 #define CONF_LINE_RET_401 "Unauthorized"
95 #define CONF_MSG_RET_401 \
96     CONF_MSG__MAKE("401", "Unauthorized", "The request requires user authentication")
97 
98 #define CONF_LINE_RET_403 "Forbidden"
99 #define CONF_MSG_RET_403 \
100     CONF_MSG__MAKE("403", "Forbidden", "The request is forbidden")
101 
102 #define CONF_LINE_RET_404 "Not Found"
103 #define CONF_MSG_RET_404 \
104     CONF_MSG__MAKE("404", "Not Found", "The document requested was not found")
105 
106 #define CONF_LINE_RET_405 "Method Not Allowed"
107 #define CONF_MSG_RET_405 \
108     CONF_MSG__MAKE("405", "Method Not Allowed", "The method specified is not allowed")
109 
110 #define CONF_LINE_RET_406 "Not Acceptable"
111 #define CONF_MSG_RET_406 \
112     CONF_MSG__MAKE("406", "Not Acceptable", "The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request")
113 
114 #define CONF_LINE_RET_410 "Gone"
115 #define CONF_MSG_RET_410 \
116     CONF_MSG__MAKE("410", "Gone", "The requested resource is no longer available at the server and no forwarding address is known. This condition is expected to be considered permanent")
117 
118 #define CONF_LINE_RET_412 "Precondition Failed"
119 #define CONF_MSG_RET_412 \
120     CONF_MSG__MAKE("412", "Precondition Failed", "The precondition given in one or more of the request-header fields evaluated to false")
121 
122 #define CONF_LINE_RET_413 "Request Entity Too Large"
123 #define CONF_MSG_RET_413 \
124     CONF_MSG__MAKE("413", "Request Entity Too Large", "The server does not accept any requests with Content (In other words if either a Content-Length or a Transfer-Encoding header is passed to the server, the request will fail)")
125 
126 #define CONF_LINE_RET_414 "Request-URI Too Long"
127 #define CONF_MSG_RET_414 \
128     CONF_MSG__MAKE("414", "Request-URI Too Long", "The document request was too long")
129 
130 /*    CONF_MSG__MAKE("415", "", "") */
131 #define CONF_LINE_RET_415 "Unsupported Media Type"
132 #define CONF_MSG_RET_415 \
133     CONF_MSG__MAKE("415", "Unsupported Media Type", "The server is refusing to service the request because the result of the request is in a format not supported, by the request")
134 
135 #define CONF_LINE_RET_416 "Requested Range Not Satisfiable"
136 #define CONF_MSG_RET_416 \
137     CONF_MSG__MAKE("416", "Requested Range Not Satisfiable", "The document request range was not valid")
138 
139 #define CONF_LINE_RET_417 "Expectation Failed"
140 #define CONF_MSG_RET_417 \
141     CONF_MSG__MAKE("417", "Expectation Failed", "The expectation given in an Expect request-header field could not be met by this server")
142 
143 #define CONF_LINE_RET_500 "Internal Server Error"
144 #define CONF_MSG_RET_500 \
145     CONF_MSG__MAKE("500", "Internal Server Error", "The server encountered something unexpected")
146 
147 #define CONF_LINE_RET_501 "Not Implemented"
148 #define CONF_MSG_RET_501 \
149     CONF_MSG__MAKE("501", "Not Implemented", "The request method is not implemented")
150 
151 #define CONF_LINE_RET_503 "Service Unavailable"
152 #define CONF_MSG_RET_503 \
153     CONF_MSG__MAKE("503", "Service Unavailable", "The server is currently unable to handle the request due to a temporary overloading or maintenance of the server")
154 
155 #define CONF_LINE_RET_505 "Version not supported"
156 #define CONF_MSG_RET_505 \
157     CONF_MSG__MAKE("505", "Version not supported", "The version of http used is not supported")
158 
159 #define HTTPD_ERR_301(req) do {                           \
160       (req)->error_code = 301;                            \
161       (req)->error_line = CONF_LINE_RET_301;              \
162       (req)->error_len  = CONF_MSG_LEN_301((req)->fname); \
163     } while (0)
164 
165 #define HTTPD_ERR_302(req) do {                           \
166       (req)->error_code = 302;                            \
167       (req)->error_line = CONF_LINE_RET_302;              \
168       (req)->error_len  = CONF_MSG_LEN_302((req)->fname); \
169     } while (0)
170 
171 #define HTTPD_ERR_303(req) do {                           \
172       (req)->error_code = 303;                            \
173       (req)->error_line = CONF_LINE_RET_303;              \
174       (req)->error_len  = CONF_MSG_LEN_303((req)->fname); \
175     } while (0)
176 
177 #define HTTPD_ERR_307(req) do {                           \
178       (req)->error_code = 307;                            \
179       (req)->error_line = CONF_LINE_RET_307;              \
180       (req)->error_len  = CONF_MSG_LEN_307((req)->fname); \
181     } while (0)
182 
183 #define HTTPD_ERR(req, code) do {                           \
184       (req)->error_code  = (code);                          \
185       (req)->error_line  = CONF_LINE_RET_ ## code ;         \
186       (req)->error_len   = strlen( CONF_MSG_RET_ ## code ); \
187       if (!(req)->head_op)                                  \
188         (req)->error_msg = CONF_MSG_RET_ ## code ;          \
189     } while (0)
190 
191 #define HTTPD_ERR_RET(req, code, val) do {              \
192       HTTPD_ERR(req, code);                             \
193       return val ;                                      \
194     } while (0)
195 
196 #define HTTPD_ERR_GOTO(req, code, label) do {           \
197       HTTPD_ERR(req, code);                             \
198       goto label ;                                      \
199     } while (0)
200 
201 #endif
202