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