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