]>
Commit | Line | Data |
---|---|---|
4a9a952e | 1 | |
30a4f2a8 | 2 | /* |
72711e31 | 3 | * $Id: errorpage.cc,v 1.163 2001/03/03 10:39:31 hno 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 | */ |
1e74c110 | 35 | |
fe40a877 | 36 | /* |
4e3f29eb | 37 | * Abstract: These routines are used to generate error messages to be |
38 | * sent to clients. The error type is used to select between | |
39 | * the various message formats. (formats are stored in the | |
40 | * Config.errorDirectory) | |
fe40a877 | 41 | */ |
42 | ||
44a47c6e | 43 | #include "squid.h" |
1e74c110 | 44 | |
02922e76 | 45 | |
46 | /* local types */ | |
47 | ||
48 | typedef struct { | |
49 | int id; | |
50 | char *page_name; | |
51 | } ErrorDynamicPageInfo; | |
52 | ||
53 | /* local constant and vars */ | |
54 | ||
1d803566 | 55 | /* |
56 | * note: hard coded error messages are not appended with %S automagically | |
57 | * to give you more control on the format | |
58 | */ | |
2ac76861 | 59 | static const struct { |
1afe05c5 | 60 | int type; /* and page_id */ |
2ac76861 | 61 | const char *text; |
62 | } error_hard_text[] = { | |
1afe05c5 | 63 | |
2ac76861 | 64 | { |
65 | ERR_SQUID_SIGNATURE, | |
66 | "\n<br clear=\"all\">\n" | |
67 | "<hr noshade size=1>\n" | |
9bc73deb | 68 | "Generated %T by %h (%s)\n" |
e32b11fc | 69 | "</BODY></HTML>\n" |
1d803566 | 70 | } |
71 | }; | |
02922e76 | 72 | |
73 | static Stack ErrorDynamicPages; | |
74 | ||
75 | /* local prototypes */ | |
76 | ||
2ac76861 | 77 | static const int error_hard_text_count = sizeof(error_hard_text) / sizeof(*error_hard_text); |
02922e76 | 78 | static char **error_text = NULL; |
79 | static int error_page_count = 0; | |
9b312a19 | 80 | |
02922e76 | 81 | static char *errorTryLoadText(const char *page_name, const char *dir); |
82 | static char *errorLoadText(const char *page_name); | |
1d803566 | 83 | static const char *errorFindHardText(err_type type); |
c68e9c6b | 84 | static ErrorDynamicPageInfo *errorDynamicPageInfoCreate(int id, const char *page_name); |
85 | static void errorDynamicPageInfoDestroy(ErrorDynamicPageInfo * info); | |
1d803566 | 86 | static MemBuf errorBuildContent(ErrorState * err); |
fe40a877 | 87 | static const char *errorConvert(char token, ErrorState * err); |
9b312a19 | 88 | static CWCB errorSendComplete; |
1e74c110 | 89 | |
fe40a877 | 90 | /* |
91 | * Function: errorInitialize | |
92 | * | |
1d803566 | 93 | * Abstract: This function finds the error messages formats, and stores |
fe40a877 | 94 | * them in error_text[]; |
95 | * | |
96 | * Global effects: | |
97 | * error_text[] - is modified | |
98 | */ | |
b8d8561b | 99 | void |
0673c0ba | 100 | errorInitialize(void) |
fa966b74 | 101 | { |
728da2ee | 102 | err_type i; |
1d803566 | 103 | const char *text; |
02922e76 | 104 | error_page_count = ERR_MAX + ErrorDynamicPages.count; |
1afe05c5 | 105 | error_text = xcalloc(error_page_count, sizeof(char *)); |
728da2ee | 106 | for (i = ERR_NONE, i++; i < error_page_count; i++) { |
1d803566 | 107 | safe_free(error_text[i]); |
108 | /* hard-coded ? */ | |
109 | if ((text = errorFindHardText(i))) | |
110 | error_text[i] = xstrdup(text); | |
c68e9c6b | 111 | else if (i < ERR_MAX) { |
1afe05c5 | 112 | /* precompiled ? */ |
02922e76 | 113 | error_text[i] = errorLoadText(err_type_str[i]); |
c68e9c6b | 114 | } else { |
115 | /* dynamic */ | |
1afe05c5 | 116 | ErrorDynamicPageInfo *info = ErrorDynamicPages.items[i - ERR_MAX]; |
02922e76 | 117 | assert(info && info->id == i && info->page_name); |
118 | error_text[i] = errorLoadText(info->page_name); | |
119 | } | |
1d803566 | 120 | assert(error_text[i]); |
121 | } | |
122 | } | |
123 | ||
c68e9c6b | 124 | void |
125 | errorClean(void) | |
126 | { | |
127 | if (error_text) { | |
128 | int i; | |
129 | for (i = ERR_NONE + 1; i < error_page_count; i++) | |
130 | safe_free(error_text[i]); | |
131 | safe_free(error_text); | |
132 | } | |
133 | while (ErrorDynamicPages.count) | |
134 | errorDynamicPageInfoDestroy(stackPop(&ErrorDynamicPages)); | |
135 | error_page_count = 0; | |
136 | } | |
137 | ||
1d803566 | 138 | static const char * |
139 | errorFindHardText(err_type type) | |
140 | { | |
141 | int i; | |
142 | for (i = 0; i < error_hard_text_count; i++) | |
143 | if (error_hard_text[i].type == type) | |
144 | return error_hard_text[i].text; | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | ||
149 | static char * | |
02922e76 | 150 | errorLoadText(const char *page_name) |
1d803566 | 151 | { |
152 | /* test configured location */ | |
02922e76 | 153 | char *text = errorTryLoadText(page_name, Config.errorDirectory); |
1d803566 | 154 | /* test default location if failed */ |
155 | if (!text && strcmp(Config.errorDirectory, DEFAULT_SQUID_ERROR_DIR)) | |
02922e76 | 156 | text = errorTryLoadText(page_name, DEFAULT_SQUID_ERROR_DIR); |
1d803566 | 157 | /* giving up if failed */ |
158 | if (!text) | |
159 | fatal("failed to find or read error text file."); | |
160 | return text; | |
161 | } | |
162 | ||
163 | static char * | |
02922e76 | 164 | errorTryLoadText(const char *page_name, const char *dir) |
1d803566 | 165 | { |
9b312a19 | 166 | int fd; |
167 | char path[MAXPATHLEN]; | |
168 | struct stat sb; | |
1d803566 | 169 | char *text; |
170 | ||
137ee196 | 171 | snprintf(path, sizeof(path), "%s/%s", dir, page_name); |
c4aefe96 | 172 | fd = file_open(path, O_RDONLY | O_TEXT); |
1d803566 | 173 | if (fd < 0 || fstat(fd, &sb) < 0) { |
174 | debug(4, 0) ("errorTryLoadText: '%s': %s\n", path, xstrerror()); | |
175 | if (fd >= 0) | |
176 | file_close(fd); | |
177 | return NULL; | |
178 | } | |
d5e36424 | 179 | text = xcalloc(sb.st_size + 2 + 1, 1); /* 2 == space for %S */ |
1d803566 | 180 | if (read(fd, text, sb.st_size) != sb.st_size) { |
181 | debug(4, 0) ("errorTryLoadText: failed to fully read: '%s': %s\n", | |
182 | path, xstrerror()); | |
183 | xfree(text); | |
184 | text = NULL; | |
1e74c110 | 185 | } |
1d803566 | 186 | file_close(fd); |
c68e9c6b | 187 | if (strstr(text, "%s") == NULL) |
188 | strcat(text, "%S"); /* add signature */ | |
1d803566 | 189 | return text; |
6eb42cae | 190 | } |
191 | ||
02922e76 | 192 | static ErrorDynamicPageInfo * |
193 | errorDynamicPageInfoCreate(int id, const char *page_name) | |
194 | { | |
195 | ErrorDynamicPageInfo *info = xcalloc(1, sizeof(ErrorDynamicPageInfo)); | |
196 | info->id = id; | |
197 | info->page_name = xstrdup(page_name); | |
198 | return info; | |
199 | } | |
200 | ||
201 | static void | |
1afe05c5 | 202 | errorDynamicPageInfoDestroy(ErrorDynamicPageInfo * info) |
02922e76 | 203 | { |
204 | assert(info); | |
205 | xfree(info->page_name); | |
206 | xfree(info); | |
207 | } | |
208 | ||
209 | int | |
210 | errorReservePageId(const char *page_name) | |
211 | { | |
1afe05c5 | 212 | ErrorDynamicPageInfo *info = |
213 | errorDynamicPageInfoCreate(ERR_MAX + ErrorDynamicPages.count, page_name); | |
02922e76 | 214 | stackPush(&ErrorDynamicPages, info); |
215 | return info->id; | |
216 | } | |
217 | ||
c68e9c6b | 218 | static const char * |
219 | errorPageName(int pageId) | |
53ad48e6 | 220 | { |
c68e9c6b | 221 | if (pageId >= ERR_NONE && pageId < ERR_MAX) /* common case */ |
222 | return err_type_str[pageId]; | |
223 | if (pageId >= ERR_MAX && pageId - ERR_MAX < ErrorDynamicPages.count) | |
224 | return ((ErrorDynamicPageInfo *) ErrorDynamicPages. | |
225 | items[pageId - ERR_MAX])->page_name; | |
226 | return "ERR_UNKNOWN"; /* should not happen */ | |
53ad48e6 | 227 | } |
228 | ||
fe40a877 | 229 | /* |
230 | * Function: errorCon | |
231 | * | |
232 | * Abstract: This function creates a ErrorState object. | |
233 | */ | |
234 | ErrorState * | |
728da2ee | 235 | errorCon(err_type type, http_status status) |
fe40a877 | 236 | { |
28c60158 | 237 | ErrorState *err; |
72711e31 | 238 | err = cbdataAlloc(ErrorState); |
1afe05c5 | 239 | err->page_id = type; /* has to be reset manually if needed */ |
fe40a877 | 240 | err->type = type; |
241 | err->http_status = status; | |
242 | return err; | |
243 | } | |
244 | ||
245 | /* | |
246 | * Function: errorAppendEntry | |
247 | * | |
248 | * Arguments: err - This object is destroyed after use in this function. | |
249 | * | |
250 | * Abstract: This function generates a error page from the info contained | |
4e3f29eb | 251 | * by 'err' and then stores the text in the specified store |
252 | * entry. This function should only be called by ``server | |
253 | * side routines'' which need to communicate errors to the | |
79e0dc20 | 254 | * client side. It should also be called from client_side.c |
255 | * because we now support persistent connections, and | |
256 | * cannot assume that we can immediately write to the socket | |
257 | * for an error. | |
fe40a877 | 258 | */ |
259 | void | |
260 | errorAppendEntry(StoreEntry * entry, ErrorState * err) | |
261 | { | |
cb69b4c7 | 262 | HttpReply *rep; |
cb69b4c7 | 263 | MemObject *mem = entry->mem_obj; |
b50179a6 | 264 | assert(mem != NULL); |
4e3f29eb | 265 | assert(mem->inmem_hi == 0); |
1ea80e98 | 266 | if (entry->store_status != STORE_PENDING) { |
267 | /* | |
268 | * If the entry is not STORE_PENDING, then no clients | |
269 | * care about it, and we don't need to generate an | |
270 | * error message | |
271 | */ | |
272 | assert(EBIT_TEST(entry->flags, ENTRY_ABORTED)); | |
273 | assert(mem->nclients == 0); | |
274 | errorStateFree(err); | |
275 | return; | |
276 | } | |
5507c7d5 | 277 | storeLockObject(entry); |
429cf70b | 278 | storeBuffer(entry); |
cb69b4c7 | 279 | rep = errorBuildReply(err); |
1cfdbcf0 | 280 | /* Add authentication header */ |
94439e4e | 281 | /* TODO: alter errorstate to be accel on|off aware. The 0 on the next line |
282 | * depends on authenticate behaviour: all schemes to date send no extra data | |
283 | * on 407/401 responses, and do not check the accel state on 401/407 responses | |
284 | */ | |
285 | authenticateFixHeader(rep, err->auth_user_request, err->request, 0); | |
cb69b4c7 | 286 | httpReplySwapOut(rep, entry); |
94439e4e | 287 | httpReplyAbsorb(mem->reply, rep); |
ee08bdf5 | 288 | EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT); |
429cf70b | 289 | storeBufferFlush(entry); |
00691420 | 290 | storeComplete(entry); |
b50179a6 | 291 | storeNegativeCache(entry); |
292 | storeReleaseRequest(entry); | |
5507c7d5 | 293 | storeUnlockObject(entry); |
fe40a877 | 294 | errorStateFree(err); |
fe40a877 | 295 | } |
296 | ||
297 | /* | |
298 | * Function: errorSend | |
299 | * | |
300 | * Arguments: err - This object is destroyed after use in this function. | |
301 | * | |
302 | * Abstract: This function generates a error page from the info contained | |
303 | * by 'err' and then sends it to the client. | |
4e3f29eb | 304 | * The callback function errorSendComplete() is called after |
305 | * the page has been written to the client socket (fd). | |
306 | * errorSendComplete() deallocates 'err'. We need to add | |
307 | * 'err' to the cbdata because comm_write() requires it | |
308 | * for all callback data pointers. | |
79e0dc20 | 309 | * |
310 | * Note, normally errorSend() should only be called from | |
311 | * routines in ssl.c and pass.c, where we don't have any | |
312 | * StoreEntry's. In client_side.c we must allocate a StoreEntry | |
313 | * for errors and use errorAppendEntry() to account for | |
314 | * persistent/pipeline connections. | |
fe40a877 | 315 | */ |
316 | void | |
317 | errorSend(int fd, ErrorState * err) | |
318 | { | |
cb69b4c7 | 319 | HttpReply *rep; |
fe40a877 | 320 | debug(4, 3) ("errorSend: FD %d, err=%p\n", fd, err); |
321 | assert(fd >= 0); | |
88aad2e5 | 322 | /* |
323 | * ugh, this is how we make sure error codes get back to | |
324 | * the client side for logging and error tracking. | |
325 | */ | |
326 | if (err->request) | |
327 | err->request->err_type = err->type; | |
cb69b4c7 | 328 | /* moved in front of errorBuildBuf @?@ */ |
b515fc11 | 329 | err->flags.flag_cbdata = 1; |
cb69b4c7 | 330 | rep = errorBuildReply(err); |
331 | comm_write_mbuf(fd, httpReplyPack(rep), errorSendComplete, err); | |
332 | httpReplyDestroy(rep); | |
fe40a877 | 333 | } |
334 | ||
335 | /* | |
336 | * Function: errorSendComplete | |
337 | * | |
4e3f29eb | 338 | * Abstract: Called by commHandleWrite() after data has been written |
339 | * to the client socket. | |
fe40a877 | 340 | * |
341 | * Note: If there is a callback, the callback is responsible for | |
342 | * closeing the FD, otherwise we do it ourseves. | |
343 | */ | |
344 | static void | |
79a15e0a | 345 | errorSendComplete(int fd, char *bufnotused, size_t size, int errflag, void *data) |
fe40a877 | 346 | { |
347 | ErrorState *err = data; | |
348 | debug(4, 3) ("errorSendComplete: FD %d, size=%d\n", fd, size); | |
349 | if (errflag != COMM_ERR_CLOSING) { | |
94439e4e | 350 | if (err->callback) { |
351 | debug(4, 3) ("errorSendComplete: callback\n"); | |
fe40a877 | 352 | err->callback(fd, err->callback_data, size); |
94439e4e | 353 | } else { |
fe40a877 | 354 | comm_close(fd); |
94439e4e | 355 | debug(4, 3) ("errorSendComplete: comm_close\n"); |
356 | } | |
fe40a877 | 357 | } |
358 | errorStateFree(err); | |
fe40a877 | 359 | } |
360 | ||
cb69b4c7 | 361 | void |
9b312a19 | 362 | errorStateFree(ErrorState * err) |
6eb42cae | 363 | { |
9b312a19 | 364 | requestUnlink(err->request); |
365 | safe_free(err->redirect_url); | |
366 | safe_free(err->url); | |
76a5501f | 367 | safe_free(err->host); |
5f3c4e9a | 368 | safe_free(err->dnsserver_msg); |
b5af8569 | 369 | safe_free(err->request_hdrs); |
9bc73deb | 370 | wordlistDestroy(&err->ftp.server_msg); |
371 | safe_free(err->ftp.request); | |
372 | safe_free(err->ftp.reply); | |
94439e4e | 373 | if (err->auth_user_request) |
374 | authenticateAuthUserRequestUnlock(err->auth_user_request); | |
28c60158 | 375 | cbdataFree(err); |
1e74c110 | 376 | } |
8213067d | 377 | |
2658f489 | 378 | #define CVT_BUF_SZ 512 |
fe40a877 | 379 | |
380 | /* | |
1d803566 | 381 | * B - URL with FTP %2f hack x |
382 | * c - Squid error code x | |
383 | * d - seconds elapsed since request received x | |
fe40a877 | 384 | * e - errno x |
385 | * E - strerror() x | |
fe40a877 | 386 | * f - FTP request line x |
969c39b9 | 387 | * F - FTP reply line x |
7131112f | 388 | * g - FTP server message x |
fe40a877 | 389 | * h - cache hostname x |
969c39b9 | 390 | * H - server host name x |
fe40a877 | 391 | * i - client IP address x |
392 | * I - server IP address x | |
393 | * L - HREF link for more info/contact x | |
394 | * M - Request Method x | |
066ed5c1 | 395 | * m - Error message returned by external Auth. x |
fe40a877 | 396 | * p - URL port # x |
397 | * P - Protocol x | |
b5af8569 | 398 | * R - Full HTTP Request x |
1d803566 | 399 | * S - squid signature from ERR_SIGNATURE x |
400 | * s - caching proxy software with version x | |
fe40a877 | 401 | * t - local time x |
402 | * T - UTC x | |
969c39b9 | 403 | * U - URL without password x |
404 | * u - URL without password, %2f added to path x | |
fe40a877 | 405 | * w - cachemgr email address x |
406 | * z - dns server error message x | |
407 | */ | |
408 | ||
409 | static const char * | |
9b312a19 | 410 | errorConvert(char token, ErrorState * err) |
8213067d | 411 | { |
2658f489 | 412 | request_t *r = err->request; |
137ee196 | 413 | static MemBuf mb = MemBufNULL; |
eeb423fb | 414 | const char *p = NULL; /* takes priority over mb if set */ |
10270faa | 415 | int do_quote = 1; |
eeb423fb | 416 | |
137ee196 | 417 | memBufReset(&mb); |
9b312a19 | 418 | switch (token) { |
8f872bb6 | 419 | case 'B': |
420 | p = r ? ftpUrlWith2f(r) : "[no URL]"; | |
421 | break; | |
42b51993 | 422 | case 'c': |
423 | assert(err->type >= ERR_NONE); | |
424 | assert(err->type < ERR_MAX); | |
425 | p = err_type_str[err->type]; | |
426 | break; | |
042461c3 | 427 | case 'e': |
137ee196 | 428 | memBufPrintf(&mb, "%d", err->xerrno); |
042461c3 | 429 | break; |
430 | case 'E': | |
23d92c64 | 431 | if (err->xerrno) |
137ee196 | 432 | memBufPrintf(&mb, "(%d) %s", err->xerrno, strerror(err->xerrno)); |
23d92c64 | 433 | else |
137ee196 | 434 | memBufPrintf(&mb, "[No Error]"); |
03d7b07f | 435 | break; |
fe40a877 | 436 | case 'f': |
437 | /* FTP REQUEST LINE */ | |
438 | if (err->ftp.request) | |
439 | p = err->ftp.request; | |
440 | else | |
a8af3a35 | 441 | p = "nothing"; |
fe40a877 | 442 | break; |
443 | case 'F': | |
444 | /* FTP REPLY LINE */ | |
445 | if (err->ftp.request) | |
446 | p = err->ftp.reply; | |
447 | else | |
a8af3a35 | 448 | p = "nothing"; |
03d7b07f | 449 | break; |
7131112f | 450 | case 'g': |
451 | /* FTP SERVER MESSAGE */ | |
9bc73deb | 452 | wordlistCat(err->ftp.server_msg, &mb); |
7131112f | 453 | break; |
03d7b07f | 454 | case 'h': |
137ee196 | 455 | memBufPrintf(&mb, "%s", getMyHostname()); |
f787fb1e | 456 | break; |
fe40a877 | 457 | case 'H': |
458 | p = r ? r->host : "[unknown host]"; | |
f787fb1e | 459 | break; |
460 | case 'i': | |
137ee196 | 461 | memBufPrintf(&mb, "%s", inet_ntoa(err->src_addr)); |
f6aa1a5c | 462 | break; |
f787fb1e | 463 | case 'I': |
464 | if (err->host) { | |
137ee196 | 465 | memBufPrintf(&mb, "%s", err->host); |
f787fb1e | 466 | } else |
fe40a877 | 467 | p = "[unknown]"; |
468 | break; | |
469 | case 'L': | |
470 | if (Config.errHtmlText) { | |
137ee196 | 471 | memBufPrintf(&mb, "%s", Config.errHtmlText); |
fe40a877 | 472 | } else |
473 | p = "[not available]"; | |
474 | break; | |
066ed5c1 | 475 | case 'm': |
94439e4e | 476 | p = authenticateAuthUserRequestMessage(err->auth_user_request) ? authenticateAuthUserRequestMessage(err->auth_user_request) : "[not available]"; |
066ed5c1 | 477 | break; |
fe40a877 | 478 | case 'M': |
479 | p = r ? RequestMethodStr[r->method] : "[unkown method]"; | |
480 | break; | |
481 | case 'p': | |
482 | if (r) { | |
137ee196 | 483 | memBufPrintf(&mb, "%d", (int) r->port); |
fe40a877 | 484 | } else { |
485 | p = "[unknown port]"; | |
486 | } | |
487 | break; | |
488 | case 'P': | |
489 | p = r ? ProtocolStr[r->protocol] : "[unkown protocol]"; | |
490 | break; | |
b5af8569 | 491 | case 'R': |
865094d7 | 492 | if (NULL != r) { |
865094d7 | 493 | Packer p; |
ccf44862 | 494 | memBufPrintf(&mb, "%s %s HTTP/%d.%d\n", |
865094d7 | 495 | RequestMethodStr[r->method], |
496 | strLen(r->urlpath) ? strBuf(r->urlpath) : "/", | |
ccf44862 | 497 | r->http_ver.major, r->http_ver.minor); |
865094d7 | 498 | packerToMemInit(&p, &mb); |
499 | httpHeaderPackInto(&r->header, &p); | |
500 | packerClean(&p); | |
865094d7 | 501 | } else if (err->request_hdrs) { |
502 | p = err->request_hdrs; | |
503 | } else { | |
504 | p = "[no request]"; | |
505 | } | |
b5af8569 | 506 | break; |
1d803566 | 507 | case 's': |
508 | p = full_appname_string; | |
509 | break; | |
510 | case 'S': | |
511 | /* signature may contain %-escapes, recursion */ | |
02922e76 | 512 | if (err->page_id != ERR_SQUID_SIGNATURE) { |
513 | const int saved_id = err->page_id; | |
137ee196 | 514 | MemBuf sign_mb; |
02922e76 | 515 | err->page_id = ERR_SQUID_SIGNATURE; |
137ee196 | 516 | sign_mb = errorBuildContent(err); |
517 | memBufPrintf(&mb, "%s", sign_mb.buf); | |
518 | memBufClean(&sign_mb); | |
02922e76 | 519 | err->page_id = saved_id; |
10270faa | 520 | do_quote = 0; |
1d803566 | 521 | } else { |
522 | /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */ | |
523 | p = "[%S]"; | |
524 | } | |
525 | break; | |
fe40a877 | 526 | case 't': |
137ee196 | 527 | memBufPrintf(&mb, "%s", mkhttpdlogtime(&squid_curtime)); |
f787fb1e | 528 | break; |
f8291f8f | 529 | case 'T': |
137ee196 | 530 | memBufPrintf(&mb, "%s", mkrfc1123(squid_curtime)); |
f8291f8f | 531 | break; |
fe40a877 | 532 | case 'U': |
b5af8569 | 533 | p = r ? urlCanonicalClean(r) : err->url ? err->url : "[no URL]"; |
b9916917 | 534 | break; |
fe40a877 | 535 | case 'w': |
137ee196 | 536 | if (Config.adminEmail) |
537 | memBufPrintf(&mb, "%s", Config.adminEmail); | |
538 | else | |
fe40a877 | 539 | p = "[unknown]"; |
540 | break; | |
541 | case 'z': | |
542 | if (err->dnsserver_msg) | |
543 | p = err->dnsserver_msg; | |
b9916917 | 544 | else |
fe40a877 | 545 | p = "[unknown]"; |
b9916917 | 546 | break; |
e347f8e5 | 547 | case '%': |
548 | p = "%"; | |
549 | break; | |
9b312a19 | 550 | default: |
6b8e7481 | 551 | memBufPrintf(&mb, "%%%c", token); |
9b312a19 | 552 | break; |
553 | } | |
137ee196 | 554 | if (!p) |
eeb423fb | 555 | p = mb.buf; /* do not use mb after this assignment! */ |
137ee196 | 556 | assert(p); |
e102ebda | 557 | debug(4, 3) ("errorConvert: %%%c --> '%s'\n", token, p); |
10270faa | 558 | if (do_quote) |
559 | p = html_quote(p); | |
9b312a19 | 560 | return p; |
8213067d | 561 | } |
e381a13d | 562 | |
cb69b4c7 | 563 | /* allocates and initializes an error response */ |
564 | HttpReply * | |
2ac76861 | 565 | errorBuildReply(ErrorState * err) |
cb69b4c7 | 566 | { |
cb69b4c7 | 567 | HttpReply *rep = httpReplyCreate(); |
1d803566 | 568 | MemBuf content = errorBuildContent(err); |
ccf44862 | 569 | http_version_t version; |
cb69b4c7 | 570 | /* no LMT for error pages; error pages expire immediately */ |
bffee5af | 571 | httpBuildVersion(&version, 1, 0); |
ccf44862 | 572 | httpReplySetHeaders(rep, version, err->http_status, NULL, "text/html", content.size, 0, squid_curtime); |
fcc62502 | 573 | /* |
574 | * include some information for downstream caches. Implicit | |
2246b732 | 575 | * replaceable content. This isn't quite sufficient. xerrno is not |
fcc62502 | 576 | * necessarily meaningful to another system, so we really should |
577 | * expand it. Additionally, we should identify ourselves. Someone | |
578 | * might want to know. Someone _will_ want to know OTOH, the first | |
579 | * X-CACHE-MISS entry should tell us who. | |
580 | */ | |
2246b732 | 581 | httpHeaderPutStrf(&rep->header, HDR_X_SQUID_ERROR, "%s %d", |
c68e9c6b | 582 | errorPageName(err->page_id), err->xerrno); |
1d21d91d | 583 | httpBodySet(&rep->body, &content); |
584 | /* do not memBufClean() the content, it was absorbed by httpBody */ | |
cb69b4c7 | 585 | return rep; |
586 | } | |
587 | ||
1d803566 | 588 | static MemBuf |
589 | errorBuildContent(ErrorState * err) | |
cb69b4c7 | 590 | { |
1d803566 | 591 | MemBuf content; |
1d803566 | 592 | const char *m; |
593 | const char *p; | |
cb69b4c7 | 594 | const char *t; |
595 | assert(err != NULL); | |
02922e76 | 596 | assert(err->page_id > ERR_NONE && err->page_id < error_page_count); |
1d803566 | 597 | memBufDefInit(&content); |
02922e76 | 598 | m = error_text[err->page_id]; |
1d803566 | 599 | assert(m); |
cb69b4c7 | 600 | while ((p = strchr(m, '%'))) { |
2ac76861 | 601 | memBufAppend(&content, m, p - m); /* copy */ |
602 | t = errorConvert(*++p, err); /* convert */ | |
603 | memBufPrintf(&content, "%s", t); /* copy */ | |
604 | m = p + 1; /* advance */ | |
cb69b4c7 | 605 | } |
1d803566 | 606 | if (*m) |
2ac76861 | 607 | memBufPrintf(&content, "%s", m); /* copy tail */ |
1d803566 | 608 | assert(content.size == strlen(content.buf)); |
cb69b4c7 | 609 | return content; |
610 | } |