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"
38 #include "auth/UserRequest.h"
40 #include "SquidTime.h"
42 #include "html_quote.h"
43 #include "HttpReply.h"
44 #include "HttpRequest.h"
45 #include "MemObject.h"
49 #include "URLScheme.h"
51 #include "err_detail_type.h"
54 \defgroup ErrorPageInternal Error Page Internals
57 \section Abstract Abstract:
58 * These routines are used to generate error messages to be
59 * sent to clients. The error type is used to select between
60 * the various message formats. (formats are stored in the
61 * Config.errorDirectory)
65 #ifndef DEFAULT_SQUID_ERROR_DIR
66 /** Where to look for errors if config path fails.
67 \note Please use ./configure --datadir=/path instead of patching
69 #define DEFAULT_SQUID_ERROR_DIR DEFAULT_SQUID_DATA_DIR"/errors"
72 /// \ingroup ErrorPageInternal
73 CBDATA_CLASS_INIT(ErrorState
);
77 /// \ingroup ErrorPageInternal
81 http_status page_redirect
;
82 } ErrorDynamicPageInfo
;
84 /* local constant and vars */
87 \ingroup ErrorPageInternal
89 \note hard coded error messages are not appended with %S
90 * automagically to give you more control on the format
93 int type
; /* and page_id */
103 "<div id=\"footer\">\n"
104 "Generated %T by %h (%s)\n"
114 /// \ingroup ErrorPageInternal
115 static Vector
<ErrorDynamicPageInfo
*> ErrorDynamicPages
;
117 /* local prototypes */
119 /// \ingroup ErrorPageInternal
120 static const int error_hard_text_count
= sizeof(error_hard_text
) / sizeof(*error_hard_text
);
122 /// \ingroup ErrorPageInternal
123 static char **error_text
= NULL
;
125 /// \ingroup ErrorPageInternal
126 static int error_page_count
= 0;
128 /// \ingroup ErrorPageInternal
129 static MemBuf error_stylesheet
;
131 static char *errorTryLoadText(const char *page_name
, const char *dir
, bool silent
= false);
132 static char *errorLoadText(const char *page_name
);
133 static const char *errorFindHardText(err_type type
);
134 static ErrorDynamicPageInfo
*errorDynamicPageInfoCreate(int id
, const char *page_name
);
135 static void errorDynamicPageInfoDestroy(ErrorDynamicPageInfo
* info
);
136 static IOCB errorSendComplete
;
139 /// \ingroup ErrorPageInternal
140 err_type
&operator++ (err_type
&anErr
)
142 int tmp
= (int)anErr
;
143 anErr
= (err_type
)(++tmp
);
147 /// \ingroup ErrorPageInternal
148 int operator - (err_type
const &anErr
, err_type
const &anErr2
)
150 return (int)anErr
- (int)anErr2
;
154 errorInitialize(void)
158 error_page_count
= ERR_MAX
+ ErrorDynamicPages
.size();
159 error_text
= static_cast<char **>(xcalloc(error_page_count
, sizeof(char *)));
161 for (i
= ERR_NONE
, ++i
; i
< error_page_count
; ++i
) {
162 safe_free(error_text
[i
]);
164 if ((text
= errorFindHardText(i
))) {
166 * Index any hard-coded error text into defaults.
168 error_text
[i
] = xstrdup(text
);
170 } else if (i
< ERR_MAX
) {
172 * Index precompiled fixed template files from one of two sources:
173 * (a) default language translation directory (error_default_language)
174 * (b) admin specified custom directory (error_directory)
176 error_text
[i
] = errorLoadText(err_type_str
[i
]);
180 * Index any unknown file names used by deny_info.
182 ErrorDynamicPageInfo
*info
= ErrorDynamicPages
.items
[i
- ERR_MAX
];
183 assert(info
&& info
->id
== i
&& info
->page_name
);
185 const char *pg
= info
->page_name
;
186 if (info
->page_redirect
!= HTTP_STATUS_NONE
)
187 pg
= info
->page_name
+4;
189 if (strchr(pg
, ':') == NULL
) {
190 /** But only if they are not redirection URL. */
191 error_text
[i
] = errorLoadText(pg
);
196 error_stylesheet
.reset();
198 // look for and load stylesheet into global MemBuf for it.
199 if (Config
.errorStylesheet
) {
200 char *temp
= errorTryLoadText(Config
.errorStylesheet
,NULL
);
202 error_stylesheet
.Printf("%s",temp
);
214 for (i
= ERR_NONE
+ 1; i
< error_page_count
; i
++)
215 safe_free(error_text
[i
]);
217 safe_free(error_text
);
220 while (ErrorDynamicPages
.size())
221 errorDynamicPageInfoDestroy(ErrorDynamicPages
.pop_back());
223 error_page_count
= 0;
226 /// \ingroup ErrorPageInternal
228 errorFindHardText(err_type type
)
232 for (i
= 0; i
< error_hard_text_count
; i
++)
233 if (error_hard_text
[i
].type
== type
)
234 return error_hard_text
[i
].text
;
240 * \ingroup ErrorPageInternal
242 * Load into the in-memory error text Index a file probably available at:
243 * (a) admin specified custom directory (error_directory)
244 * (b) default language translation directory (error_default_language)
245 * (c) English sub-directory where errors should ALWAYS exist
248 errorLoadText(const char *page_name
)
252 /** test error_directory configured location */
253 if (Config
.errorDirectory
)
254 text
= errorTryLoadText(page_name
, Config
.errorDirectory
);
257 /** test error_default_language location */
258 if (!text
&& Config
.errorDefaultLanguage
) {
260 snprintf(dir
,256,"%s/%s", DEFAULT_SQUID_ERROR_DIR
, Config
.errorDefaultLanguage
);
261 text
= errorTryLoadText(page_name
, dir
);
263 debugs(1, DBG_CRITICAL
, "Unable to load default error language files. Reset to backups.");
268 /* test default location if failed (templates == English translation base templates) */
270 text
= errorTryLoadText(page_name
, DEFAULT_SQUID_ERROR_DIR
"/templates");
273 /* giving up if failed */
275 fatal("failed to find or read error text file.");
280 /// \ingroup ErrorPageInternal
282 errorTryLoadText(const char *page_name
, const char *dir
, bool silent
)
285 char path
[MAXPATHLEN
];
291 // maybe received compound parts, maybe an absolute page_name and no dir
293 snprintf(path
, sizeof(path
), "%s/%s", dir
, page_name
);
295 snprintf(path
, sizeof(path
), "%s", page_name
);
297 fd
= file_open(path
, O_RDONLY
| O_TEXT
);
300 /* with dynamic locale negotiation we may see some failures before a success. */
302 debugs(4, DBG_CRITICAL
, HERE
<< "'" << path
<< "': " << xstrerror());
308 while ((len
= FD_READ_METHOD(fd
, buf
, sizeof(buf
))) > 0) {
309 textbuf
.append(buf
, len
);
313 debugs(4, DBG_CRITICAL
, HERE
<< "failed to fully read: '" << path
<< "': " << xstrerror());
318 /* Shrink memory size down to exact size. MemBuf has a tencendy
319 * to be rather large..
321 text
= xstrdup(textbuf
.buf
);
328 /// \ingroup ErrorPageInternal
329 static ErrorDynamicPageInfo
*
330 errorDynamicPageInfoCreate(int id
, const char *page_name
)
332 ErrorDynamicPageInfo
*info
= new ErrorDynamicPageInfo
;
334 info
->page_name
= xstrdup(page_name
);
335 info
->page_redirect
= static_cast<http_status
>(atoi(page_name
));
337 /* WARNING on redirection status:
338 * 2xx are permitted, but not documented officially.
339 * - might be useful for serving static files (PAC etc) in special cases
340 * 3xx require a URL suitable for Location: header.
341 * - the current design does not allow for a Location: URI as well as a local file template
342 * although this possibility is explicitly permitted in the specs.
343 * 4xx-5xx require a local file template.
344 * - sending Location: on these codes with no body is invalid by the specs.
345 * - current result is Squid crashing or XSS problems as dynamic deny_info load random disk files.
346 * - a future redesign of the file loading may result in loading remote objects sent inline as local body.
348 if (info
->page_redirect
== HTTP_STATUS_NONE
)
349 ; // special case okay.
350 else if (info
->page_redirect
< 200 || info
->page_redirect
> 599) {
352 debugs(0, DBG_CRITICAL
, "FATAL: status " << info
->page_redirect
<< " is not valid on '" << page_name
<< "'");
354 } else if ( /* >= 200 && */ info
->page_redirect
< 300 && strchr(&(page_name
[4]), ':')) {
355 // 2xx require a local template file
356 debugs(0, DBG_CRITICAL
, "FATAL: status " << info
->page_redirect
<< " is not valid on '" << page_name
<< "'");
358 } else if (/* >= 300 && */ info
->page_redirect
<= 399 && !strchr(&(page_name
[4]), ':')) {
359 // 3xx require an absolute URL
360 debugs(0, DBG_CRITICAL
, "FATAL: status " << info
->page_redirect
<< " is not valid on '" << page_name
<< "'");
362 } else if (info
->page_redirect
>= 400 /* && <= 599 */ && strchr(&(page_name
[4]), ':')) {
363 // 4xx/5xx require a local template file
364 debugs(0, DBG_CRITICAL
, "FATAL: status " << info
->page_redirect
<< " is not valid on '" << page_name
<< "'");
372 /// \ingroup ErrorPageInternal
374 errorDynamicPageInfoDestroy(ErrorDynamicPageInfo
* info
)
377 safe_free(info
->page_name
);
381 /// \ingroup ErrorPageInternal
383 errorPageId(const char *page_name
)
385 for (int i
= 0; i
< ERR_MAX
; i
++) {
386 if (strcmp(err_type_str
[i
], page_name
) == 0)
390 for (size_t j
= 0; j
< ErrorDynamicPages
.size(); j
++) {
391 if (strcmp(ErrorDynamicPages
.items
[j
]->page_name
, page_name
) == 0)
399 errorReservePageId(const char *page_name
)
401 ErrorDynamicPageInfo
*info
;
402 int id
= errorPageId(page_name
);
404 if (id
== ERR_NONE
) {
405 info
= errorDynamicPageInfoCreate(ERR_MAX
+ ErrorDynamicPages
.size(), page_name
);
406 ErrorDynamicPages
.push_back(info
);
413 /// \ingroup ErrorPageInternal
415 errorPageName(int pageId
)
417 if (pageId
>= ERR_NONE
&& pageId
< ERR_MAX
) /* common case */
418 return err_type_str
[pageId
];
420 if (pageId
>= ERR_MAX
&& pageId
- ERR_MAX
< (ssize_t
)ErrorDynamicPages
.size())
421 return ErrorDynamicPages
.items
[pageId
- ERR_MAX
]->page_name
;
423 return "ERR_UNKNOWN"; /* should not happen */
427 errorCon(err_type type
, http_status status
, HttpRequest
* request
)
429 ErrorState
*err
= new ErrorState
;
430 err
->page_id
= type
; /* has to be reset manually if needed */
431 err
->err_language
= NULL
;
433 err
->httpStatus
= status
;
434 if (err
->page_id
>= ERR_MAX
&& ErrorDynamicPages
.items
[err
->page_id
- ERR_MAX
]->page_redirect
!= HTTP_STATUS_NONE
)
435 err
->httpStatus
= ErrorDynamicPages
.items
[err
->page_id
- ERR_MAX
]->page_redirect
;
437 if (request
!= NULL
) {
438 err
->request
= HTTPMSGLOCK(request
);
439 err
->src_addr
= request
->client_addr
;
440 request
->detailError(type
, ERR_DETAIL_NONE
);
447 errorAppendEntry(StoreEntry
* entry
, ErrorState
* err
)
449 assert(entry
->mem_obj
!= NULL
);
450 assert (entry
->isEmpty());
451 debugs(4, 4, "Creating an error page for entry " << entry
<<
452 " with errorstate " << err
<<
453 " page id " << err
->page_id
);
455 if (entry
->store_status
!= STORE_PENDING
) {
456 debugs(4, 2, "Skipping error page due to store_status: " << entry
->store_status
);
458 * If the entry is not STORE_PENDING, then no clients
459 * care about it, and we don't need to generate an
462 assert(EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
463 assert(entry
->mem_obj
->nclients
== 0);
468 if (err
->page_id
== TCP_RESET
) {
470 debugs(4, 2, "RSTing this reply");
471 err
->request
->flags
.setResetTCP();
477 entry
->replaceHttpReply( err
->BuildHttpReply() );
478 EBIT_CLR(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
481 entry
->negativeCache();
482 entry
->releaseRequest();
488 errorSend(int fd
, ErrorState
* err
)
491 debugs(4, 3, "errorSend: FD " << fd
<< ", err=" << err
);
494 * ugh, this is how we make sure error codes get back to
495 * the client side for logging and error tracking.
499 err
->request
->detailError(err
->type
, err
->xerrno
);
501 /* moved in front of errorBuildBuf @?@ */
502 err
->flags
.flag_cbdata
= 1;
504 rep
= err
->BuildHttpReply();
506 MemBuf
*mb
= rep
->pack();
507 AsyncCall::Pointer call
= commCbCall(78, 5, "errorSendComplete",
508 CommIoCbPtrFun(&errorSendComplete
, err
));
509 Comm::Write(fd
, mb
, call
);
516 \ingroup ErrorPageAPI
518 * Called by commHandleWrite() after data has been written
519 * to the client socket.
521 \note If there is a callback, the callback is responsible for
522 * closing the FD, otherwise we do it ourselves.
525 errorSendComplete(int fd
, char *bufnotused
, size_t size
, comm_err_t errflag
, int xerrno
, void *data
)
527 ErrorState
*err
= static_cast<ErrorState
*>(data
);
528 debugs(4, 3, "errorSendComplete: FD " << fd
<< ", size=" << size
);
530 if (errflag
!= COMM_ERR_CLOSING
) {
532 debugs(4, 3, "errorSendComplete: callback");
533 err
->callback(fd
, err
->callback_data
, size
);
536 debugs(4, 3, "errorSendComplete: comm_close");
544 errorStateFree(ErrorState
* err
)
546 HTTPMSGUNLOCK(err
->request
);
547 safe_free(err
->redirect_url
);
549 safe_free(err
->request_hdrs
);
550 wordlistDestroy(&err
->ftp
.server_msg
);
551 safe_free(err
->ftp
.request
);
552 safe_free(err
->ftp
.reply
);
554 err
->auth_user_request
= NULL
;
556 safe_free(err
->err_msg
);
558 if (err
->err_language
!= Config
.errorDefaultLanguage
)
560 safe_free(err
->err_language
);
568 ErrorState::Dump(MemBuf
* mb
)
571 char ntoabuf
[MAX_IPSTRLEN
];
574 /* email subject line */
575 str
.Printf("CacheErrorInfo - %s", errorPageName(type
));
576 mb
->Printf("?subject=%s", rfc1738_escape_part(str
.buf
));
579 str
.Printf("CacheHost: %s\r\n", getMyHostname());
581 str
.Printf("ErrPage: %s\r\n", errorPageName(type
));
584 str
.Printf("Err: (%d) %s\r\n", xerrno
, strerror(xerrno
));
586 str
.Printf("Err: [none]\r\n");
589 if (auth_user_request
->denyMessage())
590 str
.Printf("Auth ErrMsg: %s\r\n", auth_user_request
->denyMessage());
592 if (dnsError
.size() > 0)
593 str
.Printf("DNS ErrMsg: %s\r\n", dnsError
.termedBuf());
596 str
.Printf("TimeStamp: %s\r\n\r\n", mkrfc1123(squid_curtime
));
599 str
.Printf("ClientIP: %s\r\n", src_addr
.NtoA(ntoabuf
,MAX_IPSTRLEN
));
601 if (request
&& request
->hier
.host
[0] != '\0') {
602 str
.Printf("ServerIP: %s\r\n", request
->hier
.host
);
607 str
.Printf("HTTP Request:\r\n");
609 if (NULL
!= request
) {
611 String urlpath_or_slash
;
613 if (request
->urlpath
.size() != 0)
614 urlpath_or_slash
= request
->urlpath
;
616 urlpath_or_slash
= "/";
618 str
.Printf("%s " SQUIDSTRINGPH
" HTTP/%d.%d\n",
619 RequestMethodStr(request
->method
),
620 SQUIDSTRINGPRINT(urlpath_or_slash
),
621 request
->http_ver
.major
, request
->http_ver
.minor
);
622 packerToMemInit(&pck
, &str
);
623 request
->header
.packInto(&pck
);
631 str
.Printf("FTP Request: %s\r\n", ftp
.request
);
632 str
.Printf("FTP Reply: %s\r\n", ftp
.reply
);
633 str
.Printf("FTP Msg: ");
634 wordlistCat(ftp
.server_msg
, &str
);
639 mb
->Printf("&body=%s", rfc1738_escape_part(str
.buf
));
644 /// \ingroup ErrorPageInternal
645 #define CVT_BUF_SZ 512
648 ErrorState::Convert(char token
, bool building_deny_info_url
, bool allowRecursion
)
651 const char *p
= NULL
; /* takes priority over mb if set */
653 int no_urlescape
= 0; /* if true then item is NOT to be further URL-encoded */
654 char ntoabuf
[MAX_IPSTRLEN
];
662 if (request
&& request
->auth_user_request
!= NULL
)
663 p
= request
->auth_user_request
->username();
670 mb
.Printf("%d", getMyPort());
674 if (building_deny_info_url
) break;
675 p
= request
? ftpUrlWith2f(request
) : "[no URL]";
679 if (building_deny_info_url
) break;
680 p
= errorPageName(type
);
685 p
= "%D"; // if recursion is not allowed, do not convert
687 // currently only SSL error details implemented
689 const String
&errDetail
= detail
->toString();
690 if (errDetail
.defined()) {
691 MemBuf
*detail_mb
= ConvertText(errDetail
.termedBuf(), false);
692 mb
.append(detail_mb
->content(), detail_mb
->contentSize());
698 if (!mb
.contentSize())
699 mb
.Printf("[No Error Detail]");
703 mb
.Printf("%d", xerrno
);
708 mb
.Printf("(%d) %s", xerrno
, strerror(xerrno
));
710 mb
.Printf("[No Error]");
714 if (building_deny_info_url
) break;
715 /* FTP REQUEST LINE */
723 if (building_deny_info_url
) break;
732 if (building_deny_info_url
) break;
733 /* FTP SERVER RESPONSE */
735 mb
.append(ftp
.listing
->content(), ftp
.listing
->contentSize());
737 } else if (ftp
.server_msg
) {
738 wordlistCat(ftp
.server_msg
, &mb
);
743 mb
.Printf("%s", getMyHostname());
748 if (request
->hier
.host
[0] != '\0') // if non-empty string.
749 p
= request
->hier
.host
;
751 p
= request
->GetHost();
752 } else if (!building_deny_info_url
)
753 p
= "[unknown host]";
757 mb
.Printf("%s", src_addr
.NtoA(ntoabuf
,MAX_IPSTRLEN
));
761 if (request
&& request
->hier
.host
[0] != '\0') // if non-empty string
762 mb
.Printf("%s", request
->hier
.host
);
763 else if (!building_deny_info_url
)
768 if (building_deny_info_url
) break;
769 mb
.append(error_stylesheet
.content(), error_stylesheet
.contentSize());
774 if (building_deny_info_url
) break;
775 if (Config
.errHtmlText
) {
776 mb
.Printf("%s", Config
.errHtmlText
);
779 p
= "[not available]";
783 if (building_deny_info_url
) break;
785 p
= auth_user_request
->denyMessage("[not available]");
793 p
= RequestMethodStr(request
->method
);
794 else if (!building_deny_info_url
)
795 p
= "[unknown method]";
799 p
= request
? request
->extacl_message
.termedBuf() : external_acl_message
;
800 if (!p
&& !building_deny_info_url
)
801 p
= "[not available]";
806 mb
.Printf("%d", (int) request
->port
);
807 } else if (!building_deny_info_url
) {
808 p
= "[unknown port]";
814 p
= AnyP::ProtocolType_str
[request
->protocol
];
815 } else if (!building_deny_info_url
) {
816 p
= "[unknown protocol]";
821 if (building_deny_info_url
) {
822 p
= (request
->urlpath
.size() != 0 ? request
->urlpath
.termedBuf() : "/");
825 if (NULL
!= request
) {
827 String urlpath_or_slash
;
829 if (request
->urlpath
.size() != 0)
830 urlpath_or_slash
= request
->urlpath
;
832 urlpath_or_slash
= "/";
834 mb
.Printf("%s " SQUIDSTRINGPH
" HTTP/%d.%d\n",
835 RequestMethodStr(request
->method
),
836 SQUIDSTRINGPRINT(urlpath_or_slash
),
837 request
->http_ver
.major
, request
->http_ver
.minor
);
838 packerToMemInit(&pck
, &mb
);
839 request
->header
.packInto(&pck
);
841 } else if (request_hdrs
) {
849 /* for backward compat we make %s show the full URL. Drop this in some future release. */
850 if (building_deny_info_url
) {
851 p
= request
? urlCanonical(request
) : url
;
852 debugs(0,0, "WARNING: deny_info now accepts coded tags. Use %u to get the full URL instead of %s");
854 p
= visible_appname_string
;
858 if (building_deny_info_url
) {
859 p
= visible_appname_string
;
862 /* signature may contain %-escapes, recursion */
863 if (page_id
!= ERR_SQUID_SIGNATURE
) {
864 const int saved_id
= page_id
;
865 page_id
= ERR_SQUID_SIGNATURE
;
866 MemBuf
*sign_mb
= BuildContent();
867 mb
.Printf("%s", sign_mb
->content());
873 /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */
879 mb
.Printf("%s", Time::FormatHttpd(squid_curtime
));
883 mb
.Printf("%s", mkrfc1123(squid_curtime
));
887 /* Using the fake-https version of canonical so error pages see https:// */
888 /* even when the url-path cannot be shown as more than '*' */
890 p
= urlCanonicalFakeHttps(request
);
893 else if (!building_deny_info_url
)
899 p
= urlCanonical(request
);
902 else if (!building_deny_info_url
)
907 if (Config
.adminEmail
)
908 mb
.Printf("%s", Config
.adminEmail
);
909 else if (!building_deny_info_url
)
914 if (building_deny_info_url
) break;
915 if (Config
.adminEmail
&& Config
.onoff
.emailErrData
)
923 mb
.Printf("%s", detail
->errorName());
926 if (!building_deny_info_url
)
927 p
= "[Unknown Error Code]";
931 if (building_deny_info_url
) break;
932 if (dnsError
.size() > 0)
933 p
= dnsError
.termedBuf();
934 else if (ftp
.cwd_msg
)
941 if (building_deny_info_url
) break;
953 mb
.Printf("%%%c", token
);
959 p
= mb
.buf
; /* do not use mb after this assignment! */
963 debugs(4, 3, "errorConvert: %%" << token
<< " --> '" << p
<< "'" );
968 if (building_deny_info_url
&& !no_urlescape
)
969 p
= rfc1738_escape_part(p
);
975 ErrorState::DenyInfoLocation(const char *name
, HttpRequest
*aRequest
, MemBuf
&result
)
977 char const *m
= name
;
982 m
+= 4; // skip "3xx:"
984 while ((p
= strchr(m
, '%'))) {
985 result
.append(m
, p
- m
); /* copy */
986 t
= Convert(*++p
, true, true); /* convert */
987 result
.Printf("%s", t
); /* copy */
988 m
= p
+ 1; /* advance */
992 result
.Printf("%s", m
); /* copy tail */
994 assert((size_t)result
.contentSize() == strlen(result
.content()));
998 ErrorState::BuildHttpReply()
1000 HttpReply
*rep
= new HttpReply
;
1001 const char *name
= errorPageName(page_id
);
1002 /* no LMT for error pages; error pages expire immediately */
1004 if (name
[0] == '3' || (name
[0] != '4' && name
[0] != '5' && strchr(name
, ':'))) {
1006 http_status status
= HTTP_MOVED_TEMPORARILY
;
1007 // Use configured 3xx reply status if set.
1009 status
= httpStatus
;
1011 // Use 307 for HTTP/1.1 non-GET/HEAD requests.
1012 if (request
->method
!= METHOD_GET
&& request
->method
!= METHOD_HEAD
&& request
->http_ver
>= HttpVersion(1,1))
1013 status
= HTTP_TEMPORARY_REDIRECT
;
1016 rep
->setHeaders(status
, NULL
, "text/html", 0, 0, -1);
1019 MemBuf redirect_location
;
1020 redirect_location
.init();
1021 DenyInfoLocation(name
, request
, redirect_location
);
1022 httpHeaderPutStrf(&rep
->header
, HDR_LOCATION
, "%s", redirect_location
.content() );
1025 httpHeaderPutStrf(&rep
->header
, HDR_X_SQUID_ERROR
, "%d %s", httpStatus
, "Access Denied");
1027 MemBuf
*content
= BuildContent();
1028 rep
->setHeaders(httpStatus
, NULL
, "text/html", content
->contentSize(), 0, -1);
1030 * include some information for downstream caches. Implicit
1031 * replaceable content. This isn't quite sufficient. xerrno is not
1032 * necessarily meaningful to another system, so we really should
1033 * expand it. Additionally, we should identify ourselves. Someone
1034 * might want to know. Someone _will_ want to know OTOH, the first
1035 * X-CACHE-MISS entry should tell us who.
1037 httpHeaderPutStrf(&rep
->header
, HDR_X_SQUID_ERROR
, "%s %d", name
, xerrno
);
1041 * If error page auto-negotiate is enabled in any way, send the Vary.
1042 * RFC 2616 section 13.6 and 14.44 says MAY and SHOULD do this.
1043 * We have even better reasons though:
1044 * see http://wiki.squid-cache.org/KnowledgeBase/VaryNotCaching
1046 if (!Config
.errorDirectory
) {
1047 /* We 'negotiated' this ONLY from the Accept-Language. */
1048 rep
->header
.delById(HDR_VARY
);
1049 rep
->header
.putStr(HDR_VARY
, "Accept-Language");
1052 /* add the Content-Language header according to RFC section 14.12 */
1054 rep
->header
.putStr(HDR_CONTENT_LANGUAGE
, err_language
);
1056 #endif /* USE_ERROR_LOCALES */
1058 /* default templates are in English */
1059 /* language is known unless error_directory override used */
1060 if (!Config
.errorDirectory
)
1061 rep
->header
.putStr(HDR_CONTENT_LANGUAGE
, "en");
1064 httpBodySet(&rep
->body
, content
);
1065 /* do not memBufClean() or delete the content, it was absorbed by httpBody */
1072 ErrorState::BuildContent()
1074 const char *m
= NULL
;
1076 assert(page_id
> ERR_NONE
&& page_id
< error_page_count
);
1082 const char *freePage
= NULL
;
1084 /** error_directory option in squid.conf overrides translations.
1085 * Custom errors are always found either in error_directory or the templates directory.
1086 * Otherwise locate the Accept-Language header
1088 if (!Config
.errorDirectory
&& page_id
< ERR_MAX
&& request
&& request
->header
.getList(HDR_ACCEPT_LANGUAGE
, &hdr
) ) {
1090 size_t pos
= 0; // current parsing position in header string
1091 char *reset
= NULL
; // where to reset the p pointer for each new tag file
1094 /* prep the directory path string to prevent snprintf ... */
1095 l
= strlen(DEFAULT_SQUID_ERROR_DIR
);
1096 memcpy(dir
, DEFAULT_SQUID_ERROR_DIR
, l
);
1098 reset
= dt
= dir
+ l
;
1100 debugs(4, 6, HERE
<< "Testing Header: '" << hdr
<< "'");
1102 while ( pos
< hdr
.size() ) {
1104 /* skip any initial whitespace. */
1105 while (pos
< hdr
.size() && xisspace(hdr
[pos
])) pos
++;
1108 * Header value format:
1109 * - sequence of whitespace delimited tags
1110 * - each tag may suffix with ';'.* which we can ignore.
1111 * - IFF a tag contains only two characters we can wildcard ANY translations matching: <it> '-'? .*
1112 * with preference given to an exact match.
1114 bool invalid_byte
= false;
1115 while (pos
< hdr
.size() && hdr
[pos
] != ';' && hdr
[pos
] != ',' && !xisspace(hdr
[pos
]) && dt
< (dir
+256) ) {
1116 if (!invalid_byte
) {
1117 #if USE_HTTP_VIOLATIONS
1118 // if accepting violations we may as well accept some broken browsers
1119 // which may send us the right code, wrong ISO formatting.
1120 if (hdr
[pos
] == '_')
1124 *dt
= xtolower(hdr
[pos
]);
1125 // valid codes only contain A-Z, hyphen (-) and *
1126 if (*dt
!= '-' && *dt
!= '*' && (*dt
< 'a' || *dt
> 'z') )
1127 invalid_byte
= true;
1129 dt
++; // move to next destination byte.
1133 *dt
++ = '\0'; // nul-terminated the filename content string before system use.
1135 debugs(4, 9, HERE
<< "STATE: dt='" << dt
<< "', reset='" << reset
<< "', pos=" << pos
<< ", buf='" << ((pos
< hdr
.size()) ? hdr
.substr(pos
,hdr
.size()) : "") << "'");
1137 /* if we found anything we might use, try it. */
1138 if (*reset
!= '\0' && !invalid_byte
) {
1140 /* wildcard uses the configured default language */
1141 if (reset
[0] == '*' && reset
[1] == '\0') {
1142 debugs(4, 6, HERE
<< "Found language '" << reset
<< "'. Using configured default.");
1143 m
= error_text
[page_id
];
1144 if (!Config
.errorDirectory
)
1145 err_language
= Config
.errorDefaultLanguage
;
1149 debugs(4, 6, HERE
<< "Found language '" << reset
<< "', testing for available template in: '" << dir
<< "'");
1151 m
= errorTryLoadText( err_type_str
[page_id
], dir
, false);
1154 /* store the language we found for the Content-Language reply header */
1155 err_language
= xstrdup(reset
);
1158 } else if (Config
.errorLogMissingLanguages
) {
1159 debugs(4, DBG_IMPORTANT
, "WARNING: Error Pages Missing Language: " << reset
);
1163 if ( (dt
- reset
) == 2) {
1164 /* TODO glob the error directory for sub-dirs matching: <tag> '-*' */
1165 /* use first result. */
1166 debugs(4,2, HERE
<< "wildcard fallback errors not coded yet.");
1171 dt
= reset
; // reset for next tag testing. we replace the failed name instead of cloning.
1173 // IFF we terminated the tag on whitespace or ';' we need to skip to the next ',' or end of header.
1174 while (pos
< hdr
.size() && hdr
[pos
] != ',') pos
++;
1175 if (hdr
[pos
] == ',') pos
++;
1178 #endif /* USE_ERR_LOCALES */
1181 * If client-specific error templates are not enabled or available.
1182 * fall back to the old style squid.conf settings.
1185 m
= error_text
[page_id
];
1187 if (!Config
.errorDirectory
)
1188 err_language
= Config
.errorDefaultLanguage
;
1190 debugs(4, 2, HERE
<< "No existing error page language negotiated for " << errorPageName(page_id
) << ". Using default error file.");
1193 MemBuf
*result
= ConvertText(m
, true);
1195 safe_free(freePage
);
1201 MemBuf
*ErrorState::ConvertText(const char *text
, bool allowRecursion
)
1203 MemBuf
*content
= new MemBuf
;
1205 const char *m
= text
;
1209 while ((p
= strchr(m
, '%'))) {
1210 content
->append(m
, p
- m
); /* copy */
1211 const char *t
= Convert(*++p
, false, allowRecursion
); /* convert */
1212 content
->Printf("%s", t
); /* copy */
1213 m
= p
+ 1; /* advance */
1217 content
->Printf("%s", m
); /* copy tail */
1219 assert((size_t)content
->contentSize() == strlen(content
->content()));