2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 04 Error Generation */
11 #ifndef SQUID_ERRORPAGE_H
12 #define SQUID_ERRORPAGE_H
15 #include "comm/forward.h"
16 #include "err_detail_type.h"
18 #include "http/forward.h"
19 #include "http/StatusCode.h"
20 #include "ip/Address.h"
21 #include "log/forward.h"
22 #include "sbuf/SBuf.h"
23 #include "SquidString.h"
24 /* auth/UserRequest.h is empty unless USE_AUTH is defined */
25 #include "auth/UserRequest.h"
27 #include "ssl/ErrorDetail.h"
30 /// error page callback
31 typedef void ERCB(int fd
, void *, size_t);
34 \defgroup ErrorPageAPI Error Pages API
36 \section ErrorPageStringCodes Error Page % codes for text insertion.
40 A - Local listening IP address x
41 B - URL with FTP %2f hack x
42 c - Squid error code x
43 d - seconds elapsed since request received x
47 f - FTP request line x
49 g - FTP server message x
51 H - server host name x
52 i - client IP address x
53 I - server IP address x
54 l - HREF link for CSS stylesheet inclusion x
55 L - HREF link for more info/contact x
57 m - Error message returned by auth helper x
58 o - Message returned external acl helper x
61 R - Full HTTP Request x
62 S - squid signature from ERR_SIGNATURE x
63 s - caching proxy software with version x
66 U - URL without password x
67 u - URL with password x
68 w - cachemgr email address x
69 W - error data (to be included in the mailto links)
71 z - dns server error message x
72 Z - Preformatted error message x
75 * Plus logformat %codes embedded using @Squid{%logformat_code} syntax.
86 } // namespace ErrorPage
88 /// \ingroup ErrorPageAPI
91 CBDATA_CLASS(ErrorState
);
94 /// creates an error of type other than ERR_RELAY_REMOTE
95 ErrorState(err_type type
, Http::StatusCode
, HttpRequest
* request
, const AccessLogEntryPointer
&al
);
96 ErrorState() = delete; // not implemented.
98 /// creates an ERR_RELAY_REMOTE error
99 ErrorState(HttpRequest
* request
, HttpReply
*);
103 /// Creates a general request forwarding error with the right http_status.
104 static ErrorState
*NewForwarding(err_type
, HttpRequestPointer
&, const AccessLogEntryPointer
&);
107 * Allocates and initializes an error response
109 HttpReply
*BuildHttpReply(void);
111 /// set error type-specific detail code
112 void detailError(int dCode
) {detailCode
= dCode
;}
114 /// ensures that a future BuildHttpReply() is likely to succeed
117 /// the source of the error template (for reporting purposes)
121 typedef ErrorPage::Build Build
;
123 /// initializations shared by public constructors
124 explicit ErrorState(err_type type
);
126 /// locates the right error page template for this error and compiles it
129 /// compiles error page or error detail template (i.e. anything but deny_url)
130 /// \param input the template text to be compiled
131 /// \param allowRecursion whether to compile %codes which produce %codes
132 SBuf
compileBody(const char *text
, bool allowRecursion
);
134 /// compile a single-letter %code like %D
135 void compileLegacyCode(Build
&build
);
137 /// compile @Squid{%code} sequence containing a single logformat %code
138 void compileLogformatCode(Build
&build
);
140 /// replaces all legacy and logformat %codes in the given input
141 /// \param input the template text to be converted
142 /// \param building_deny_info_url whether input is a deny_info URL parameter
143 /// \param allowRecursion whether to compile %codes which produce %codes
144 /// \returns the given input with all %codes substituted
145 SBuf
compile(const char *input
, bool building_deny_info_url
, bool allowRecursion
);
147 /// React to a compile() error, throwing if buildContext allows.
148 /// \param msg description of what went wrong
149 /// \param near approximate start of the problematic input
150 void noteBuildError(const char *msg
, const char *near
) {
151 noteBuildError_(msg
, near
, false);
154 /// Note a compile() error but do not throw for backwards
155 /// compatibility with older configurations that may have such errors.
156 /// Should eventually be replaced with noteBuildError().
157 /// \param msg description of what went wrong
158 /// \param near approximate start of the problematic input
159 void bypassBuildErrorXXX(const char *msg
, const char *near
) {
160 noteBuildError_(msg
, near
, true);
164 * CacheManager / Debug dump of the ErrorState object.
165 * Writes output into the given MemBuf.
166 \retval 0 successful completion.
168 int Dump(MemBuf
* mb
);
171 err_type type
= ERR_NONE
;
172 int page_id
= ERR_NONE
;
173 char *err_language
= nullptr;
174 Http::StatusCode httpStatus
= Http::scNone
;
176 Auth::UserRequest::Pointer auth_user_request
;
178 HttpRequestPointer request
;
181 unsigned short port
= 0;
182 String dnsError
; ///< DNS lookup error message
185 Ip::Address src_addr
;
186 char *redirect_url
= nullptr;
188 void *callback_data
= nullptr;
191 wordlist
*server_msg
= nullptr;
192 char *request
= nullptr;
193 char *reply
= nullptr;
194 char *cwd_msg
= nullptr;
195 MemBuf
*listing
= nullptr;
198 char *request_hdrs
= nullptr;
199 char *err_msg
= nullptr; /* Preformatted error message from the cache */
201 AccessLogEntryPointer ale
; ///< transaction details (or nil)
204 Ssl::ErrorDetail
*detail
= nullptr;
206 /// type-specific detail about the transaction error;
207 /// overwrites xerrno; overwritten by detail, if any.
208 int detailCode
= ERR_DETAIL_NONE
;
210 HttpReplyPointer response_
;
213 void noteBuildError_(const char *msg
, const char *near
, const bool forceBypass
);
215 static const SBuf LogformatMagic
; ///< marks each embedded logformat entry
219 \ingroup ErrorPageAPI
221 * This function finds the error messages formats, and stores
222 * them in error_text[]
225 * error_text[] - is modified
227 void errorInitialize(void);
229 /// \ingroup ErrorPageAPI
230 void errorClean(void);
233 * \ingroup ErrorPageAPI
235 * This function generates a error page from the info contained
236 * by err and then sends it to the client.
237 * The callback function errorSendComplete() is called after
238 * the page has been written to the client (clientConn).
239 * errorSendComplete() deallocates err. We need to add
240 * err to the cbdata because comm_write() requires it
241 * for all callback data pointers.
243 \note normally errorSend() should only be called from
244 * routines in ssl.c and pass.c, where we don't have any
245 * StoreEntry's. In client_side.c we must allocate a StoreEntry
246 * for errors and use errorAppendEntry() to account for
247 * persistent/pipeline connections.
249 \param clientConn socket where page object is to be written
250 \param err This object is destroyed after use in this function.
252 void errorSend(const Comm::ConnectionPointer
&conn
, ErrorState
*err
);
255 \ingroup ErrorPageAPI
257 * This function generates a error page from the info contained
258 * by err and then stores the text in the specified store
260 * This function should only be called by "server
261 * side routines" which need to communicate errors to the
262 * client side. It should also be called from client_side.c
263 * because we now support persistent connections, and
264 * cannot assume that we can immediately write to the socket
268 \param err This object is destroyed after use in this function.
270 void errorAppendEntry(StoreEntry
*entry
, ErrorState
*err
);
272 /// allocates a new slot for the error page
273 err_type
errorReservePageId(const char *page_name
, const SBuf
&cfgLocation
);
275 const char *errorPageName(int pageId
); ///< error ID to string
278 \ingroup ErrorPageAPI
280 * loads text templates used for error pages and details;
281 * supports translation of templates
286 TemplateFile(const char *name
, const err_type code
);
287 virtual ~TemplateFile() {}
289 /// return true if the data loaded from disk without any problem
290 bool loaded() const {return wasLoaded
;}
293 * Load the page_name template from a file which probably exist at:
294 * (a) admin specified custom directory (error_directory)
295 * (b) default language translation directory (error_default_language)
296 * (c) English sub-directory where errors should ALWAYS exist
297 * If all of the above fail, setDefault() is called.
302 * Load an error template for a given HTTP request. This function examines the
303 * Accept-Language header and select the first available template. If the default
304 * template selected (eg because of a "Accept-Language: *"), or not available
305 * template found this function return false.
307 bool loadFor(const HttpRequest
*request
);
310 * Load the file given by "path". It uses the "parse()" method.
311 * On success return true and sets the "defined" member
313 bool loadFromFile(const char *path
);
315 /// The language used for the template
316 const char *language() {return errLanguage
.termedBuf();}
318 SBuf filename
; ///< where the template was loaded from
320 bool silent
; ///< Whether to print error messages on cache.log file or not. It is user defined.
323 /// post-process the loaded template
324 virtual bool parse() { return true; }
326 /// recover from loadDefault() failure to load or parse() a template
327 virtual void setDefault() {}
330 * Try to load the "page_name" template for a given language "lang"
331 * from squid errors directory
332 \return true on success false otherwise
334 bool tryLoadTemplate(const char *lang
);
336 SBuf template_
; ///< raw template contents
337 bool wasLoaded
; ///< True if the template data read from disk without any problem
338 String errLanguage
; ///< The error language of the template.
339 String templateName
; ///< The name of the template
340 err_type templateCode
; ///< The internal code for this template.
344 * Parses the Accept-Language header value and return one language item on
346 * Will ignore any whitespace, q-values, and detectably invalid language
347 * codes in the header.
349 * \param hdr is the Accept-Language header value
350 * \param lang a buffer to store parsed language code in
351 * \param langlen the length of the lang buffer
352 * \param pos is used to store the offset state of parsing. Must be "0" on first call.
353 * Will be altered to point at the start of next field-value.
354 * \return true if something looking like a language token has been placed in lang, false otherwise
356 bool strHdrAcptLangGetItem(const String
&hdr
, char *lang
, int langLen
, size_t &pos
);
358 #endif /* SQUID_ERRORPAGE_H */