]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/ErrorDetailManager.cc
2 #include "ErrorDetail.h"
4 #include "ErrorDetailManager.h"
6 void Ssl::errorDetailInitialize()
8 Ssl::ErrorDetailsManager::GetInstance();
11 void Ssl::errorDetailClean()
13 Ssl::ErrorDetailsManager::Shutdown();
19 /// manages error detail templates
20 class ErrorDetailFile
: public TemplateFile
23 explicit ErrorDetailFile(ErrorDetailsList::Pointer
const details
): TemplateFile("error-details.txt", ERR_NONE
) {
30 ErrorDetailsList::Pointer theDetails
;
31 virtual bool parse(const char *buf
, int len
, bool eof
);
37 Ssl::ErrorDetailsList::getRecord(Ssl::ssl_error_t value
, ErrorDetailEntry
&entry
)
39 const ErrorDetails::const_iterator it
= theList
.find(value
);
40 if (it
!= theList
.end()) {
41 entry
.error_no
= it
->second
.error_no
;
42 entry
.name
= it
->second
.name
;
43 entry
.detail
= it
->second
.detail
;
44 entry
.descr
= it
->second
.descr
;
51 Ssl::ErrorDetailsList::getErrorDescr(Ssl::ssl_error_t value
)
53 const ErrorDetails::const_iterator it
= theList
.find(value
);
54 if (it
!= theList
.end()) {
55 return it
->second
.descr
.termedBuf();
62 Ssl::ErrorDetailsList::getErrorDetail(Ssl::ssl_error_t value
)
64 const ErrorDetails::const_iterator it
= theList
.find(value
);
65 if (it
!= theList
.end()) {
66 return it
->second
.detail
.termedBuf();
72 Ssl::ErrorDetailsManager
*Ssl::ErrorDetailsManager::TheDetailsManager
= NULL
;
74 Ssl::ErrorDetailsManager
&Ssl::ErrorDetailsManager::GetInstance()
76 if (!TheDetailsManager
)
77 TheDetailsManager
= new Ssl::ErrorDetailsManager
;
79 assert(TheDetailsManager
);
80 return *TheDetailsManager
;
83 void Ssl::ErrorDetailsManager::Shutdown()
85 delete TheDetailsManager
;
86 TheDetailsManager
= NULL
;
90 Ssl::ErrorDetailsManager::ErrorDetailsManager()
92 theDefaultErrorDetails
= new ErrorDetailsList();
93 ErrorDetailFile
detailTmpl(theDefaultErrorDetails
);
94 detailTmpl
.loadDefault();
97 Ssl::ErrorDetailsList::Pointer
Ssl::ErrorDetailsManager::getCachedDetails(const char *lang
)
100 it
= cache
.find(lang
);
101 if (it
!= cache
.end()) {
102 debugs(83, 8, HERE
<< "Found template details in cache for language: " << lang
);
109 void Ssl::ErrorDetailsManager::cacheDetails(ErrorDetailsList::Pointer
&errorDetails
)
111 const char *lang
= errorDetails
->errLanguage
.termedBuf();
113 if (cache
.find(lang
) == cache
.end())
114 cache
[lang
] = errorDetails
;
118 Ssl::ErrorDetailsManager::getErrorDetail(Ssl::ssl_error_t value
, HttpRequest
*request
, ErrorDetailEntry
&entry
)
122 if (request
&& request
->header
.getList(HDR_ACCEPT_LANGUAGE
, &hdr
)) {
123 ErrorDetailsList::Pointer errDetails
= NULL
;
124 //Try to retrieve from cache
127 // Get the first ellement of the Accept-Language header
128 strHdrAcptLangGetItem(hdr
, lang
, 256, pos
);
129 errDetails
= getCachedDetails(lang
); // search in cache
131 if (!errDetails
) { // Else try to load from disk
132 debugs(83, 8, HERE
<< "Creating new ErrDetailList to read from disk");
133 errDetails
= new ErrorDetailsList();
134 ErrorDetailFile
detailTmpl(errDetails
);
135 if (detailTmpl
.loadFor(request
)) {
136 if (detailTmpl
.language()) {
137 debugs(83, 8, HERE
<< "Found details on disk for language " << detailTmpl
.language());
138 errDetails
->errLanguage
= detailTmpl
.language();
139 cacheDetails(errDetails
);
144 if (errDetails
!= NULL
&& errDetails
->getRecord(value
, entry
))
149 // else try the default
150 if (theDefaultErrorDetails
->getRecord(value
, entry
)) {
151 debugs(83, 8, HERE
<< "Found default details record for error: " << GetErrorName(value
));
159 Ssl::ErrorDetailsManager::getDefaultErrorDescr(Ssl::ssl_error_t value
)
161 return theDefaultErrorDetails
->getErrorDescr(value
);
165 Ssl::ErrorDetailsManager::getDefaultErrorDetail(Ssl::ssl_error_t value
)
167 return theDefaultErrorDetails
->getErrorDetail(value
);
170 // Use HttpHeaders parser to parse error-details.txt files
171 class DetailEntryParser
: public HttpHeader
174 DetailEntryParser():HttpHeader(hoErrorDetail
) {}
177 //The end of an error detrail entry is a double "\n". The headersEnd
178 // functions can detect it
179 inline size_t detailEntryEnd(const char *s
, size_t len
) {return headersEnd(s
, len
);}
182 Ssl::ErrorDetailFile::parse(const char *buffer
, int len
, bool eof
)
188 buf
.append(buffer
, len
);
192 buf
.append("\n\n", 1);
194 while (size_t size
= detailEntryEnd(buf
.content(), buf
.contentSize())) {
195 const char *e
= buf
.content() + size
;
197 //ignore spaces, new lines and comment lines (starting with #) at the beggining
199 for (s
= buf
.content(); (*s
== '\n' || *s
== ' ' || *s
== '\t' || *s
== '#') && s
< e
; s
++) {
201 while (s
<e
&& *s
!= '\n') s
++; // skip untill the end of line
205 DetailEntryParser parser
;
206 if (!parser
.parse(s
, e
)) {
207 debugs(83, DBG_IMPORTANT
, HERE
<<
208 "WARNING! parse error on:" << s
);
212 const char *errorName
= parser
.getByName("name").termedBuf();
214 debugs(83, DBG_IMPORTANT
, HERE
<<
215 "WARNING! invalid or no error detail name on:" << s
);
219 Ssl::ssl_error_t ssl_error
= Ssl::GetErrorCode(errorName
);
220 if (ssl_error
== SSL_ERROR_NONE
) {
221 debugs(83, DBG_IMPORTANT
, HERE
<<
222 "WARNING! invalid error detail name: " << errorName
);
226 if (theDetails
->getErrorDetail(ssl_error
)) {
227 debugs(83, DBG_IMPORTANT
, HERE
<<
228 "WARNING! duplicate entry: " << errorName
);
232 ErrorDetailEntry
&entry
= theDetails
->theList
[ssl_error
];
233 entry
.error_no
= ssl_error
;
234 entry
.name
= errorName
;
235 String tmp
= parser
.getByName("detail");
236 httpHeaderParseQuotedString(tmp
.termedBuf(), tmp
.size(), &entry
.detail
);
237 tmp
= parser
.getByName("descr");
238 httpHeaderParseQuotedString(tmp
.termedBuf(), tmp
.size(), &entry
.descr
);
239 bool parseOK
= entry
.descr
.defined() && entry
.detail
.defined();
242 debugs(83, DBG_IMPORTANT
, HERE
<<
243 "WARNING! missing imporant field for detail error: " << errorName
);
246 }// else {only spaces and black lines; just ignore}
250 debugs(83, 9, HERE
<< " Remain size: " << buf
.contentSize() << " Content: " << buf
.content());