]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
f70aedc4 | 2 | * Copyright (C) 1996-2021 The Squid Software Foundation and contributors |
e25c139f | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
30a4f2a8 | 7 | */ |
bbc27441 AJ |
8 | |
9 | /* DEBUG: section 04 Error Generation */ | |
10 | ||
f7f3304a | 11 | #include "squid.h" |
7e6eabbc | 12 | #include "AccessLogEntry.h" |
8a01b99e | 13 | #include "cache_cf.h" |
92ae4c86 | 14 | #include "clients/forward.h" |
e0d28505 | 15 | #include "comm/Connection.h" |
ec41b64c | 16 | #include "comm/Write.h" |
83b053a0 CT |
17 | #include "error/Detail.h" |
18 | #include "error/SysErrorDetail.h" | |
aa839030 | 19 | #include "errorpage.h" |
602d9612 | 20 | #include "fde.h" |
7e6eabbc | 21 | #include "format/Format.h" |
b3f7fd88 | 22 | #include "fs_io.h" |
25f98340 | 23 | #include "html_quote.h" |
a5bac1d2 | 24 | #include "HttpHeaderTools.h" |
528b2c61 | 25 | #include "HttpReply.h" |
26 | #include "HttpRequest.h" | |
0eb49b6d | 27 | #include "MemBuf.h" |
602d9612 | 28 | #include "MemObject.h" |
1fa9b1a7 | 29 | #include "rfc1738.h" |
7e6eabbc | 30 | #include "sbuf/Stream.h" |
4d5904f7 | 31 | #include "SquidConfig.h" |
7e6eabbc | 32 | #include "SquidTime.h" |
602d9612 A |
33 | #include "Store.h" |
34 | #include "tools.h" | |
d295d770 | 35 | #include "wordlist.h" |
4e540555 FC |
36 | #if USE_AUTH |
37 | #include "auth/UserRequest.h" | |
38 | #endif | |
cb4f4424 | 39 | #if USE_OPENSSL |
4e540555 FC |
40 | #include "ssl/ErrorDetailManager.h" |
41 | #endif | |
02922e76 | 42 | |
63be0a78 | 43 | /** |
44 | \defgroup ErrorPageInternal Error Page Internals | |
45 | \ingroup ErrorPageAPI | |
46 | * | |
f439fbd2 | 47 | \section ErrorPagesAbstract Abstract: |
63be0a78 | 48 | * These routines are used to generate error messages to be |
49 | * sent to clients. The error type is used to select between | |
50 | * the various message formats. (formats are stored in the | |
51 | * Config.errorDirectory) | |
52 | */ | |
53 | ||
605f2c3e | 54 | #if !defined(DEFAULT_SQUID_ERROR_DIR) |
43000484 AJ |
55 | /** Where to look for errors if config path fails. |
56 | \note Please use ./configure --datadir=/path instead of patching | |
57 | */ | |
58 | #define DEFAULT_SQUID_ERROR_DIR DEFAULT_SQUID_DATA_DIR"/errors" | |
59 | #endif | |
60 | ||
63be0a78 | 61 | /// \ingroup ErrorPageInternal |
aa839030 | 62 | CBDATA_CLASS_INIT(ErrorState); |
63 | ||
7e6eabbc CT |
64 | const SBuf ErrorState::LogformatMagic("@Squid{"); |
65 | ||
02922e76 | 66 | /* local types */ |
67 | ||
7e6eabbc CT |
68 | /// an error page created from admin-configurable metadata (e.g. deny_info) |
69 | class ErrorDynamicPageInfo { | |
70 | public: | |
71 | ErrorDynamicPageInfo(const int anId, const char *aName, const SBuf &aCfgLocation); | |
72 | ~ErrorDynamicPageInfo() { xfree(page_name); } | |
73 | ||
74 | /// error_text[] index for response body (unused in redirection responses) | |
02922e76 | 75 | int id; |
7e6eabbc CT |
76 | |
77 | /// Primary deny_info parameter: | |
78 | /// * May start with an HTTP status code. | |
79 | /// * Either a well-known error page name, a filename, or a redirect URL. | |
02922e76 | 80 | char *page_name; |
7e6eabbc CT |
81 | |
82 | /// admin-configured HTTP Location header value for redirection responses | |
83 | const char *uri; | |
84 | ||
85 | /// admin-configured name for the error page template (custom or standard) | |
86 | const char *filename; | |
87 | ||
88 | /// deny_info directive position in squid.conf (for reporting) | |
89 | SBuf cfgLocation; | |
90 | ||
91 | // XXX: Misnamed. Not just for redirects. | |
92 | /// admin-configured HTTP status code | |
955394ce | 93 | Http::StatusCode page_redirect; |
7e6eabbc CT |
94 | |
95 | private: | |
96 | // no copying of any kind | |
97 | ErrorDynamicPageInfo(ErrorDynamicPageInfo &&) = delete; | |
98 | }; | |
99 | ||
100 | namespace ErrorPage { | |
101 | ||
102 | /// state and parameters shared by several ErrorState::compile*() methods | |
103 | class Build | |
104 | { | |
105 | public: | |
106 | SBuf output; ///< compilation result | |
107 | const char *input = nullptr; ///< template bytes that need to be compiled | |
108 | bool building_deny_info_url = false; ///< whether we compile deny_info URI | |
109 | bool allowRecursion = false; ///< whether top-level compile() calls are OK | |
110 | }; | |
111 | ||
112 | /// pretty-prints error page/deny_info building error | |
113 | class BuildErrorPrinter | |
114 | { | |
115 | public: | |
116 | BuildErrorPrinter(const SBuf &anInputLocation, int aPage, const char *aMsg, const char *aNear): inputLocation(anInputLocation), page_id(aPage), msg(aMsg), near(aNear) {} | |
117 | ||
118 | /// reports error details (for admin-visible exceptions and debugging) | |
119 | std::ostream &print(std::ostream &) const; | |
120 | ||
121 | /// print() helper to report where the error was found | |
122 | std::ostream &printLocation(std::ostream &os) const; | |
123 | ||
124 | /* saved constructor parameters */ | |
125 | const SBuf &inputLocation; | |
126 | const int page_id; | |
127 | const char *msg; | |
128 | const char *near; | |
129 | }; | |
130 | ||
131 | static inline std::ostream & | |
132 | operator <<(std::ostream &os, const BuildErrorPrinter &context) | |
133 | { | |
134 | return context.print(os); | |
135 | } | |
136 | ||
137 | static const char *IsDenyInfoUri(const int page_id); | |
138 | ||
139 | static void ImportStaticErrorText(const int page_id, const char *text, const SBuf &inputLocation); | |
140 | static void ValidateStaticError(const int page_id, const SBuf &inputLocation); | |
141 | ||
142 | } // namespace ErrorPage | |
02922e76 | 143 | |
144 | /* local constant and vars */ | |
145 | ||
63be0a78 | 146 | /** |
cfdb8f88 | 147 | \ingroup ErrorPageInternal |
63be0a78 | 148 | * |
149 | \note hard coded error messages are not appended with %S | |
150 | * automagically to give you more control on the format | |
1d803566 | 151 | */ |
9e008dda | 152 | static const struct { |
f53969cc | 153 | int type; /* and page_id */ |
2ac76861 | 154 | const char *text; |
62e76326 | 155 | } |
156 | ||
157 | error_hard_text[] = { | |
158 | ||
9e008dda AJ |
159 | { |
160 | ERR_SQUID_SIGNATURE, | |
161 | "\n<br>\n" | |
162 | "<hr>\n" | |
163 | "<div id=\"footer\">\n" | |
164 | "Generated %T by %h (%s)\n" | |
165 | "</div>\n" | |
166 | "</body></html>\n" | |
167 | }, | |
168 | { | |
169 | TCP_RESET, | |
170 | "reset" | |
7e6eabbc | 171 | }, |
83b053a0 CT |
172 | { |
173 | ERR_CLIENT_GONE, | |
174 | "unexpected client disconnect" | |
175 | }, | |
7e6eabbc CT |
176 | { |
177 | ERR_SECURE_ACCEPT_FAIL, | |
178 | "secure accept fail" | |
179 | }, | |
180 | { | |
181 | ERR_REQUEST_START_TIMEOUT, | |
182 | "request start timedout" | |
183 | }, | |
184 | { | |
185 | MGR_INDEX, | |
186 | "mgr_index" | |
9e008dda AJ |
187 | } |
188 | }; | |
02922e76 | 189 | |
63be0a78 | 190 | /// \ingroup ErrorPageInternal |
c8ea3cc0 | 191 | static std::vector<ErrorDynamicPageInfo *> ErrorDynamicPages; |
02922e76 | 192 | |
193 | /* local prototypes */ | |
194 | ||
63be0a78 | 195 | /// \ingroup ErrorPageInternal |
2ac76861 | 196 | static const int error_hard_text_count = sizeof(error_hard_text) / sizeof(*error_hard_text); |
63be0a78 | 197 | |
198 | /// \ingroup ErrorPageInternal | |
02922e76 | 199 | static char **error_text = NULL; |
63be0a78 | 200 | |
201 | /// \ingroup ErrorPageInternal | |
02922e76 | 202 | static int error_page_count = 0; |
9b312a19 | 203 | |
5b52cb6c | 204 | /// \ingroup ErrorPageInternal |
0ae3294a | 205 | static MemBuf error_stylesheet; |
5b52cb6c | 206 | |
1d803566 | 207 | static const char *errorFindHardText(err_type type); |
2b663917 | 208 | static IOCB errorSendComplete; |
1e74c110 | 209 | |
02259ff8 CT |
210 | /// \ingroup ErrorPageInternal |
211 | /// manages an error page template | |
dc49061a A |
212 | class ErrorPageFile: public TemplateFile |
213 | { | |
02259ff8 | 214 | public: |
7e6eabbc | 215 | ErrorPageFile(const char *name, const err_type code) : TemplateFile(name, code) {} |
02259ff8 CT |
216 | |
217 | /// The template text data read from disk | |
7e6eabbc | 218 | const char *text() { return template_.c_str(); } |
02259ff8 | 219 | |
7e6eabbc CT |
220 | protected: |
221 | virtual void setDefault() override { | |
222 | template_ = "Internal Error: Missing Template "; | |
223 | template_.append(templateName.termedBuf()); | |
02259ff8 | 224 | } |
02259ff8 | 225 | }; |
e6ccf245 | 226 | |
63be0a78 | 227 | /// \ingroup ErrorPageInternal |
e6ccf245 | 228 | err_type &operator++ (err_type &anErr) |
229 | { | |
1f1ae50a | 230 | int tmp = (int)anErr; |
231 | anErr = (err_type)(++tmp); | |
e6ccf245 | 232 | return anErr; |
233 | } | |
4ff59fc7 | 234 | |
63be0a78 | 235 | /// \ingroup ErrorPageInternal |
62e76326 | 236 | int operator - (err_type const &anErr, err_type const &anErr2) |
237 | { | |
4ff59fc7 | 238 | return (int)anErr - (int)anErr2; |
239 | } | |
240 | ||
7e6eabbc CT |
241 | /// \return deny_info URL if the given page is a deny_info page with a URL |
242 | /// \return nullptr otherwise | |
243 | static const char * | |
244 | ErrorPage::IsDenyInfoUri(const int page_id) | |
245 | { | |
246 | if (ERR_MAX <= page_id && page_id < error_page_count) | |
247 | return ErrorDynamicPages.at(page_id - ERR_MAX)->uri; // may be nil | |
248 | return nullptr; | |
249 | } | |
250 | ||
b8d8561b | 251 | void |
0673c0ba | 252 | errorInitialize(void) |
fa966b74 | 253 | { |
7e6eabbc CT |
254 | using ErrorPage::ImportStaticErrorText; |
255 | ||
728da2ee | 256 | err_type i; |
1d803566 | 257 | const char *text; |
4ff59fc7 | 258 | error_page_count = ERR_MAX + ErrorDynamicPages.size(); |
e6ccf245 | 259 | error_text = static_cast<char **>(xcalloc(error_page_count, sizeof(char *))); |
62e76326 | 260 | |
e6ccf245 | 261 | for (i = ERR_NONE, ++i; i < error_page_count; ++i) { |
62e76326 | 262 | safe_free(error_text[i]); |
62e76326 | 263 | |
43000484 AJ |
264 | if ((text = errorFindHardText(i))) { |
265 | /**\par | |
266 | * Index any hard-coded error text into defaults. | |
267 | */ | |
7e6eabbc CT |
268 | static const SBuf builtIn("built-in"); |
269 | ImportStaticErrorText(i, text, builtIn); | |
43000484 AJ |
270 | |
271 | } else if (i < ERR_MAX) { | |
272 | /**\par | |
273 | * Index precompiled fixed template files from one of two sources: | |
274 | * (a) default language translation directory (error_default_language) | |
275 | * (b) admin specified custom directory (error_directory) | |
276 | */ | |
8ff2520a | 277 | ErrorPageFile errTmpl(err_type_str[i], i); |
7e6eabbc CT |
278 | errTmpl.loadDefault(); |
279 | ImportStaticErrorText(i, errTmpl.text(), errTmpl.filename); | |
62e76326 | 280 | } else { |
43000484 AJ |
281 | /** \par |
282 | * Index any unknown file names used by deny_info. | |
283 | */ | |
e6334d92 | 284 | ErrorDynamicPageInfo *info = ErrorDynamicPages.at(i - ERR_MAX); |
62e76326 | 285 | assert(info && info->id == i && info->page_name); |
286 | ||
7e6eabbc | 287 | if (info->filename) { |
43000484 | 288 | /** But only if they are not redirection URL. */ |
7e6eabbc CT |
289 | ErrorPageFile errTmpl(info->filename, ERR_MAX); |
290 | errTmpl.loadDefault(); | |
291 | ImportStaticErrorText(i, errTmpl.text(), errTmpl.filename); | |
292 | } else { | |
293 | assert(info->uri); | |
294 | ErrorPage::ValidateStaticError(i, info->cfgLocation); | |
62e76326 | 295 | } |
296 | } | |
1d803566 | 297 | } |
5b52cb6c | 298 | |
0ae3294a AJ |
299 | error_stylesheet.reset(); |
300 | ||
5b52cb6c | 301 | // look for and load stylesheet into global MemBuf for it. |
9e008dda | 302 | if (Config.errorStylesheet) { |
8ff2520a | 303 | ErrorPageFile tmpl("StylesSheet", ERR_MAX); |
02259ff8 | 304 | tmpl.loadFromFile(Config.errorStylesheet); |
4391cd15 | 305 | error_stylesheet.appendf("%s",tmpl.text()); |
5b52cb6c | 306 | } |
02259ff8 | 307 | |
cb4f4424 | 308 | #if USE_OPENSSL |
02259ff8 | 309 | Ssl::errorDetailInitialize(); |
c259af96 | 310 | #endif |
1d803566 | 311 | } |
312 | ||
c68e9c6b | 313 | void |
314 | errorClean(void) | |
315 | { | |
316 | if (error_text) { | |
62e76326 | 317 | int i; |
318 | ||
95dc7ff4 | 319 | for (i = ERR_NONE + 1; i < error_page_count; ++i) |
62e76326 | 320 | safe_free(error_text[i]); |
321 | ||
322 | safe_free(error_text); | |
c68e9c6b | 323 | } |
62e76326 | 324 | |
385acf91 | 325 | while (!ErrorDynamicPages.empty()) { |
7e6eabbc | 326 | delete ErrorDynamicPages.back(); |
38c22baf FC |
327 | ErrorDynamicPages.pop_back(); |
328 | } | |
62e76326 | 329 | |
c68e9c6b | 330 | error_page_count = 0; |
02259ff8 | 331 | |
cb4f4424 | 332 | #if USE_OPENSSL |
02259ff8 | 333 | Ssl::errorDetailClean(); |
c259af96 | 334 | #endif |
c68e9c6b | 335 | } |
336 | ||
63be0a78 | 337 | /// \ingroup ErrorPageInternal |
1d803566 | 338 | static const char * |
339 | errorFindHardText(err_type type) | |
340 | { | |
341 | int i; | |
62e76326 | 342 | |
95dc7ff4 | 343 | for (i = 0; i < error_hard_text_count; ++i) |
62e76326 | 344 | if (error_hard_text[i].type == type) |
345 | return error_hard_text[i].text; | |
346 | ||
1d803566 | 347 | return NULL; |
348 | } | |
349 | ||
8ff2520a | 350 | TemplateFile::TemplateFile(const char *name, const err_type code): silent(false), wasLoaded(false), templateName(name), templateCode(code) |
1d803566 | 351 | { |
02259ff8 CT |
352 | assert(name); |
353 | } | |
354 | ||
7e6eabbc | 355 | void |
02259ff8 CT |
356 | TemplateFile::loadDefault() |
357 | { | |
358 | if (loaded()) // already loaded? | |
7e6eabbc | 359 | return; |
43000484 AJ |
360 | |
361 | /** test error_directory configured location */ | |
02259ff8 CT |
362 | if (Config.errorDirectory) { |
363 | char path[MAXPATHLEN]; | |
364 | snprintf(path, sizeof(path), "%s/%s", Config.errorDirectory, templateName.termedBuf()); | |
365 | loadFromFile(path); | |
366 | } | |
43000484 AJ |
367 | |
368 | #if USE_ERR_LOCALES | |
369 | /** test error_default_language location */ | |
02259ff8 CT |
370 | if (!loaded() && Config.errorDefaultLanguage) { |
371 | if (!tryLoadTemplate(Config.errorDefaultLanguage)) { | |
8ff2520a | 372 | debugs(1, (templateCode < TCP_RESET ? DBG_CRITICAL : 3), "Unable to load default error language files. Reset to backups."); |
43000484 AJ |
373 | } |
374 | } | |
375 | #endif | |
62e76326 | 376 | |
43000484 | 377 | /* test default location if failed (templates == English translation base templates) */ |
02259ff8 CT |
378 | if (!loaded()) { |
379 | tryLoadTemplate("templates"); | |
5bf33a09 | 380 | } |
62e76326 | 381 | |
1d803566 | 382 | /* giving up if failed */ |
b073fc4b | 383 | if (!loaded()) { |
8ff2520a | 384 | debugs(1, (templateCode < TCP_RESET ? DBG_CRITICAL : 3), "WARNING: failed to find or read error text file " << templateName); |
7e6eabbc CT |
385 | template_.clear(); |
386 | setDefault(); | |
387 | wasLoaded = true; | |
b073fc4b | 388 | } |
1d803566 | 389 | } |
390 | ||
02259ff8 CT |
391 | bool |
392 | TemplateFile::tryLoadTemplate(const char *lang) | |
1d803566 | 393 | { |
02259ff8 CT |
394 | assert(lang); |
395 | ||
9b312a19 | 396 | char path[MAXPATHLEN]; |
02259ff8 CT |
397 | /* TODO: prep the directory path string to prevent snprintf ... */ |
398 | snprintf(path, sizeof(path), "%s/%s/%s", | |
399 | DEFAULT_SQUID_ERROR_DIR, lang, templateName.termedBuf()); | |
400 | path[MAXPATHLEN-1] = '\0'; | |
401 | ||
402 | if (loadFromFile(path)) | |
403 | return true; | |
404 | ||
405 | #if HAVE_GLOB | |
406 | if ( strlen(lang) == 2) { | |
407 | /* TODO glob the error directory for sub-dirs matching: <tag> '-*' */ | |
408 | /* use first result. */ | |
409 | debugs(4,2, HERE << "wildcard fallback errors not coded yet."); | |
410 | } | |
411 | #endif | |
412 | ||
413 | return false; | |
414 | } | |
415 | ||
416 | bool | |
417 | TemplateFile::loadFromFile(const char *path) | |
418 | { | |
419 | int fd; | |
e8f6c5c7 | 420 | char buf[4096]; |
e8f6c5c7 | 421 | ssize_t len; |
1d803566 | 422 | |
02259ff8 CT |
423 | if (loaded()) // already loaded? |
424 | return true; | |
45308e4d | 425 | |
c4aefe96 | 426 | fd = file_open(path, O_RDONLY | O_TEXT); |
e8f6c5c7 | 427 | |
428 | if (fd < 0) { | |
43000484 | 429 | /* with dynamic locale negotiation we may see some failures before a success. */ |
b69e9ffa AJ |
430 | if (!silent && templateCode < TCP_RESET) { |
431 | int xerrno = errno; | |
ec1c5408 | 432 | debugs(4, DBG_CRITICAL, "ERROR: loading file '" << path << "': " << xstrerr(xerrno)); |
b69e9ffa | 433 | } |
02259ff8 CT |
434 | wasLoaded = false; |
435 | return wasLoaded; | |
1d803566 | 436 | } |
62e76326 | 437 | |
7e6eabbc | 438 | template_.clear(); |
9e008dda | 439 | while ((len = FD_READ_METHOD(fd, buf, sizeof(buf))) > 0) { |
7e6eabbc | 440 | template_.append(buf, len); |
e8f6c5c7 | 441 | } |
62e76326 | 442 | |
e8f6c5c7 | 443 | if (len < 0) { |
b69e9ffa | 444 | int xerrno = errno; |
7e6eabbc | 445 | file_close(fd); |
b69e9ffa | 446 | debugs(4, DBG_CRITICAL, MYNAME << "ERROR: failed to fully read: '" << path << "': " << xstrerr(xerrno)); |
7e6eabbc CT |
447 | wasLoaded = false; |
448 | return false; | |
1e74c110 | 449 | } |
62e76326 | 450 | |
1d803566 | 451 | file_close(fd); |
e8f6c5c7 | 452 | |
7e6eabbc CT |
453 | filename = SBuf(path); |
454 | ||
455 | if (!parse()) { | |
456 | debugs(4, DBG_CRITICAL, "ERROR: parsing error in template file: " << path); | |
457 | wasLoaded = false; | |
458 | return false; | |
459 | } | |
460 | ||
02259ff8 CT |
461 | wasLoaded = true; |
462 | return wasLoaded; | |
463 | } | |
464 | ||
465 | bool strHdrAcptLangGetItem(const String &hdr, char *lang, int langLen, size_t &pos) | |
466 | { | |
dc49061a | 467 | while (pos < hdr.size()) { |
02259ff8 | 468 | |
a32e7644 AJ |
469 | /* skip any initial whitespace. */ |
470 | while (pos < hdr.size() && xisspace(hdr[pos])) | |
471 | ++pos; | |
62e76326 | 472 | |
02259ff8 CT |
473 | /* |
474 | * Header value format: | |
475 | * - sequence of whitespace delimited tags | |
476 | * - each tag may suffix with ';'.* which we can ignore. | |
477 | * - IFF a tag contains only two characters we can wildcard ANY translations matching: <it> '-'? .* | |
478 | * with preference given to an exact match. | |
479 | */ | |
480 | bool invalid_byte = false; | |
3d4c0e37 | 481 | char *dt = lang; |
02259ff8 CT |
482 | while (pos < hdr.size() && hdr[pos] != ';' && hdr[pos] != ',' && !xisspace(hdr[pos]) && dt < (lang + (langLen -1)) ) { |
483 | if (!invalid_byte) { | |
484 | #if USE_HTTP_VIOLATIONS | |
485 | // if accepting violations we may as well accept some broken browsers | |
486 | // which may send us the right code, wrong ISO formatting. | |
487 | if (hdr[pos] == '_') | |
488 | *dt = '-'; | |
489 | else | |
490 | #endif | |
491 | *dt = xtolower(hdr[pos]); | |
492 | // valid codes only contain A-Z, hyphen (-) and * | |
493 | if (*dt != '-' && *dt != '*' && (*dt < 'a' || *dt > 'z') ) | |
494 | invalid_byte = true; | |
495 | else | |
95dc7ff4 | 496 | ++dt; // move to next destination byte. |
02259ff8 | 497 | } |
95dc7ff4 | 498 | ++pos; |
02259ff8 | 499 | } |
a38ec4b1 | 500 | *dt = '\0'; // nul-terminated the filename content string before system use. |
62e76326 | 501 | |
a32e7644 AJ |
502 | // if we terminated the tag on garbage or ';' we need to skip to the next ',' or end of header. |
503 | while (pos < hdr.size() && hdr[pos] != ',') | |
504 | ++pos; | |
505 | ||
506 | if (pos < hdr.size() && hdr[pos] == ',') | |
507 | ++pos; | |
508 | ||
3d4c0e37 | 509 | debugs(4, 9, "STATE: lang=" << lang << ", pos=" << pos << ", buf='" << ((pos < hdr.size()) ? hdr.substr(pos,hdr.size()) : "") << "'"); |
02259ff8 CT |
510 | |
511 | /* if we found anything we might use, try it. */ | |
512 | if (*lang != '\0' && !invalid_byte) | |
513 | return true; | |
514 | } | |
515 | return false; | |
516 | } | |
517 | ||
518 | bool | |
b248c2a3 | 519 | TemplateFile::loadFor(const HttpRequest *request) |
02259ff8 CT |
520 | { |
521 | String hdr; | |
522 | ||
cf0a0bdf | 523 | #if USE_ERR_LOCALES |
02259ff8 CT |
524 | if (loaded()) // already loaded? |
525 | return true; | |
526 | ||
4c7c97f3 | 527 | if (!request || !request->header.getList(Http::HdrType::ACCEPT_LANGUAGE, &hdr)) |
02259ff8 CT |
528 | return false; |
529 | ||
530 | char lang[256]; | |
531 | size_t pos = 0; // current parsing position in header string | |
532 | ||
533 | debugs(4, 6, HERE << "Testing Header: '" << hdr << "'"); | |
534 | ||
535 | while ( strHdrAcptLangGetItem(hdr, lang, 256, pos) ) { | |
536 | ||
537 | /* wildcard uses the configured default language */ | |
538 | if (lang[0] == '*' && lang[1] == '\0') { | |
539 | debugs(4, 6, HERE << "Found language '" << lang << "'. Using configured default."); | |
540 | return false; | |
541 | } | |
542 | ||
543 | debugs(4, 6, HERE << "Found language '" << lang << "', testing for available template"); | |
544 | ||
545 | if (tryLoadTemplate(lang)) { | |
546 | /* store the language we found for the Content-Language reply header */ | |
547 | errLanguage = lang; | |
548 | break; | |
549 | } else if (Config.errorLogMissingLanguages) { | |
550 | debugs(4, DBG_IMPORTANT, "WARNING: Error Pages Missing Language: " << lang); | |
551 | } | |
552 | } | |
cf0a0bdf | 553 | #endif |
02259ff8 CT |
554 | |
555 | return loaded(); | |
6eb42cae | 556 | } |
557 | ||
7e6eabbc CT |
558 | ErrorDynamicPageInfo::ErrorDynamicPageInfo(const int anId, const char *aName, const SBuf &aCfgLocation): |
559 | id(anId), | |
560 | page_name(xstrdup(aName)), | |
561 | uri(nullptr), | |
562 | filename(nullptr), | |
563 | cfgLocation(aCfgLocation), | |
564 | page_redirect(static_cast<Http::StatusCode>(atoi(page_name))) | |
02922e76 | 565 | { |
7e6eabbc CT |
566 | const char *filenameOrUri = nullptr; |
567 | if (xisdigit(*page_name)) { | |
568 | if (const char *statusCodeEnd = strchr(page_name, ':')) | |
569 | filenameOrUri = statusCodeEnd + 1; | |
570 | } else { | |
571 | assert(!page_redirect); | |
572 | filenameOrUri = page_name; | |
573 | } | |
574 | ||
575 | // Guessed uri, filename, or both values may be nil or malformed. | |
576 | // They are validated later. | |
577 | if (!page_redirect) { | |
578 | if (filenameOrUri && strchr(filenameOrUri, ':')) // looks like a URL | |
579 | uri = filenameOrUri; | |
580 | else | |
581 | filename = filenameOrUri; | |
582 | } | |
583 | else if (page_redirect/100 == 3) { | |
584 | // redirects imply a URL | |
585 | uri = filenameOrUri; | |
586 | } else { | |
587 | // non-redirects imply an error page name | |
588 | filename = filenameOrUri; | |
589 | } | |
590 | ||
591 | const auto info = this; // source code change reduction hack | |
592 | // TODO: Move and refactor to avoid self_destruct()s in reconfigure. | |
aed9a15b AJ |
593 | |
594 | /* WARNING on redirection status: | |
595 | * 2xx are permitted, but not documented officially. | |
596 | * - might be useful for serving static files (PAC etc) in special cases | |
597 | * 3xx require a URL suitable for Location: header. | |
598 | * - the current design does not allow for a Location: URI as well as a local file template | |
599 | * although this possibility is explicitly permitted in the specs. | |
600 | * 4xx-5xx require a local file template. | |
601 | * - sending Location: on these codes with no body is invalid by the specs. | |
602 | * - current result is Squid crashing or XSS problems as dynamic deny_info load random disk files. | |
603 | * - a future redesign of the file loading may result in loading remote objects sent inline as local body. | |
604 | */ | |
955394ce | 605 | if (info->page_redirect == Http::scNone) |
aed9a15b AJ |
606 | ; // special case okay. |
607 | else if (info->page_redirect < 200 || info->page_redirect > 599) { | |
608 | // out of range | |
609 | debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " is not valid on '" << page_name << "'"); | |
610 | self_destruct(); | |
611 | } else if ( /* >= 200 && */ info->page_redirect < 300 && strchr(&(page_name[4]), ':')) { | |
612 | // 2xx require a local template file | |
0ff52055 | 613 | debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a template on '" << page_name << "'"); |
aed9a15b | 614 | self_destruct(); |
0ff52055 | 615 | } else if (info->page_redirect >= 300 && info->page_redirect <= 399 && !strchr(&(page_name[4]), ':')) { |
aed9a15b | 616 | // 3xx require an absolute URL |
0ff52055 | 617 | debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a URL on '" << page_name << "'"); |
aed9a15b AJ |
618 | self_destruct(); |
619 | } else if (info->page_redirect >= 400 /* && <= 599 */ && strchr(&(page_name[4]), ':')) { | |
620 | // 4xx/5xx require a local template file | |
0ff52055 | 621 | debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a template on '" << page_name << "'"); |
aed9a15b AJ |
622 | self_destruct(); |
623 | } | |
624 | // else okay. | |
02922e76 | 625 | } |
626 | ||
63be0a78 | 627 | /// \ingroup ErrorPageInternal |
76cdc28d | 628 | static int |
629 | errorPageId(const char *page_name) | |
630 | { | |
95dc7ff4 | 631 | for (int i = 0; i < ERR_MAX; ++i) { |
62e76326 | 632 | if (strcmp(err_type_str[i], page_name) == 0) |
633 | return i; | |
76cdc28d | 634 | } |
62e76326 | 635 | |
95dc7ff4 | 636 | for (size_t j = 0; j < ErrorDynamicPages.size(); ++j) { |
4c9eadc2 | 637 | if (strcmp(ErrorDynamicPages[j]->page_name, page_name) == 0) |
190154cf | 638 | return j + ERR_MAX; |
76cdc28d | 639 | } |
62e76326 | 640 | |
76cdc28d | 641 | return ERR_NONE; |
642 | } | |
643 | ||
e6ccf245 | 644 | err_type |
7e6eabbc | 645 | errorReservePageId(const char *page_name, const SBuf &cfgLocation) |
02922e76 | 646 | { |
76cdc28d | 647 | int id = errorPageId(page_name); |
62e76326 | 648 | |
76cdc28d | 649 | if (id == ERR_NONE) { |
7e6eabbc CT |
650 | id = ERR_MAX + ErrorDynamicPages.size(); |
651 | const auto info = new ErrorDynamicPageInfo(id, page_name, cfgLocation); | |
62e76326 | 652 | ErrorDynamicPages.push_back(info); |
76cdc28d | 653 | } |
62e76326 | 654 | |
e6ccf245 | 655 | return (err_type)id; |
02922e76 | 656 | } |
657 | ||
63be0a78 | 658 | /// \ingroup ErrorPageInternal |
64b66b76 | 659 | const char * |
c68e9c6b | 660 | errorPageName(int pageId) |
53ad48e6 | 661 | { |
f53969cc | 662 | if (pageId >= ERR_NONE && pageId < ERR_MAX) /* common case */ |
62e76326 | 663 | return err_type_str[pageId]; |
664 | ||
4ff59fc7 | 665 | if (pageId >= ERR_MAX && pageId - ERR_MAX < (ssize_t)ErrorDynamicPages.size()) |
4c9eadc2 | 666 | return ErrorDynamicPages[pageId - ERR_MAX]->page_name; |
62e76326 | 667 | |
f53969cc | 668 | return "ERR_UNKNOWN"; /* should not happen */ |
53ad48e6 | 669 | } |
670 | ||
a23223bf | 671 | ErrorState * |
7e6eabbc | 672 | ErrorState::NewForwarding(err_type type, HttpRequestPointer &request, const AccessLogEntry::Pointer &ale) |
a23223bf | 673 | { |
4c7c97f3 | 674 | const Http::StatusCode status = (request && request->flags.needValidation) ? |
e2849af8 | 675 | Http::scGatewayTimeout : Http::scServiceUnavailable; |
7e6eabbc | 676 | return new ErrorState(type, status, request.getRaw(), ale); |
a23223bf CT |
677 | } |
678 | ||
f5e17947 | 679 | ErrorState::ErrorState(err_type t) : |
f53969cc SM |
680 | type(t), |
681 | page_id(t), | |
f5e17947 CT |
682 | callback(nullptr) |
683 | { | |
684 | } | |
685 | ||
686 | ErrorState::ErrorState(err_type t, Http::StatusCode status, HttpRequest * req, const AccessLogEntry::Pointer &anAle) : | |
687 | ErrorState(t) | |
fe40a877 | 688 | { |
4c9eadc2 FC |
689 | if (page_id >= ERR_MAX && ErrorDynamicPages[page_id - ERR_MAX]->page_redirect != Http::scNone) |
690 | httpStatus = ErrorDynamicPages[page_id - ERR_MAX]->page_redirect; | |
f5e17947 CT |
691 | else |
692 | httpStatus = status; | |
693 | ||
694 | if (req) { | |
695 | request = req; | |
696 | src_addr = req->client_addr; | |
697 | } | |
698 | ||
699 | ale = anAle; | |
700 | } | |
701 | ||
702 | ErrorState::ErrorState(HttpRequest * req, HttpReply *errorReply) : | |
703 | ErrorState(ERR_RELAY_REMOTE) | |
704 | { | |
705 | Must(errorReply); | |
706 | response_ = errorReply; | |
707 | httpStatus = errorReply->sline.status(); | |
913524f0 | 708 | |
fc94fd66 | 709 | if (req) { |
b248c2a3 | 710 | request = req; |
913524f0 | 711 | src_addr = req->client_addr; |
2cc81f1f | 712 | } |
fe40a877 | 713 | } |
714 | ||
fe40a877 | 715 | void |
716 | errorAppendEntry(StoreEntry * entry, ErrorState * err) | |
717 | { | |
e4a67a80 | 718 | assert(entry->mem_obj != NULL); |
528b2c61 | 719 | assert (entry->isEmpty()); |
7ec92ef0 | 720 | debugs(4, 4, "storing " << err << " in " << *entry); |
62e76326 | 721 | |
1ea80e98 | 722 | if (entry->store_status != STORE_PENDING) { |
0b86805b | 723 | debugs(4, 2, "Skipping error page due to store_status: " << entry->store_status); |
62e76326 | 724 | /* |
725 | * If the entry is not STORE_PENDING, then no clients | |
726 | * care about it, and we don't need to generate an | |
727 | * error message | |
728 | */ | |
729 | assert(EBIT_TEST(entry->flags, ENTRY_ABORTED)); | |
6ea35247 | 730 | assert(entry->mem_obj->nclients == 0); |
913524f0 | 731 | delete err; |
62e76326 | 732 | return; |
1ea80e98 | 733 | } |
62e76326 | 734 | |
76cdc28d | 735 | if (err->page_id == TCP_RESET) { |
62e76326 | 736 | if (err->request) { |
bf8fe701 | 737 | debugs(4, 2, "RSTing this reply"); |
e857372a | 738 | err->request->flags.resetTcp = true; |
62e76326 | 739 | } |
98264874 | 740 | } |
62e76326 | 741 | |
eacfca83 | 742 | entry->storeErrorResponse(err->BuildHttpReply()); |
913524f0 | 743 | delete err; |
fe40a877 | 744 | } |
745 | ||
fe40a877 | 746 | void |
e0d28505 | 747 | errorSend(const Comm::ConnectionPointer &conn, ErrorState * err) |
fe40a877 | 748 | { |
4c7c97f3 | 749 | debugs(4, 3, conn << ", err=" << err); |
e0d28505 | 750 | assert(Comm::IsConnOpen(conn)); |
62e76326 | 751 | |
4c7c97f3 | 752 | HttpReplyPointer rep(err->BuildHttpReply()); |
62e76326 | 753 | |
ddbe383d | 754 | MemBuf *mb = rep->pack(); |
ec41b64c AJ |
755 | AsyncCall::Pointer call = commCbCall(78, 5, "errorSendComplete", |
756 | CommIoCbPtrFun(&errorSendComplete, err)); | |
b0388924 | 757 | Comm::Write(conn, mb, call); |
ddbe383d | 758 | delete mb; |
fe40a877 | 759 | } |
760 | ||
63be0a78 | 761 | /** |
762 | \ingroup ErrorPageAPI | |
fe40a877 | 763 | * |
63be0a78 | 764 | * Called by commHandleWrite() after data has been written |
765 | * to the client socket. | |
fe40a877 | 766 | * |
63be0a78 | 767 | \note If there is a callback, the callback is responsible for |
b3802bdc | 768 | * closing the FD, otherwise we do it ourselves. |
fe40a877 | 769 | */ |
770 | static void | |
ced8def3 | 771 | errorSendComplete(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag errflag, int, void *data) |
fe40a877 | 772 | { |
e6ccf245 | 773 | ErrorState *err = static_cast<ErrorState *>(data); |
e0d28505 | 774 | debugs(4, 3, HERE << conn << ", size=" << size); |
62e76326 | 775 | |
c8407295 | 776 | if (errflag != Comm::ERR_CLOSING) { |
62e76326 | 777 | if (err->callback) { |
bf8fe701 | 778 | debugs(4, 3, "errorSendComplete: callback"); |
e0d28505 | 779 | err->callback(conn->fd, err->callback_data, size); |
62e76326 | 780 | } else { |
bf8fe701 | 781 | debugs(4, 3, "errorSendComplete: comm_close"); |
80463bb4 | 782 | conn->close(); |
62e76326 | 783 | } |
fe40a877 | 784 | } |
62e76326 | 785 | |
913524f0 | 786 | delete err; |
fe40a877 | 787 | } |
788 | ||
913524f0 | 789 | ErrorState::~ErrorState() |
6eb42cae | 790 | { |
913524f0 AJ |
791 | safe_free(redirect_url); |
792 | safe_free(url); | |
793 | safe_free(request_hdrs); | |
794 | wordlistDestroy(&ftp.server_msg); | |
795 | safe_free(ftp.request); | |
796 | safe_free(ftp.reply); | |
913524f0 | 797 | safe_free(err_msg); |
5bf33a09 | 798 | #if USE_ERR_LOCALES |
913524f0 | 799 | if (err_language != Config.errorDefaultLanguage) |
5bf33a09 | 800 | #endif |
913524f0 | 801 | safe_free(err_language); |
1e74c110 | 802 | } |
8213067d | 803 | |
c70281f8 AJ |
804 | int |
805 | ErrorState::Dump(MemBuf * mb) | |
b5fb34f1 | 806 | { |
032785bf | 807 | MemBuf str; |
cc192b50 | 808 | char ntoabuf[MAX_IPSTRLEN]; |
809 | ||
2fe7eff9 | 810 | str.reset(); |
b5fb34f1 | 811 | /* email subject line */ |
4391cd15 AJ |
812 | str.appendf("CacheErrorInfo - %s", errorPageName(type)); |
813 | mb->appendf("?subject=%s", rfc1738_escape_part(str.buf)); | |
2fe7eff9 | 814 | str.reset(); |
b5fb34f1 | 815 | /* email body */ |
4391cd15 | 816 | str.appendf("CacheHost: %s\r\n", getMyHostname()); |
b5fb34f1 | 817 | /* - Err Msgs */ |
4391cd15 | 818 | str.appendf("ErrPage: %s\r\n", errorPageName(type)); |
62e76326 | 819 | |
c70281f8 | 820 | if (xerrno) { |
4391cd15 | 821 | str.appendf("Err: (%d) %s\r\n", xerrno, strerror(xerrno)); |
b5fb34f1 | 822 | } else { |
4391cd15 | 823 | str.append("Err: [none]\r\n", 13); |
b5fb34f1 | 824 | } |
2f1431ea | 825 | #if USE_AUTH |
98340a7b | 826 | if (auth_user_request.getRaw() && auth_user_request->denyMessage()) |
4391cd15 | 827 | str.appendf("Auth ErrMsg: %s\r\n", auth_user_request->denyMessage()); |
2f1431ea | 828 | #endif |
3ff65596 | 829 | if (dnsError.size() > 0) |
4391cd15 | 830 | str.appendf("DNS ErrMsg: %s\r\n", dnsError.termedBuf()); |
62e76326 | 831 | |
b5fb34f1 | 832 | /* - TimeStamp */ |
4391cd15 | 833 | str.appendf("TimeStamp: %s\r\n\r\n", mkrfc1123(squid_curtime)); |
62e76326 | 834 | |
b5fb34f1 | 835 | /* - IP stuff */ |
4391cd15 | 836 | str.appendf("ClientIP: %s\r\n", src_addr.toStr(ntoabuf,MAX_IPSTRLEN)); |
62e76326 | 837 | |
b3802bdc | 838 | if (request && request->hier.host[0] != '\0') { |
4391cd15 | 839 | str.appendf("ServerIP: %s\r\n", request->hier.host); |
b5fb34f1 | 840 | } |
62e76326 | 841 | |
4391cd15 | 842 | str.append("\r\n", 2); |
b5fb34f1 | 843 | /* - HTTP stuff */ |
4391cd15 | 844 | str.append("HTTP Request:\r\n", 15); |
51b5dcf5 AJ |
845 | if (request) { |
846 | str.appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\n", | |
4391cd15 | 847 | SQUIDSBUFPRINT(request->method.image()), |
51b5dcf5 | 848 | SQUIDSBUFPRINT(request->url.path()), |
4391cd15 AJ |
849 | AnyP::ProtocolType_str[request->http_ver.protocol], |
850 | request->http_ver.major, request->http_ver.minor); | |
10201568 | 851 | request->header.packInto(&str); |
b5fb34f1 | 852 | } |
62e76326 | 853 | |
4391cd15 | 854 | str.append("\r\n", 2); |
b5fb34f1 | 855 | /* - FTP stuff */ |
62e76326 | 856 | |
c70281f8 | 857 | if (ftp.request) { |
4391cd15 AJ |
858 | str.appendf("FTP Request: %s\r\n", ftp.request); |
859 | str.appendf("FTP Reply: %s\r\n", (ftp.reply? ftp.reply:"[none]")); | |
860 | str.append("FTP Msg: ", 9); | |
c70281f8 | 861 | wordlistCat(ftp.server_msg, &str); |
4391cd15 | 862 | str.append("\r\n", 2); |
b5fb34f1 | 863 | } |
62e76326 | 864 | |
4391cd15 AJ |
865 | str.append("\r\n", 2); |
866 | mb->appendf("&body=%s", rfc1738_escape_part(str.buf)); | |
2fe7eff9 | 867 | str.clean(); |
b5fb34f1 | 868 | return 0; |
869 | } | |
870 | ||
63be0a78 | 871 | /// \ingroup ErrorPageInternal |
2658f489 | 872 | #define CVT_BUF_SZ 512 |
fe40a877 | 873 | |
7e6eabbc CT |
874 | void |
875 | ErrorState::compileLogformatCode(Build &build) | |
876 | { | |
877 | assert(LogformatMagic.cmp(build.input, LogformatMagic.length()) == 0); | |
878 | ||
879 | try { | |
880 | const auto logformat = build.input + LogformatMagic.length(); | |
881 | ||
882 | // Logformat supports undocumented "external" encoding specifications | |
883 | // like [%>h] or "%<a". To preserve the possibility of extending | |
884 | // @Squid{} syntax to non-logformat sequences, we require logformat | |
885 | // sequences to start with '%'. This restriction does not limit | |
886 | // logformat quoting abilities. TODO: Deprecate "external" encoding? | |
887 | if (*logformat != '%') | |
888 | throw TexcHere("logformat expressions that do not start with % are not supported"); | |
889 | ||
890 | static MemBuf result; | |
891 | result.reset(); | |
892 | const auto logformatLen = Format::AssembleOne(logformat, result, ale); | |
893 | assert(logformatLen > 0); | |
894 | const auto closure = logformat + logformatLen; | |
895 | if (*closure != '}') | |
896 | throw TexcHere("Missing closing brace (})"); | |
897 | build.output.append(result.content(), result.contentSize()); | |
898 | build.input = closure + 1; | |
899 | return; | |
900 | } catch (...) { | |
901 | noteBuildError("Bad @Squid{logformat} sequence", build.input); | |
902 | } | |
903 | ||
904 | // we cannot recover reliably so stop interpreting the rest of input | |
905 | const auto remainingSize = strlen(build.input); | |
906 | build.output.append(build.input, remainingSize); | |
907 | build.input += remainingSize; | |
908 | } | |
909 | ||
910 | void | |
911 | ErrorState::compileLegacyCode(Build &build) | |
8213067d | 912 | { |
032785bf | 913 | static MemBuf mb; |
f53969cc | 914 | const char *p = NULL; /* takes priority over mb if set */ |
10270faa | 915 | int do_quote = 1; |
4ad8d23d | 916 | int no_urlescape = 0; /* if true then item is NOT to be further URL-encoded */ |
cc192b50 | 917 | char ntoabuf[MAX_IPSTRLEN]; |
eeb423fb | 918 | |
2fe7eff9 | 919 | mb.reset(); |
62e76326 | 920 | |
7e6eabbc CT |
921 | const auto &building_deny_info_url = build.building_deny_info_url; // a change reduction hack |
922 | ||
923 | const auto letter = build.input[1]; | |
924 | ||
925 | switch (letter) { | |
62e76326 | 926 | |
523f44f5 | 927 | case 'a': |
2f1431ea | 928 | #if USE_AUTH |
4c7c97f3 | 929 | if (request && request->auth_user_request) |
c70281f8 | 930 | p = request->auth_user_request->username(); |
523f44f5 | 931 | if (!p) |
2f1431ea | 932 | #endif |
523f44f5 | 933 | p = "-"; |
523f44f5 | 934 | break; |
88d1e459 | 935 | |
ea35939b A |
936 | case 'A': |
937 | // TODO: When/if we get ALE here, pass it as well | |
938 | if (const auto addr = FindListeningPortAddress(request.getRaw(), nullptr)) | |
939 | mb.appendf("%s", addr->toStr(ntoabuf, MAX_IPSTRLEN)); | |
940 | else | |
941 | p = "-"; | |
942 | break; | |
943 | ||
88d1e459 | 944 | case 'b': |
4391cd15 | 945 | mb.appendf("%u", getMyPort()); |
88d1e459 AJ |
946 | break; |
947 | ||
8f872bb6 | 948 | case 'B': |
1e98e28b | 949 | if (building_deny_info_url) break; |
851feda6 | 950 | if (request) { |
4c7c97f3 | 951 | const SBuf &tmp = Ftp::UrlWith2f(request.getRaw()); |
851feda6 AJ |
952 | mb.append(tmp.rawContent(), tmp.length()); |
953 | } else | |
954 | p = "[no URL]"; | |
62e76326 | 955 | break; |
956 | ||
42b51993 | 957 | case 'c': |
1e98e28b | 958 | if (building_deny_info_url) break; |
c70281f8 | 959 | p = errorPageName(type); |
62e76326 | 960 | break; |
961 | ||
4d16918e | 962 | case 'D': |
7e6eabbc | 963 | if (!build.allowRecursion) |
4d16918e | 964 | p = "%D"; // if recursion is not allowed, do not convert |
4d16918e | 965 | else if (detail) { |
83b053a0 CT |
966 | auto rawDetail = detail->verbose(request); |
967 | // XXX: Performance regression. c_str() reallocates | |
968 | const auto compiledDetail = compileBody(rawDetail.c_str(), false); | |
969 | mb.append(compiledDetail.rawContent(), compiledDetail.length()); | |
970 | do_quote = 0; | |
8211c314 | 971 | } |
8211c314 | 972 | if (!mb.contentSize()) |
4391cd15 | 973 | mb.append("[No Error Detail]", 17); |
4d16918e CT |
974 | break; |
975 | ||
042461c3 | 976 | case 'e': |
4391cd15 | 977 | mb.appendf("%d", xerrno); |
62e76326 | 978 | break; |
979 | ||
042461c3 | 980 | case 'E': |
c70281f8 | 981 | if (xerrno) |
4391cd15 | 982 | mb.appendf("(%d) %s", xerrno, strerror(xerrno)); |
62e76326 | 983 | else |
4391cd15 | 984 | mb.append("[No Error]", 10); |
62e76326 | 985 | break; |
986 | ||
fe40a877 | 987 | case 'f': |
1e98e28b | 988 | if (building_deny_info_url) break; |
62e76326 | 989 | /* FTP REQUEST LINE */ |
c70281f8 AJ |
990 | if (ftp.request) |
991 | p = ftp.request; | |
62e76326 | 992 | else |
993 | p = "nothing"; | |
62e76326 | 994 | break; |
995 | ||
fe40a877 | 996 | case 'F': |
1e98e28b | 997 | if (building_deny_info_url) break; |
62e76326 | 998 | /* FTP REPLY LINE */ |
c6b684dd | 999 | if (ftp.reply) |
c70281f8 | 1000 | p = ftp.reply; |
62e76326 | 1001 | else |
1002 | p = "nothing"; | |
62e76326 | 1003 | break; |
1004 | ||
7131112f | 1005 | case 'g': |
1e98e28b | 1006 | if (building_deny_info_url) break; |
a91d3505 AJ |
1007 | /* FTP SERVER RESPONSE */ |
1008 | if (ftp.listing) { | |
0477a072 AJ |
1009 | mb.append(ftp.listing->content(), ftp.listing->contentSize()); |
1010 | do_quote = 0; | |
a91d3505 AJ |
1011 | } else if (ftp.server_msg) { |
1012 | wordlistCat(ftp.server_msg, &mb); | |
0477a072 | 1013 | } |
62e76326 | 1014 | break; |
1015 | ||
03d7b07f | 1016 | case 'h': |
4391cd15 | 1017 | mb.appendf("%s", getMyHostname()); |
62e76326 | 1018 | break; |
1019 | ||
fe40a877 | 1020 | case 'H': |
c70281f8 | 1021 | if (request) { |
b3802bdc | 1022 | if (request->hier.host[0] != '\0') // if non-empty string. |
c70281f8 | 1023 | p = request->hier.host; |
beed27a2 | 1024 | else |
5c51bffb | 1025 | p = request->url.host(); |
1e98e28b | 1026 | } else if (!building_deny_info_url) |
beed27a2 | 1027 | p = "[unknown host]"; |
62e76326 | 1028 | break; |
1029 | ||
f787fb1e | 1030 | case 'i': |
4391cd15 | 1031 | mb.appendf("%s", src_addr.toStr(ntoabuf,MAX_IPSTRLEN)); |
62e76326 | 1032 | break; |
1033 | ||
f787fb1e | 1034 | case 'I': |
4c7c97f3 | 1035 | if (request && request->hier.tcpServer) |
4dd643d5 | 1036 | p = request->hier.tcpServer->remote.toStr(ntoabuf,MAX_IPSTRLEN); |
1e98e28b | 1037 | else if (!building_deny_info_url) |
62e76326 | 1038 | p = "[unknown]"; |
62e76326 | 1039 | break; |
1040 | ||
5b52cb6c | 1041 | case 'l': |
1e98e28b | 1042 | if (building_deny_info_url) break; |
0ae3294a | 1043 | mb.append(error_stylesheet.content(), error_stylesheet.contentSize()); |
5b52cb6c AJ |
1044 | do_quote = 0; |
1045 | break; | |
1046 | ||
fe40a877 | 1047 | case 'L': |
1e98e28b | 1048 | if (building_deny_info_url) break; |
62e76326 | 1049 | if (Config.errHtmlText) { |
4391cd15 | 1050 | mb.appendf("%s", Config.errHtmlText); |
62e76326 | 1051 | do_quote = 0; |
1e98e28b | 1052 | } else |
62e76326 | 1053 | p = "[not available]"; |
62e76326 | 1054 | break; |
1055 | ||
066ed5c1 | 1056 | case 'm': |
1e98e28b | 1057 | if (building_deny_info_url) break; |
2f1431ea | 1058 | #if USE_AUTH |
52ba2948 CT |
1059 | if (auth_user_request.getRaw()) |
1060 | p = auth_user_request->denyMessage("[not available]"); | |
1061 | else | |
1062 | p = "[not available]"; | |
2f1431ea AJ |
1063 | #else |
1064 | p = "-"; | |
1065 | #endif | |
62e76326 | 1066 | break; |
1067 | ||
fe40a877 | 1068 | case 'M': |
7f06a3d8 | 1069 | if (request) { |
e2849af8 A |
1070 | const SBuf &m = request->method.image(); |
1071 | mb.append(m.rawContent(), m.length()); | |
7f06a3d8 AJ |
1072 | } else if (!building_deny_info_url) |
1073 | p = "[unknown method]"; | |
62e76326 | 1074 | break; |
1075 | ||
05dbf66c | 1076 | case 'O': |
f3bd9c5e AJ |
1077 | if (!building_deny_info_url) |
1078 | do_quote = 0; | |
4a972fa2 | 1079 | case 'o': |
8c93a598 | 1080 | p = request ? request->extacl_message.termedBuf() : external_acl_message; |
1e98e28b | 1081 | if (!p && !building_deny_info_url) |
d73b593b | 1082 | p = "[not available]"; |
4a972fa2 | 1083 | break; |
1084 | ||
fe40a877 | 1085 | case 'p': |
c70281f8 | 1086 | if (request) { |
5c51bffb | 1087 | mb.appendf("%u", request->url.port()); |
1e98e28b | 1088 | } else if (!building_deny_info_url) { |
62e76326 | 1089 | p = "[unknown port]"; |
1090 | } | |
62e76326 | 1091 | break; |
1092 | ||
fe40a877 | 1093 | case 'P': |
e7aae06b | 1094 | if (request) { |
d31d59d8 AJ |
1095 | const SBuf &m = request->url.getScheme().image(); |
1096 | mb.append(m.rawContent(), m.length()); | |
1e98e28b | 1097 | } else if (!building_deny_info_url) { |
e7aae06b A |
1098 | p = "[unknown protocol]"; |
1099 | } | |
62e76326 | 1100 | break; |
1101 | ||
b5af8569 | 1102 | case 'R': |
1e98e28b | 1103 | if (building_deny_info_url) { |
1b091aec | 1104 | if (request != NULL) { |
b2b6c9f4 AJ |
1105 | const SBuf &tmp = request->url.path(); |
1106 | mb.append(tmp.rawContent(), tmp.length()); | |
1b091aec CT |
1107 | no_urlescape = 1; |
1108 | } else | |
1109 | p = "[no request]"; | |
15b02e9a AJ |
1110 | break; |
1111 | } | |
4c7c97f3 | 1112 | if (request) { |
51b5dcf5 | 1113 | mb.appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\n", |
4391cd15 | 1114 | SQUIDSBUFPRINT(request->method.image()), |
51b5dcf5 | 1115 | SQUIDSBUFPRINT(request->url.path()), |
4391cd15 AJ |
1116 | AnyP::ProtocolType_str[request->http_ver.protocol], |
1117 | request->http_ver.major, request->http_ver.minor); | |
10201568 | 1118 | request->header.packInto(&mb, true); //hide authorization data |
c70281f8 AJ |
1119 | } else if (request_hdrs) { |
1120 | p = request_hdrs; | |
62e76326 | 1121 | } else { |
1122 | p = "[no request]"; | |
1123 | } | |
62e76326 | 1124 | break; |
1125 | ||
1d803566 | 1126 | case 's': |
15b02e9a | 1127 | /* for backward compat we make %s show the full URL. Drop this in some future release. */ |
1e98e28b | 1128 | if (building_deny_info_url) { |
851feda6 AJ |
1129 | if (request) { |
1130 | const SBuf &tmp = request->effectiveRequestUri(); | |
1131 | mb.append(tmp.rawContent(), tmp.length()); | |
1132 | } else | |
1133 | p = url; | |
fa84c01d | 1134 | debugs(0, DBG_CRITICAL, "WARNING: deny_info now accepts coded tags. Use %u to get the full URL instead of %s"); |
05320519 | 1135 | } else |
15b02e9a | 1136 | p = visible_appname_string; |
62e76326 | 1137 | break; |
1138 | ||
1d803566 | 1139 | case 'S': |
1e98e28b | 1140 | if (building_deny_info_url) { |
4ad8d23d | 1141 | p = visible_appname_string; |
e7aae06b A |
1142 | break; |
1143 | } | |
62e76326 | 1144 | /* signature may contain %-escapes, recursion */ |
c70281f8 AJ |
1145 | if (page_id != ERR_SQUID_SIGNATURE) { |
1146 | const int saved_id = page_id; | |
1147 | page_id = ERR_SQUID_SIGNATURE; | |
7e6eabbc CT |
1148 | const auto signature = buildBody(); |
1149 | mb.append(signature.rawContent(), signature.length()); | |
c70281f8 | 1150 | page_id = saved_id; |
62e76326 | 1151 | do_quote = 0; |
1152 | } else { | |
1153 | /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */ | |
1154 | p = "[%S]"; | |
1155 | } | |
62e76326 | 1156 | break; |
1157 | ||
fe40a877 | 1158 | case 't': |
4391cd15 | 1159 | mb.appendf("%s", Time::FormatHttpd(squid_curtime)); |
62e76326 | 1160 | break; |
1161 | ||
f8291f8f | 1162 | case 'T': |
4391cd15 | 1163 | mb.appendf("%s", mkrfc1123(squid_curtime)); |
62e76326 | 1164 | break; |
1165 | ||
fe40a877 | 1166 | case 'U': |
851feda6 | 1167 | /* Using the fake-https version of absolute-URI so error pages see https:// */ |
b3802bdc | 1168 | /* even when the url-path cannot be shown as more than '*' */ |
e7aae06b | 1169 | if (request) |
4c7c97f3 | 1170 | p = urlCanonicalFakeHttps(request.getRaw()); |
e7aae06b A |
1171 | else if (url) |
1172 | p = url; | |
1e98e28b | 1173 | else if (!building_deny_info_url) |
e7aae06b | 1174 | p = "[no URL]"; |
62e76326 | 1175 | break; |
1176 | ||
76cdc28d | 1177 | case 'u': |
851feda6 AJ |
1178 | if (request) { |
1179 | const SBuf &tmp = request->effectiveRequestUri(); | |
1180 | mb.append(tmp.rawContent(), tmp.length()); | |
1181 | } else if (url) | |
1e98e28b AJ |
1182 | p = url; |
1183 | else if (!building_deny_info_url) | |
1184 | p = "[no URL]"; | |
62e76326 | 1185 | break; |
1186 | ||
fe40a877 | 1187 | case 'w': |
62e76326 | 1188 | if (Config.adminEmail) |
4391cd15 | 1189 | mb.appendf("%s", Config.adminEmail); |
1e98e28b | 1190 | else if (!building_deny_info_url) |
62e76326 | 1191 | p = "[unknown]"; |
62e76326 | 1192 | break; |
1193 | ||
b5fb34f1 | 1194 | case 'W': |
1e98e28b | 1195 | if (building_deny_info_url) break; |
62e76326 | 1196 | if (Config.adminEmail && Config.onoff.emailErrData) |
c70281f8 | 1197 | Dump(&mb); |
4ad8d23d | 1198 | no_urlescape = 1; |
62e76326 | 1199 | break; |
1200 | ||
e4a8468d | 1201 | case 'x': |
83b053a0 CT |
1202 | if (detail) { |
1203 | const auto brief = detail->brief(); | |
1204 | mb.append(brief.rawContent(), brief.length()); | |
1205 | } else if (!building_deny_info_url) { | |
1206 | p = "[Unknown Error Code]"; | |
1207 | } | |
e4a8468d CT |
1208 | break; |
1209 | ||
fe40a877 | 1210 | case 'z': |
1e98e28b | 1211 | if (building_deny_info_url) break; |
3ff65596 AR |
1212 | if (dnsError.size() > 0) |
1213 | p = dnsError.termedBuf(); | |
0477a072 AJ |
1214 | else if (ftp.cwd_msg) |
1215 | p = ftp.cwd_msg; | |
62e76326 | 1216 | else |
1217 | p = "[unknown]"; | |
62e76326 | 1218 | break; |
1219 | ||
43ae1d95 | 1220 | case 'Z': |
1e98e28b | 1221 | if (building_deny_info_url) break; |
c70281f8 AJ |
1222 | if (err_msg) |
1223 | p = err_msg; | |
43ae1d95 | 1224 | else |
1225 | p = "[unknown]"; | |
43ae1d95 | 1226 | break; |
1227 | ||
e347f8e5 | 1228 | case '%': |
62e76326 | 1229 | p = "%"; |
62e76326 | 1230 | break; |
1231 | ||
9b312a19 | 1232 | default: |
7e6eabbc CT |
1233 | if (building_deny_info_url) |
1234 | bypassBuildErrorXXX("Unsupported deny_info %code", build.input); | |
1235 | else if (letter != ';') | |
1236 | bypassBuildErrorXXX("Unsupported error page %code", build.input); | |
1237 | // else too many "font-size: 100%;" template errors to report | |
1238 | ||
1239 | mb.append(build.input, 2); | |
cbba2ba2 | 1240 | do_quote = 0; |
62e76326 | 1241 | break; |
9b312a19 | 1242 | } |
62e76326 | 1243 | |
137ee196 | 1244 | if (!p) |
f53969cc | 1245 | p = mb.buf; /* do not use mb after this assignment! */ |
62e76326 | 1246 | |
137ee196 | 1247 | assert(p); |
62e76326 | 1248 | |
7e6eabbc | 1249 | debugs(4, 3, "%" << letter << " --> '" << p << "'" ); |
62e76326 | 1250 | |
10270faa | 1251 | if (do_quote) |
62e76326 | 1252 | p = html_quote(p); |
1253 | ||
1e98e28b | 1254 | if (building_deny_info_url && !no_urlescape) |
15b02e9a AJ |
1255 | p = rfc1738_escape_part(p); |
1256 | ||
7e6eabbc CT |
1257 | // TODO: Optimize by replacing mb with direct build.output usage. |
1258 | build.output.append(p, strlen(p)); | |
1259 | build.input += 2; | |
8213067d | 1260 | } |
e381a13d | 1261 | |
15b02e9a | 1262 | void |
7e6eabbc | 1263 | ErrorState::validate() |
15b02e9a | 1264 | { |
7e6eabbc CT |
1265 | if (const auto urlTemplate = ErrorPage::IsDenyInfoUri(page_id)) { |
1266 | (void)compile(urlTemplate, true, true); | |
1267 | } else { | |
1268 | assert(page_id > ERR_NONE); | |
1269 | assert(page_id < error_page_count); | |
1270 | (void)compileBody(error_text[page_id], true); | |
15b02e9a | 1271 | } |
15b02e9a AJ |
1272 | } |
1273 | ||
cb69b4c7 | 1274 | HttpReply * |
c70281f8 | 1275 | ErrorState::BuildHttpReply() |
cb69b4c7 | 1276 | { |
f5e17947 CT |
1277 | if (response_) |
1278 | return response_.getRaw(); | |
1279 | ||
06a5ae20 | 1280 | HttpReply *rep = new HttpReply; |
c70281f8 | 1281 | const char *name = errorPageName(page_id); |
cb69b4c7 | 1282 | /* no LMT for error pages; error pages expire immediately */ |
62e76326 | 1283 | |
7e6eabbc | 1284 | if (const auto urlTemplate = ErrorPage::IsDenyInfoUri(page_id)) { |
62e76326 | 1285 | /* Redirection */ |
f11c8e2f | 1286 | Http::StatusCode status = Http::scFound; |
aed9a15b AJ |
1287 | // Use configured 3xx reply status if set. |
1288 | if (name[0] == '3') | |
1289 | status = httpStatus; | |
1290 | else { | |
1291 | // Use 307 for HTTP/1.1 non-GET/HEAD requests. | |
4c7c97f3 | 1292 | if (request && request->method != Http::METHOD_GET && request->method != Http::METHOD_HEAD && request->http_ver >= Http::ProtocolVersion(1,1)) |
955394ce | 1293 | status = Http::scTemporaryRedirect; |
aed9a15b AJ |
1294 | } |
1295 | ||
ec80c740 | 1296 | rep->setHeaders(status, NULL, "text/html;charset=utf-8", 0, 0, -1); |
c44950c4 | 1297 | |
c70281f8 | 1298 | if (request) { |
7e6eabbc CT |
1299 | auto location = compile(urlTemplate, true, true); |
1300 | rep->header.putStr(Http::HdrType::LOCATION, location.c_str()); | |
c44950c4 | 1301 | } |
1302 | ||
789217a2 | 1303 | httpHeaderPutStrf(&rep->header, Http::HdrType::X_SQUID_ERROR, "%d %s", httpStatus, "Access Denied"); |
76cdc28d | 1304 | } else { |
7e6eabbc CT |
1305 | const auto body = buildBody(); |
1306 | rep->setHeaders(httpStatus, NULL, "text/html;charset=utf-8", body.length(), 0, -1); | |
62e76326 | 1307 | /* |
1308 | * include some information for downstream caches. Implicit | |
1309 | * replaceable content. This isn't quite sufficient. xerrno is not | |
1310 | * necessarily meaningful to another system, so we really should | |
1311 | * expand it. Additionally, we should identify ourselves. Someone | |
1312 | * might want to know. Someone _will_ want to know OTOH, the first | |
1313 | * X-CACHE-MISS entry should tell us who. | |
1314 | */ | |
789217a2 | 1315 | httpHeaderPutStrf(&rep->header, Http::HdrType::X_SQUID_ERROR, "%s %d", name, xerrno); |
ccb24616 | 1316 | |
5bf33a09 AJ |
1317 | #if USE_ERR_LOCALES |
1318 | /* | |
1319 | * If error page auto-negotiate is enabled in any way, send the Vary. | |
1320 | * RFC 2616 section 13.6 and 14.44 says MAY and SHOULD do this. | |
1321 | * We have even better reasons though: | |
1322 | * see http://wiki.squid-cache.org/KnowledgeBase/VaryNotCaching | |
1323 | */ | |
9e008dda | 1324 | if (!Config.errorDirectory) { |
5bf33a09 | 1325 | /* We 'negotiated' this ONLY from the Accept-Language. */ |
789217a2 FC |
1326 | rep->header.delById(Http::HdrType::VARY); |
1327 | rep->header.putStr(Http::HdrType::VARY, "Accept-Language"); | |
5bf33a09 AJ |
1328 | } |
1329 | ||
1330 | /* add the Content-Language header according to RFC section 14.12 */ | |
9e008dda | 1331 | if (err_language) { |
789217a2 | 1332 | rep->header.putStr(Http::HdrType::CONTENT_LANGUAGE, err_language); |
9e008dda | 1333 | } else |
5bf33a09 AJ |
1334 | #endif /* USE_ERROR_LOCALES */ |
1335 | { | |
ccb24616 | 1336 | /* default templates are in English */ |
5bf33a09 | 1337 | /* language is known unless error_directory override used */ |
9e008dda | 1338 | if (!Config.errorDirectory) |
789217a2 | 1339 | rep->header.putStr(Http::HdrType::CONTENT_LANGUAGE, "en"); |
ccb24616 AJ |
1340 | } |
1341 | ||
7e6eabbc | 1342 | rep->body.set(body); |
76cdc28d | 1343 | } |
62e76326 | 1344 | |
7a957a93 AR |
1345 | // Make sure error codes get back to the client side for logging and |
1346 | // error tracking. | |
129fe2a1 | 1347 | if (request) { |
129fe2a1 | 1348 | if (detail) |
83b053a0 | 1349 | request->detailError(type, detail); |
129fe2a1 | 1350 | else |
83b053a0 | 1351 | request->detailError(type, SysErrorDetail::NewIfAny(xerrno)); |
129fe2a1 CT |
1352 | } |
1353 | ||
cb69b4c7 | 1354 | return rep; |
1355 | } | |
1356 | ||
7e6eabbc CT |
1357 | SBuf |
1358 | ErrorState::buildBody() | |
cb69b4c7 | 1359 | { |
c70281f8 | 1360 | assert(page_id > ERR_NONE && page_id < error_page_count); |
43000484 AJ |
1361 | |
1362 | #if USE_ERR_LOCALES | |
43000484 | 1363 | /** error_directory option in squid.conf overrides translations. |
30b78ef1 | 1364 | * Custom errors are always found either in error_directory or the templates directory. |
43000484 AJ |
1365 | * Otherwise locate the Accept-Language header |
1366 | */ | |
02259ff8 CT |
1367 | if (!Config.errorDirectory && page_id < ERR_MAX) { |
1368 | if (err_language && err_language != Config.errorDefaultLanguage) | |
1369 | safe_free(err_language); | |
1370 | ||
7e6eabbc CT |
1371 | ErrorPageFile localeTmpl(err_type_str[page_id], static_cast<err_type>(page_id)); |
1372 | if (localeTmpl.loadFor(request.getRaw())) { | |
1373 | inputLocation = localeTmpl.filename; | |
1374 | assert(localeTmpl.language()); | |
1375 | err_language = xstrdup(localeTmpl.language()); | |
1376 | return compileBody(localeTmpl.text(), true); | |
43000484 AJ |
1377 | } |
1378 | } | |
1379 | #endif /* USE_ERR_LOCALES */ | |
1380 | ||
1381 | /** \par | |
1382 | * If client-specific error templates are not enabled or available. | |
1383 | * fall back to the old style squid.conf settings. | |
1384 | */ | |
5bf33a09 | 1385 | #if USE_ERR_LOCALES |
7e6eabbc CT |
1386 | if (!Config.errorDirectory) |
1387 | err_language = Config.errorDefaultLanguage; | |
5bf33a09 | 1388 | #endif |
7ec92ef0 | 1389 | debugs(4, 2, "No existing error page language negotiated for " << this << ". Using default error file."); |
7e6eabbc CT |
1390 | return compileBody(error_text[page_id], true); |
1391 | } | |
43000484 | 1392 | |
7e6eabbc CT |
1393 | SBuf |
1394 | ErrorState::compileBody(const char *input, bool allowRecursion) | |
1395 | { | |
1396 | return compile(input, false, allowRecursion); | |
4d16918e CT |
1397 | } |
1398 | ||
7e6eabbc CT |
1399 | SBuf |
1400 | ErrorState::compile(const char *input, bool building_deny_info_url, bool allowRecursion) | |
4d16918e | 1401 | { |
7e6eabbc CT |
1402 | assert(input); |
1403 | ||
1404 | Build build; | |
1405 | build.building_deny_info_url = building_deny_info_url; | |
1406 | build.allowRecursion = allowRecursion; | |
1407 | build.input = input; | |
1408 | ||
1409 | auto blockStart = build.input; | |
1410 | while (const auto letter = *build.input) { | |
1411 | if (letter == '%') { | |
1412 | build.output.append(blockStart, build.input - blockStart); | |
1413 | compileLegacyCode(build); | |
1414 | blockStart = build.input; | |
1415 | } | |
1416 | else if (letter == '@' && LogformatMagic.cmp(build.input, LogformatMagic.length()) == 0) { | |
1417 | build.output.append(blockStart, build.input - blockStart); | |
1418 | compileLogformatCode(build); | |
1419 | blockStart = build.input; | |
1420 | } else { | |
1421 | ++build.input; | |
1422 | } | |
1423 | } | |
1424 | build.output.append(blockStart, build.input - blockStart); | |
1425 | return build.output; | |
1426 | } | |
1427 | ||
1428 | /// react to a compile() error | |
1429 | /// \param msg description of what went wrong | |
1430 | /// \param near approximate start of the problematic input | |
1431 | /// \param forceBypass whether detection of this error was introduced late, | |
1432 | /// after old configurations containing this error could have been | |
1433 | /// successfully validated and deployed (i.e. the admin may not be | |
1434 | /// able to fix this newly detected but old problem quickly) | |
1435 | void | |
1436 | ErrorState::noteBuildError_(const char *msg, const char *near, const bool forceBypass) | |
1437 | { | |
1438 | using ErrorPage::BuildErrorPrinter; | |
1439 | const auto runtime = !starting_up; | |
1440 | if (runtime || forceBypass) { | |
1441 | // swallow this problem because the admin may not be (and/or the page | |
1442 | // building code is not) ready to handle throwing consequences | |
1443 | ||
1444 | static unsigned int seenErrors = 0; | |
1445 | ++seenErrors; | |
1446 | ||
1447 | const auto debugLevel = | |
1448 | (seenErrors > 100) ? DBG_DATA: | |
1449 | (starting_up || reconfiguring) ? DBG_CRITICAL: | |
1450 | 3; // most other errors have been reported as configuration errors | |
1451 | ||
1452 | // Error fatality depends on the error context: Reconfiguration errors | |
1453 | // are, like startup ones, DBG_CRITICAL but will never become FATAL. | |
1454 | if (starting_up && seenErrors <= 10) | |
1455 | debugs(4, debugLevel, "WARNING: The following configuration error will be fatal in future Squid versions"); | |
1456 | ||
1457 | debugs(4, debugLevel, "ERROR: " << BuildErrorPrinter(inputLocation, page_id, msg, near)); | |
1458 | } else { | |
1459 | throw TexcHere(ToSBuf(BuildErrorPrinter(inputLocation, page_id, msg, near))); | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | /* ErrorPage::BuildErrorPrinter */ | |
1464 | ||
1465 | std::ostream & | |
1466 | ErrorPage::BuildErrorPrinter::printLocation(std::ostream &os) const { | |
1467 | if (!inputLocation.isEmpty()) | |
1468 | return os << inputLocation; | |
1469 | ||
1470 | if (page_id < ERR_NONE || page_id >= error_page_count) | |
1471 | return os << "[error page " << page_id << "]"; // should not happen | |
1472 | ||
1473 | if (page_id < ERR_MAX) | |
1474 | return os << err_type_str[page_id]; | |
1475 | ||
1476 | return os << "deny_info " << ErrorDynamicPages.at(page_id - ERR_MAX)->page_name; | |
1477 | } | |
1478 | ||
1479 | std::ostream & | |
1480 | ErrorPage::BuildErrorPrinter::print(std::ostream &os) const { | |
1481 | printLocation(os) << ": " << msg << " near "; | |
1482 | ||
1483 | // TODO: Add support for prefix printing to Raw | |
1484 | const size_t maxContextLength = 15; // plus "..." | |
1485 | if (strlen(near) > maxContextLength) { | |
1486 | os.write(near, maxContextLength); | |
1487 | os << "..."; | |
1488 | } else { | |
1489 | os << near; | |
cb69b4c7 | 1490 | } |
62e76326 | 1491 | |
7e6eabbc CT |
1492 | // XXX: We should not be converting (inner) exception to text if we are |
1493 | // going to throw again. See "add arbitrary (re)thrower-supplied details" | |
1494 | // TODO in TextException.h for a long-term in-catcher solution. | |
1495 | if (std::current_exception()) | |
1496 | os << "\n additional info: " << CurrentException; | |
62e76326 | 1497 | |
7e6eabbc CT |
1498 | return os; |
1499 | } | |
857e4512 | 1500 | |
7e6eabbc CT |
1501 | /// add error page template to the global index |
1502 | static void | |
1503 | ErrorPage::ImportStaticErrorText(const int page_id, const char *text, const SBuf &inputLocation) | |
1504 | { | |
1505 | assert(!error_text[page_id]); | |
1506 | error_text[page_id] = xstrdup(text); | |
1507 | ValidateStaticError(page_id, inputLocation); | |
1508 | } | |
62e76326 | 1509 | |
7e6eabbc CT |
1510 | /// validate static error page |
1511 | static void | |
1512 | ErrorPage::ValidateStaticError(const int page_id, const SBuf &inputLocation) | |
1513 | { | |
1514 | // Supplying nil ALE pointer limits validation to logformat %code | |
1515 | // recognition by Format::Token::parse(). This is probably desirable | |
1516 | // because actual %code assembly is slow and should not affect validation | |
1517 | // when our ALE cannot have any real data (this code is not associated | |
1518 | // with any real transaction). | |
1519 | ErrorState anErr(err_type(page_id), Http::scNone, nullptr, nullptr); | |
1520 | anErr.inputLocation = inputLocation; | |
1521 | anErr.validate(); | |
cb69b4c7 | 1522 | } |
f53969cc | 1523 | |
7ec92ef0 JR |
1524 | std::ostream & |
1525 | operator <<(std::ostream &os, const ErrorState *err) | |
1526 | { | |
1527 | if (err) | |
1528 | os << errorPageName(err->page_id); | |
1529 | else | |
1530 | os << "[none]"; | |
1531 | return os; | |
1532 | } | |
1533 |