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