]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
bde978a6 | 2 | * Copyright (C) 1996-2015 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" |
8a01b99e | 12 | #include "cache_cf.h" |
92ae4c86 | 13 | #include "clients/forward.h" |
e0d28505 | 14 | #include "comm/Connection.h" |
ec41b64c | 15 | #include "comm/Write.h" |
438b04d4 | 16 | #include "disk.h" |
582c2af2 | 17 | #include "err_detail_type.h" |
aa839030 | 18 | #include "errorpage.h" |
602d9612 | 19 | #include "fde.h" |
25f98340 | 20 | #include "html_quote.h" |
a5bac1d2 | 21 | #include "HttpHeaderTools.h" |
528b2c61 | 22 | #include "HttpReply.h" |
23 | #include "HttpRequest.h" | |
0eb49b6d | 24 | #include "MemBuf.h" |
602d9612 | 25 | #include "MemObject.h" |
1fa9b1a7 | 26 | #include "rfc1738.h" |
4d5904f7 | 27 | #include "SquidConfig.h" |
602d9612 A |
28 | #include "Store.h" |
29 | #include "tools.h" | |
b1bd952a | 30 | #include "URL.h" |
d295d770 | 31 | #include "wordlist.h" |
4e540555 FC |
32 | #if USE_AUTH |
33 | #include "auth/UserRequest.h" | |
34 | #endif | |
35 | #include "SquidTime.h" | |
cb4f4424 | 36 | #if USE_OPENSSL |
4e540555 FC |
37 | #include "ssl/ErrorDetailManager.h" |
38 | #endif | |
02922e76 | 39 | |
63be0a78 | 40 | /** |
41 | \defgroup ErrorPageInternal Error Page Internals | |
42 | \ingroup ErrorPageAPI | |
43 | * | |
44 | \section Abstract Abstract: | |
45 | * These routines are used to generate error messages to be | |
46 | * sent to clients. The error type is used to select between | |
47 | * the various message formats. (formats are stored in the | |
48 | * Config.errorDirectory) | |
49 | */ | |
50 | ||
605f2c3e | 51 | #if !defined(DEFAULT_SQUID_ERROR_DIR) |
43000484 AJ |
52 | /** Where to look for errors if config path fails. |
53 | \note Please use ./configure --datadir=/path instead of patching | |
54 | */ | |
55 | #define DEFAULT_SQUID_ERROR_DIR DEFAULT_SQUID_DATA_DIR"/errors" | |
56 | #endif | |
57 | ||
63be0a78 | 58 | /// \ingroup ErrorPageInternal |
aa839030 | 59 | CBDATA_CLASS_INIT(ErrorState); |
60 | ||
02922e76 | 61 | /* local types */ |
62 | ||
63be0a78 | 63 | /// \ingroup ErrorPageInternal |
9e008dda | 64 | typedef struct { |
02922e76 | 65 | int id; |
66 | char *page_name; | |
955394ce | 67 | Http::StatusCode page_redirect; |
2fadd50d | 68 | } ErrorDynamicPageInfo; |
02922e76 | 69 | |
70 | /* local constant and vars */ | |
71 | ||
63be0a78 | 72 | /** |
cfdb8f88 | 73 | \ingroup ErrorPageInternal |
63be0a78 | 74 | * |
75 | \note hard coded error messages are not appended with %S | |
76 | * automagically to give you more control on the format | |
1d803566 | 77 | */ |
9e008dda | 78 | static const struct { |
f53969cc | 79 | int type; /* and page_id */ |
2ac76861 | 80 | const char *text; |
62e76326 | 81 | } |
82 | ||
83 | error_hard_text[] = { | |
84 | ||
9e008dda AJ |
85 | { |
86 | ERR_SQUID_SIGNATURE, | |
87 | "\n<br>\n" | |
88 | "<hr>\n" | |
89 | "<div id=\"footer\">\n" | |
90 | "Generated %T by %h (%s)\n" | |
91 | "</div>\n" | |
92 | "</body></html>\n" | |
93 | }, | |
94 | { | |
95 | TCP_RESET, | |
96 | "reset" | |
97 | } | |
98 | }; | |
02922e76 | 99 | |
63be0a78 | 100 | /// \ingroup ErrorPageInternal |
c8ea3cc0 | 101 | static std::vector<ErrorDynamicPageInfo *> ErrorDynamicPages; |
02922e76 | 102 | |
103 | /* local prototypes */ | |
104 | ||
63be0a78 | 105 | /// \ingroup ErrorPageInternal |
2ac76861 | 106 | static const int error_hard_text_count = sizeof(error_hard_text) / sizeof(*error_hard_text); |
63be0a78 | 107 | |
108 | /// \ingroup ErrorPageInternal | |
02922e76 | 109 | static char **error_text = NULL; |
63be0a78 | 110 | |
111 | /// \ingroup ErrorPageInternal | |
02922e76 | 112 | static int error_page_count = 0; |
9b312a19 | 113 | |
5b52cb6c | 114 | /// \ingroup ErrorPageInternal |
0ae3294a | 115 | static MemBuf error_stylesheet; |
5b52cb6c | 116 | |
1d803566 | 117 | static const char *errorFindHardText(err_type type); |
c68e9c6b | 118 | static ErrorDynamicPageInfo *errorDynamicPageInfoCreate(int id, const char *page_name); |
119 | static void errorDynamicPageInfoDestroy(ErrorDynamicPageInfo * info); | |
2b663917 | 120 | static IOCB errorSendComplete; |
1e74c110 | 121 | |
02259ff8 CT |
122 | /// \ingroup ErrorPageInternal |
123 | /// manages an error page template | |
dc49061a A |
124 | class ErrorPageFile: public TemplateFile |
125 | { | |
02259ff8 | 126 | public: |
ced8def3 | 127 | ErrorPageFile(const char *name, const err_type code) : TemplateFile(name,code) {textBuf.init();} |
02259ff8 CT |
128 | |
129 | /// The template text data read from disk | |
130 | const char *text() { return textBuf.content(); } | |
131 | ||
132 | private: | |
133 | /// stores the data read from disk to a local buffer | |
ced8def3 | 134 | virtual bool parse(const char *buf, int len, bool) { |
02259ff8 CT |
135 | if (len) |
136 | textBuf.append(buf, len); | |
137 | return true; | |
138 | } | |
139 | ||
140 | MemBuf textBuf; ///< A buffer to store the error page | |
141 | }; | |
e6ccf245 | 142 | |
63be0a78 | 143 | /// \ingroup ErrorPageInternal |
e6ccf245 | 144 | err_type &operator++ (err_type &anErr) |
145 | { | |
1f1ae50a | 146 | int tmp = (int)anErr; |
147 | anErr = (err_type)(++tmp); | |
e6ccf245 | 148 | return anErr; |
149 | } | |
4ff59fc7 | 150 | |
63be0a78 | 151 | /// \ingroup ErrorPageInternal |
62e76326 | 152 | int operator - (err_type const &anErr, err_type const &anErr2) |
153 | { | |
4ff59fc7 | 154 | return (int)anErr - (int)anErr2; |
155 | } | |
156 | ||
b8d8561b | 157 | void |
0673c0ba | 158 | errorInitialize(void) |
fa966b74 | 159 | { |
728da2ee | 160 | err_type i; |
1d803566 | 161 | const char *text; |
4ff59fc7 | 162 | error_page_count = ERR_MAX + ErrorDynamicPages.size(); |
e6ccf245 | 163 | error_text = static_cast<char **>(xcalloc(error_page_count, sizeof(char *))); |
62e76326 | 164 | |
e6ccf245 | 165 | for (i = ERR_NONE, ++i; i < error_page_count; ++i) { |
62e76326 | 166 | safe_free(error_text[i]); |
62e76326 | 167 | |
43000484 AJ |
168 | if ((text = errorFindHardText(i))) { |
169 | /**\par | |
170 | * Index any hard-coded error text into defaults. | |
171 | */ | |
62e76326 | 172 | error_text[i] = xstrdup(text); |
43000484 AJ |
173 | |
174 | } else if (i < ERR_MAX) { | |
175 | /**\par | |
176 | * Index precompiled fixed template files from one of two sources: | |
177 | * (a) default language translation directory (error_default_language) | |
178 | * (b) admin specified custom directory (error_directory) | |
179 | */ | |
8ff2520a | 180 | ErrorPageFile errTmpl(err_type_str[i], i); |
02259ff8 | 181 | error_text[i] = errTmpl.loadDefault() ? xstrdup(errTmpl.text()) : NULL; |
62e76326 | 182 | } else { |
43000484 AJ |
183 | /** \par |
184 | * Index any unknown file names used by deny_info. | |
185 | */ | |
e6334d92 | 186 | ErrorDynamicPageInfo *info = ErrorDynamicPages.at(i - ERR_MAX); |
62e76326 | 187 | assert(info && info->id == i && info->page_name); |
188 | ||
aed9a15b | 189 | const char *pg = info->page_name; |
955394ce | 190 | if (info->page_redirect != Http::scNone) |
aed9a15b AJ |
191 | pg = info->page_name +4; |
192 | ||
193 | if (strchr(pg, ':') == NULL) { | |
43000484 | 194 | /** But only if they are not redirection URL. */ |
8ff2520a | 195 | ErrorPageFile errTmpl(pg, ERR_MAX); |
02259ff8 | 196 | error_text[i] = errTmpl.loadDefault() ? xstrdup(errTmpl.text()) : NULL; |
62e76326 | 197 | } |
198 | } | |
1d803566 | 199 | } |
5b52cb6c | 200 | |
0ae3294a AJ |
201 | error_stylesheet.reset(); |
202 | ||
5b52cb6c | 203 | // look for and load stylesheet into global MemBuf for it. |
9e008dda | 204 | if (Config.errorStylesheet) { |
8ff2520a | 205 | ErrorPageFile tmpl("StylesSheet", ERR_MAX); |
02259ff8 | 206 | tmpl.loadFromFile(Config.errorStylesheet); |
4391cd15 | 207 | error_stylesheet.appendf("%s",tmpl.text()); |
5b52cb6c | 208 | } |
02259ff8 | 209 | |
cb4f4424 | 210 | #if USE_OPENSSL |
02259ff8 | 211 | Ssl::errorDetailInitialize(); |
c259af96 | 212 | #endif |
1d803566 | 213 | } |
214 | ||
c68e9c6b | 215 | void |
216 | errorClean(void) | |
217 | { | |
218 | if (error_text) { | |
62e76326 | 219 | int i; |
220 | ||
95dc7ff4 | 221 | for (i = ERR_NONE + 1; i < error_page_count; ++i) |
62e76326 | 222 | safe_free(error_text[i]); |
223 | ||
224 | safe_free(error_text); | |
c68e9c6b | 225 | } |
62e76326 | 226 | |
385acf91 | 227 | while (!ErrorDynamicPages.empty()) { |
38c22baf FC |
228 | errorDynamicPageInfoDestroy(ErrorDynamicPages.back()); |
229 | ErrorDynamicPages.pop_back(); | |
230 | } | |
62e76326 | 231 | |
c68e9c6b | 232 | error_page_count = 0; |
02259ff8 | 233 | |
cb4f4424 | 234 | #if USE_OPENSSL |
02259ff8 | 235 | Ssl::errorDetailClean(); |
c259af96 | 236 | #endif |
c68e9c6b | 237 | } |
238 | ||
63be0a78 | 239 | /// \ingroup ErrorPageInternal |
1d803566 | 240 | static const char * |
241 | errorFindHardText(err_type type) | |
242 | { | |
243 | int i; | |
62e76326 | 244 | |
95dc7ff4 | 245 | for (i = 0; i < error_hard_text_count; ++i) |
62e76326 | 246 | if (error_hard_text[i].type == type) |
247 | return error_hard_text[i].text; | |
248 | ||
1d803566 | 249 | return NULL; |
250 | } | |
251 | ||
8ff2520a | 252 | TemplateFile::TemplateFile(const char *name, const err_type code): silent(false), wasLoaded(false), templateName(name), templateCode(code) |
1d803566 | 253 | { |
02259ff8 CT |
254 | assert(name); |
255 | } | |
256 | ||
257 | bool | |
258 | TemplateFile::loadDefault() | |
259 | { | |
260 | if (loaded()) // already loaded? | |
261 | return true; | |
43000484 AJ |
262 | |
263 | /** test error_directory configured location */ | |
02259ff8 CT |
264 | if (Config.errorDirectory) { |
265 | char path[MAXPATHLEN]; | |
266 | snprintf(path, sizeof(path), "%s/%s", Config.errorDirectory, templateName.termedBuf()); | |
267 | loadFromFile(path); | |
268 | } | |
43000484 AJ |
269 | |
270 | #if USE_ERR_LOCALES | |
271 | /** test error_default_language location */ | |
02259ff8 CT |
272 | if (!loaded() && Config.errorDefaultLanguage) { |
273 | if (!tryLoadTemplate(Config.errorDefaultLanguage)) { | |
8ff2520a | 274 | debugs(1, (templateCode < TCP_RESET ? DBG_CRITICAL : 3), "Unable to load default error language files. Reset to backups."); |
43000484 AJ |
275 | } |
276 | } | |
277 | #endif | |
62e76326 | 278 | |
43000484 | 279 | /* test default location if failed (templates == English translation base templates) */ |
02259ff8 CT |
280 | if (!loaded()) { |
281 | tryLoadTemplate("templates"); | |
5bf33a09 | 282 | } |
62e76326 | 283 | |
1d803566 | 284 | /* giving up if failed */ |
b073fc4b | 285 | if (!loaded()) { |
8ff2520a | 286 | debugs(1, (templateCode < TCP_RESET ? DBG_CRITICAL : 3), "WARNING: failed to find or read error text file " << templateName); |
b073fc4b AJ |
287 | parse("Internal Error: Missing Template ", 33, '\0'); |
288 | parse(templateName.termedBuf(), templateName.size(), '\0'); | |
289 | } | |
62e76326 | 290 | |
02259ff8 | 291 | return true; |
1d803566 | 292 | } |
293 | ||
02259ff8 CT |
294 | bool |
295 | TemplateFile::tryLoadTemplate(const char *lang) | |
1d803566 | 296 | { |
02259ff8 CT |
297 | assert(lang); |
298 | ||
9b312a19 | 299 | char path[MAXPATHLEN]; |
02259ff8 CT |
300 | /* TODO: prep the directory path string to prevent snprintf ... */ |
301 | snprintf(path, sizeof(path), "%s/%s/%s", | |
302 | DEFAULT_SQUID_ERROR_DIR, lang, templateName.termedBuf()); | |
303 | path[MAXPATHLEN-1] = '\0'; | |
304 | ||
305 | if (loadFromFile(path)) | |
306 | return true; | |
307 | ||
308 | #if HAVE_GLOB | |
309 | if ( strlen(lang) == 2) { | |
310 | /* TODO glob the error directory for sub-dirs matching: <tag> '-*' */ | |
311 | /* use first result. */ | |
312 | debugs(4,2, HERE << "wildcard fallback errors not coded yet."); | |
313 | } | |
314 | #endif | |
315 | ||
316 | return false; | |
317 | } | |
318 | ||
319 | bool | |
320 | TemplateFile::loadFromFile(const char *path) | |
321 | { | |
322 | int fd; | |
e8f6c5c7 | 323 | char buf[4096]; |
e8f6c5c7 | 324 | ssize_t len; |
1d803566 | 325 | |
02259ff8 CT |
326 | if (loaded()) // already loaded? |
327 | return true; | |
45308e4d | 328 | |
c4aefe96 | 329 | fd = file_open(path, O_RDONLY | O_TEXT); |
e8f6c5c7 | 330 | |
331 | if (fd < 0) { | |
43000484 | 332 | /* with dynamic locale negotiation we may see some failures before a success. */ |
8ff2520a | 333 | if (!silent && templateCode < TCP_RESET) |
43000484 | 334 | debugs(4, DBG_CRITICAL, HERE << "'" << path << "': " << xstrerror()); |
02259ff8 CT |
335 | wasLoaded = false; |
336 | return wasLoaded; | |
1d803566 | 337 | } |
62e76326 | 338 | |
9e008dda | 339 | while ((len = FD_READ_METHOD(fd, buf, sizeof(buf))) > 0) { |
02259ff8 CT |
340 | if (!parse(buf, len, false)) { |
341 | debugs(4, DBG_CRITICAL, HERE << " parse error while reading template file: " << path); | |
342 | wasLoaded = false; | |
343 | return wasLoaded; | |
344 | } | |
e8f6c5c7 | 345 | } |
02259ff8 | 346 | parse(buf, 0, true); |
62e76326 | 347 | |
e8f6c5c7 | 348 | if (len < 0) { |
c70281f8 | 349 | debugs(4, DBG_CRITICAL, HERE << "failed to fully read: '" << path << "': " << xstrerror()); |
1e74c110 | 350 | } |
62e76326 | 351 | |
1d803566 | 352 | file_close(fd); |
e8f6c5c7 | 353 | |
02259ff8 CT |
354 | wasLoaded = true; |
355 | return wasLoaded; | |
356 | } | |
357 | ||
358 | bool strHdrAcptLangGetItem(const String &hdr, char *lang, int langLen, size_t &pos) | |
359 | { | |
dc49061a | 360 | while (pos < hdr.size()) { |
02259ff8 CT |
361 | char *dt = lang; |
362 | ||
a32e7644 AJ |
363 | /* skip any initial whitespace. */ |
364 | while (pos < hdr.size() && xisspace(hdr[pos])) | |
365 | ++pos; | |
62e76326 | 366 | |
02259ff8 CT |
367 | /* |
368 | * Header value format: | |
369 | * - sequence of whitespace delimited tags | |
370 | * - each tag may suffix with ';'.* which we can ignore. | |
371 | * - IFF a tag contains only two characters we can wildcard ANY translations matching: <it> '-'? .* | |
372 | * with preference given to an exact match. | |
373 | */ | |
374 | bool invalid_byte = false; | |
375 | while (pos < hdr.size() && hdr[pos] != ';' && hdr[pos] != ',' && !xisspace(hdr[pos]) && dt < (lang + (langLen -1)) ) { | |
376 | if (!invalid_byte) { | |
377 | #if USE_HTTP_VIOLATIONS | |
378 | // if accepting violations we may as well accept some broken browsers | |
379 | // which may send us the right code, wrong ISO formatting. | |
380 | if (hdr[pos] == '_') | |
381 | *dt = '-'; | |
382 | else | |
383 | #endif | |
384 | *dt = xtolower(hdr[pos]); | |
385 | // valid codes only contain A-Z, hyphen (-) and * | |
386 | if (*dt != '-' && *dt != '*' && (*dt < 'a' || *dt > 'z') ) | |
387 | invalid_byte = true; | |
388 | else | |
95dc7ff4 | 389 | ++dt; // move to next destination byte. |
02259ff8 | 390 | } |
95dc7ff4 | 391 | ++pos; |
02259ff8 | 392 | } |
a38ec4b1 FC |
393 | *dt = '\0'; // nul-terminated the filename content string before system use. |
394 | ++dt; | |
62e76326 | 395 | |
a32e7644 AJ |
396 | // if we terminated the tag on garbage or ';' we need to skip to the next ',' or end of header. |
397 | while (pos < hdr.size() && hdr[pos] != ',') | |
398 | ++pos; | |
399 | ||
400 | if (pos < hdr.size() && hdr[pos] == ',') | |
401 | ++pos; | |
402 | ||
02259ff8 CT |
403 | debugs(4, 9, HERE << "STATE: dt='" << dt << "', lang='" << lang << "', pos=" << pos << ", buf='" << ((pos < hdr.size()) ? hdr.substr(pos,hdr.size()) : "") << "'"); |
404 | ||
405 | /* if we found anything we might use, try it. */ | |
406 | if (*lang != '\0' && !invalid_byte) | |
407 | return true; | |
408 | } | |
409 | return false; | |
410 | } | |
411 | ||
412 | bool | |
b248c2a3 | 413 | TemplateFile::loadFor(const HttpRequest *request) |
02259ff8 CT |
414 | { |
415 | String hdr; | |
416 | ||
cf0a0bdf | 417 | #if USE_ERR_LOCALES |
02259ff8 CT |
418 | if (loaded()) // already loaded? |
419 | return true; | |
420 | ||
421 | if (!request || !request->header.getList(HDR_ACCEPT_LANGUAGE, &hdr) ) | |
422 | return false; | |
423 | ||
424 | char lang[256]; | |
425 | size_t pos = 0; // current parsing position in header string | |
426 | ||
427 | debugs(4, 6, HERE << "Testing Header: '" << hdr << "'"); | |
428 | ||
429 | while ( strHdrAcptLangGetItem(hdr, lang, 256, pos) ) { | |
430 | ||
431 | /* wildcard uses the configured default language */ | |
432 | if (lang[0] == '*' && lang[1] == '\0') { | |
433 | debugs(4, 6, HERE << "Found language '" << lang << "'. Using configured default."); | |
434 | return false; | |
435 | } | |
436 | ||
437 | debugs(4, 6, HERE << "Found language '" << lang << "', testing for available template"); | |
438 | ||
439 | if (tryLoadTemplate(lang)) { | |
440 | /* store the language we found for the Content-Language reply header */ | |
441 | errLanguage = lang; | |
442 | break; | |
443 | } else if (Config.errorLogMissingLanguages) { | |
444 | debugs(4, DBG_IMPORTANT, "WARNING: Error Pages Missing Language: " << lang); | |
445 | } | |
446 | } | |
cf0a0bdf | 447 | #endif |
02259ff8 CT |
448 | |
449 | return loaded(); | |
6eb42cae | 450 | } |
451 | ||
63be0a78 | 452 | /// \ingroup ErrorPageInternal |
02922e76 | 453 | static ErrorDynamicPageInfo * |
454 | errorDynamicPageInfoCreate(int id, const char *page_name) | |
455 | { | |
4ff59fc7 | 456 | ErrorDynamicPageInfo *info = new ErrorDynamicPageInfo; |
02922e76 | 457 | info->id = id; |
458 | info->page_name = xstrdup(page_name); | |
955394ce | 459 | info->page_redirect = static_cast<Http::StatusCode>(atoi(page_name)); |
aed9a15b AJ |
460 | |
461 | /* WARNING on redirection status: | |
462 | * 2xx are permitted, but not documented officially. | |
463 | * - might be useful for serving static files (PAC etc) in special cases | |
464 | * 3xx require a URL suitable for Location: header. | |
465 | * - the current design does not allow for a Location: URI as well as a local file template | |
466 | * although this possibility is explicitly permitted in the specs. | |
467 | * 4xx-5xx require a local file template. | |
468 | * - sending Location: on these codes with no body is invalid by the specs. | |
469 | * - current result is Squid crashing or XSS problems as dynamic deny_info load random disk files. | |
470 | * - a future redesign of the file loading may result in loading remote objects sent inline as local body. | |
471 | */ | |
955394ce | 472 | if (info->page_redirect == Http::scNone) |
aed9a15b AJ |
473 | ; // special case okay. |
474 | else if (info->page_redirect < 200 || info->page_redirect > 599) { | |
475 | // out of range | |
476 | debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " is not valid on '" << page_name << "'"); | |
477 | self_destruct(); | |
478 | } else if ( /* >= 200 && */ info->page_redirect < 300 && strchr(&(page_name[4]), ':')) { | |
479 | // 2xx require a local template file | |
0ff52055 | 480 | debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a template on '" << page_name << "'"); |
aed9a15b | 481 | self_destruct(); |
0ff52055 | 482 | } else if (info->page_redirect >= 300 && info->page_redirect <= 399 && !strchr(&(page_name[4]), ':')) { |
aed9a15b | 483 | // 3xx require an absolute URL |
0ff52055 | 484 | debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a URL on '" << page_name << "'"); |
aed9a15b AJ |
485 | self_destruct(); |
486 | } else if (info->page_redirect >= 400 /* && <= 599 */ && strchr(&(page_name[4]), ':')) { | |
487 | // 4xx/5xx require a local template file | |
0ff52055 | 488 | debugs(0, DBG_CRITICAL, "FATAL: status " << info->page_redirect << " requires a template on '" << page_name << "'"); |
aed9a15b AJ |
489 | self_destruct(); |
490 | } | |
491 | // else okay. | |
492 | ||
02922e76 | 493 | return info; |
494 | } | |
495 | ||
63be0a78 | 496 | /// \ingroup ErrorPageInternal |
02922e76 | 497 | static void |
1afe05c5 | 498 | errorDynamicPageInfoDestroy(ErrorDynamicPageInfo * info) |
02922e76 | 499 | { |
500 | assert(info); | |
c70281f8 | 501 | safe_free(info->page_name); |
4ff59fc7 | 502 | delete info; |
02922e76 | 503 | } |
504 | ||
63be0a78 | 505 | /// \ingroup ErrorPageInternal |
76cdc28d | 506 | static int |
507 | errorPageId(const char *page_name) | |
508 | { | |
95dc7ff4 | 509 | for (int i = 0; i < ERR_MAX; ++i) { |
62e76326 | 510 | if (strcmp(err_type_str[i], page_name) == 0) |
511 | return i; | |
76cdc28d | 512 | } |
62e76326 | 513 | |
95dc7ff4 | 514 | for (size_t j = 0; j < ErrorDynamicPages.size(); ++j) { |
4c9eadc2 | 515 | if (strcmp(ErrorDynamicPages[j]->page_name, page_name) == 0) |
190154cf | 516 | return j + ERR_MAX; |
76cdc28d | 517 | } |
62e76326 | 518 | |
76cdc28d | 519 | return ERR_NONE; |
520 | } | |
521 | ||
e6ccf245 | 522 | err_type |
02922e76 | 523 | errorReservePageId(const char *page_name) |
524 | { | |
76cdc28d | 525 | ErrorDynamicPageInfo *info; |
526 | int id = errorPageId(page_name); | |
62e76326 | 527 | |
76cdc28d | 528 | if (id == ERR_NONE) { |
62e76326 | 529 | info = errorDynamicPageInfoCreate(ERR_MAX + ErrorDynamicPages.size(), page_name); |
530 | ErrorDynamicPages.push_back(info); | |
531 | id = info->id; | |
76cdc28d | 532 | } |
62e76326 | 533 | |
e6ccf245 | 534 | return (err_type)id; |
02922e76 | 535 | } |
536 | ||
63be0a78 | 537 | /// \ingroup ErrorPageInternal |
64b66b76 | 538 | const char * |
c68e9c6b | 539 | errorPageName(int pageId) |
53ad48e6 | 540 | { |
f53969cc | 541 | if (pageId >= ERR_NONE && pageId < ERR_MAX) /* common case */ |
62e76326 | 542 | return err_type_str[pageId]; |
543 | ||
4ff59fc7 | 544 | if (pageId >= ERR_MAX && pageId - ERR_MAX < (ssize_t)ErrorDynamicPages.size()) |
4c9eadc2 | 545 | return ErrorDynamicPages[pageId - ERR_MAX]->page_name; |
62e76326 | 546 | |
f53969cc | 547 | return "ERR_UNKNOWN"; /* should not happen */ |
53ad48e6 | 548 | } |
549 | ||
a23223bf CT |
550 | ErrorState * |
551 | ErrorState::NewForwarding(err_type type, HttpRequest *request) | |
552 | { | |
553 | assert(request); | |
554 | const Http::StatusCode status = request->flags.needValidation ? | |
e2849af8 | 555 | Http::scGatewayTimeout : Http::scServiceUnavailable; |
a23223bf CT |
556 | return new ErrorState(type, status, request); |
557 | } | |
558 | ||
955394ce | 559 | ErrorState::ErrorState(err_type t, Http::StatusCode status, HttpRequest * req) : |
f53969cc SM |
560 | type(t), |
561 | page_id(t), | |
562 | err_language(NULL), | |
563 | httpStatus(status), | |
913524f0 | 564 | #if USE_AUTH |
f53969cc | 565 | auth_user_request (NULL), |
913524f0 | 566 | #endif |
f53969cc SM |
567 | request(NULL), |
568 | url(NULL), | |
569 | xerrno(0), | |
570 | port(0), | |
571 | dnsError(), | |
572 | ttl(0), | |
573 | src_addr(), | |
574 | redirect_url(NULL), | |
575 | callback(NULL), | |
576 | callback_data(NULL), | |
577 | request_hdrs(NULL), | |
578 | err_msg(NULL), | |
cb4f4424 | 579 | #if USE_OPENSSL |
f53969cc | 580 | detail(NULL), |
913524f0 | 581 | #endif |
f53969cc | 582 | detailCode(ERR_DETAIL_NONE) |
fe40a877 | 583 | { |
913524f0 AJ |
584 | memset(&ftp, 0, sizeof(ftp)); |
585 | ||
4c9eadc2 FC |
586 | if (page_id >= ERR_MAX && ErrorDynamicPages[page_id - ERR_MAX]->page_redirect != Http::scNone) |
587 | httpStatus = ErrorDynamicPages[page_id - ERR_MAX]->page_redirect; | |
913524f0 AJ |
588 | |
589 | if (req != NULL) { | |
b248c2a3 AJ |
590 | request = req; |
591 | HTTPMSGLOCK(request); | |
913524f0 | 592 | src_addr = req->client_addr; |
2cc81f1f | 593 | } |
fe40a877 | 594 | } |
595 | ||
fe40a877 | 596 | void |
597 | errorAppendEntry(StoreEntry * entry, ErrorState * err) | |
598 | { | |
e4a67a80 | 599 | assert(entry->mem_obj != NULL); |
528b2c61 | 600 | assert (entry->isEmpty()); |
0b86805b | 601 | debugs(4, 4, "Creating an error page for entry " << entry << |
602 | " with errorstate " << err << | |
603 | " page id " << err->page_id); | |
62e76326 | 604 | |
1ea80e98 | 605 | if (entry->store_status != STORE_PENDING) { |
0b86805b | 606 | debugs(4, 2, "Skipping error page due to store_status: " << entry->store_status); |
62e76326 | 607 | /* |
608 | * If the entry is not STORE_PENDING, then no clients | |
609 | * care about it, and we don't need to generate an | |
610 | * error message | |
611 | */ | |
612 | assert(EBIT_TEST(entry->flags, ENTRY_ABORTED)); | |
6ea35247 | 613 | assert(entry->mem_obj->nclients == 0); |
913524f0 | 614 | delete err; |
62e76326 | 615 | return; |
1ea80e98 | 616 | } |
62e76326 | 617 | |
76cdc28d | 618 | if (err->page_id == TCP_RESET) { |
62e76326 | 619 | if (err->request) { |
bf8fe701 | 620 | debugs(4, 2, "RSTing this reply"); |
e857372a | 621 | err->request->flags.resetTcp = true; |
62e76326 | 622 | } |
98264874 | 623 | } |
62e76326 | 624 | |
eacfca83 | 625 | entry->storeErrorResponse(err->BuildHttpReply()); |
913524f0 | 626 | delete err; |
fe40a877 | 627 | } |
628 | ||
fe40a877 | 629 | void |
e0d28505 | 630 | errorSend(const Comm::ConnectionPointer &conn, ErrorState * err) |
fe40a877 | 631 | { |
cb69b4c7 | 632 | HttpReply *rep; |
e0d28505 AJ |
633 | debugs(4, 3, HERE << conn << ", err=" << err); |
634 | assert(Comm::IsConnOpen(conn)); | |
62e76326 | 635 | |
c70281f8 | 636 | rep = err->BuildHttpReply(); |
62e76326 | 637 | |
ddbe383d | 638 | MemBuf *mb = rep->pack(); |
ec41b64c AJ |
639 | AsyncCall::Pointer call = commCbCall(78, 5, "errorSendComplete", |
640 | CommIoCbPtrFun(&errorSendComplete, err)); | |
b0388924 | 641 | Comm::Write(conn, mb, call); |
ddbe383d | 642 | delete mb; |
62e76326 | 643 | |
06a5ae20 | 644 | delete rep; |
fe40a877 | 645 | } |
646 | ||
63be0a78 | 647 | /** |
648 | \ingroup ErrorPageAPI | |
fe40a877 | 649 | * |
63be0a78 | 650 | * Called by commHandleWrite() after data has been written |
651 | * to the client socket. | |
fe40a877 | 652 | * |
63be0a78 | 653 | \note If there is a callback, the callback is responsible for |
b3802bdc | 654 | * closing the FD, otherwise we do it ourselves. |
fe40a877 | 655 | */ |
656 | static void | |
ced8def3 | 657 | errorSendComplete(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag errflag, int, void *data) |
fe40a877 | 658 | { |
e6ccf245 | 659 | ErrorState *err = static_cast<ErrorState *>(data); |
e0d28505 | 660 | debugs(4, 3, HERE << conn << ", size=" << size); |
62e76326 | 661 | |
c8407295 | 662 | if (errflag != Comm::ERR_CLOSING) { |
62e76326 | 663 | if (err->callback) { |
bf8fe701 | 664 | debugs(4, 3, "errorSendComplete: callback"); |
e0d28505 | 665 | err->callback(conn->fd, err->callback_data, size); |
62e76326 | 666 | } else { |
bf8fe701 | 667 | debugs(4, 3, "errorSendComplete: comm_close"); |
80463bb4 | 668 | conn->close(); |
62e76326 | 669 | } |
fe40a877 | 670 | } |
62e76326 | 671 | |
913524f0 | 672 | delete err; |
fe40a877 | 673 | } |
674 | ||
913524f0 | 675 | ErrorState::~ErrorState() |
6eb42cae | 676 | { |
913524f0 AJ |
677 | HTTPMSGUNLOCK(request); |
678 | safe_free(redirect_url); | |
679 | safe_free(url); | |
680 | safe_free(request_hdrs); | |
681 | wordlistDestroy(&ftp.server_msg); | |
682 | safe_free(ftp.request); | |
683 | safe_free(ftp.reply); | |
2f1431ea | 684 | #if USE_AUTH |
913524f0 | 685 | auth_user_request = NULL; |
2f1431ea | 686 | #endif |
913524f0 | 687 | safe_free(err_msg); |
5bf33a09 | 688 | #if USE_ERR_LOCALES |
913524f0 | 689 | if (err_language != Config.errorDefaultLanguage) |
5bf33a09 | 690 | #endif |
913524f0 | 691 | safe_free(err_language); |
cb4f4424 | 692 | #if USE_OPENSSL |
913524f0 | 693 | delete detail; |
4d16918e | 694 | #endif |
1e74c110 | 695 | } |
8213067d | 696 | |
c70281f8 AJ |
697 | int |
698 | ErrorState::Dump(MemBuf * mb) | |
b5fb34f1 | 699 | { |
032785bf | 700 | MemBuf str; |
cc192b50 | 701 | char ntoabuf[MAX_IPSTRLEN]; |
702 | ||
2fe7eff9 | 703 | str.reset(); |
b5fb34f1 | 704 | /* email subject line */ |
4391cd15 AJ |
705 | str.appendf("CacheErrorInfo - %s", errorPageName(type)); |
706 | mb->appendf("?subject=%s", rfc1738_escape_part(str.buf)); | |
2fe7eff9 | 707 | str.reset(); |
b5fb34f1 | 708 | /* email body */ |
4391cd15 | 709 | str.appendf("CacheHost: %s\r\n", getMyHostname()); |
b5fb34f1 | 710 | /* - Err Msgs */ |
4391cd15 | 711 | str.appendf("ErrPage: %s\r\n", errorPageName(type)); |
62e76326 | 712 | |
c70281f8 | 713 | if (xerrno) { |
4391cd15 | 714 | str.appendf("Err: (%d) %s\r\n", xerrno, strerror(xerrno)); |
b5fb34f1 | 715 | } else { |
4391cd15 | 716 | str.append("Err: [none]\r\n", 13); |
b5fb34f1 | 717 | } |
2f1431ea | 718 | #if USE_AUTH |
98340a7b | 719 | if (auth_user_request.getRaw() && auth_user_request->denyMessage()) |
4391cd15 | 720 | str.appendf("Auth ErrMsg: %s\r\n", auth_user_request->denyMessage()); |
2f1431ea | 721 | #endif |
3ff65596 | 722 | if (dnsError.size() > 0) |
4391cd15 | 723 | str.appendf("DNS ErrMsg: %s\r\n", dnsError.termedBuf()); |
62e76326 | 724 | |
b5fb34f1 | 725 | /* - TimeStamp */ |
4391cd15 | 726 | str.appendf("TimeStamp: %s\r\n\r\n", mkrfc1123(squid_curtime)); |
62e76326 | 727 | |
b5fb34f1 | 728 | /* - IP stuff */ |
4391cd15 | 729 | str.appendf("ClientIP: %s\r\n", src_addr.toStr(ntoabuf,MAX_IPSTRLEN)); |
62e76326 | 730 | |
b3802bdc | 731 | if (request && request->hier.host[0] != '\0') { |
4391cd15 | 732 | str.appendf("ServerIP: %s\r\n", request->hier.host); |
b5fb34f1 | 733 | } |
62e76326 | 734 | |
4391cd15 | 735 | str.append("\r\n", 2); |
b5fb34f1 | 736 | /* - HTTP stuff */ |
4391cd15 | 737 | str.append("HTTP Request:\r\n", 15); |
62e76326 | 738 | |
c70281f8 | 739 | if (NULL != request) { |
bb790702 FC |
740 | String urlpath_or_slash; |
741 | ||
742 | if (request->urlpath.size() != 0) | |
743 | urlpath_or_slash = request->urlpath; | |
744 | else | |
745 | urlpath_or_slash = "/"; | |
746 | ||
4391cd15 AJ |
747 | str.appendf(SQUIDSBUFPH " " SQUIDSTRINGPH " %s/%d.%d\n", |
748 | SQUIDSBUFPRINT(request->method.image()), | |
749 | SQUIDSTRINGPRINT(urlpath_or_slash), | |
750 | AnyP::ProtocolType_str[request->http_ver.protocol], | |
751 | request->http_ver.major, request->http_ver.minor); | |
10201568 | 752 | request->header.packInto(&str); |
b5fb34f1 | 753 | } |
62e76326 | 754 | |
4391cd15 | 755 | str.append("\r\n", 2); |
b5fb34f1 | 756 | /* - FTP stuff */ |
62e76326 | 757 | |
c70281f8 | 758 | if (ftp.request) { |
4391cd15 AJ |
759 | str.appendf("FTP Request: %s\r\n", ftp.request); |
760 | str.appendf("FTP Reply: %s\r\n", (ftp.reply? ftp.reply:"[none]")); | |
761 | str.append("FTP Msg: ", 9); | |
c70281f8 | 762 | wordlistCat(ftp.server_msg, &str); |
4391cd15 | 763 | str.append("\r\n", 2); |
b5fb34f1 | 764 | } |
62e76326 | 765 | |
4391cd15 AJ |
766 | str.append("\r\n", 2); |
767 | mb->appendf("&body=%s", rfc1738_escape_part(str.buf)); | |
2fe7eff9 | 768 | str.clean(); |
b5fb34f1 | 769 | return 0; |
770 | } | |
771 | ||
63be0a78 | 772 | /// \ingroup ErrorPageInternal |
2658f489 | 773 | #define CVT_BUF_SZ 512 |
fe40a877 | 774 | |
c70281f8 | 775 | const char * |
4d16918e | 776 | ErrorState::Convert(char token, bool building_deny_info_url, bool allowRecursion) |
8213067d | 777 | { |
032785bf | 778 | static MemBuf mb; |
f53969cc | 779 | const char *p = NULL; /* takes priority over mb if set */ |
10270faa | 780 | int do_quote = 1; |
4ad8d23d | 781 | int no_urlescape = 0; /* if true then item is NOT to be further URL-encoded */ |
cc192b50 | 782 | char ntoabuf[MAX_IPSTRLEN]; |
eeb423fb | 783 | |
2fe7eff9 | 784 | mb.reset(); |
62e76326 | 785 | |
9b312a19 | 786 | switch (token) { |
62e76326 | 787 | |
523f44f5 | 788 | case 'a': |
2f1431ea | 789 | #if USE_AUTH |
a33a428a | 790 | if (request && request->auth_user_request != NULL) |
c70281f8 | 791 | p = request->auth_user_request->username(); |
523f44f5 | 792 | if (!p) |
2f1431ea | 793 | #endif |
523f44f5 | 794 | p = "-"; |
523f44f5 | 795 | break; |
88d1e459 AJ |
796 | |
797 | case 'b': | |
4391cd15 | 798 | mb.appendf("%u", getMyPort()); |
88d1e459 AJ |
799 | break; |
800 | ||
8f872bb6 | 801 | case 'B': |
1e98e28b | 802 | if (building_deny_info_url) break; |
5517260a | 803 | p = request ? Ftp::UrlWith2f(request) : "[no URL]"; |
62e76326 | 804 | break; |
805 | ||
42b51993 | 806 | case 'c': |
1e98e28b | 807 | if (building_deny_info_url) break; |
c70281f8 | 808 | p = errorPageName(type); |
62e76326 | 809 | break; |
810 | ||
4d16918e CT |
811 | case 'D': |
812 | if (!allowRecursion) | |
813 | p = "%D"; // if recursion is not allowed, do not convert | |
cb4f4424 | 814 | #if USE_OPENSSL |
4d16918e CT |
815 | // currently only SSL error details implemented |
816 | else if (detail) { | |
02259ff8 | 817 | detail->useRequest(request); |
4d16918e | 818 | const String &errDetail = detail->toString(); |
b38b26cb | 819 | if (errDetail.size() > 0) { |
8211c314 CT |
820 | MemBuf *detail_mb = ConvertText(errDetail.termedBuf(), false); |
821 | mb.append(detail_mb->content(), detail_mb->contentSize()); | |
822 | delete detail_mb; | |
823 | do_quote = 0; | |
824 | } | |
825 | } | |
4d16918e | 826 | #endif |
8211c314 | 827 | if (!mb.contentSize()) |
4391cd15 | 828 | mb.append("[No Error Detail]", 17); |
4d16918e CT |
829 | break; |
830 | ||
042461c3 | 831 | case 'e': |
4391cd15 | 832 | mb.appendf("%d", xerrno); |
62e76326 | 833 | break; |
834 | ||
042461c3 | 835 | case 'E': |
c70281f8 | 836 | if (xerrno) |
4391cd15 | 837 | mb.appendf("(%d) %s", xerrno, strerror(xerrno)); |
62e76326 | 838 | else |
4391cd15 | 839 | mb.append("[No Error]", 10); |
62e76326 | 840 | break; |
841 | ||
fe40a877 | 842 | case 'f': |
1e98e28b | 843 | if (building_deny_info_url) break; |
62e76326 | 844 | /* FTP REQUEST LINE */ |
c70281f8 AJ |
845 | if (ftp.request) |
846 | p = ftp.request; | |
62e76326 | 847 | else |
848 | p = "nothing"; | |
62e76326 | 849 | break; |
850 | ||
fe40a877 | 851 | case 'F': |
1e98e28b | 852 | if (building_deny_info_url) break; |
62e76326 | 853 | /* FTP REPLY LINE */ |
c6b684dd | 854 | if (ftp.reply) |
c70281f8 | 855 | p = ftp.reply; |
62e76326 | 856 | else |
857 | p = "nothing"; | |
62e76326 | 858 | break; |
859 | ||
7131112f | 860 | case 'g': |
1e98e28b | 861 | if (building_deny_info_url) break; |
a91d3505 AJ |
862 | /* FTP SERVER RESPONSE */ |
863 | if (ftp.listing) { | |
0477a072 AJ |
864 | mb.append(ftp.listing->content(), ftp.listing->contentSize()); |
865 | do_quote = 0; | |
a91d3505 AJ |
866 | } else if (ftp.server_msg) { |
867 | wordlistCat(ftp.server_msg, &mb); | |
0477a072 | 868 | } |
62e76326 | 869 | break; |
870 | ||
03d7b07f | 871 | case 'h': |
4391cd15 | 872 | mb.appendf("%s", getMyHostname()); |
62e76326 | 873 | break; |
874 | ||
fe40a877 | 875 | case 'H': |
c70281f8 | 876 | if (request) { |
b3802bdc | 877 | if (request->hier.host[0] != '\0') // if non-empty string. |
c70281f8 | 878 | p = request->hier.host; |
beed27a2 | 879 | else |
c70281f8 | 880 | p = request->GetHost(); |
1e98e28b | 881 | } else if (!building_deny_info_url) |
beed27a2 | 882 | p = "[unknown host]"; |
62e76326 | 883 | break; |
884 | ||
f787fb1e | 885 | case 'i': |
4391cd15 | 886 | mb.appendf("%s", src_addr.toStr(ntoabuf,MAX_IPSTRLEN)); |
62e76326 | 887 | break; |
888 | ||
f787fb1e | 889 | case 'I': |
72e4bea1 | 890 | if (request && request->hier.tcpServer != NULL) |
4dd643d5 | 891 | p = request->hier.tcpServer->remote.toStr(ntoabuf,MAX_IPSTRLEN); |
1e98e28b | 892 | else if (!building_deny_info_url) |
62e76326 | 893 | p = "[unknown]"; |
62e76326 | 894 | break; |
895 | ||
5b52cb6c | 896 | case 'l': |
1e98e28b | 897 | if (building_deny_info_url) break; |
0ae3294a | 898 | mb.append(error_stylesheet.content(), error_stylesheet.contentSize()); |
5b52cb6c AJ |
899 | do_quote = 0; |
900 | break; | |
901 | ||
fe40a877 | 902 | case 'L': |
1e98e28b | 903 | if (building_deny_info_url) break; |
62e76326 | 904 | if (Config.errHtmlText) { |
4391cd15 | 905 | mb.appendf("%s", Config.errHtmlText); |
62e76326 | 906 | do_quote = 0; |
1e98e28b | 907 | } else |
62e76326 | 908 | p = "[not available]"; |
62e76326 | 909 | break; |
910 | ||
066ed5c1 | 911 | case 'm': |
1e98e28b | 912 | if (building_deny_info_url) break; |
2f1431ea | 913 | #if USE_AUTH |
52ba2948 CT |
914 | if (auth_user_request.getRaw()) |
915 | p = auth_user_request->denyMessage("[not available]"); | |
916 | else | |
917 | p = "[not available]"; | |
2f1431ea AJ |
918 | #else |
919 | p = "-"; | |
920 | #endif | |
62e76326 | 921 | break; |
922 | ||
fe40a877 | 923 | case 'M': |
7f06a3d8 | 924 | if (request) { |
e2849af8 A |
925 | const SBuf &m = request->method.image(); |
926 | mb.append(m.rawContent(), m.length()); | |
7f06a3d8 AJ |
927 | } else if (!building_deny_info_url) |
928 | p = "[unknown method]"; | |
62e76326 | 929 | break; |
930 | ||
4a972fa2 | 931 | case 'o': |
8c93a598 | 932 | p = request ? request->extacl_message.termedBuf() : external_acl_message; |
1e98e28b | 933 | if (!p && !building_deny_info_url) |
d73b593b | 934 | p = "[not available]"; |
4a972fa2 | 935 | break; |
936 | ||
fe40a877 | 937 | case 'p': |
c70281f8 | 938 | if (request) { |
4391cd15 | 939 | mb.appendf("%u", request->port); |
1e98e28b | 940 | } else if (!building_deny_info_url) { |
62e76326 | 941 | p = "[unknown port]"; |
942 | } | |
62e76326 | 943 | break; |
944 | ||
fe40a877 | 945 | case 'P': |
e7aae06b | 946 | if (request) { |
4e3f4dc7 | 947 | p = request->url.getScheme().c_str(); |
1e98e28b | 948 | } else if (!building_deny_info_url) { |
e7aae06b A |
949 | p = "[unknown protocol]"; |
950 | } | |
62e76326 | 951 | break; |
952 | ||
b5af8569 | 953 | case 'R': |
1e98e28b | 954 | if (building_deny_info_url) { |
1b091aec CT |
955 | if (request != NULL) { |
956 | p = (request->urlpath.size() != 0 ? request->urlpath.termedBuf() : "/"); | |
957 | no_urlescape = 1; | |
958 | } else | |
959 | p = "[no request]"; | |
15b02e9a AJ |
960 | break; |
961 | } | |
c70281f8 | 962 | if (NULL != request) { |
bb790702 FC |
963 | String urlpath_or_slash; |
964 | ||
965 | if (request->urlpath.size() != 0) | |
966 | urlpath_or_slash = request->urlpath; | |
967 | else | |
968 | urlpath_or_slash = "/"; | |
969 | ||
4391cd15 AJ |
970 | mb.appendf(SQUIDSBUFPH " " SQUIDSTRINGPH " %s/%d.%d\n", |
971 | SQUIDSBUFPRINT(request->method.image()), | |
972 | SQUIDSTRINGPRINT(urlpath_or_slash), | |
973 | AnyP::ProtocolType_str[request->http_ver.protocol], | |
974 | request->http_ver.major, request->http_ver.minor); | |
10201568 | 975 | request->header.packInto(&mb, true); //hide authorization data |
c70281f8 AJ |
976 | } else if (request_hdrs) { |
977 | p = request_hdrs; | |
62e76326 | 978 | } else { |
979 | p = "[no request]"; | |
980 | } | |
62e76326 | 981 | break; |
982 | ||
1d803566 | 983 | case 's': |
15b02e9a | 984 | /* for backward compat we make %s show the full URL. Drop this in some future release. */ |
1e98e28b | 985 | if (building_deny_info_url) { |
15b02e9a | 986 | p = request ? urlCanonical(request) : url; |
fa84c01d | 987 | debugs(0, DBG_CRITICAL, "WARNING: deny_info now accepts coded tags. Use %u to get the full URL instead of %s"); |
05320519 | 988 | } else |
15b02e9a | 989 | p = visible_appname_string; |
62e76326 | 990 | break; |
991 | ||
1d803566 | 992 | case 'S': |
1e98e28b | 993 | if (building_deny_info_url) { |
4ad8d23d | 994 | p = visible_appname_string; |
e7aae06b A |
995 | break; |
996 | } | |
62e76326 | 997 | /* signature may contain %-escapes, recursion */ |
c70281f8 AJ |
998 | if (page_id != ERR_SQUID_SIGNATURE) { |
999 | const int saved_id = page_id; | |
1000 | page_id = ERR_SQUID_SIGNATURE; | |
1001 | MemBuf *sign_mb = BuildContent(); | |
4391cd15 | 1002 | mb.append(sign_mb->content(), sign_mb->contentSize()); |
2fe7eff9 | 1003 | sign_mb->clean(); |
032785bf | 1004 | delete sign_mb; |
c70281f8 | 1005 | page_id = saved_id; |
62e76326 | 1006 | do_quote = 0; |
1007 | } else { | |
1008 | /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */ | |
1009 | p = "[%S]"; | |
1010 | } | |
62e76326 | 1011 | break; |
1012 | ||
fe40a877 | 1013 | case 't': |
4391cd15 | 1014 | mb.appendf("%s", Time::FormatHttpd(squid_curtime)); |
62e76326 | 1015 | break; |
1016 | ||
f8291f8f | 1017 | case 'T': |
4391cd15 | 1018 | mb.appendf("%s", mkrfc1123(squid_curtime)); |
62e76326 | 1019 | break; |
1020 | ||
fe40a877 | 1021 | case 'U': |
b3802bdc AJ |
1022 | /* Using the fake-https version of canonical so error pages see https:// */ |
1023 | /* even when the url-path cannot be shown as more than '*' */ | |
e7aae06b A |
1024 | if (request) |
1025 | p = urlCanonicalFakeHttps(request); | |
1026 | else if (url) | |
1027 | p = url; | |
1e98e28b | 1028 | else if (!building_deny_info_url) |
e7aae06b | 1029 | p = "[no URL]"; |
62e76326 | 1030 | break; |
1031 | ||
76cdc28d | 1032 | case 'u': |
1e98e28b AJ |
1033 | if (request) |
1034 | p = urlCanonical(request); | |
1035 | else if (url) | |
1036 | p = url; | |
1037 | else if (!building_deny_info_url) | |
1038 | p = "[no URL]"; | |
62e76326 | 1039 | break; |
1040 | ||
fe40a877 | 1041 | case 'w': |
62e76326 | 1042 | if (Config.adminEmail) |
4391cd15 | 1043 | mb.appendf("%s", Config.adminEmail); |
1e98e28b | 1044 | else if (!building_deny_info_url) |
62e76326 | 1045 | p = "[unknown]"; |
62e76326 | 1046 | break; |
1047 | ||
b5fb34f1 | 1048 | case 'W': |
1e98e28b | 1049 | if (building_deny_info_url) break; |
62e76326 | 1050 | if (Config.adminEmail && Config.onoff.emailErrData) |
c70281f8 | 1051 | Dump(&mb); |
4ad8d23d | 1052 | no_urlescape = 1; |
62e76326 | 1053 | break; |
1054 | ||
e4a8468d | 1055 | case 'x': |
cb4f4424 | 1056 | #if USE_OPENSSL |
e4a8468d | 1057 | if (detail) |
4391cd15 | 1058 | mb.appendf("%s", detail->errorName()); |
0ab68838 CT |
1059 | else |
1060 | #endif | |
1061 | if (!building_deny_info_url) | |
1062 | p = "[Unknown Error Code]"; | |
e4a8468d CT |
1063 | break; |
1064 | ||
fe40a877 | 1065 | case 'z': |
1e98e28b | 1066 | if (building_deny_info_url) break; |
3ff65596 AR |
1067 | if (dnsError.size() > 0) |
1068 | p = dnsError.termedBuf(); | |
0477a072 AJ |
1069 | else if (ftp.cwd_msg) |
1070 | p = ftp.cwd_msg; | |
62e76326 | 1071 | else |
1072 | p = "[unknown]"; | |
62e76326 | 1073 | break; |
1074 | ||
43ae1d95 | 1075 | case 'Z': |
1e98e28b | 1076 | if (building_deny_info_url) break; |
c70281f8 AJ |
1077 | if (err_msg) |
1078 | p = err_msg; | |
43ae1d95 | 1079 | else |
1080 | p = "[unknown]"; | |
43ae1d95 | 1081 | break; |
1082 | ||
e347f8e5 | 1083 | case '%': |
62e76326 | 1084 | p = "%"; |
62e76326 | 1085 | break; |
1086 | ||
9b312a19 | 1087 | default: |
4391cd15 | 1088 | mb.appendf("%%%c", token); |
cbba2ba2 | 1089 | do_quote = 0; |
62e76326 | 1090 | break; |
9b312a19 | 1091 | } |
62e76326 | 1092 | |
137ee196 | 1093 | if (!p) |
f53969cc | 1094 | p = mb.buf; /* do not use mb after this assignment! */ |
62e76326 | 1095 | |
137ee196 | 1096 | assert(p); |
62e76326 | 1097 | |
bf8fe701 | 1098 | debugs(4, 3, "errorConvert: %%" << token << " --> '" << p << "'" ); |
62e76326 | 1099 | |
10270faa | 1100 | if (do_quote) |
62e76326 | 1101 | p = html_quote(p); |
1102 | ||
1e98e28b | 1103 | if (building_deny_info_url && !no_urlescape) |
15b02e9a AJ |
1104 | p = rfc1738_escape_part(p); |
1105 | ||
9b312a19 | 1106 | return p; |
8213067d | 1107 | } |
e381a13d | 1108 | |
15b02e9a | 1109 | void |
ced8def3 | 1110 | ErrorState::DenyInfoLocation(const char *name, HttpRequest *, MemBuf &result) |
15b02e9a AJ |
1111 | { |
1112 | char const *m = name; | |
1113 | char const *p = m; | |
1114 | char const *t; | |
1115 | ||
aed9a15b AJ |
1116 | if (m[0] == '3') |
1117 | m += 4; // skip "3xx:" | |
1118 | ||
15b02e9a AJ |
1119 | while ((p = strchr(m, '%'))) { |
1120 | result.append(m, p - m); /* copy */ | |
4d16918e | 1121 | t = Convert(*++p, true, true); /* convert */ |
4391cd15 | 1122 | result.appendf("%s", t); /* copy */ |
15b02e9a AJ |
1123 | m = p + 1; /* advance */ |
1124 | } | |
1125 | ||
1126 | if (*m) | |
4391cd15 | 1127 | result.appendf("%s", m); /* copy tail */ |
15b02e9a AJ |
1128 | |
1129 | assert((size_t)result.contentSize() == strlen(result.content())); | |
1130 | } | |
1131 | ||
cb69b4c7 | 1132 | HttpReply * |
c70281f8 | 1133 | ErrorState::BuildHttpReply() |
cb69b4c7 | 1134 | { |
06a5ae20 | 1135 | HttpReply *rep = new HttpReply; |
c70281f8 | 1136 | const char *name = errorPageName(page_id); |
cb69b4c7 | 1137 | /* no LMT for error pages; error pages expire immediately */ |
62e76326 | 1138 | |
d4d63422 | 1139 | if (name[0] == '3' || (name[0] != '2' && name[0] != '4' && name[0] != '5' && strchr(name, ':'))) { |
62e76326 | 1140 | /* Redirection */ |
f11c8e2f | 1141 | Http::StatusCode status = Http::scFound; |
aed9a15b AJ |
1142 | // Use configured 3xx reply status if set. |
1143 | if (name[0] == '3') | |
1144 | status = httpStatus; | |
1145 | else { | |
1146 | // Use 307 for HTTP/1.1 non-GET/HEAD requests. | |
1b091aec | 1147 | if (request != NULL && request->method != Http::METHOD_GET && request->method != Http::METHOD_HEAD && request->http_ver >= Http::ProtocolVersion(1,1)) |
955394ce | 1148 | status = Http::scTemporaryRedirect; |
aed9a15b AJ |
1149 | } |
1150 | ||
ec80c740 | 1151 | rep->setHeaders(status, NULL, "text/html;charset=utf-8", 0, 0, -1); |
c44950c4 | 1152 | |
c70281f8 | 1153 | if (request) { |
15b02e9a AJ |
1154 | MemBuf redirect_location; |
1155 | redirect_location.init(); | |
1156 | DenyInfoLocation(name, request, redirect_location); | |
1157 | httpHeaderPutStrf(&rep->header, HDR_LOCATION, "%s", redirect_location.content() ); | |
c44950c4 | 1158 | } |
1159 | ||
c70281f8 | 1160 | httpHeaderPutStrf(&rep->header, HDR_X_SQUID_ERROR, "%d %s", httpStatus, "Access Denied"); |
76cdc28d | 1161 | } else { |
c70281f8 | 1162 | MemBuf *content = BuildContent(); |
ec80c740 | 1163 | rep->setHeaders(httpStatus, NULL, "text/html;charset=utf-8", content->contentSize(), 0, -1); |
62e76326 | 1164 | /* |
1165 | * include some information for downstream caches. Implicit | |
1166 | * replaceable content. This isn't quite sufficient. xerrno is not | |
1167 | * necessarily meaningful to another system, so we really should | |
1168 | * expand it. Additionally, we should identify ourselves. Someone | |
1169 | * might want to know. Someone _will_ want to know OTOH, the first | |
1170 | * X-CACHE-MISS entry should tell us who. | |
1171 | */ | |
c70281f8 | 1172 | httpHeaderPutStrf(&rep->header, HDR_X_SQUID_ERROR, "%s %d", name, xerrno); |
ccb24616 | 1173 | |
5bf33a09 AJ |
1174 | #if USE_ERR_LOCALES |
1175 | /* | |
1176 | * If error page auto-negotiate is enabled in any way, send the Vary. | |
1177 | * RFC 2616 section 13.6 and 14.44 says MAY and SHOULD do this. | |
1178 | * We have even better reasons though: | |
1179 | * see http://wiki.squid-cache.org/KnowledgeBase/VaryNotCaching | |
1180 | */ | |
9e008dda | 1181 | if (!Config.errorDirectory) { |
5bf33a09 | 1182 | /* We 'negotiated' this ONLY from the Accept-Language. */ |
70d1b64c AJ |
1183 | rep->header.delById(HDR_VARY); |
1184 | rep->header.putStr(HDR_VARY, "Accept-Language"); | |
5bf33a09 AJ |
1185 | } |
1186 | ||
1187 | /* add the Content-Language header according to RFC section 14.12 */ | |
9e008dda | 1188 | if (err_language) { |
70d1b64c | 1189 | rep->header.putStr(HDR_CONTENT_LANGUAGE, err_language); |
9e008dda | 1190 | } else |
5bf33a09 AJ |
1191 | #endif /* USE_ERROR_LOCALES */ |
1192 | { | |
ccb24616 | 1193 | /* default templates are in English */ |
5bf33a09 | 1194 | /* language is known unless error_directory override used */ |
9e008dda | 1195 | if (!Config.errorDirectory) |
70d1b64c | 1196 | rep->header.putStr(HDR_CONTENT_LANGUAGE, "en"); |
ccb24616 AJ |
1197 | } |
1198 | ||
0521f8be | 1199 | rep->body.setMb(content); |
032785bf | 1200 | /* do not memBufClean() or delete the content, it was absorbed by httpBody */ |
76cdc28d | 1201 | } |
62e76326 | 1202 | |
7a957a93 AR |
1203 | // Make sure error codes get back to the client side for logging and |
1204 | // error tracking. | |
129fe2a1 CT |
1205 | if (request) { |
1206 | int edc = ERR_DETAIL_NONE; // error detail code | |
cb4f4424 | 1207 | #if USE_OPENSSL |
129fe2a1 CT |
1208 | if (detail) |
1209 | edc = detail->errorNo(); | |
1210 | else | |
1211 | #endif | |
1212 | if (detailCode) | |
1213 | edc = detailCode; | |
1214 | else | |
1215 | edc = xerrno; | |
1216 | request->detailError(type, edc); | |
1217 | } | |
1218 | ||
cb69b4c7 | 1219 | return rep; |
1220 | } | |
1221 | ||
c70281f8 AJ |
1222 | MemBuf * |
1223 | ErrorState::BuildContent() | |
cb69b4c7 | 1224 | { |
43000484 | 1225 | const char *m = NULL; |
43000484 | 1226 | |
c70281f8 | 1227 | assert(page_id > ERR_NONE && page_id < error_page_count); |
43000484 AJ |
1228 | |
1229 | #if USE_ERR_LOCALES | |
8ff2520a | 1230 | ErrorPageFile *localeTmpl = NULL; |
43000484 AJ |
1231 | |
1232 | /** error_directory option in squid.conf overrides translations. | |
30b78ef1 | 1233 | * Custom errors are always found either in error_directory or the templates directory. |
43000484 AJ |
1234 | * Otherwise locate the Accept-Language header |
1235 | */ | |
02259ff8 CT |
1236 | if (!Config.errorDirectory && page_id < ERR_MAX) { |
1237 | if (err_language && err_language != Config.errorDefaultLanguage) | |
1238 | safe_free(err_language); | |
1239 | ||
8ff2520a | 1240 | localeTmpl = new ErrorPageFile(err_type_str[page_id], static_cast<err_type>(page_id)); |
02259ff8 CT |
1241 | if (localeTmpl->loadFor(request)) { |
1242 | m = localeTmpl->text(); | |
1243 | assert(localeTmpl->language()); | |
1244 | err_language = xstrdup(localeTmpl->language()); | |
43000484 AJ |
1245 | } |
1246 | } | |
1247 | #endif /* USE_ERR_LOCALES */ | |
1248 | ||
1249 | /** \par | |
1250 | * If client-specific error templates are not enabled or available. | |
1251 | * fall back to the old style squid.conf settings. | |
1252 | */ | |
9e008dda | 1253 | if (!m) { |
43000484 | 1254 | m = error_text[page_id]; |
5bf33a09 | 1255 | #if USE_ERR_LOCALES |
9e008dda | 1256 | if (!Config.errorDirectory) |
5bf33a09 AJ |
1257 | err_language = Config.errorDefaultLanguage; |
1258 | #endif | |
58d4b38b | 1259 | debugs(4, 2, HERE << "No existing error page language negotiated for " << errorPageName(page_id) << ". Using default error file."); |
43000484 AJ |
1260 | } |
1261 | ||
6551ae9d | 1262 | MemBuf *result = ConvertText(m, true); |
960cb599 | 1263 | #if USE_ERR_LOCALES |
02259ff8 CT |
1264 | if (localeTmpl) |
1265 | delete localeTmpl; | |
960cb599 | 1266 | #endif |
6551ae9d | 1267 | return result; |
4d16918e CT |
1268 | } |
1269 | ||
1270 | MemBuf *ErrorState::ConvertText(const char *text, bool allowRecursion) | |
1271 | { | |
1272 | MemBuf *content = new MemBuf; | |
1273 | const char *p; | |
1274 | const char *m = text; | |
1d803566 | 1275 | assert(m); |
43000484 | 1276 | content->init(); |
62e76326 | 1277 | |
cb69b4c7 | 1278 | while ((p = strchr(m, '%'))) { |
f53969cc SM |
1279 | content->append(m, p - m); /* copy */ |
1280 | const char *t = Convert(*++p, false, allowRecursion); /* convert */ | |
4391cd15 | 1281 | content->appendf("%s", t); /* copy */ |
f53969cc | 1282 | m = p + 1; /* advance */ |
cb69b4c7 | 1283 | } |
62e76326 | 1284 | |
1d803566 | 1285 | if (*m) |
4391cd15 | 1286 | content->appendf("%s", m); /* copy tail */ |
62e76326 | 1287 | |
857e4512 AJ |
1288 | content->terminate(); |
1289 | ||
032785bf | 1290 | assert((size_t)content->contentSize() == strlen(content->content())); |
62e76326 | 1291 | |
cb69b4c7 | 1292 | return content; |
1293 | } | |
f53969cc | 1294 |