2 * Copyright (C) 1996-2023 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.
11 #include "ErrorDetail.h"
12 #include "ErrorDetailManager.h"
13 #include "errorpage.h"
14 #include "http/ContentLengthInterpreter.h"
15 #include "mime_header.h"
17 void Ssl::errorDetailInitialize()
19 Ssl::ErrorDetailsManager::GetInstance();
22 void Ssl::errorDetailClean()
24 Ssl::ErrorDetailsManager::Shutdown();
30 /// manages error detail templates
31 class ErrorDetailFile
: public TemplateFile
34 explicit ErrorDetailFile(ErrorDetailsList::Pointer
const details
): TemplateFile("error-details.txt", ERR_NONE
) {
39 ErrorDetailsList::Pointer theDetails
;
40 bool parse() override
;
46 Ssl::ErrorDetailsList::getRecord(Security::ErrorCode value
, ErrorDetailEntry
&entry
)
48 const ErrorDetails::const_iterator it
= theList
.find(value
);
49 if (it
!= theList
.end()) {
50 entry
.error_no
= it
->second
.error_no
;
51 entry
.name
= it
->second
.name
;
52 entry
.detail
= it
->second
.detail
;
53 entry
.descr
= it
->second
.descr
;
60 Ssl::ErrorDetailsList::getErrorDescr(Security::ErrorCode value
)
62 const ErrorDetails::const_iterator it
= theList
.find(value
);
63 if (it
!= theList
.end()) {
64 return it
->second
.descr
.termedBuf();
71 Ssl::ErrorDetailsList::getErrorDetail(Security::ErrorCode value
)
73 const ErrorDetails::const_iterator it
= theList
.find(value
);
74 if (it
!= theList
.end()) {
75 return it
->second
.detail
.termedBuf();
81 Ssl::ErrorDetailsManager
*Ssl::ErrorDetailsManager::TheDetailsManager
= nullptr;
83 Ssl::ErrorDetailsManager
&Ssl::ErrorDetailsManager::GetInstance()
85 if (!TheDetailsManager
)
86 TheDetailsManager
= new Ssl::ErrorDetailsManager
;
88 assert(TheDetailsManager
);
89 return *TheDetailsManager
;
92 void Ssl::ErrorDetailsManager::Shutdown()
94 delete TheDetailsManager
;
95 TheDetailsManager
= nullptr;
98 Ssl::ErrorDetailsManager::ErrorDetailsManager()
100 theDefaultErrorDetails
= new ErrorDetailsList();
101 ErrorDetailFile
detailTmpl(theDefaultErrorDetails
);
102 detailTmpl
.loadDefault();
105 Ssl::ErrorDetailsList::Pointer
Ssl::ErrorDetailsManager::getCachedDetails(const char *lang
)
108 it
= cache
.find(lang
);
109 if (it
!= cache
.end()) {
110 debugs(83, 8, "Found template details in cache for language: " << lang
);
117 void Ssl::ErrorDetailsManager::cacheDetails(ErrorDetailsList::Pointer
&errorDetails
)
119 const char *lang
= errorDetails
->errLanguage
.termedBuf();
121 if (cache
.find(lang
) == cache
.end())
122 cache
[lang
] = errorDetails
;
126 Ssl::ErrorDetailsManager::getErrorDetail(Security::ErrorCode value
, const HttpRequest::Pointer
&request
, ErrorDetailEntry
&entry
)
130 if (request
!= nullptr && request
->header
.getList(Http::HdrType::ACCEPT_LANGUAGE
, &hdr
)) {
131 ErrorDetailsList::Pointer errDetails
= nullptr;
132 //Try to retrieve from cache
135 // Get the first ellement of the Accept-Language header
136 strHdrAcptLangGetItem(hdr
, lang
, 256, pos
);
137 errDetails
= getCachedDetails(lang
); // search in cache
139 if (!errDetails
) { // Else try to load from disk
140 debugs(83, 8, "Creating new ErrDetailList to read from disk");
141 errDetails
= new ErrorDetailsList();
142 ErrorDetailFile
detailTmpl(errDetails
);
143 if (detailTmpl
.loadFor(request
.getRaw())) {
144 if (detailTmpl
.language()) {
145 debugs(83, 8, "Found details on disk for language " << detailTmpl
.language());
146 errDetails
->errLanguage
= detailTmpl
.language();
147 cacheDetails(errDetails
);
152 if (errDetails
!= nullptr && errDetails
->getRecord(value
, entry
))
159 // else try the default
160 if (theDefaultErrorDetails
->getRecord(value
, entry
)) {
161 debugs(83, 8, "Found default details record for error: " << GetErrorName(value
));
169 Ssl::ErrorDetailsManager::getDefaultErrorDescr(Security::ErrorCode value
)
171 return theDefaultErrorDetails
->getErrorDescr(value
);
175 Ssl::ErrorDetailsManager::getDefaultErrorDetail(Security::ErrorCode value
)
177 return theDefaultErrorDetails
->getErrorDetail(value
);
180 // Use HttpHeaders parser to parse error-details.txt files
181 class DetailEntryParser
: public HttpHeader
184 DetailEntryParser():HttpHeader(hoErrorDetail
) {}
187 //The end of an error detrail entry is a double "\n". The headersEnd
188 // functions can detect it
189 inline size_t detailEntryEnd(const char *s
, size_t len
) {return headersEnd(s
, len
);}
192 Ssl::ErrorDetailFile::parse()
197 auto buf
= template_
;
198 buf
.append("\n\n"); // ensure detailEntryEnd() finds the last entry
200 while (const auto size
= detailEntryEnd(buf
.rawContent(), buf
.length())) {
201 auto *s
= buf
.c_str();
202 const auto e
= s
+ size
;
204 //ignore spaces, new lines and comment lines (starting with #) at the beginning
205 for (; (*s
== '\n' || *s
== ' ' || *s
== '\t' || *s
== '#') && s
< e
; ++s
) {
207 while (s
<e
&& *s
!= '\n')
208 ++s
; // skip until the end of line
212 DetailEntryParser parser
;
213 Http::ContentLengthInterpreter interpreter
;
214 // no applyStatusCodeRules() -- error templates lack HTTP status code
215 if (!parser
.parse(s
, e
- s
, interpreter
)) {
216 debugs(83, DBG_IMPORTANT
, "WARNING: parse error on:" << s
);
220 const String errorName
= parser
.getByName("name");
221 if (!errorName
.size()) {
222 debugs(83, DBG_IMPORTANT
, "WARNING: invalid or no error detail name on:" << s
);
226 Security::ErrorCode ssl_error
= Ssl::GetErrorCode(errorName
.termedBuf());
227 if (ssl_error
!= SSL_ERROR_NONE
) {
229 if (theDetails
->getErrorDetail(ssl_error
)) {
230 debugs(83, DBG_IMPORTANT
, "WARNING: duplicate entry: " << errorName
);
234 ErrorDetailEntry
&entry
= theDetails
->theList
[ssl_error
];
235 entry
.error_no
= ssl_error
;
236 entry
.name
= errorName
;
237 String tmp
= parser
.getByName("detail");
238 const int detailsParseOk
= httpHeaderParseQuotedString(tmp
.termedBuf(), tmp
.size(), &entry
.detail
);
239 tmp
= parser
.getByName("descr");
240 const int descrParseOk
= httpHeaderParseQuotedString(tmp
.termedBuf(), tmp
.size(), &entry
.descr
);
241 // TODO: Validate "descr" and "detail" field values.
243 if (!detailsParseOk
|| !descrParseOk
) {
244 debugs(83, DBG_IMPORTANT
, "WARNING: missing important field for detail error: " << errorName
);
248 } else if (!Ssl::ErrorIsOptional(errorName
.termedBuf())) {
249 debugs(83, DBG_IMPORTANT
, "WARNING: invalid error detail name: " << errorName
);
253 }// else {only spaces and black lines; just ignore}
257 debugs(83, 9, Raw("unparsed data", buf
.rawContent(), buf
.length()));