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