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