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