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