]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/ErrorDetailManager.cc
2 * Copyright (C) 1996-2014 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.
10 #include "ErrorDetail.h"
11 #include "ErrorDetailManager.h"
12 #include "errorpage.h"
13 #include "mime_header.h"
15 void Ssl::errorDetailInitialize()
17 Ssl::ErrorDetailsManager::GetInstance();
20 void Ssl::errorDetailClean()
22 Ssl::ErrorDetailsManager::Shutdown();
28 /// manages error detail templates
29 class ErrorDetailFile
: public TemplateFile
32 explicit ErrorDetailFile(ErrorDetailsList::Pointer
const details
): TemplateFile("error-details.txt", ERR_NONE
) {
39 ErrorDetailsList::Pointer theDetails
;
40 virtual bool parse(const char *buf
, int len
, bool eof
);
46 Ssl::ErrorDetailsList::getRecord(Ssl::ssl_error_t 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(Ssl::ssl_error_t 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(Ssl::ssl_error_t 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
= NULL
;
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
= NULL
;
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, HERE
<< "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(Ssl::ssl_error_t value
, const HttpRequest::Pointer
&request
, ErrorDetailEntry
&entry
)
130 if (request
!= NULL
&& request
->header
.getList(HDR_ACCEPT_LANGUAGE
, &hdr
)) {
131 ErrorDetailsList::Pointer errDetails
= NULL
;
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, HERE
<< "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, HERE
<< "Found details on disk for language " << detailTmpl
.language());
146 errDetails
->errLanguage
= detailTmpl
.language();
147 cacheDetails(errDetails
);
152 if (errDetails
!= NULL
&& errDetails
->getRecord(value
, entry
))
157 // else try the default
158 if (theDefaultErrorDetails
->getRecord(value
, entry
)) {
159 debugs(83, 8, HERE
<< "Found default details record for error: " << GetErrorName(value
));
167 Ssl::ErrorDetailsManager::getDefaultErrorDescr(Ssl::ssl_error_t value
)
169 return theDefaultErrorDetails
->getErrorDescr(value
);
173 Ssl::ErrorDetailsManager::getDefaultErrorDetail(Ssl::ssl_error_t value
)
175 return theDefaultErrorDetails
->getErrorDetail(value
);
178 // Use HttpHeaders parser to parse error-details.txt files
179 class DetailEntryParser
: public HttpHeader
182 DetailEntryParser():HttpHeader(hoErrorDetail
) {}
185 //The end of an error detrail entry is a double "\n". The headersEnd
186 // functions can detect it
187 inline size_t detailEntryEnd(const char *s
, size_t len
) {return headersEnd(s
, len
);}
190 Ssl::ErrorDetailFile::parse(const char *buffer
, int len
, bool eof
)
196 buf
.append(buffer
, len
);
200 buf
.append("\n\n", 1);
202 while (size_t size
= detailEntryEnd(buf
.content(), buf
.contentSize())) {
203 const char *e
= buf
.content() + size
;
205 //ignore spaces, new lines and comment lines (starting with #) at the beggining
207 for (s
= buf
.content(); (*s
== '\n' || *s
== ' ' || *s
== '\t' || *s
== '#') && s
< e
; ++s
) {
209 while (s
<e
&& *s
!= '\n')
210 ++s
; // skip untill the end of line
214 DetailEntryParser parser
;
215 if (!parser
.parse(s
, e
- s
)) {
216 debugs(83, DBG_IMPORTANT
, HERE
<<
217 "WARNING! parse error on:" << s
);
221 const String errorName
= parser
.getByName("name");
222 if (!errorName
.size()) {
223 debugs(83, DBG_IMPORTANT
, HERE
<<
224 "WARNING! invalid or no error detail name on:" << s
);
228 Ssl::ssl_error_t ssl_error
= Ssl::GetErrorCode(errorName
.termedBuf());
229 if (ssl_error
!= SSL_ERROR_NONE
) {
231 if (theDetails
->getErrorDetail(ssl_error
)) {
232 debugs(83, DBG_IMPORTANT
, HERE
<<
233 "WARNING! duplicate entry: " << errorName
);
237 ErrorDetailEntry
&entry
= theDetails
->theList
[ssl_error
];
238 entry
.error_no
= ssl_error
;
239 entry
.name
= errorName
;
240 String tmp
= parser
.getByName("detail");
241 const int detailsParseOk
= httpHeaderParseQuotedString(tmp
.termedBuf(), tmp
.size(), &entry
.detail
);
242 tmp
= parser
.getByName("descr");
243 const int descrParseOk
= httpHeaderParseQuotedString(tmp
.termedBuf(), tmp
.size(), &entry
.descr
);
245 if (!detailsParseOk
|| !descrParseOk
) {
246 debugs(83, DBG_IMPORTANT
, HERE
<<
247 "WARNING! missing important field for detail error: " << errorName
);
251 } else if (!Ssl::ErrorIsOptional(errorName
.termedBuf())) {
252 debugs(83, DBG_IMPORTANT
, HERE
<<
253 "WARNING! invalid error detail name: " << errorName
);
257 }// else {only spaces and black lines; just ignore}
261 debugs(83, 9, HERE
<< " Remain size: " << buf
.contentSize() << " Content: " << buf
.content());