ErrorDetailManager.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 #include "squid.h"
10 #include "ErrorDetail.h"
11 #include "ErrorDetailManager.h"
12 #include "errorpage.h"
13 #include "mime_header.h"
14 
16 {
18 }
19 
21 {
23 }
24 
25 namespace Ssl
26 {
27 
30 {
31 public:
32  explicit ErrorDetailFile(ErrorDetailsList::Pointer const details): TemplateFile("error-details.txt", ERR_NONE) {
33  buf.init();
34  theDetails = details;
35  }
36 
37 private:
40  virtual bool parse(const char *buf, int len, bool eof);
41 };
42 }// namespace Ssl
43 
44 /******************/
45 bool
47 {
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;
54  return true;
55  }
56  return false;
57 }
58 
59 const char *
61 {
62  const ErrorDetails::const_iterator it = theList.find(value);
63  if (it != theList.end()) {
64  return it->second.descr.termedBuf();
65  }
66 
67  return NULL;
68 }
69 
70 const char *
72 {
73  const ErrorDetails::const_iterator it = theList.find(value);
74  if (it != theList.end()) {
75  return it->second.detail.termedBuf();
76  }
77 
78  return NULL;
79 }
80 
81 Ssl::ErrorDetailsManager *Ssl::ErrorDetailsManager::TheDetailsManager = NULL;
82 
84 {
85  if (!TheDetailsManager)
86  TheDetailsManager = new Ssl::ErrorDetailsManager;
87 
88  assert(TheDetailsManager);
89  return *TheDetailsManager;
90 }
91 
93 {
94  delete TheDetailsManager;
95  TheDetailsManager = NULL;
96 }
97 
99 {
100  theDefaultErrorDetails = new ErrorDetailsList();
101  ErrorDetailFile detailTmpl(theDefaultErrorDetails);
102  detailTmpl.loadDefault();
103 }
104 
106 {
107  Cache::iterator it;
108  it = cache.find(lang);
109  if (it != cache.end()) {
110  debugs(83, 8, HERE << "Found template details in cache for language: " << lang);
111  return it->second;
112  }
113 
114  return NULL;
115 }
116 
118 {
119  const char *lang = errorDetails->errLanguage.termedBuf();
120  assert(lang);
121  if (cache.find(lang) == cache.end())
122  cache[lang] = errorDetails;
123 }
124 
125 bool
127 {
128 #if USE_ERR_LOCALES
129  String hdr;
130  if (request != NULL && request->header.getList(Http::HdrType::ACCEPT_LANGUAGE, &hdr)) {
131  ErrorDetailsList::Pointer errDetails = NULL;
132  //Try to retrieve from cache
133  size_t pos = 0;
134  char lang[256];
135  // Get the first ellement of the Accept-Language header
136  strHdrAcptLangGetItem(hdr, lang, 256, pos);
137  errDetails = getCachedDetails(lang); // search in cache
138 
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);
148  }
149  }
150  }
151 
152  if (errDetails != NULL && errDetails->getRecord(value, entry))
153  return true;
154  }
155 #endif
156 
157  // else try the default
158  if (theDefaultErrorDetails->getRecord(value, entry)) {
159  debugs(83, 8, HERE << "Found default details record for error: " << GetErrorName(value));
160  return true;
161  }
162 
163  return false;
164 }
165 
166 const char *
168 {
169  return theDefaultErrorDetails->getErrorDescr(value);
170 }
171 
172 const char *
174 {
175  return theDefaultErrorDetails->getErrorDetail(value);
176 }
177 
178 // Use HttpHeaders parser to parse error-details.txt files
180 {
181 public:
183 };
184 
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);}
188 
189 bool
190 Ssl::ErrorDetailFile::parse(const char *buffer, int len, bool eof)
191 {
192  if (!theDetails)
193  return false;
194 
195  if (len) {
196  buf.append(buffer, len);
197  }
198 
199  if (eof)
200  buf.append("\n\n", 1);
201 
202  while (size_t size = detailEntryEnd(buf.content(), buf.contentSize())) {
203  const char *e = buf.content() + size;
204 
205  //ignore spaces, new lines and comment lines (starting with #) at the beggining
206  const char *s;
207  for (s = buf.content(); (*s == '\n' || *s == ' ' || *s == '\t' || *s == '#') && s < e; ++s) {
208  if (*s == '#')
209  while (s<e && *s != '\n')
210  ++s; // skip untill the end of line
211  }
212 
213  if ( s != e) {
214  DetailEntryParser parser;
215  if (!parser.parse(s, e - s)) {
216  debugs(83, DBG_IMPORTANT, HERE <<
217  "WARNING! parse error on:" << s);
218  return false;
219  }
220 
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);
225  return false;
226  }
227 
228  Security::ErrorCode ssl_error = Ssl::GetErrorCode(errorName.termedBuf());
229  if (ssl_error != SSL_ERROR_NONE) {
230 
231  if (theDetails->getErrorDetail(ssl_error)) {
232  debugs(83, DBG_IMPORTANT, HERE <<
233  "WARNING! duplicate entry: " << errorName);
234  return false;
235  }
236 
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);
244 
245  if (!detailsParseOk || !descrParseOk) {
246  debugs(83, DBG_IMPORTANT, HERE <<
247  "WARNING! missing important field for detail error: " << errorName);
248  return false;
249  }
250 
251  } else if (!Ssl::ErrorIsOptional(errorName.termedBuf())) {
252  debugs(83, DBG_IMPORTANT, HERE <<
253  "WARNING! invalid error detail name: " << errorName);
254  return false;
255  }
256 
257  }// else {only spaces and black lines; just ignore}
258 
259  buf.consume(size);
260  }
261  debugs(83, 9, HERE << " Remain size: " << buf.contentSize() << " Content: " << buf.content());
262  return true;
263 }
264 
ErrorDetailsList::Pointer theDetails
#define assert(EX)
Definition: assert.h:17
String detail
for error page D macro expansion; may contain macros
bool getRecord(Security::ErrorCode value, ErrorDetailEntry &entry)
int httpHeaderParseQuotedString(const char *start, const int len, String *val)
const char * GetErrorName(Security::ErrorCode value)
The string representation of the TLS error "value".
Definition: ErrorDetail.cc:392
static void Shutdown()
reset the ErrorDetailsManager instance
static ErrorDetailsManager & GetInstance()
Instance class.
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
virtual bool parse(const char *buf, int len, bool eof)
Used to parse (if parsing required) the template data .
Security::ErrorCode GetErrorCode(const char *name)
The Security::ErrorCode code of the error described by "name".
Definition: ErrorDetail.cc:345
String getByName(const SBuf &name) const
Definition: HttpHeader.cc:879
size_type size() const
Definition: SquidString.h:71
void cacheDetails(ErrorDetailsList::Pointer &errorDetails)
cache the given error details list.
bool ErrorIsOptional(const char *name)
Definition: ErrorDetail.cc:405
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:105
const char * getDefaultErrorDetail(Security::ErrorCode value)
the default error details for a given error
ErrorDetailFile(ErrorDetailsList::Pointer const details)
bool getErrorDetail(Security::ErrorCode value, const HttpRequest::Pointer &request, ErrorDetailEntry &entry)
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
ErrorDetails theList
The list of error details entries.
#define DBG_IMPORTANT
Definition: Debug.h:45
int ErrorCode
Squid defined error code (<0), an error code returned by X.509 API, or SSL_ERROR_NONE.
Definition: forward.h:116
char const * termedBuf() const
Definition: SquidString.h:90
String getList(Http::HdrType id) const
Definition: HttpHeader.cc:819
const char * getErrorDetail(Security::ErrorCode value)
an error details for an error if exist in list.
int parse(const char *header_start, size_t len)
Definition: HttpHeader.cc:367
size_t detailEntryEnd(const char *s, size_t len)
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
void const char * buf
Definition: stub_helper.cc:16
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
bool loadDefault()
Definition: errorpage.cc:258
HttpHeader header
Definition: Message.h:74
void errorDetailClean()
Security::ErrorCode error_no
The SSL error code.
String descr
short error description (for use in debug messages or error pages)
ErrorDetailsList::Pointer getCachedDetails(const char *lang)
Return cached error details list for a given language if exist.
void errorDetailInitialize()
const char * getDefaultErrorDescr(Security::ErrorCode value)
the default error description for a given error
Definition: MemBuf.h:23
bool strHdrAcptLangGetItem(const String &hdr, char *lang, int langLen, size_t &pos)
Definition: errorpage.cc:361
bool loadFor(const HttpRequest *request)
Definition: errorpage.cc:415
const char * language()
The language used for the template.
Definition: errorpage.h:274
String name
a name for the error
size_t headersEnd(const char *mime, size_t l, bool &containsObsFold)
Definition: mime_header.cc:16
manages error detail templates
C * getRaw() const
Definition: RefCount.h:74
#define NULL
Definition: types.h:166
int size
Definition: ModDevPoll.cc:77
const char * getErrorDescr(Security::ErrorCode value)
an error description for an error if exist in list.

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors