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 "error/Detail.h"
17 #include "error/forward.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 /// error page callback
28 typedef void ERCB(int fd
, void *, size_t);
31 \defgroup ErrorPageAPI Error Pages API
33 \section ErrorPageStringCodes Error Page % codes for text insertion.
37 A - Local listening IP address x
38 B - URL with FTP %2f hack x
39 c - Squid error code x
40 d - seconds elapsed since request received x
44 f - FTP request line x
46 g - FTP server message x
48 H - server host name x
49 i - client IP address x
50 I - server IP address x
51 l - HREF link for CSS stylesheet inclusion x
52 L - HREF link for more info/contact x
54 m - Error message returned by auth helper x
55 o - Message returned external acl helper x
58 R - Full HTTP Request x
59 S - squid signature from ERR_SIGNATURE x
60 s - caching proxy software with version x
63 U - URL without password x
64 u - URL with password x
65 w - cachemgr email address x
66 W - error data (to be included in the mailto links)
68 z - dns server error message x
69 Z - Preformatted error message x
72 * Plus logformat %codes embedded using @Squid{%logformat_code} syntax.
83 } // namespace ErrorPage
85 /// \ingroup ErrorPageAPI
88 CBDATA_CLASS(ErrorState
);
91 /// creates an error of type other than ERR_RELAY_REMOTE
92 ErrorState(err_type type
, Http::StatusCode
, HttpRequest
* request
, const AccessLogEntryPointer
&al
);
93 ErrorState() = delete; // not implemented.
95 /// creates an ERR_RELAY_REMOTE error
96 ErrorState(HttpRequest
* request
, HttpReply
*);
100 /// Creates a general request forwarding error with the right http_status.
101 static ErrorState
*NewForwarding(err_type
, HttpRequestPointer
&, const AccessLogEntryPointer
&);
104 * Allocates and initializes an error response
106 HttpReply
*BuildHttpReply(void);
108 /// set error type-specific detail code
109 void detailError(const ErrorDetail::Pointer
&dCode
) { detail
= dCode
; }
111 /// ensures that a future BuildHttpReply() is likely to succeed
114 /// the source of the error template (for reporting purposes)
118 typedef ErrorPage::Build Build
;
120 /// initializations shared by public constructors
121 explicit ErrorState(err_type type
);
123 /// locates the right error page template for this error and compiles it
126 /// compiles error page or error detail template (i.e. anything but deny_url)
127 /// \param input the template text to be compiled
128 /// \param allowRecursion whether to compile %codes which produce %codes
129 SBuf
compileBody(const char *text
, bool allowRecursion
);
131 /// compile a single-letter %code like %D
132 void compileLegacyCode(Build
&build
);
134 /// compile @Squid{%code} sequence containing a single logformat %code
135 void compileLogformatCode(Build
&build
);
137 /// replaces all legacy and logformat %codes in the given input
138 /// \param input the template text to be converted
139 /// \param building_deny_info_url whether input is a deny_info URL parameter
140 /// \param allowRecursion whether to compile %codes which produce %codes
141 /// \returns the given input with all %codes substituted
142 SBuf
compile(const char *input
, bool building_deny_info_url
, bool allowRecursion
);
144 /// React to a compile() error, throwing if buildContext allows.
145 /// \param msg description of what went wrong
146 /// \param near approximate start of the problematic input
147 void noteBuildError(const char *msg
, const char *near
) {
148 noteBuildError_(msg
, near
, false);
151 /// Note a compile() error but do not throw for backwards
152 /// compatibility with older configurations that may have such errors.
153 /// Should eventually be replaced with noteBuildError().
154 /// \param msg description of what went wrong
155 /// \param near approximate start of the problematic input
156 void bypassBuildErrorXXX(const char *msg
, const char *near
) {
157 noteBuildError_(msg
, near
, true);
161 * CacheManager / Debug dump of the ErrorState object.
162 * Writes output into the given MemBuf.
163 \retval 0 successful completion.
165 int Dump(MemBuf
* mb
);
168 err_type type
= ERR_NONE
;
169 int page_id
= ERR_NONE
;
170 char *err_language
= nullptr;
171 Http::StatusCode httpStatus
= Http::scNone
;
173 Auth::UserRequest::Pointer auth_user_request
;
175 HttpRequestPointer request
;
178 unsigned short port
= 0;
179 String dnsError
; ///< DNS lookup error message
182 Ip::Address src_addr
;
183 char *redirect_url
= nullptr;
185 void *callback_data
= nullptr;
188 wordlist
*server_msg
= nullptr;
189 char *request
= nullptr;
190 char *reply
= nullptr;
191 char *cwd_msg
= nullptr;
192 MemBuf
*listing
= nullptr;
195 char *request_hdrs
= nullptr;
196 char *err_msg
= nullptr; /* Preformatted error message from the cache */
198 AccessLogEntryPointer ale
; ///< transaction details (or nil)
200 // TODO: Replace type, xerrno and detail with Error while adding a virtual
201 // Error::Detail::sysError() method to extract errno in detailError().
202 /// type-specific detail about the transaction error;
203 /// overwrites xerrno;
204 ErrorDetail::Pointer detail
;
206 HttpReplyPointer response_
;
209 void noteBuildError_(const char *msg
, const char *near
, const bool forceBypass
);
211 static const SBuf LogformatMagic
; ///< marks each embedded logformat entry
215 \ingroup ErrorPageAPI
217 * This function finds the error messages formats, and stores
218 * them in error_text[]
221 * error_text[] - is modified
223 void errorInitialize(void);
225 /// \ingroup ErrorPageAPI
226 void errorClean(void);
229 * \ingroup ErrorPageAPI
231 * This function generates a error page from the info contained
232 * by err and then sends it to the client.
233 * The callback function errorSendComplete() is called after
234 * the page has been written to the client (clientConn).
235 * errorSendComplete() deallocates err. We need to add
236 * err to the cbdata because comm_write() requires it
237 * for all callback data pointers.
239 \note normally errorSend() should only be called from
240 * routines in ssl.c and pass.c, where we don't have any
241 * StoreEntry's. In client_side.c we must allocate a StoreEntry
242 * for errors and use errorAppendEntry() to account for
243 * persistent/pipeline connections.
245 \param clientConn socket where page object is to be written
246 \param err This object is destroyed after use in this function.
248 void errorSend(const Comm::ConnectionPointer
&conn
, ErrorState
*err
);
251 \ingroup ErrorPageAPI
253 * This function generates a error page from the info contained
254 * by err and then stores the text in the specified store
256 * This function should only be called by "server
257 * side routines" which need to communicate errors to the
258 * client side. It should also be called from client_side.c
259 * because we now support persistent connections, and
260 * cannot assume that we can immediately write to the socket
264 \param err This object is destroyed after use in this function.
266 void errorAppendEntry(StoreEntry
*entry
, ErrorState
*err
);
268 /// allocates a new slot for the error page
269 err_type
errorReservePageId(const char *page_name
, const SBuf
&cfgLocation
);
271 const char *errorPageName(int pageId
); ///< error ID to string
274 \ingroup ErrorPageAPI
276 * loads text templates used for error pages and details;
277 * supports translation of templates
282 TemplateFile(const char *name
, const err_type code
);
283 virtual ~TemplateFile() {}
285 /// return true if the data loaded from disk without any problem
286 bool loaded() const {return wasLoaded
;}
289 * Load the page_name template from a file which probably exist at:
290 * (a) admin specified custom directory (error_directory)
291 * (b) default language translation directory (error_default_language)
292 * (c) English sub-directory where errors should ALWAYS exist
293 * If all of the above fail, setDefault() is called.
298 * Load an error template for a given HTTP request. This function examines the
299 * Accept-Language header and select the first available template. If the default
300 * template selected (eg because of a "Accept-Language: *"), or not available
301 * template found this function return false.
303 bool loadFor(const HttpRequest
*request
);
306 * Load the file given by "path". It uses the "parse()" method.
307 * On success return true and sets the "defined" member
309 bool loadFromFile(const char *path
);
311 /// The language used for the template
312 const char *language() {return errLanguage
.termedBuf();}
314 SBuf filename
; ///< where the template was loaded from
316 bool silent
; ///< Whether to print error messages on cache.log file or not. It is user defined.
319 /// post-process the loaded template
320 virtual bool parse() { return true; }
322 /// recover from loadDefault() failure to load or parse() a template
323 virtual void setDefault() {}
326 * Try to load the "page_name" template for a given language "lang"
327 * from squid errors directory
328 \return true on success false otherwise
330 bool tryLoadTemplate(const char *lang
);
332 SBuf template_
; ///< raw template contents
333 bool wasLoaded
; ///< True if the template data read from disk without any problem
334 String errLanguage
; ///< The error language of the template.
335 String templateName
; ///< The name of the template
336 err_type templateCode
; ///< The internal code for this template.
340 * Parses the Accept-Language header value and return one language item on
342 * Will ignore any whitespace, q-values, and detectably invalid language
343 * codes in the header.
345 * \param hdr is the Accept-Language header value
346 * \param lang a buffer to store parsed language code in
347 * \param langlen the length of the lang buffer
348 * \param pos is used to store the offset state of parsing. Must be "0" on first call.
349 * Will be altered to point at the start of next field-value.
350 * \return true if something looking like a language token has been placed in lang, false otherwise
352 bool strHdrAcptLangGetItem(const String
&hdr
, char *lang
, int langLen
, size_t &pos
);
354 #endif /* SQUID_ERRORPAGE_H */