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