Fawkes API Fawkes Development Version
error_reply.cpp
1
2/***************************************************************************
3 * error_reply.h - Web request reply for an error page
4 *
5 * Created: Fri Oct 24 19:57:19 2008
6 * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#include <core/exceptions/software.h>
24#include <webview/error_reply.h>
25
26#include <cstdio>
27#include <cstdlib>
28
29namespace fawkes {
30
31/** @class WebErrorPageReply <webview/error_reply.h>
32 * Static error page reply.
33 * Shows a simple error page based on the given code.
34 * @author Tim Niemueller
35 */
36
37/** Constructor.
38 * @param code error code, must be a 4xx or 5xx HTTP code
39 * @param format format for additional error message, use format as
40 * known from sprintf.
41 */
42WebErrorPageReply::WebErrorPageReply(Code code, const char *format, ...) : WebPageReply(code)
43{
44 if (((int)code < 400) || ((int)code > 599)) {
45 throw fawkes::OutOfBoundsException("Error code invalid", code, 400, 599);
46 }
47
48 switch (code) {
50 _title = "400 BAD_REQUEST";
51 _body = "<h1>400 BAD_REQUEST</h1>";
52 break;
53
55 _title = "401 UNAUTHORIZED";
56 _body = "<h1>401 UNAUTHORIZED</h1>";
57 break;
58
60 _title = "402 PAYMENT_REQUIRED";
61 _body = "<h1>402 PAYMENT_REQUIRED</h1>";
62 break;
63
64 case HTTP_FORBIDDEN:
65 _title = "403 FORBIDDEN";
66 _body = "<h1>403 FORBIDDEN</h1>";
67 break;
68
69 case HTTP_NOT_FOUND:
70 _title = "404 NOT_FOUND";
71 _body = "<h1>404 NOT_FOUND</h1>";
72 break;
73
75 _title = "405 METHOD_NOT_ALLOWED";
76 _body = "<h1>405 METHOD_NOT_ALLOWED</h1>";
77 break;
78
80 _title = "406 METHOD_NOT_ACCEPTABLE";
81 _body = "<h1>406 METHOD_NOT_ACCEPTABLE</h1>";
82 break;
83
85 _title = "407 PROXY_AUTHENTICATION_REQUIRED";
86 _body = "<h1>407 PROXY_AUTHENTICATION_REQUIRED</h1>";
87 break;
88
90 _title = "408 REQUEST_TIMEOUT";
91 _body = "<h1>408 REQUEST_TIMEOUT</h1>";
92 break;
93
94 case HTTP_CONFLICT:
95 _title = "409 CONFLICT";
96 _body = "<h1>409 CONFLICT</h1>";
97 break;
98
99 case HTTP_GONE:
100 _title = "410 GONE";
101 _body = "<h1>410 GONE</h1>";
102 break;
103
105 _title = "411 LENGTH_REQUIRED";
106 _body = "<h1>411 LENGTH_REQUIRED</h1>";
107 break;
108
110 _title = "412 PRECONDITION_FAILED";
111 _body = "<h1>412 PRECONDITION_FAILED</h1>";
112 break;
113
115 _title = "413 REQUEST_ENTITY_TOO_LARGE";
116 _body = "<h1>413 REQUEST_ENTITY_TOO_LARGE</h1>";
117 break;
118
120 _title = "414 REQUEST_URI_TOO_LONG";
121 _body = "<h1>414 REQUEST_URI_TOO_LONG</h1>";
122 break;
123
125 _title = "415 UNSUPPORTED_MEDIA_TYPE";
126 _body = "<h1>415 UNSUPPORTED_MEDIA_TYPE</h1>";
127 break;
128
130 _title = "416 REQUESTED_RANGE_NOT_SATISFIABLE";
131 _body = "<h1>416 REQUESTED_RANGE_NOT_SATISFIABLE</h1>";
132 break;
133
135 _title = "417 EXPECTATION_FAILED";
136 _body = "<h1>417 EXPECTATION_FAILED</h1>";
137 break;
138
140 _title = "422 UNPROCESSABLE_ENTITY";
141 _body = "<h1>422 UNPROCESSABLE_ENTITY</h1>";
142 break;
143
144 case HTTP_LOCKED:
145 _title = "423 LOCKED";
146 _body = "<h1>423 LOCKED</h1>";
147 break;
148
150 _title = "424 FAILED_DEPENDENCY";
151 _body = "<h1>424 FAILED_DEPENDENCY</h1>";
152 break;
153
155 _title = "425 UNORDERED_COLLECTION";
156 _body = "<h1>425 UNORDERED_COLLECTION</h1>";
157 break;
158
160 _title = "426 UPGRADE_REQUIRED";
161 _body = "<h1>426 UPGRADE_REQUIRED</h1>";
162 break;
163
164 case HTTP_RETRY_WITH:
165 _title = "449 RETRY_WITH";
166 _body = "<h1>449 RETRY_WITH</h1>";
167 break;
168
170 _title = "500 INTERNAL_SERVER_ERROR";
171 _body = "<h1>500 INTERNAL_SERVER_ERROR</h1>";
172 break;
173
175 _title = "501 NOT_IMPLEMENTED";
176 _body = "<h1>501 NOT_IMPLEMENTED</h1>";
177 break;
178
179 case HTTP_BAD_GATEWAY:
180 _title = "502 BAD_GATEWAY";
181 _body = "<h1>502 BAD_GATEWAY</h1>";
182 break;
183
185 _title = "503 SERVICE_UNAVAILABLE";
186 _body = "<h1>503 SERVICE_UNAVAILABLE</h1>";
187 break;
188
190 _title = "504 GATEWAY_TIMEOUT";
191 _body = "<h1>504 GATEWAY_TIMEOUT</h1>";
192 break;
193
195 _title = "505 HTTP_VERSION_NOT_SUPPORTED";
196 _body = "<h1>505 HTTP_VERSION_NOT_SUPPORTED</h1>";
197 break;
198
200 _title = "506 VARIANT_ALSO_NEGOTIATES";
201 _body = "<h1>506 VARIANT_ALSO_NEGOTIATES</h1>";
202 break;
203
205 _title = "507 INSUFFICIENT_STORAGE";
206 _body = "<h1>507 INSUFFICIENT_STORAGE</h1>";
207 break;
208
210 _title = "509 BANDWIDTH_LIMIT_EXCEEDED";
211 _body = "<h1>509 BANDWIDTH_LIMIT_EXCEEDED</h1>";
212 break;
213
215 _title = "510 NOT_EXTENDED";
216 _body = "<h1>510 NOT_EXTENDED</h1>";
217 break;
218
219 default:
220 _title = "Unknown Error";
221 _body = "<h1>Unknown Error</h1>";
222 break;
223 }
224
225 if (format) {
226 va_list args;
227 va_start(args, format);
228 char *tmp;
229 if (vasprintf(&tmp, format, args) != -1) {
230 _body += std::string("<br />\n<b>") + tmp + "</b>\n";
231 free(tmp);
232 }
233 }
234}
235
236} // end namespace fawkes
Index out of bounds.
Definition: software.h:86
std::string _body
Body of the reply.
Definition: reply.h:151
WebErrorPageReply(Code error_code, const char *format=NULL,...)
Constructor.
Definition: error_reply.cpp:42
Basic page reply.
Definition: page_reply.h:34
std::string _title
Title of the page.
Definition: page_reply.h:61
Code code() const
Get response code.
Definition: reply.cpp:104
Code
HTTP response code.
Definition: reply.h:37
@ HTTP_UNAUTHORIZED
UNAUTHORIZED.
Definition: reply.h:61
@ HTTP_BANDWIDTH_LIMIT_EXCEEDED
BANDWIDTH_LIMIT_EXCEEDED.
Definition: reply.h:93
@ HTTP_UPGRADE_REQUIRED
UPGRADE_REQUIRED.
Definition: reply.h:82
@ HTTP_UNPROCESSABLE_ENTITY
UNPROCESSABLE_ENTITY.
Definition: reply.h:78
@ HTTP_PROXY_AUTHENTICATION_REQUIRED
PROXY_AUTHENTICATION_REQUIRED.
Definition: reply.h:67
@ HTTP_REQUEST_TIMEOUT
REQUEST_TIMEOUT.
Definition: reply.h:68
@ HTTP_METHOD_NOT_ALLOWED
METHOD_NOT_ALLOWED.
Definition: reply.h:65
@ HTTP_UNSUPPORTED_MEDIA_TYPE
UNSUPPORTED_MEDIA_TYPE.
Definition: reply.h:75
@ HTTP_VARIANT_ALSO_NEGOTIATES
VARIANT_ALSO_NEGOTIATES.
Definition: reply.h:91
@ HTTP_EXPECTATION_FAILED
EXPECTATION_FAILED.
Definition: reply.h:77
@ HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
REQUESTED_RANGE_NOT_SATISFIABLE.
Definition: reply.h:76
@ HTTP_HTTP_VERSION_NOT_SUPPORTED
HTTP_VERSION_NOT_SUPPORTED.
Definition: reply.h:90
@ HTTP_BAD_GATEWAY
BAD_GATEWAY.
Definition: reply.h:87
@ HTTP_REQUEST_ENTITY_TOO_LARGE
REQUEST_ENTITY_TOO_LARGE.
Definition: reply.h:73
@ HTTP_REQUEST_URI_TOO_LONG
REQUEST_URI_TOO_LONG.
Definition: reply.h:74
@ HTTP_INTERNAL_SERVER_ERROR
INTERNAL_SERVER_ERROR.
Definition: reply.h:85
@ HTTP_FORBIDDEN
FORBIDDEN.
Definition: reply.h:63
@ HTTP_RETRY_WITH
RETRY_WITH.
Definition: reply.h:83
@ HTTP_METHOD_NOT_ACCEPTABLE
METHOD_NOT_ACCEPTABLE.
Definition: reply.h:66
@ HTTP_INSUFFICIENT_STORAGE
INSUFFICIENT_STORAGE.
Definition: reply.h:92
@ HTTP_PAYMENT_REQUIRED
PAYMENT_REQUIRED.
Definition: reply.h:62
@ HTTP_GONE
GONE.
Definition: reply.h:70
@ HTTP_LENGTH_REQUIRED
LENGTH_REQUIRED.
Definition: reply.h:71
@ HTTP_BAD_REQUEST
BAD_REQUEST.
Definition: reply.h:60
@ HTTP_PRECONDITION_FAILED
PRECONDITION_FAILED.
Definition: reply.h:72
@ HTTP_UNORDERED_COLLECTION
UNORDERED_COLLECTION.
Definition: reply.h:81
@ HTTP_CONFLICT
CONFLICT.
Definition: reply.h:69
@ HTTP_NOT_EXTENDED
NOT_EXTENDED.
Definition: reply.h:94
@ HTTP_FAILED_DEPENDENCY
FAILED_DEPENDENCY.
Definition: reply.h:80
@ HTTP_GATEWAY_TIMEOUT
GATEWAY_TIMEOUT.
Definition: reply.h:89
@ HTTP_NOT_IMPLEMENTED
NOT_IMPLEMENTED.
Definition: reply.h:86
@ HTTP_LOCKED
LOCKED.
Definition: reply.h:79
@ HTTP_NOT_FOUND
NOT_FOUND.
Definition: reply.h:64
@ HTTP_SERVICE_UNAVAILABLE
SERVICE_UNAVAILABLE.
Definition: reply.h:88
Fawkes library namespace.