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