]> git.ipfire.org Git - thirdparty/squid.git/blob - src/errorpage.h
Polished names, comments, and code formatting.
[thirdparty/squid.git] / src / errorpage.h
1 /*
2 * DEBUG: section 04 Error Generation
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
32 */
33
34 #ifndef SQUID_ERRORPAGE_H
35 #define SQUID_ERRORPAGE_H
36
37 #include "squid-old.h"
38 #if USE_AUTH
39 #include "auth/UserRequest.h"
40 #endif
41 #include "cbdata.h"
42 #include "comm/forward.h"
43 #include "err_detail_type.h"
44 #include "ip/Address.h"
45 #include "MemBuf.h"
46 #if USE_SSL
47 #include "ssl/ErrorDetail.h"
48 #endif
49
50 /**
51 \defgroup ErrorPageAPI Error Pages API
52 \ingroup Components
53 \section ErrorPageStringCodes Error Page % codes for text insertion.
54 *
55 \verbatim
56 a - User identity x
57 B - URL with FTP %2f hack x
58 c - Squid error code x
59 d - seconds elapsed since request received x
60 D - Error details x
61 e - errno x
62 E - strerror() x
63 f - FTP request line x
64 F - FTP reply line x
65 g - FTP server message x
66 h - cache hostname x
67 H - server host name x
68 i - client IP address x
69 I - server IP address x
70 l - HREF link for CSS stylesheet inclusion x
71 L - HREF link for more info/contact x
72 M - Request Method x
73 m - Error message returned by auth helper x
74 o - Message returned external acl helper x
75 p - URL port # x
76 P - Protocol x
77 R - Full HTTP Request x
78 S - squid signature from ERR_SIGNATURE x
79 s - caching proxy software with version x
80 t - local time x
81 T - UTC x
82 U - URL without password x
83 u - URL with password x
84 w - cachemgr email address x
85 W - error data (to be included in the mailto links)
86 x - error name x
87 z - dns server error message x
88 Z - Preformatted error message x
89 \endverbatim
90 */
91
92 class HttpReply;
93 class MemBuf;
94
95 /// \ingroup ErrorPageAPI
96 class ErrorState
97 {
98 public:
99 ErrorState(err_type type, http_status, HttpRequest * request);
100 ErrorState(); // not implemented.
101 ~ErrorState();
102
103 /**
104 * Allocates and initializes an error response
105 */
106 HttpReply *BuildHttpReply(void);
107
108 /// set error type-specific detail code
109 void detailError(int detailCode);
110
111 private:
112 /**
113 * Locates error page template to be used for this error
114 * and constructs the HTML page content from it.
115 */
116 MemBuf *BuildContent(void);
117
118 /**
119 * Convert the given template string into textual output
120 *
121 * \param text The string to be converted
122 * \param allowRecursion Whether to convert codes which output may contain codes
123 */
124 MemBuf *ConvertText(const char *text, bool allowRecursion);
125
126 /**
127 * Generates the Location: header value for a deny_info error page
128 * to be used for this error.
129 */
130 void DenyInfoLocation(const char *name, HttpRequest *request, MemBuf &result);
131
132 /**
133 * Map the Error page and deny_info template % codes into textual output.
134 *
135 * Several of the codes produce blocks of non-URL compatible results.
136 * When processing the deny_info location URL they will be skipped.
137 *
138 * \param token The token following % which need to be converted
139 * \param building_deny_info_url Perform special deny_info actions, such as URL-encoding and token skipping.
140 * \ allowRecursion True if the codes which do recursions should converted
141 */
142 const char *Convert(char token, bool building_deny_info_url, bool allowRecursion);
143
144 /**
145 * CacheManager / Debug dump of the ErrorState object.
146 * Writes output into the given MemBuf.
147 \retval 0 successful completion.
148 */
149 int Dump(MemBuf * mb);
150
151 public:
152 err_type type;
153 int page_id;
154 char *err_language;
155 http_status httpStatus;
156 #if USE_AUTH
157 Auth::UserRequest::Pointer auth_user_request;
158 #endif
159 HttpRequest *request;
160 char *url;
161 int xerrno;
162 unsigned short port;
163 String dnsError; ///< DNS lookup error message
164 time_t ttl;
165
166 Ip::Address src_addr;
167 char *redirect_url;
168 ERCB *callback;
169 void *callback_data;
170
171 struct {
172 unsigned int flag_cbdata:1;
173 } flags;
174
175 struct {
176 wordlist *server_msg;
177 char *request;
178 char *reply;
179 char *cwd_msg;
180 MemBuf *listing;
181 } ftp;
182
183 char *request_hdrs;
184 char *err_msg; /* Preformatted error message from the cache */
185
186 #if USE_SSL
187 Ssl::ErrorDetail *detail;
188 #endif
189 /// type-specific detail about the transaction error;
190 /// overwrites xerrno; overwritten by detail, if any.
191 int detailCode;
192 private:
193 CBDATA_CLASS2(ErrorState);
194 };
195
196 /**
197 \ingroup ErrorPageAPI
198 *
199 * This function finds the error messages formats, and stores
200 * them in error_text[]
201 *
202 \par Global effects:
203 * error_text[] - is modified
204 */
205 SQUIDCEXTERN void errorInitialize(void);
206
207 /// \ingroup ErrorPageAPI
208 SQUIDCEXTERN void errorClean(void);
209
210 /**
211 * \ingroup ErrorPageAPI
212 *
213 * This function generates a error page from the info contained
214 * by err and then sends it to the client.
215 * The callback function errorSendComplete() is called after
216 * the page has been written to the client (clientConn).
217 * errorSendComplete() deallocates err. We need to add
218 * err to the cbdata because comm_write() requires it
219 * for all callback data pointers.
220 *
221 \note normally errorSend() should only be called from
222 * routines in ssl.c and pass.c, where we don't have any
223 * StoreEntry's. In client_side.c we must allocate a StoreEntry
224 * for errors and use errorAppendEntry() to account for
225 * persistent/pipeline connections.
226 *
227 \param clientConn socket where page object is to be written
228 \param err This object is destroyed after use in this function.
229 */
230 SQUIDCEXTERN void errorSend(const Comm::ConnectionPointer &conn, ErrorState *err);
231
232 /**
233 \ingroup ErrorPageAPI
234 *
235 * This function generates a error page from the info contained
236 * by err and then stores the text in the specified store
237 * entry.
238 * This function should only be called by "server
239 * side routines" which need to communicate errors to the
240 * client side. It should also be called from client_side.c
241 * because we now support persistent connections, and
242 * cannot assume that we can immediately write to the socket
243 * for an error.
244 *
245 \param entry ??
246 \param err This object is destroyed after use in this function.
247 */
248 SQUIDCEXTERN void errorAppendEntry(StoreEntry *entry, ErrorState *err);
249
250 /// \ingroup ErrorPageAPI
251 SQUIDCEXTERN err_type errorReservePageId(const char *page_name);
252
253 SQUIDCEXTERN const char *errorPageName(int pageId); ///< error ID to string
254
255 /**
256 \ingroup ErrorPageAPI
257 *
258 * loads text templates used for error pages and details;
259 * supports translation of templates
260 */
261 class TemplateFile
262 {
263 public:
264 TemplateFile(const char *name, const err_type code);
265 virtual ~TemplateFile() {}
266
267 /// return true if the data loaded from disk without any problem
268 bool loaded() const {return wasLoaded;}
269
270 /**
271 * Load the page_name template from a file which probably exist at:
272 * (a) admin specified custom directory (error_directory)
273 * (b) default language translation directory (error_default_language)
274 * (c) English sub-directory where errors should ALWAYS exist
275 */
276 bool loadDefault();
277
278 /**
279 * Load an error template for a given HTTP request. This function examines the
280 * Accept-Language header and select the first available template. If the default
281 * template selected (eg because of a "Accept-Language: *"), or not available
282 * template found this function return false.
283 */
284 bool loadFor(HttpRequest *request);
285
286 /**
287 * Load the file given by "path". It uses the "parse()" method.
288 * On success return true and sets the "defined" member
289 */
290 bool loadFromFile(const char *path);
291
292 /// The language used for the template
293 const char *language() {return errLanguage.termedBuf();}
294
295 bool silent; ///< Whether to print error messages on cache.log file or not. It is user defined.
296
297 protected:
298 /// Used to parse (if parsing required) the template data .
299 virtual bool parse(const char *buf, int len, bool eof) = 0;
300
301 /**
302 * Try to load the "page_name" template for a given language "lang"
303 * from squid errors directory
304 \return true on success false otherwise
305 */
306 bool tryLoadTemplate(const char *lang);
307
308 bool wasLoaded; ///< True if the template data read from disk without any problem
309 String errLanguage; ///< The error language of the template.
310 String templateName; ///< The name of the template
311 err_type templateCode; ///< The internal code for this template.
312 };
313
314 /**
315 * Parses the Accept-Language header value and return one language item on
316 * each call.
317 * \param hdr is the Accept-Language header value
318 * \param lang a buffer given by the user to store parsed language
319 * \param langlen the length of the lang buffer
320 * \param pos it is used to store the state of parsing. Must be "0" on first call
321 * \return true on success, false otherwise
322 */
323 bool strHdrAcptLangGetItem(const String &hdr, char *lang, int langLen, size_t &pos);
324 #endif /* SQUID_ERRORPAGE_H */