ErrorDetailManager.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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"
14 #include "mime_header.h"
15 
17 {
19 }
20 
22 {
24 }
25 
26 namespace Ssl
27 {
28 
31 {
32 public:
33  explicit ErrorDetailFile(ErrorDetailsList::Pointer const details): TemplateFile("error-details.txt", ERR_NONE) {
34  theDetails = details;
35  }
36 
37 private:
39  virtual bool parse() override;
40 };
41 }// namespace Ssl
42 
43 /******************/
44 bool
46 {
47  const ErrorDetails::const_iterator it = theList.find(value);
48  if (it != theList.end()) {
49  entry.error_no = it->second.error_no;
50  entry.name = it->second.name;
51  entry.detail = it->second.detail;
52  entry.descr = it->second.descr;
53  return true;
54  }
55  return false;
56 }
57 
58 const char *
60 {
61  const ErrorDetails::const_iterator it = theList.find(value);
62  if (it != theList.end()) {
63  return it->second.descr.termedBuf();
64  }
65 
66  return NULL;
67 }
68 
69 const char *
71 {
72  const ErrorDetails::const_iterator it = theList.find(value);
73  if (it != theList.end()) {
74  return it->second.detail.termedBuf();
75  }
76 
77  return NULL;
78 }
79 
81 
83 {
84  if (!TheDetailsManager)
85  TheDetailsManager = new Ssl::ErrorDetailsManager;
86 
87  assert(TheDetailsManager);
88  return *TheDetailsManager;
89 }
90 
92 {
93  delete TheDetailsManager;
94  TheDetailsManager = NULL;
95 }
96 
98 {
99  theDefaultErrorDetails = new ErrorDetailsList();
100  ErrorDetailFile detailTmpl(theDefaultErrorDetails);
101  detailTmpl.loadDefault();
102 }
103 
105 {
106  Cache::iterator it;
107  it = cache.find(lang);
108  if (it != cache.end()) {
109  debugs(83, 8, HERE << "Found template details in cache for language: " << lang);
110  return it->second;
111  }
112 
113  return NULL;
114 }
115 
117 {
118  const char *lang = errorDetails->errLanguage.termedBuf();
119  assert(lang);
120  if (cache.find(lang) == cache.end())
121  cache[lang] = errorDetails;
122 }
123 
124 bool
126 {
127 #if USE_ERR_LOCALES
128  String hdr;
129  if (request != NULL && request->header.getList(Http::HdrType::ACCEPT_LANGUAGE, &hdr)) {
130  ErrorDetailsList::Pointer errDetails = NULL;
131  //Try to retrieve from cache
132  size_t pos = 0;
133  char lang[256];
134  // Get the first ellement of the Accept-Language header
135  strHdrAcptLangGetItem(hdr, lang, 256, pos);
136  errDetails = getCachedDetails(lang); // search in cache
137 
138  if (!errDetails) { // Else try to load from disk
139  debugs(83, 8, HERE << "Creating new ErrDetailList to read from disk");
140  errDetails = new ErrorDetailsList();
141  ErrorDetailFile detailTmpl(errDetails);
142  if (detailTmpl.loadFor(request.getRaw())) {
143  if (detailTmpl.language()) {
144  debugs(83, 8, HERE << "Found details on disk for language " << detailTmpl.language());
145  errDetails->errLanguage = detailTmpl.language();
146  cacheDetails(errDetails);
147  }
148  }
149  }
150 
151  if (errDetails != NULL && errDetails->getRecord(value, entry))
152  return true;
153  }
154 #else
155  (void)request;
156 #endif
157 
158  // else try the default
159  if (theDefaultErrorDetails->getRecord(value, entry)) {
160  debugs(83, 8, HERE << "Found default details record for error: " << GetErrorName(value));
161  return true;
162  }
163 
164  return false;
165 }
166 
167 const char *
169 {
170  return theDefaultErrorDetails->getErrorDescr(value);
171 }
172 
173 const char *
175 {
176  return theDefaultErrorDetails->getErrorDetail(value);
177 }
178 
179 // Use HttpHeaders parser to parse error-details.txt files
181 {
182 public:
184 };
185 
186 //The end of an error detrail entry is a double "\n". The headersEnd
187 // functions can detect it
188 inline size_t detailEntryEnd(const char *s, size_t len) {return headersEnd(s, len);}
189 
190 bool
192 {
193  if (!theDetails)
194  return false;
195 
196  auto buf = template_;
197  buf.append("\n\n"); // ensure detailEntryEnd() finds the last entry
198 
199  while (const auto size = detailEntryEnd(buf.rawContent(), buf.length())) {
200  auto *s = buf.c_str();
201  const auto e = s + size;
202 
203  //ignore spaces, new lines and comment lines (starting with #) at the beginning
204  for (; (*s == '\n' || *s == ' ' || *s == '\t' || *s == '#') && s < e; ++s) {
205  if (*s == '#')
206  while (s<e && *s != '\n')
207  ++s; // skip until the end of line
208  }
209 
210  if ( s != e) {
211  DetailEntryParser parser;
212  Http::ContentLengthInterpreter interpreter;
213  // no applyStatusCodeRules() -- error templates lack HTTP status code
214  if (!parser.parse(s, e - s, interpreter)) {
215  debugs(83, DBG_IMPORTANT, HERE <<
216  "WARNING! parse error on:" << s);
217  return false;
218  }
219 
220  const String errorName = parser.getByName("name");
221  if (!errorName.size()) {
222  debugs(83, DBG_IMPORTANT, HERE <<
223  "WARNING! invalid or no error detail name on:" << s);
224  return false;
225  }
226 
227  Security::ErrorCode ssl_error = Ssl::GetErrorCode(errorName.termedBuf());
228  if (ssl_error != SSL_ERROR_NONE) {
229 
230  if (theDetails->getErrorDetail(ssl_error)) {
231  debugs(83, DBG_IMPORTANT, HERE <<
232  "WARNING! duplicate entry: " << errorName);
233  return false;
234  }
235 
236  ErrorDetailEntry &entry = theDetails->theList[ssl_error];
237  entry.error_no = ssl_error;
238  entry.name = errorName;
239  String tmp = parser.getByName("detail");
240  const int detailsParseOk = httpHeaderParseQuotedString(tmp.termedBuf(), tmp.size(), &entry.detail);
241  tmp = parser.getByName("descr");
242  const int descrParseOk = httpHeaderParseQuotedString(tmp.termedBuf(), tmp.size(), &entry.descr);
243  // TODO: Validate "descr" and "detail" field values.
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, Raw("unparsed data", buf.rawContent(), buf.length()));
262  return true;
263 }
264 
size_t detailEntryEnd(const char *s, size_t len)
int ErrorCode
Squid-defined error code (<0), an error code returned by X.509 API, or zero.
Definition: forward.h:102
ErrorDetails theList
The list of error details entries.
const char * getErrorDetail(Security::ErrorCode value)
an error details for an error if exist in list.
String descr
short error description (for use in debug messages or error pages)
Security::ErrorCode error_no
TLS error;.
virtual bool parse() override
post-process the loaded template
const char * GetErrorName(const Security::ErrorCode code, const bool prefixRawCode=false)
Definition: ErrorDetail.h:36
ErrorDetailsList::Pointer theDetails
int parse(const char *header_start, size_t len, Http::ContentLengthInterpreter &interpreter)
Definition: HttpHeader.cc:377
@ ERR_NONE
Definition: forward.h:15
ErrorDetailFile(ErrorDetailsList::Pointer const details)
static ErrorDetailsManager * TheDetailsManager
An instance of ErrorDetailsManager to be used by squid (ssl/ErrorDetails.*)
#define DBG_IMPORTANT
Definition: Debug.h:41
void errorDetailInitialize()
Definition: Debug.h:184
void loadDefault()
Definition: errorpage.cc:358
int size
Definition: ModDevPoll.cc:76
#define NULL
Definition: types.h:166
String name
a name for the error
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
void errorDetailClean()
@ ACCEPT_LANGUAGE
const char * getDefaultErrorDescr(Security::ErrorCode value)
the default error description for a given error
int httpHeaderParseQuotedString(const char *start, const int len, String *val)
bool strHdrAcptLangGetItem(const String &hdr, char *lang, int langLen, size_t &pos)
Definition: errorpage.cc:467
bool getErrorDetail(Security::ErrorCode value, const HttpRequest::Pointer &request, ErrorDetailEntry &entry)
Definition: Xaction.cc:49
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:152
String detail
for error page D macro expansion; may contain macros
#define assert(EX)
Definition: assert.h:19
char const * termedBuf() const
Definition: SquidString.h:92
const char * getErrorDescr(Security::ErrorCode value)
an error description for an error if exist in list.
Security::ErrorCode GetErrorCode(const char *name)
The Security::ErrorCode code of the error described by "name".
Definition: ErrorDetail.h:28
static void Shutdown()
reset the ErrorDetailsManager instance
@ hoErrorDetail
Definition: HttpHeader.h:39
static ErrorDetailsManager & GetInstance()
Instance class.
const char * language()
The language used for the template.
Definition: errorpage.h:312
String getByName(const SBuf &name) const
Definition: HttpHeader.cc:901
bool getRecord(Security::ErrorCode value, ErrorDetailEntry &entry)
manages error detail templates
ErrorDetailsList::Pointer getCachedDetails(const char *lang)
Return cached error details list for a given language if exist.
size_type size() const
Definition: SquidString.h:73
bool loadFor(const HttpRequest *request)
Definition: errorpage.cc:521
void cacheDetails(ErrorDetailsList::Pointer &errorDetails)
cache the given error details list.
const char * getDefaultErrorDetail(Security::ErrorCode value)
the default error details for a given error
String errLanguage
The language of the error-details.txt template, if any.
static StatHist s
bool ErrorIsOptional(const char *name)
Definition: ErrorDetail.cc:148
size_t headersEnd(const char *mime, size_t l, bool &containsObsFold)
Definition: mime_header.cc:17
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors