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