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/Connection.h"
36 #include "comm/Write.h"
37 #include "errorpage.h"
39 #include "auth/UserRequest.h"
41 #include "SquidTime.h"
42 #include "ssl/ErrorDetailManager.h"
44 #include "html_quote.h"
45 #include "HttpReply.h"
46 #include "HttpRequest.h"
47 #include "MemObject.h"
51 #include "URLScheme.h"
53 #include "err_detail_type.h"
56 \defgroup ErrorPageInternal Error Page Internals
59 \section Abstract Abstract:
60 * These routines are used to generate error messages to be
61 * sent to clients. The error type is used to select between
62 * the various message formats. (formats are stored in the
63 * Config.errorDirectory)
67 #ifndef DEFAULT_SQUID_ERROR_DIR
68 /** Where to look for errors if config path fails.
69 \note Please use ./configure --datadir=/path instead of patching
71 #define DEFAULT_SQUID_ERROR_DIR DEFAULT_SQUID_DATA_DIR"/errors"
74 /// \ingroup ErrorPageInternal
75 CBDATA_CLASS_INIT(ErrorState
);
79 /// \ingroup ErrorPageInternal
83 http_status page_redirect
;
84 } ErrorDynamicPageInfo
;
86 /* local constant and vars */
89 \ingroup ErrorPageInternal
91 \note hard coded error messages are not appended with %S
92 * automagically to give you more control on the format
95 int type
; /* and page_id */
105 "<div id=\"footer\">\n"
106 "Generated %T by %h (%s)\n"
116 /// \ingroup ErrorPageInternal
117 static Vector
<ErrorDynamicPageInfo
*> ErrorDynamicPages
;
119 /* local prototypes */
121 /// \ingroup ErrorPageInternal
122 static const int error_hard_text_count
= sizeof(error_hard_text
) / sizeof(*error_hard_text
);
124 /// \ingroup ErrorPageInternal
125 static char **error_text
= NULL
;
127 /// \ingroup ErrorPageInternal
128 static int error_page_count
= 0;
130 /// \ingroup ErrorPageInternal
131 static MemBuf error_stylesheet
;
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
;
138 /// \ingroup ErrorPageInternal
139 /// manages an error page template
140 class ErrorPageFile
: public TemplateFile
143 ErrorPageFile(const char *name
): TemplateFile(name
) { textBuf
.init();}
145 /// The template text data read from disk
146 const char *text() { return textBuf
.content(); }
149 /// stores the data read from disk to a local buffer
150 virtual bool parse(const char *buf
, int len
, bool eof
) {
152 textBuf
.append(buf
, len
);
156 MemBuf textBuf
; ///< A buffer to store the error page
159 /// \ingroup ErrorPageInternal
160 err_type
&operator++ (err_type
&anErr
)
162 int tmp
= (int)anErr
;
163 anErr
= (err_type
)(++tmp
);
167 /// \ingroup ErrorPageInternal
168 int operator - (err_type
const &anErr
, err_type
const &anErr2
)
170 return (int)anErr
- (int)anErr2
;
174 errorInitialize(void)
178 error_page_count
= ERR_MAX
+ ErrorDynamicPages
.size();
179 error_text
= static_cast<char **>(xcalloc(error_page_count
, sizeof(char *)));
181 for (i
= ERR_NONE
, ++i
; i
< error_page_count
; ++i
) {
182 safe_free(error_text
[i
]);
184 if ((text
= errorFindHardText(i
))) {
186 * Index any hard-coded error text into defaults.
188 error_text
[i
] = xstrdup(text
);
190 } else if (i
< ERR_MAX
) {
192 * Index precompiled fixed template files from one of two sources:
193 * (a) default language translation directory (error_default_language)
194 * (b) admin specified custom directory (error_directory)
196 ErrorPageFile
errTmpl(err_type_str
[i
]);
197 error_text
[i
] = errTmpl
.loadDefault() ? xstrdup(errTmpl
.text()) : NULL
;
200 * Index any unknown file names used by deny_info.
202 ErrorDynamicPageInfo
*info
= ErrorDynamicPages
.items
[i
- ERR_MAX
];
203 assert(info
&& info
->id
== i
&& info
->page_name
);
205 const char *pg
= info
->page_name
;
206 if (info
->page_redirect
!= HTTP_STATUS_NONE
)
207 pg
= info
->page_name
+4;
209 if (strchr(pg
, ':') == NULL
) {
210 /** But only if they are not redirection URL. */
211 ErrorPageFile
errTmpl(pg
);
212 error_text
[i
] = errTmpl
.loadDefault() ? xstrdup(errTmpl
.text()) : NULL
;
217 error_stylesheet
.reset();
219 // look for and load stylesheet into global MemBuf for it.
220 if (Config
.errorStylesheet
) {
221 ErrorPageFile
tmpl("StylesSheet");
222 tmpl
.loadFromFile(Config
.errorStylesheet
);
223 error_stylesheet
.Printf("%s",tmpl
.text());
227 Ssl::errorDetailInitialize();
237 for (i
= ERR_NONE
+ 1; i
< error_page_count
; i
++)
238 safe_free(error_text
[i
]);
240 safe_free(error_text
);
243 while (ErrorDynamicPages
.size())
244 errorDynamicPageInfoDestroy(ErrorDynamicPages
.pop_back());
246 error_page_count
= 0;
249 Ssl::errorDetailClean();
253 /// \ingroup ErrorPageInternal
255 errorFindHardText(err_type type
)
259 for (i
= 0; i
< error_hard_text_count
; i
++)
260 if (error_hard_text
[i
].type
== type
)
261 return error_hard_text
[i
].text
;
266 TemplateFile::TemplateFile(const char *name
): silent(false), wasLoaded(false), templateName(name
)
272 TemplateFile::loadDefault()
274 if (loaded()) // already loaded?
277 /** test error_directory configured location */
278 if (Config
.errorDirectory
) {
279 char path
[MAXPATHLEN
];
280 snprintf(path
, sizeof(path
), "%s/%s", Config
.errorDirectory
, templateName
.termedBuf());
285 /** test error_default_language location */
286 if (!loaded() && Config
.errorDefaultLanguage
) {
287 if (!tryLoadTemplate(Config
.errorDefaultLanguage
)) {
288 debugs(1, DBG_CRITICAL
, "Unable to load default error language files. Reset to backups.");
293 /* test default location if failed (templates == English translation base templates) */
295 tryLoadTemplate("templates");
298 /* giving up if failed */
300 fatal("failed to find or read error text file.");
306 TemplateFile::tryLoadTemplate(const char *lang
)
310 char path
[MAXPATHLEN
];
311 /* TODO: prep the directory path string to prevent snprintf ... */
312 snprintf(path
, sizeof(path
), "%s/%s/%s",
313 DEFAULT_SQUID_ERROR_DIR
, lang
, templateName
.termedBuf());
314 path
[MAXPATHLEN
-1] = '\0';
316 if (loadFromFile(path
))
320 if ( strlen(lang
) == 2) {
321 /* TODO glob the error directory for sub-dirs matching: <tag> '-*' */
322 /* use first result. */
323 debugs(4,2, HERE
<< "wildcard fallback errors not coded yet.");
331 TemplateFile::loadFromFile(const char *path
)
337 if (loaded()) // already loaded?
340 fd
= file_open(path
, O_RDONLY
| O_TEXT
);
343 /* with dynamic locale negotiation we may see some failures before a success. */
345 debugs(4, DBG_CRITICAL
, HERE
<< "'" << path
<< "': " << xstrerror());
350 while ((len
= FD_READ_METHOD(fd
, buf
, sizeof(buf
))) > 0) {
351 if (!parse(buf
, len
, false)) {
352 debugs(4, DBG_CRITICAL
, HERE
<< " parse error while reading template file: " << path
);
360 debugs(4, DBG_CRITICAL
, HERE
<< "failed to fully read: '" << path
<< "': " << xstrerror());
369 bool strHdrAcptLangGetItem(const String
&hdr
, char *lang
, int langLen
, size_t &pos
)
371 while (pos
< hdr
.size()) {
375 /* skip any initial whitespace. */
376 while (pos
< hdr
.size() && xisspace(hdr
[pos
])) pos
++;
378 // IFF we terminated the tag on whitespace or ';' we need to skip to the next ',' or end of header.
379 while (pos
< hdr
.size() && hdr
[pos
] != ',') pos
++;
380 if (hdr
[pos
] == ',') pos
++;
384 * Header value format:
385 * - sequence of whitespace delimited tags
386 * - each tag may suffix with ';'.* which we can ignore.
387 * - IFF a tag contains only two characters we can wildcard ANY translations matching: <it> '-'? .*
388 * with preference given to an exact match.
390 bool invalid_byte
= false;
391 while (pos
< hdr
.size() && hdr
[pos
] != ';' && hdr
[pos
] != ',' && !xisspace(hdr
[pos
]) && dt
< (lang
+ (langLen
-1)) ) {
393 #if USE_HTTP_VIOLATIONS
394 // if accepting violations we may as well accept some broken browsers
395 // which may send us the right code, wrong ISO formatting.
400 *dt
= xtolower(hdr
[pos
]);
401 // valid codes only contain A-Z, hyphen (-) and *
402 if (*dt
!= '-' && *dt
!= '*' && (*dt
< 'a' || *dt
> 'z') )
405 dt
++; // move to next destination byte.
409 *dt
++ = '\0'; // nul-terminated the filename content string before system use.
411 debugs(4, 9, HERE
<< "STATE: dt='" << dt
<< "', lang='" << lang
<< "', pos=" << pos
<< ", buf='" << ((pos
< hdr
.size()) ? hdr
.substr(pos
,hdr
.size()) : "") << "'");
413 /* if we found anything we might use, try it. */
414 if (*lang
!= '\0' && !invalid_byte
)
421 TemplateFile::loadFor(HttpRequest
*request
)
426 if (loaded()) // already loaded?
429 if (!request
|| !request
->header
.getList(HDR_ACCEPT_LANGUAGE
, &hdr
) )
433 size_t pos
= 0; // current parsing position in header string
435 debugs(4, 6, HERE
<< "Testing Header: '" << hdr
<< "'");
437 while ( strHdrAcptLangGetItem(hdr
, lang
, 256, pos
) ) {
439 /* wildcard uses the configured default language */
440 if (lang
[0] == '*' && lang
[1] == '\0') {
441 debugs(4, 6, HERE
<< "Found language '" << lang
<< "'. Using configured default.");
445 debugs(4, 6, HERE
<< "Found language '" << lang
<< "', testing for available template");
447 if (tryLoadTemplate(lang
)) {
448 /* store the language we found for the Content-Language reply header */
451 } else if (Config
.errorLogMissingLanguages
) {
452 debugs(4, DBG_IMPORTANT
, "WARNING: Error Pages Missing Language: " << lang
);
460 /// \ingroup ErrorPageInternal
461 static ErrorDynamicPageInfo
*
462 errorDynamicPageInfoCreate(int id
, const char *page_name
)
464 ErrorDynamicPageInfo
*info
= new ErrorDynamicPageInfo
;
466 info
->page_name
= xstrdup(page_name
);
467 info
->page_redirect
= static_cast<http_status
>(atoi(page_name
));
469 /* WARNING on redirection status:
470 * 2xx are permitted, but not documented officially.
471 * - might be useful for serving static files (PAC etc) in special cases
472 * 3xx require a URL suitable for Location: header.
473 * - the current design does not allow for a Location: URI as well as a local file template
474 * although this possibility is explicitly permitted in the specs.
475 * 4xx-5xx require a local file template.
476 * - sending Location: on these codes with no body is invalid by the specs.
477 * - current result is Squid crashing or XSS problems as dynamic deny_info load random disk files.
478 * - a future redesign of the file loading may result in loading remote objects sent inline as local body.
480 if (info
->page_redirect
== HTTP_STATUS_NONE
)
481 ; // special case okay.
482 else if (info
->page_redirect
< 200 || info
->page_redirect
> 599) {
484 debugs(0, DBG_CRITICAL
, "FATAL: status " << info
->page_redirect
<< " is not valid on '" << page_name
<< "'");
486 } else if ( /* >= 200 && */ info
->page_redirect
< 300 && strchr(&(page_name
[4]), ':')) {
487 // 2xx require a local template file
488 debugs(0, DBG_CRITICAL
, "FATAL: status " << info
->page_redirect
<< " is not valid on '" << page_name
<< "'");
490 } else if (/* >= 300 && */ info
->page_redirect
<= 399 && !strchr(&(page_name
[4]), ':')) {
491 // 3xx require an absolute URL
492 debugs(0, DBG_CRITICAL
, "FATAL: status " << info
->page_redirect
<< " is not valid on '" << page_name
<< "'");
494 } else if (info
->page_redirect
>= 400 /* && <= 599 */ && strchr(&(page_name
[4]), ':')) {
495 // 4xx/5xx require a local template file
496 debugs(0, DBG_CRITICAL
, "FATAL: status " << info
->page_redirect
<< " is not valid on '" << page_name
<< "'");
504 /// \ingroup ErrorPageInternal
506 errorDynamicPageInfoDestroy(ErrorDynamicPageInfo
* info
)
509 safe_free(info
->page_name
);
513 /// \ingroup ErrorPageInternal
515 errorPageId(const char *page_name
)
517 for (int i
= 0; i
< ERR_MAX
; i
++) {
518 if (strcmp(err_type_str
[i
], page_name
) == 0)
522 for (size_t j
= 0; j
< ErrorDynamicPages
.size(); j
++) {
523 if (strcmp(ErrorDynamicPages
.items
[j
]->page_name
, page_name
) == 0)
531 errorReservePageId(const char *page_name
)
533 ErrorDynamicPageInfo
*info
;
534 int id
= errorPageId(page_name
);
536 if (id
== ERR_NONE
) {
537 info
= errorDynamicPageInfoCreate(ERR_MAX
+ ErrorDynamicPages
.size(), page_name
);
538 ErrorDynamicPages
.push_back(info
);
545 /// \ingroup ErrorPageInternal
547 errorPageName(int pageId
)
549 if (pageId
>= ERR_NONE
&& pageId
< ERR_MAX
) /* common case */
550 return err_type_str
[pageId
];
552 if (pageId
>= ERR_MAX
&& pageId
- ERR_MAX
< (ssize_t
)ErrorDynamicPages
.size())
553 return ErrorDynamicPages
.items
[pageId
- ERR_MAX
]->page_name
;
555 return "ERR_UNKNOWN"; /* should not happen */
559 errorCon(err_type type
, http_status status
, HttpRequest
* request
)
561 ErrorState
*err
= new ErrorState
;
562 err
->page_id
= type
; /* has to be reset manually if needed */
563 err
->err_language
= NULL
;
565 err
->httpStatus
= status
;
566 if (err
->page_id
>= ERR_MAX
&& ErrorDynamicPages
.items
[err
->page_id
- ERR_MAX
]->page_redirect
!= HTTP_STATUS_NONE
)
567 err
->httpStatus
= ErrorDynamicPages
.items
[err
->page_id
- ERR_MAX
]->page_redirect
;
569 if (request
!= NULL
) {
570 err
->request
= HTTPMSGLOCK(request
);
571 err
->src_addr
= request
->client_addr
;
572 request
->detailError(type
, ERR_DETAIL_NONE
);
579 errorAppendEntry(StoreEntry
* entry
, ErrorState
* err
)
581 assert(entry
->mem_obj
!= NULL
);
582 assert (entry
->isEmpty());
583 debugs(4, 4, "Creating an error page for entry " << entry
<<
584 " with errorstate " << err
<<
585 " page id " << err
->page_id
);
587 if (entry
->store_status
!= STORE_PENDING
) {
588 debugs(4, 2, "Skipping error page due to store_status: " << entry
->store_status
);
590 * If the entry is not STORE_PENDING, then no clients
591 * care about it, and we don't need to generate an
594 assert(EBIT_TEST(entry
->flags
, ENTRY_ABORTED
));
595 assert(entry
->mem_obj
->nclients
== 0);
600 if (err
->page_id
== TCP_RESET
) {
602 debugs(4, 2, "RSTing this reply");
603 err
->request
->flags
.setResetTCP();
609 entry
->replaceHttpReply( err
->BuildHttpReply() );
610 EBIT_CLR(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
613 entry
->negativeCache();
614 entry
->releaseRequest();
620 errorSend(const Comm::ConnectionPointer
&conn
, ErrorState
* err
)
623 debugs(4, 3, HERE
<< conn
<< ", err=" << err
);
624 assert(Comm::IsConnOpen(conn
));
626 * ugh, this is how we make sure error codes get back to
627 * the client side for logging and error tracking.
631 err
->request
->detailError(err
->type
, err
->xerrno
);
633 /* moved in front of errorBuildBuf @?@ */
634 err
->flags
.flag_cbdata
= 1;
636 rep
= err
->BuildHttpReply();
638 MemBuf
*mb
= rep
->pack();
639 AsyncCall::Pointer call
= commCbCall(78, 5, "errorSendComplete",
640 CommIoCbPtrFun(&errorSendComplete
, err
));
641 Comm::Write(conn
, mb
, call
);
648 \ingroup ErrorPageAPI
650 * Called by commHandleWrite() after data has been written
651 * to the client socket.
653 \note If there is a callback, the callback is responsible for
654 * closing the FD, otherwise we do it ourselves.
657 errorSendComplete(const Comm::ConnectionPointer
&conn
, char *bufnotused
, size_t size
, comm_err_t errflag
, int xerrno
, void *data
)
659 ErrorState
*err
= static_cast<ErrorState
*>(data
);
660 debugs(4, 3, HERE
<< conn
<< ", size=" << size
);
662 if (errflag
!= COMM_ERR_CLOSING
) {
664 debugs(4, 3, "errorSendComplete: callback");
665 err
->callback(conn
->fd
, err
->callback_data
, size
);
667 debugs(4, 3, "errorSendComplete: comm_close");
676 errorStateFree(ErrorState
* err
)
678 HTTPMSGUNLOCK(err
->request
);
679 safe_free(err
->redirect_url
);
681 safe_free(err
->request_hdrs
);
682 wordlistDestroy(&err
->ftp
.server_msg
);
683 safe_free(err
->ftp
.request
);
684 safe_free(err
->ftp
.reply
);
686 err
->auth_user_request
= NULL
;
688 safe_free(err
->err_msg
);
690 if (err
->err_language
!= Config
.errorDefaultLanguage
)
692 safe_free(err
->err_language
);
700 ErrorState::Dump(MemBuf
* mb
)
703 char ntoabuf
[MAX_IPSTRLEN
];
706 /* email subject line */
707 str
.Printf("CacheErrorInfo - %s", errorPageName(type
));
708 mb
->Printf("?subject=%s", rfc1738_escape_part(str
.buf
));
711 str
.Printf("CacheHost: %s\r\n", getMyHostname());
713 str
.Printf("ErrPage: %s\r\n", errorPageName(type
));
716 str
.Printf("Err: (%d) %s\r\n", xerrno
, strerror(xerrno
));
718 str
.Printf("Err: [none]\r\n");
721 if (auth_user_request
->denyMessage())
722 str
.Printf("Auth ErrMsg: %s\r\n", auth_user_request
->denyMessage());
724 if (dnsError
.size() > 0)
725 str
.Printf("DNS ErrMsg: %s\r\n", dnsError
.termedBuf());
728 str
.Printf("TimeStamp: %s\r\n\r\n", mkrfc1123(squid_curtime
));
731 str
.Printf("ClientIP: %s\r\n", src_addr
.NtoA(ntoabuf
,MAX_IPSTRLEN
));
733 if (request
&& request
->hier
.host
[0] != '\0') {
734 str
.Printf("ServerIP: %s\r\n", request
->hier
.host
);
739 str
.Printf("HTTP Request:\r\n");
741 if (NULL
!= request
) {
743 String urlpath_or_slash
;
745 if (request
->urlpath
.size() != 0)
746 urlpath_or_slash
= request
->urlpath
;
748 urlpath_or_slash
= "/";
750 str
.Printf("%s " SQUIDSTRINGPH
" %s/%d.%d\n",
751 RequestMethodStr(request
->method
),
752 SQUIDSTRINGPRINT(urlpath_or_slash
),
753 AnyP::ProtocolType_str
[request
->http_ver
.protocol
],
754 request
->http_ver
.major
, request
->http_ver
.minor
);
755 packerToMemInit(&pck
, &str
);
756 request
->header
.packInto(&pck
);
764 str
.Printf("FTP Request: %s\r\n", ftp
.request
);
765 str
.Printf("FTP Reply: %s\r\n", ftp
.reply
);
766 str
.Printf("FTP Msg: ");
767 wordlistCat(ftp
.server_msg
, &str
);
772 mb
->Printf("&body=%s", rfc1738_escape_part(str
.buf
));
777 /// \ingroup ErrorPageInternal
778 #define CVT_BUF_SZ 512
781 ErrorState::Convert(char token
, bool building_deny_info_url
, bool allowRecursion
)
784 const char *p
= NULL
; /* takes priority over mb if set */
786 int no_urlescape
= 0; /* if true then item is NOT to be further URL-encoded */
787 char ntoabuf
[MAX_IPSTRLEN
];
795 if (request
&& request
->auth_user_request
!= NULL
)
796 p
= request
->auth_user_request
->username();
803 mb
.Printf("%d", getMyPort());
807 if (building_deny_info_url
) break;
808 p
= request
? ftpUrlWith2f(request
) : "[no URL]";
812 if (building_deny_info_url
) break;
813 p
= errorPageName(type
);
818 p
= "%D"; // if recursion is not allowed, do not convert
820 // currently only SSL error details implemented
822 detail
->useRequest(request
);
823 const String
&errDetail
= detail
->toString();
824 if (errDetail
.defined()) {
825 MemBuf
*detail_mb
= ConvertText(errDetail
.termedBuf(), false);
826 mb
.append(detail_mb
->content(), detail_mb
->contentSize());
832 if (!mb
.contentSize())
833 mb
.Printf("[No Error Detail]");
837 mb
.Printf("%d", xerrno
);
842 mb
.Printf("(%d) %s", xerrno
, strerror(xerrno
));
844 mb
.Printf("[No Error]");
848 if (building_deny_info_url
) break;
849 /* FTP REQUEST LINE */
857 if (building_deny_info_url
) break;
866 if (building_deny_info_url
) break;
867 /* FTP SERVER RESPONSE */
869 mb
.append(ftp
.listing
->content(), ftp
.listing
->contentSize());
871 } else if (ftp
.server_msg
) {
872 wordlistCat(ftp
.server_msg
, &mb
);
877 mb
.Printf("%s", getMyHostname());
882 if (request
->hier
.host
[0] != '\0') // if non-empty string.
883 p
= request
->hier
.host
;
885 p
= request
->GetHost();
886 } else if (!building_deny_info_url
)
887 p
= "[unknown host]";
891 mb
.Printf("%s", src_addr
.NtoA(ntoabuf
,MAX_IPSTRLEN
));
895 if (request
&& request
->hier
.host
[0] != '\0') // if non-empty string
896 mb
.Printf("%s", request
->hier
.host
);
897 else if (!building_deny_info_url
)
902 if (building_deny_info_url
) break;
903 mb
.append(error_stylesheet
.content(), error_stylesheet
.contentSize());
908 if (building_deny_info_url
) break;
909 if (Config
.errHtmlText
) {
910 mb
.Printf("%s", Config
.errHtmlText
);
913 p
= "[not available]";
917 if (building_deny_info_url
) break;
919 p
= auth_user_request
->denyMessage("[not available]");
927 p
= RequestMethodStr(request
->method
);
928 else if (!building_deny_info_url
)
929 p
= "[unknown method]";
933 p
= request
? request
->extacl_message
.termedBuf() : external_acl_message
;
934 if (!p
&& !building_deny_info_url
)
935 p
= "[not available]";
940 mb
.Printf("%d", (int) request
->port
);
941 } else if (!building_deny_info_url
) {
942 p
= "[unknown port]";
948 p
= AnyP::ProtocolType_str
[request
->protocol
];
949 } else if (!building_deny_info_url
) {
950 p
= "[unknown protocol]";
955 if (building_deny_info_url
) {
956 p
= (request
->urlpath
.size() != 0 ? request
->urlpath
.termedBuf() : "/");
959 if (NULL
!= request
) {
961 String urlpath_or_slash
;
963 if (request
->urlpath
.size() != 0)
964 urlpath_or_slash
= request
->urlpath
;
966 urlpath_or_slash
= "/";
968 mb
.Printf("%s " SQUIDSTRINGPH
" %s/%d.%d\n",
969 RequestMethodStr(request
->method
),
970 SQUIDSTRINGPRINT(urlpath_or_slash
),
971 AnyP::ProtocolType_str
[request
->http_ver
.protocol
],
972 request
->http_ver
.major
, request
->http_ver
.minor
);
973 packerToMemInit(&pck
, &mb
);
974 request
->header
.packInto(&pck
);
976 } else if (request_hdrs
) {
984 /* for backward compat we make %s show the full URL. Drop this in some future release. */
985 if (building_deny_info_url
) {
986 p
= request
? urlCanonical(request
) : url
;
987 debugs(0,0, "WARNING: deny_info now accepts coded tags. Use %u to get the full URL instead of %s");
989 p
= visible_appname_string
;
993 if (building_deny_info_url
) {
994 p
= visible_appname_string
;
997 /* signature may contain %-escapes, recursion */
998 if (page_id
!= ERR_SQUID_SIGNATURE
) {
999 const int saved_id
= page_id
;
1000 page_id
= ERR_SQUID_SIGNATURE
;
1001 MemBuf
*sign_mb
= BuildContent();
1002 mb
.Printf("%s", sign_mb
->content());
1008 /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */
1014 mb
.Printf("%s", Time::FormatHttpd(squid_curtime
));
1018 mb
.Printf("%s", mkrfc1123(squid_curtime
));
1022 /* Using the fake-https version of canonical so error pages see https:// */
1023 /* even when the url-path cannot be shown as more than '*' */
1025 p
= urlCanonicalFakeHttps(request
);
1028 else if (!building_deny_info_url
)
1034 p
= urlCanonical(request
);
1037 else if (!building_deny_info_url
)
1042 if (Config
.adminEmail
)
1043 mb
.Printf("%s", Config
.adminEmail
);
1044 else if (!building_deny_info_url
)
1049 if (building_deny_info_url
) break;
1050 if (Config
.adminEmail
&& Config
.onoff
.emailErrData
)
1058 mb
.Printf("%s", detail
->errorName());
1061 if (!building_deny_info_url
)
1062 p
= "[Unknown Error Code]";
1066 if (building_deny_info_url
) break;
1067 if (dnsError
.size() > 0)
1068 p
= dnsError
.termedBuf();
1069 else if (ftp
.cwd_msg
)
1076 if (building_deny_info_url
) break;
1088 mb
.Printf("%%%c", token
);
1094 p
= mb
.buf
; /* do not use mb after this assignment! */
1098 debugs(4, 3, "errorConvert: %%" << token
<< " --> '" << p
<< "'" );
1103 if (building_deny_info_url
&& !no_urlescape
)
1104 p
= rfc1738_escape_part(p
);
1110 ErrorState::DenyInfoLocation(const char *name
, HttpRequest
*aRequest
, MemBuf
&result
)
1112 char const *m
= name
;
1117 m
+= 4; // skip "3xx:"
1119 while ((p
= strchr(m
, '%'))) {
1120 result
.append(m
, p
- m
); /* copy */
1121 t
= Convert(*++p
, true, true); /* convert */
1122 result
.Printf("%s", t
); /* copy */
1123 m
= p
+ 1; /* advance */
1127 result
.Printf("%s", m
); /* copy tail */
1129 assert((size_t)result
.contentSize() == strlen(result
.content()));
1133 ErrorState::BuildHttpReply()
1135 HttpReply
*rep
= new HttpReply
;
1136 const char *name
= errorPageName(page_id
);
1137 /* no LMT for error pages; error pages expire immediately */
1139 if (name
[0] == '3' || (name
[0] != '4' && name
[0] != '5' && strchr(name
, ':'))) {
1141 http_status status
= HTTP_MOVED_TEMPORARILY
;
1142 // Use configured 3xx reply status if set.
1144 status
= httpStatus
;
1146 // Use 307 for HTTP/1.1 non-GET/HEAD requests.
1147 if (request
->method
!= METHOD_GET
&& request
->method
!= METHOD_HEAD
&& request
->http_ver
>= HttpVersion(1,1))
1148 status
= HTTP_TEMPORARY_REDIRECT
;
1151 rep
->setHeaders(status
, NULL
, "text/html", 0, 0, -1);
1154 MemBuf redirect_location
;
1155 redirect_location
.init();
1156 DenyInfoLocation(name
, request
, redirect_location
);
1157 httpHeaderPutStrf(&rep
->header
, HDR_LOCATION
, "%s", redirect_location
.content() );
1160 httpHeaderPutStrf(&rep
->header
, HDR_X_SQUID_ERROR
, "%d %s", httpStatus
, "Access Denied");
1162 MemBuf
*content
= BuildContent();
1163 rep
->setHeaders(httpStatus
, NULL
, "text/html", content
->contentSize(), 0, -1);
1165 * include some information for downstream caches. Implicit
1166 * replaceable content. This isn't quite sufficient. xerrno is not
1167 * necessarily meaningful to another system, so we really should
1168 * expand it. Additionally, we should identify ourselves. Someone
1169 * might want to know. Someone _will_ want to know OTOH, the first
1170 * X-CACHE-MISS entry should tell us who.
1172 httpHeaderPutStrf(&rep
->header
, HDR_X_SQUID_ERROR
, "%s %d", name
, xerrno
);
1176 * If error page auto-negotiate is enabled in any way, send the Vary.
1177 * RFC 2616 section 13.6 and 14.44 says MAY and SHOULD do this.
1178 * We have even better reasons though:
1179 * see http://wiki.squid-cache.org/KnowledgeBase/VaryNotCaching
1181 if (!Config
.errorDirectory
) {
1182 /* We 'negotiated' this ONLY from the Accept-Language. */
1183 rep
->header
.delById(HDR_VARY
);
1184 rep
->header
.putStr(HDR_VARY
, "Accept-Language");
1187 /* add the Content-Language header according to RFC section 14.12 */
1189 rep
->header
.putStr(HDR_CONTENT_LANGUAGE
, err_language
);
1191 #endif /* USE_ERROR_LOCALES */
1193 /* default templates are in English */
1194 /* language is known unless error_directory override used */
1195 if (!Config
.errorDirectory
)
1196 rep
->header
.putStr(HDR_CONTENT_LANGUAGE
, "en");
1199 httpBodySet(&rep
->body
, content
);
1200 /* do not memBufClean() or delete the content, it was absorbed by httpBody */
1207 ErrorState::BuildContent()
1209 const char *m
= NULL
;
1211 assert(page_id
> ERR_NONE
&& page_id
< error_page_count
);
1214 ErrorPageFile
*localeTmpl
= NULL
;
1216 /** error_directory option in squid.conf overrides translations.
1217 * Custom errors are always found either in error_directory or the templates directory.
1218 * Otherwise locate the Accept-Language header
1220 if (!Config
.errorDirectory
&& page_id
< ERR_MAX
) {
1221 if (err_language
&& err_language
!= Config
.errorDefaultLanguage
)
1222 safe_free(err_language
);
1224 localeTmpl
= new ErrorPageFile(err_type_str
[page_id
]);
1225 if (localeTmpl
->loadFor(request
)) {
1226 m
= localeTmpl
->text();
1227 assert(localeTmpl
->language());
1228 err_language
= xstrdup(localeTmpl
->language());
1231 #endif /* USE_ERR_LOCALES */
1234 * If client-specific error templates are not enabled or available.
1235 * fall back to the old style squid.conf settings.
1238 m
= error_text
[page_id
];
1240 if (!Config
.errorDirectory
)
1241 err_language
= Config
.errorDefaultLanguage
;
1243 debugs(4, 2, HERE
<< "No existing error page language negotiated for " << errorPageName(page_id
) << ". Using default error file.");
1246 MemBuf
*result
= ConvertText(m
, true);
1254 MemBuf
*ErrorState::ConvertText(const char *text
, bool allowRecursion
)
1256 MemBuf
*content
= new MemBuf
;
1258 const char *m
= text
;
1262 while ((p
= strchr(m
, '%'))) {
1263 content
->append(m
, p
- m
); /* copy */
1264 const char *t
= Convert(*++p
, false, allowRecursion
); /* convert */
1265 content
->Printf("%s", t
); /* copy */
1266 m
= p
+ 1; /* advance */
1270 content
->Printf("%s", m
); /* copy tail */
1272 assert((size_t)content
->contentSize() == strlen(content
->content()));