]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/errorpage.cc
4 * DEBUG: section 04 Error Generation
5 * AUTHOR: Duane Wessels
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
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.
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.
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.
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.
35 #include "comm/Write.h"
36 #include "errorpage.h"
37 #include "auth/UserRequest.h"
38 #include "SquidTime.h"
40 #include "html_quote.h"
41 #include "HttpReply.h"
42 #include "HttpRequest.h"
43 #include "MemObject.h"
47 #include "URLScheme.h"
49 #include "err_detail_type.h"
52 \defgroup ErrorPageInternal Error Page Internals
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)
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
67 #define DEFAULT_SQUID_ERROR_DIR DEFAULT_SQUID_DATA_DIR"/errors"
70 /// \ingroup ErrorPageInternal
71 CBDATA_CLASS_INIT(ErrorState
);
75 /// \ingroup ErrorPageInternal
79 } ErrorDynamicPageInfo
;
81 /* local constant and vars */
84 \ingroup ErrorPageInternal
86 \note hard coded error messages are not appended with %S
87 * automagically to give you more control on the format
90 int type
; /* and page_id */
100 "<div id=\"footer\">\n"
101 "Generated %T by %h (%s)\n"
111 /// \ingroup ErrorPageInternal
112 static Vector
<ErrorDynamicPageInfo
*> ErrorDynamicPages
;
114 /* local prototypes */
116 /// \ingroup ErrorPageInternal
117 static const int error_hard_text_count
= sizeof(error_hard_text
) / sizeof(*error_hard_text
);
119 /// \ingroup ErrorPageInternal
120 static char **error_text
= NULL
;
122 /// \ingroup ErrorPageInternal
123 static int error_page_count
= 0;
125 /// \ingroup ErrorPageInternal
126 static MemBuf error_stylesheet
;
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
;
136 /// \ingroup ErrorPageInternal
137 err_type
&operator++ (err_type
&anErr
)
139 int tmp
= (int)anErr
;
140 anErr
= (err_type
)(++tmp
);
144 /// \ingroup ErrorPageInternal
145 int operator - (err_type
const &anErr
, err_type
const &anErr2
)
147 return (int)anErr
- (int)anErr2
;
151 errorInitialize(void)
155 error_page_count
= ERR_MAX
+ ErrorDynamicPages
.size();
156 error_text
= static_cast<char **>(xcalloc(error_page_count
, sizeof(char *)));
158 for (i
= ERR_NONE
, ++i
; i
< error_page_count
; ++i
) {
159 safe_free(error_text
[i
]);
161 if ((text
= errorFindHardText(i
))) {
163 * Index any hard-coded error text into defaults.
165 error_text
[i
] = xstrdup(text
);
167 } else if (i
< ERR_MAX
) {
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)
173 error_text
[i
] = errorLoadText(err_type_str
[i
]);
177 * Index any unknown file names used by deny_info.
179 ErrorDynamicPageInfo
*info
= ErrorDynamicPages
.items
[i
- ERR_MAX
];
180 assert(info
&& info
->id
== i
&& info
->page_name
);
182 if (strchr(info
->page_name
, ':') == NULL
) {
183 /** But only if they are not redirection URL. */
184 error_text
[i
] = errorLoadText(info
->page_name
);
189 error_stylesheet
.reset();
191 // look for and load stylesheet into global MemBuf for it.
192 if (Config
.errorStylesheet
) {
193 char *temp
= errorTryLoadText(Config
.errorStylesheet
,NULL
);
195 error_stylesheet
.Printf("%s",temp
);
207 for (i
= ERR_NONE
+ 1; i
< error_page_count
; i
++)
208 safe_free(error_text
[i
]);
210 safe_free(error_text
);
213 while (ErrorDynamicPages
.size())
214 errorDynamicPageInfoDestroy(ErrorDynamicPages
.pop_back());
216 error_page_count
= 0;
219 /// \ingroup ErrorPageInternal
221 errorFindHardText(err_type type
)
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
;
233 * \ingroup ErrorPageInternal
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
241 errorLoadText(const char *page_name
)
245 /** test error_directory configured location */
246 if (Config
.errorDirectory
)
247 text
= errorTryLoadText(page_name
, Config
.errorDirectory
);
250 /** test error_default_language location */
251 if (!text
&& Config
.errorDefaultLanguage
) {
253 snprintf(dir
,256,"%s/%s", DEFAULT_SQUID_ERROR_DIR
, Config
.errorDefaultLanguage
);
254 text
= errorTryLoadText(page_name
, dir
);
256 debugs(1, DBG_CRITICAL
, "Unable to load default error language files. Reset to backups.");
261 /* test default location if failed (templates == English translation base templates) */
263 text
= errorTryLoadText(page_name
, DEFAULT_SQUID_ERROR_DIR
"/templates");
266 /* giving up if failed */
268 fatal("failed to find or read error text file.");
273 /// \ingroup ErrorPageInternal
275 errorTryLoadText(const char *page_name
, const char *dir
, bool silent
)
278 char path
[MAXPATHLEN
];
284 // maybe received compound parts, maybe an absolute page_name and no dir
286 snprintf(path
, sizeof(path
), "%s/%s", dir
, page_name
);
288 snprintf(path
, sizeof(path
), "%s", page_name
);
290 fd
= file_open(path
, O_RDONLY
| O_TEXT
);
293 /* with dynamic locale negotiation we may see some failures before a success. */
295 debugs(4, DBG_CRITICAL
, HERE
<< "'" << path
<< "': " << xstrerror());
301 while ((len
= FD_READ_METHOD(fd
, buf
, sizeof(buf
))) > 0) {
302 textbuf
.append(buf
, len
);
306 debugs(4, DBG_CRITICAL
, HERE
<< "failed to fully read: '" << path
<< "': " << xstrerror());
311 /* Shrink memory size down to exact size. MemBuf has a tencendy
312 * to be rather large..
314 text
= xstrdup(textbuf
.buf
);
321 /// \ingroup ErrorPageInternal
322 static ErrorDynamicPageInfo
*
323 errorDynamicPageInfoCreate(int id
, const char *page_name
)
325 ErrorDynamicPageInfo
*info
= new ErrorDynamicPageInfo
;
327 info
->page_name
= xstrdup(page_name
);
331 /// \ingroup ErrorPageInternal
333 errorDynamicPageInfoDestroy(ErrorDynamicPageInfo
* info
)
336 safe_free(info
->page_name
);
340 /// \ingroup ErrorPageInternal
342 errorPageId(const char *page_name
)
344 for (int i
= 0; i
< ERR_MAX
; i
++) {
345 if (strcmp(err_type_str
[i
], page_name
) == 0)
349 for (size_t j
= 0; j
< ErrorDynamicPages
.size(); j
++) {
350 if (strcmp(ErrorDynamicPages
.items
[j
]->page_name
, page_name
) == 0)
358 errorReservePageId(const char *page_name
)
360 ErrorDynamicPageInfo
*info
;
361 int id
= errorPageId(page_name
);
363 if (id
== ERR_NONE
) {
364 info
= errorDynamicPageInfoCreate(ERR_MAX
+ ErrorDynamicPages
.size(), page_name
);
365 ErrorDynamicPages
.push_back(info
);
372 /// \ingroup ErrorPageInternal
374 errorPageName(int pageId
)
376 if (pageId
>= ERR_NONE
&& pageId
< ERR_MAX
) /* common case */
377 return err_type_str
[pageId
];
379 if (pageId
>= ERR_MAX
&& pageId
- ERR_MAX
< (ssize_t
)ErrorDynamicPages
.size())
380 return ErrorDynamicPages
.items
[pageId
- ERR_MAX
]->page_name
;
382 return "ERR_UNKNOWN"; /* should not happen */
386 errorCon(err_type type
, http_status status
, HttpRequest
* request
)
388 ErrorState
*err
= new ErrorState
;
389 err
->page_id
= type
; /* has to be reset manually if needed */
390 err
->err_language
= NULL
;
392 err
->httpStatus
= status
;
394 if (request
!= NULL
) {
395 err
->request
= HTTPMSGLOCK(request
);
396 err
->src_addr
= request
->client_addr
;
397 request
->detailError(type
, ERR_DETAIL_NONE
);
404 errorAppendEntry(StoreEntry
* entry
, ErrorState
* err
)
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
);
412 if (entry
->store_status
!= STORE_PENDING
) {
413 debugs(4, 2, "Skipping error page due to store_status: " << entry
->store_status
);
415 * If the entry is not STORE_PENDING, then no clients
416 * care about it, and we don't need to generate an
419 assert(EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
420 assert(entry
->mem_obj
->nclients
== 0);
425 if (err
->page_id
== TCP_RESET
) {
427 debugs(4, 2, "RSTing this reply");
428 err
->request
->flags
.setResetTCP();
434 entry
->replaceHttpReply( err
->BuildHttpReply() );
435 EBIT_CLR(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
438 entry
->negativeCache();
439 entry
->releaseRequest();
445 errorSend(int fd
, ErrorState
* err
)
448 debugs(4, 3, "errorSend: FD " << fd
<< ", err=" << err
);
451 * ugh, this is how we make sure error codes get back to
452 * the client side for logging and error tracking.
456 err
->request
->detailError(err
->type
, err
->xerrno
);
458 /* moved in front of errorBuildBuf @?@ */
459 err
->flags
.flag_cbdata
= 1;
461 rep
= err
->BuildHttpReply();
463 MemBuf
*mb
= rep
->pack();
464 AsyncCall::Pointer call
= commCbCall(78, 5, "errorSendComplete",
465 CommIoCbPtrFun(&errorSendComplete
, err
));
466 Comm::Write(fd
, mb
, call
);
473 \ingroup ErrorPageAPI
475 * Called by commHandleWrite() after data has been written
476 * to the client socket.
478 \note If there is a callback, the callback is responsible for
479 * closing the FD, otherwise we do it ourselves.
482 errorSendComplete(int fd
, char *bufnotused
, size_t size
, comm_err_t errflag
, int xerrno
, void *data
)
484 ErrorState
*err
= static_cast<ErrorState
*>(data
);
485 debugs(4, 3, "errorSendComplete: FD " << fd
<< ", size=" << size
);
487 if (errflag
!= COMM_ERR_CLOSING
) {
489 debugs(4, 3, "errorSendComplete: callback");
490 err
->callback(fd
, err
->callback_data
, size
);
493 debugs(4, 3, "errorSendComplete: comm_close");
501 errorStateFree(ErrorState
* err
)
503 HTTPMSGUNLOCK(err
->request
);
504 safe_free(err
->redirect_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
);
513 if (err
->err_language
!= Config
.errorDefaultLanguage
)
515 safe_free(err
->err_language
);
520 ErrorState::Dump(MemBuf
* mb
)
523 const char *p
= NULL
; /* takes priority over mb if set */
524 char ntoabuf
[MAX_IPSTRLEN
];
527 /* email subject line */
528 str
.Printf("CacheErrorInfo - %s", errorPageName(type
));
529 mb
->Printf("?subject=%s", rfc1738_escape_part(str
.buf
));
532 str
.Printf("CacheHost: %s\r\n", getMyHostname());
534 str
.Printf("ErrPage: %s\r\n", errorPageName(type
));
537 str
.Printf("Err: (%d) %s\r\n", xerrno
, strerror(xerrno
));
539 str
.Printf("Err: [none]\r\n");
542 if (auth_user_request
->denyMessage())
543 str
.Printf("Auth ErrMsg: %s\r\n", auth_user_request
->denyMessage());
545 if (dnsError
.size() > 0)
546 str
.Printf("DNS ErrMsg: %s\r\n", dnsError
.termedBuf());
549 str
.Printf("TimeStamp: %s\r\n\r\n", mkrfc1123(squid_curtime
));
552 str
.Printf("ClientIP: %s\r\n", src_addr
.NtoA(ntoabuf
,MAX_IPSTRLEN
));
554 if (request
&& request
->hier
.host
[0] != '\0') {
555 str
.Printf("ServerIP: %s\r\n", request
->hier
.host
);
560 str
.Printf("HTTP Request:\r\n");
562 if (NULL
!= request
) {
564 String urlpath_or_slash
;
566 if (request
->urlpath
.size() != 0)
567 urlpath_or_slash
= request
->urlpath
;
569 urlpath_or_slash
= "/";
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
);
578 } else if (request_hdrs
) {
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
);
596 mb
->Printf("&body=%s", rfc1738_escape_part(str
.buf
));
601 /// \ingroup ErrorPageInternal
602 #define CVT_BUF_SZ 512
605 ErrorState::Convert(char token
, bool building_deny_info_url
)
608 const char *p
= NULL
; /* takes priority over mb if set */
610 int no_urlescape
= 0; /* if true then item is NOT to be further URL-encoded */
611 char ntoabuf
[MAX_IPSTRLEN
];
618 if (request
&& request
->auth_user_request
!= NULL
)
619 p
= request
->auth_user_request
->username();
625 if (building_deny_info_url
) break;
626 p
= request
? ftpUrlWith2f(request
) : "[no URL]";
630 if (building_deny_info_url
) break;
631 p
= errorPageName(type
);
635 mb
.Printf("%d", xerrno
);
640 mb
.Printf("(%d) %s", xerrno
, strerror(xerrno
));
642 mb
.Printf("[No Error]");
646 if (building_deny_info_url
) break;
647 /* FTP REQUEST LINE */
655 if (building_deny_info_url
) break;
664 if (building_deny_info_url
) break;
665 /* FTP SERVER MESSAGE */
667 wordlistCat(ftp
.server_msg
, &mb
);
668 else if (ftp
.listing
) {
669 mb
.append(ftp
.listing
->content(), ftp
.listing
->contentSize());
675 mb
.Printf("%s", getMyHostname());
680 if (request
->hier
.host
[0] != '\0') // if non-empty string.
681 p
= request
->hier
.host
;
683 p
= request
->GetHost();
684 } else if (!building_deny_info_url
)
685 p
= "[unknown host]";
689 mb
.Printf("%s", src_addr
.NtoA(ntoabuf
,MAX_IPSTRLEN
));
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
)
700 if (building_deny_info_url
) break;
701 mb
.append(error_stylesheet
.content(), error_stylesheet
.contentSize());
706 if (building_deny_info_url
) break;
707 if (Config
.errHtmlText
) {
708 mb
.Printf("%s", Config
.errHtmlText
);
711 p
= "[not available]";
715 if (building_deny_info_url
) break;
716 p
= auth_user_request
->denyMessage("[not available]");
721 p
= RequestMethodStr(request
->method
);
722 else if (!building_deny_info_url
)
723 p
= "[unknown method]";
727 p
= request
? request
->extacl_message
.termedBuf() : external_acl_message
;
728 if (!p
&& !building_deny_info_url
)
729 p
= "[not available]";
734 mb
.Printf("%d", (int) request
->port
);
735 } else if (!building_deny_info_url
) {
736 p
= "[unknown port]";
742 p
= ProtocolStr
[request
->protocol
];
743 } else if (!building_deny_info_url
) {
744 p
= "[unknown protocol]";
749 if (building_deny_info_url
) {
750 p
= (request
->urlpath
.size() != 0 ? request
->urlpath
.termedBuf() : "/");
753 if (NULL
!= request
) {
755 String urlpath_or_slash
;
757 if (request
->urlpath
.size() != 0)
758 urlpath_or_slash
= request
->urlpath
;
760 urlpath_or_slash
= "/";
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
);
769 } else if (request_hdrs
) {
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");
782 p
= visible_appname_string
;
786 if (building_deny_info_url
) {
787 p
= visible_appname_string
;
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());
801 /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */
807 mb
.Printf("%s", mkhttpdlogtime(&squid_curtime
));
811 mb
.Printf("%s", mkrfc1123(squid_curtime
));
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 '*' */
818 p
= urlCanonicalFakeHttps(request
);
821 else if (!building_deny_info_url
)
827 p
= urlCanonical(request
);
830 else if (!building_deny_info_url
)
835 if (Config
.adminEmail
)
836 mb
.Printf("%s", Config
.adminEmail
);
837 else if (!building_deny_info_url
)
842 if (building_deny_info_url
) break;
843 if (Config
.adminEmail
&& Config
.onoff
.emailErrData
)
849 if (building_deny_info_url
) break;
850 if (dnsError
.size() > 0)
851 p
= dnsError
.termedBuf();
852 else if (ftp
.cwd_msg
)
859 if (building_deny_info_url
) break;
871 mb
.Printf("%%%c", token
);
877 p
= mb
.buf
; /* do not use mb after this assignment! */
881 debugs(4, 3, "errorConvert: %%" << token
<< " --> '" << p
<< "'" );
886 if (building_deny_info_url
&& !no_urlescape
)
887 p
= rfc1738_escape_part(p
);
893 ErrorState::DenyInfoLocation(const char *name
, HttpRequest
*aRequest
, MemBuf
&result
)
895 char const *m
= name
;
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 */
907 result
.Printf("%s", m
); /* copy tail */
909 assert((size_t)result
.contentSize() == strlen(result
.content()));
913 ErrorState::BuildHttpReply()
915 HttpReply
*rep
= new HttpReply
;
916 const char *name
= errorPageName(page_id
);
917 /* no LMT for error pages; error pages expire immediately */
919 if (strchr(name
, ':')) {
921 rep
->setHeaders(HTTP_MOVED_TEMPORARILY
, NULL
, "text/html", 0, 0, -1);
924 MemBuf redirect_location
;
925 redirect_location
.init();
926 DenyInfoLocation(name
, request
, redirect_location
);
927 httpHeaderPutStrf(&rep
->header
, HDR_LOCATION
, "%s", redirect_location
.content() );
930 httpHeaderPutStrf(&rep
->header
, HDR_X_SQUID_ERROR
, "%d %s", httpStatus
, "Access Denied");
932 MemBuf
*content
= BuildContent();
933 rep
->setHeaders(httpStatus
, NULL
, "text/html", content
->contentSize(), 0, -1);
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.
942 httpHeaderPutStrf(&rep
->header
, HDR_X_SQUID_ERROR
, "%s %d", name
, xerrno
);
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
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");
957 /* add the Content-Language header according to RFC section 14.12 */
959 rep
->header
.putStr(HDR_CONTENT_LANGUAGE
, err_language
);
961 #endif /* USE_ERROR_LOCALES */
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");
969 httpBodySet(&rep
->body
, content
);
970 /* do not memBufClean() or delete the content, it was absorbed by httpBody */
977 ErrorState::BuildContent()
979 MemBuf
*content
= new MemBuf
;
980 const char *m
= NULL
;
984 assert(page_id
> ERR_NONE
&& page_id
< error_page_count
);
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
995 if (!Config
.errorDirectory
&& page_id
< ERR_MAX
&& request
&& request
->header
.getList(HDR_ACCEPT_LANGUAGE
, &hdr
) ) {
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
1001 /* prep the directory path string to prevent snprintf ... */
1002 l
= strlen(DEFAULT_SQUID_ERROR_DIR
);
1003 memcpy(dir
, DEFAULT_SQUID_ERROR_DIR
, l
);
1005 reset
= dt
= dir
+ l
;
1007 debugs(4, 6, HERE
<< "Testing Header: '" << hdr
<< "'");
1009 while ( pos
< hdr
.size() ) {
1011 /* skip any initial whitespace. */
1012 while (pos
< hdr
.size() && xisspace(hdr
[pos
])) pos
++;
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.
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
] == '_')
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;
1036 dt
++; // move to next destination byte.
1040 *dt
++ = '\0'; // nul-terminated the filename content string before system use.
1042 debugs(4, 9, HERE
<< "STATE: dt='" << dt
<< "', reset='" << reset
<< "', pos=" << pos
<< ", buf='" << ((pos
< hdr
.size()) ? hdr
.substr(pos
,hdr
.size()) : "") << "'");
1044 /* if we found anything we might use, try it. */
1045 if (*reset
!= '\0' && !invalid_byte
) {
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
;
1056 debugs(4, 6, HERE
<< "Found language '" << reset
<< "', testing for available template in: '" << dir
<< "'");
1058 m
= errorTryLoadText( err_type_str
[page_id
], dir
, false);
1061 /* store the language we found for the Content-Language reply header */
1062 err_language
= xstrdup(reset
);
1064 } else if (Config
.errorLogMissingLanguages
) {
1065 debugs(4, DBG_IMPORTANT
, "WARNING: Error Pages Missing Language: " << reset
);
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.");
1077 dt
= reset
; // reset for next tag testing. we replace the failed name instead of cloning.
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
++;
1084 #endif /* USE_ERR_LOCALES */
1087 * If client-specific error templates are not enabled or available.
1088 * fall back to the old style squid.conf settings.
1091 m
= error_text
[page_id
];
1093 if (!Config
.errorDirectory
)
1094 err_language
= Config
.errorDefaultLanguage
;
1096 debugs(4, 2, HERE
<< "No existing error page language negotiated for " << errorPageName(page_id
) << ". Using default error file.");
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 */
1110 content
->Printf("%s", m
); /* copy tail */
1112 assert((size_t)content
->contentSize() == strlen(content
->content()));