Options.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 "adaptation/icap/Config.h"
12 #include "base/TextException.h"
13 #include "HttpReply.h"
14 #include "SquidTime.h"
15 #include "StrList.h"
16 #include "wordlist.h"
17 
19  error("unconfigured"),
20  max_connections(-1),
21  allow204(false),
22  allow206(false),
23  preview(-1),
24  theTTL(-1),
25  theTimestamp(0)
26 {
27  theTransfers.preview.name = "Transfer-Preview";
29  theTransfers.ignore.name = "Transfer-Ignore";
31  theTransfers.complete.name = "Transfer-Complete";
33 
34  // Section 4.10.2 of RFC 3507 says that default is no Preview
35  // TODO: provide a squid.conf option to overwrite the default
37 }
38 
40 {
41 }
42 
43 // future optimization note: this method is called by ICAP ACL code at least
44 // twice for each HTTP message to see if the message should be ignored. For any
45 // non-ignored HTTP message, ICAP calls to check whether a preview is needed.
48 {
49  if (theTransfers.preview.matches(urlPath))
50  return xferPreview;
51 
52  if (theTransfers.complete.matches(urlPath))
53  return xferComplete;
54 
55  if (theTransfers.ignore.matches(urlPath))
56  return xferIgnore;
57 
58  debugs(93,7, "url " << urlPath << " matches no extensions; " <<
59  "using default: " << theTransfers.byDefault->name);
60  return theTransfers.byDefault->kind;
61 }
62 
64 {
65  return !error;
66 }
67 
69 {
70  return squid_curtime <= expire();
71 }
72 
74 {
75  Must(valid());
76  return theTTL >= 0 ? theTTL : TheConfig.default_options_ttl;
77 }
78 
80 {
81  Must(valid());
82  return theTimestamp + ttl();
83 }
84 
86 {
87  error = NULL; // reset initial "unconfigured" value (or an old error?)
88 
89  const HttpHeader *h = &reply->header;
90 
91  if (reply->sline.status() != Http::scOkay)
92  error = "unsupported status code of OPTIONS response";
93 
94  // Methods
95  if (h->hasByNameListMember("Methods", "REQMOD", ','))
96  cfgMethod(ICAP::methodReqmod);
97 
98  if (h->hasByNameListMember("Methods", "RESPMOD", ','))
99  cfgMethod(ICAP::methodRespmod);
100 
101  service = h->getByName("Service");
102 
103  serviceId = h->getByName("ServiceId");
104 
105  istag = h->getByName("ISTag");
106 
107  if (h->getByName("Opt-body-type").size()) {
108  // TODO: add a class to rate-limit such warnings using FadingCounter
109  debugs(93,DBG_IMPORTANT, "WARNING: Ignoring unsupported ICAP " <<
110  "OPTIONS body; type: " << h->getByName("Opt-body-type"));
111  // Do not set error, assuming the response headers are valid.
112  }
113 
114  cfgIntHeader(h, "Max-Connections", max_connections);
115  if (max_connections == 0)
116  debugs(93, DBG_IMPORTANT, "WARNING: Max-Connections is set to zero! ");
117 
118  cfgIntHeader(h, "Options-TTL", theTTL);
119 
120  theTimestamp = h->getTime(Http::HdrType::DATE);
121 
122  if (theTimestamp < 0)
123  theTimestamp = squid_curtime;
124 
125  if (h->hasListMember(Http::HdrType::ALLOW, "204", ','))
126  allow204 = true;
127 
128  if (h->hasListMember(Http::HdrType::ALLOW, "206", ','))
129  allow206 = true;
130 
131  cfgIntHeader(h, "Preview", preview);
132 
133  cfgTransferList(h, theTransfers.preview);
134  cfgTransferList(h, theTransfers.ignore);
135  cfgTransferList(h, theTransfers.complete);
136 }
137 
139 {
140  Must(m != ICAP::methodNone);
141  methods.push_back(m);
142 }
143 
144 // TODO: HttpHeader should provide a general method for this type of conversion
145 void Adaptation::Icap::Options::cfgIntHeader(const HttpHeader *h, const char *fname, int &value)
146 {
147  const String s = h->getByName(fname);
148 
149  if (s.size() && xisdigit(*s.termedBuf()))
150  value = atoi(s.termedBuf());
151  else
152  value = -1;
153 
154  debugs(93,5, HERE << "int header: " << fname << ": " << value);
155 }
156 
158 {
159  const String buf = h->getByName(list.name);
160  bool foundStar = false;
161  list.parse(buf, foundStar);
162 
163  if (foundStar) {
164  theTransfers.byDefault = &list;
165  debugs(93,5, HERE << "set default transfer to " << list.name);
166  }
167 
168  list.report(5, "Adaptation::Icap::Options::cfgTransferList: ");
169 }
170 
171 /* Adaptation::Icap::Options::TransferList */
172 
174  kind(xferNone)
175 {
176 };
177 
179 {
180  wordlistDestroy(&extensions);
181 };
182 
184 {
185  wordlistAdd(&extensions, extension);
186 };
187 
189 {
190  const SBuf::size_type urlLen = urlPath.length();
191  for (wordlist *e = extensions; e; e = e->next) {
192  // optimize: store extension lengths
193  const size_t eLen = strlen(e->key);
194 
195  // assume URL contains at least '/' before the extension
196  if (eLen < urlLen) {
197  const size_t eOff = urlLen - eLen;
198  // RFC 3507 examples imply that extensions come without leading '.'
199  if (urlPath[eOff-1] == '.' && urlPath.substr(eOff).cmp(e->key, eLen) == 0) {
200  debugs(93,7, "url " << urlPath << " matches " << name << " extension " << e->key);
201  return true;
202  }
203  }
204  }
205  debugs(93,8, "url " << urlPath << " matches no " << name << " extensions");
206  return false;
207 }
208 
210 {
211  foundStar = false;
212 
213  const char *item;
214  const char *pos = NULL;
215  int ilen;
216  while (strListGetItem(&buf, ',', &item, &ilen, &pos)) {
217  if (ilen == 1 && *item == '*')
218  foundStar = true;
219  else {
220  const char *tmp = xstrndup(item, ilen+1);
221  add(tmp);
222  xfree(tmp);
223  }
224  }
225 }
226 
227 void Adaptation::Icap::Options::TransferList::report(int level, const char *prefix) const
228 {
229  if (extensions) {
230  for (wordlist *e = extensions; e; e = e->next)
231  debugs(93,level, prefix << name << ": " << e->key);
232  } else {
233  debugs(93,level, prefix << "no " << name << " extensions");
234  }
235 }
236 
void parse(const String &buf, bool &foundStar)
Definition: Options.cc:209
int strListGetItem(const String *str, char del, const char **item, int *ilen, const char **pos)
Definition: StrList.cc:77
void configure(const HttpReply *reply)
Definition: Options.cc:85
const char * wordlistAdd(wordlist **list, const char *key)
Definition: wordlist.cc:25
Definition: SBuf.h:87
time_t expire() const
Definition: Options.cc:79
void error(char *format,...)
char * xstrndup(const char *s, size_t n)
Definition: xstring.cc:56
void wordlistDestroy(wordlist **list)
destroy a wordlist
Definition: wordlist.cc:16
Config TheConfig
Definition: Config.cc:19
void cfgTransferList(const HttpHeader *h, TransferList &l)
Definition: Options.cc:157
String getByName(const SBuf &name) const
Definition: HttpHeader.cc:879
size_type size() const
Definition: SquidString.h:71
time_t squid_curtime
Definition: stub_time.cc:17
void report(int level, const char *prefix) const
Definition: Options.cc:227
int hasByNameListMember(const char *name, const char *member, const char separator) const
Definition: HttpHeader.cc:1689
bool valid() const
Definition: Options.cc:63
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:405
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
#define DBG_IMPORTANT
Definition: Debug.h:45
static int max_connections
Definition: tcp-banger2.c:94
char const * termedBuf() const
Definition: SquidString.h:90
struct Adaptation::Icap::Options::Transfers theTransfers
bool matches(const SBuf &urlPath) const
Definition: Options.cc:188
Http::StatusLine sline
Definition: HttpReply.h:60
int hasListMember(Http::HdrType id, const char *member, const char separator) const
Definition: HttpHeader.cc:1665
TransferKind transferKind(const SBuf &urlPath) const
Definition: Options.cc:47
void const char * buf
Definition: stub_helper.cc:16
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
#define Must(cond)
Definition: TextException.h:89
HttpHeader header
Definition: Message.h:74
bool fresh() const
Definition: Options.cc:68
void cfgMethod(ICAP::Method m)
Definition: Options.cc:138
#define xisdigit(x)
Definition: xis.h:20
int cmp(const SBuf &S, const size_type n) const
shorthand version for compare()
Definition: SBuf.h:265
time_t getTime(Http::HdrType id) const
Definition: HttpHeader.cc:1186
void cfgIntHeader(const HttpHeader *h, const char *fname, int &value)
Definition: Options.cc:145
#define xfree
void add(const char *extension)
Definition: Options.cc:183
MemBlob::size_type size_type
Definition: SBuf.h:90
SBuf substr(size_type pos, size_type n=npos) const
Definition: SBuf.cc:606
#define NULL
Definition: types.h:166
#define false
Definition: GnuRegex.c:233
Http::StatusCode status() const
retrieve the status code for this status line
Definition: StatusLine.h:45
wordlist * next
Definition: wordlist.h:34

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors