]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHeaderTools.cc
Changed some structs to classes.
[thirdparty/squid.git] / src / HttpHeaderTools.cc
CommitLineData
7faf2bdb 1/*
7faf2bdb 2 * DEBUG: section 66 HTTP Header Tools
3 * AUTHOR: Alex Rousskov
4 *
2b6662ba 5 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 6 * ----------------------------------------------------------
7faf2bdb 7 *
2b6662ba 8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
7faf2bdb 16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
26ac0430 21 *
7faf2bdb 22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26ac0430 26 *
7faf2bdb 27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
cbdec147 29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 30 *
7faf2bdb 31 */
32
582c2af2 33#include "squid.h"
c0941a6a 34#include "acl/FilledChecklist.h"
3b07476b 35#include "acl/Gadgets.h"
f4698e0b 36#include "client_side_request.h"
582c2af2 37#include "client_side.h"
f4698e0b 38#include "comm/Connection.h"
27bc2077 39#include "compat/strtoll.h"
f4698e0b 40#include "fde.h"
27bc2077
AJ
41#include "HttpHdrContRange.h"
42#include "HttpHeader.h"
3b07476b
CT
43#include "HttpHeaderTools.h"
44#include "HttpRequest.h"
0eb49b6d 45#include "MemBuf.h"
582c2af2 46#include "Store.h"
28204b3b 47#include "StrList.h"
582c2af2 48
f4698e0b
CT
49#if USE_SSL
50#include "ssl/support.h"
51#endif
582c2af2 52
f4698e0b 53#include <algorithm>
f4698e0b 54#include <string>
21d845b1
FC
55#if HAVE_ERRNO_H
56#include <errno.h>
57#endif
7faf2bdb 58
2246b732 59static void httpHeaderPutStrvf(HttpHeader * hdr, http_hdr_type id, const char *fmt, va_list vargs);
de336bbe 60
de336bbe 61HttpHeaderFieldInfo *
b644367b 62httpHeaderBuildFieldsInfo(const HttpHeaderFieldAttrs * attrs, int count)
7faf2bdb 63{
64 int i;
de336bbe 65 HttpHeaderFieldInfo *table = NULL;
66 assert(attrs && count);
67
68 /* allocate space */
0353e724 69 table = new HttpHeaderFieldInfo[count];
7faf2bdb 70
7faf2bdb 71 for (i = 0; i < count; ++i) {
62e76326 72 const http_hdr_type id = attrs[i].id;
73 HttpHeaderFieldInfo *info = table + id;
74 /* sanity checks */
75 assert(id >= 0 && id < count);
76 assert(attrs[i].name);
0353e724 77 assert(info->id == HDR_ACCEPT && info->type == ftInvalid); /* was not set before */
62e76326 78 /* copy and init fields */
79 info->id = id;
80 info->type = attrs[i].type;
81 info->name = attrs[i].name;
82 assert(info->name.size());
7faf2bdb 83 }
62e76326 84
de336bbe 85 return table;
86}
87
88void
b644367b 89httpHeaderDestroyFieldsInfo(HttpHeaderFieldInfo * table, int count)
de336bbe 90{
91 int i;
62e76326 92
de336bbe 93 for (i = 0; i < count; ++i)
30abd221 94 table[i].name.clean();
62e76326 95
7f4c8dec 96 delete [] table;
7faf2bdb 97}
98
d8b249ef 99void
97474590 100httpHeaderMaskInit(HttpHeaderMask * mask, int value)
d8b249ef 101{
97474590 102 memset(mask, value, sizeof(*mask));
d8b249ef 103}
104
b50e327b 105/** calculates a bit mask of a given array; does not reset mask! */
d8b249ef 106void
8abf232c 107httpHeaderCalcMask(HttpHeaderMask * mask, http_hdr_type http_hdr_type_enums[], size_t count)
7faf2bdb 108{
e6ccf245 109 size_t i;
8abf232c 110 const int * enums = (const int *) http_hdr_type_enums;
d8b249ef 111 assert(mask && enums);
99edd1c3 112 assert(count < sizeof(*mask) * 8); /* check for overflow */
7faf2bdb 113
114 for (i = 0; i < count; ++i) {
62e76326 115 assert(!CBIT_TEST(*mask, enums[i])); /* check for duplicates */
116 CBIT_SET(*mask, enums[i]);
7faf2bdb 117 }
7faf2bdb 118}
119
2246b732 120/* same as httpHeaderPutStr, but formats the string using snprintf first */
2246b732 121void
eeb423fb 122httpHeaderPutStrf(HttpHeader * hdr, http_hdr_type id, const char *fmt,...)
2246b732 123{
9bc73deb 124 va_list args;
125 va_start(args, fmt);
62e76326 126
2246b732 127 httpHeaderPutStrvf(hdr, id, fmt, args);
128 va_end(args);
129}
130
131/* used by httpHeaderPutStrf */
132static void
133httpHeaderPutStrvf(HttpHeader * hdr, http_hdr_type id, const char *fmt, va_list vargs)
134{
2246b732 135 MemBuf mb;
2fe7eff9 136 mb.init();
137 mb.vPrintf(fmt, vargs);
a9925b40 138 hdr->putStr(id, mb.buf);
2fe7eff9 139 mb.clean();
2246b732 140}
141
9035d1d5 142/** wrapper arrounf PutContRange */
d192d11f 143void
47f6e231 144httpHeaderAddContRange(HttpHeader * hdr, HttpHdrRangeSpec spec, int64_t ent_len)
d192d11f 145{
146 HttpHdrContRange *cr = httpHdrContRangeCreate();
147 assert(hdr && ent_len >= 0);
148 httpHdrContRangeSet(cr, spec, ent_len);
a9925b40 149 hdr->putContRange(cr);
d192d11f 150 httpHdrContRangeDestroy(cr);
151}
152
9035d1d5 153/**
9bc73deb 154 * return true if a given directive is found in at least one of
155 * the "connection" header-fields note: if HDR_PROXY_CONNECTION is
156 * present we ignore HDR_CONNECTION.
99edd1c3 157 */
158int
5999b776 159httpHeaderHasConnDir(const HttpHeader * hdr, const char *directive)
99edd1c3 160{
30abd221 161 String list;
9bc73deb 162 int res;
163 /* what type of header do we have? */
62e76326 164
95e78500 165#if USE_HTTP_VIOLATIONS
a9925b40 166 if (hdr->has(HDR_PROXY_CONNECTION))
95e78500
AJ
167 list = hdr->getList(HDR_PROXY_CONNECTION);
168 else
169#endif
2e881a6f
A
170 if (hdr->has(HDR_CONNECTION))
171 list = hdr->getList(HDR_CONNECTION);
172 else
173 return 0;
9bc73deb 174
9bc73deb 175 res = strListIsMember(&list, directive, ',');
62e76326 176
30abd221 177 list.clean();
178
9bc73deb 179 return res;
99edd1c3 180}
181
99edd1c3 182
7faf2bdb 183
b50e327b 184/** handy to printf prefixes of potentially very long buffers */
7faf2bdb 185const char *
d8b249ef 186getStringPrefix(const char *str, const char *end)
7faf2bdb 187{
d8b249ef 188#define SHORT_PREFIX_SIZE 512
7faf2bdb 189 LOCAL_ARRAY(char, buf, SHORT_PREFIX_SIZE);
b644367b 190 const int sz = 1 + (end ? end - str : strlen(str));
d8b249ef 191 xstrncpy(buf, str, (sz > SHORT_PREFIX_SIZE) ? SHORT_PREFIX_SIZE : sz);
7faf2bdb 192 return buf;
193}
b5107edb 194
b50e327b 195/**
b5107edb 196 * parses an int field, complains if soemthing went wrong, returns true on
197 * success
198 */
199int
200httpHeaderParseInt(const char *start, int *value)
201{
202 assert(value);
203 *value = atoi(start);
62e76326 204
b6a2f15e 205 if (!*value && !xisdigit(*start)) {
bf8fe701 206 debugs(66, 2, "failed to parse an int header field near '" << start << "'");
62e76326 207 return 0;
b5107edb 208 }
62e76326 209
b5107edb 210 return 1;
211}
212
47f6e231 213int
214httpHeaderParseOffset(const char *start, int64_t * value)
215{
66e255bc 216 errno = 0;
47f6e231 217 int64_t res = strtoll(start, NULL, 10);
218 if (!res && EINVAL == errno) /* maybe not portable? */
26ac0430 219 return 0;
47f6e231 220 *value = res;
221 return 1;
222}
223
b50e327b
AJ
224/**
225 * Parses a quoted-string field (RFC 2616 section 2.2), complains if
43ae1d95 226 * something went wrong, returns non-zero on success.
34460e19 227 * Un-escapes quoted-pair characters found within the string.
229af339 228 * start should point at the first double-quote.
43ae1d95 229 */
230int
34460e19 231httpHeaderParseQuotedString(const char *start, const int len, String *val)
43ae1d95 232{
233 const char *end, *pos;
30abd221 234 val->clean();
9abd1514 235 if (*start != '"') {
5f5ddef6 236 debugs(66, 2, HERE << "failed to parse a quoted-string header field near '" << start << "'");
6d97f5f1 237 return 0;
9abd1514 238 }
43ae1d95 239 pos = start + 1;
240
34460e19 241 while (*pos != '"' && len > (pos-start)) {
5f5ddef6
CT
242
243 if (*pos =='\r') {
95dc7ff4 244 ++pos;
5f5ddef6
CT
245 if ((pos-start) > len || *pos != '\n') {
246 debugs(66, 2, HERE << "failed to parse a quoted-string header field with '\\r' octet " << (start-pos)
247 << " bytes into '" << start << "'");
248 val->clean();
249 return 0;
250 }
251 }
252
253 if (*pos == '\n') {
95dc7ff4 254 ++pos;
5f5ddef6
CT
255 if ( (pos-start) > len || (*pos != ' ' && *pos != '\t')) {
256 debugs(66, 2, HERE << "failed to parse multiline quoted-string header field '" << start << "'");
257 val->clean();
258 return 0;
259 }
260 // TODO: replace the entire LWS with a space
261 val->append(" ");
95dc7ff4 262 ++pos;
5f5ddef6
CT
263 debugs(66, 2, HERE << "len < pos-start => " << len << " < " << (pos-start));
264 continue;
265 }
266
a0133f10 267 bool quoted = (*pos == '\\');
5f5ddef6 268 if (quoted) {
95dc7ff4 269 ++pos;
5f5ddef6
CT
270 if (!*pos || (pos-start) > len) {
271 debugs(66, 2, HERE << "failed to parse a quoted-string header field near '" << start << "'");
272 val->clean();
273 return 0;
274 }
6d97f5f1 275 }
34460e19 276 end = pos;
c4b86597 277 while (end < (start+len) && *end != '\\' && *end != '\"' && (unsigned char)*end > 0x1F && *end != 0x7F)
95dc7ff4 278 ++end;
c4b86597 279 if (((unsigned char)*end <= 0x1F && *end != '\r' && *end != '\n') || *end == 0x7F) {
5f5ddef6 280 debugs(66, 2, HERE << "failed to parse a quoted-string header field with CTL octet " << (start-pos)
34460e19
AJ
281 << " bytes into '" << start << "'");
282 val->clean();
283 return 0;
284 }
6d97f5f1
A
285 val->append(pos, end-pos);
286 pos = end;
43ae1d95 287 }
5f5ddef6
CT
288
289 if (*pos != '\"') {
290 debugs(66, 2, HERE << "failed to parse a quoted-string header field which did not end with \" ");
291 val->clean();
292 return 0;
293 }
9abd1514
HN
294 /* Make sure it's defined even if empty "" */
295 if (!val->defined())
6d97f5f1 296 val->limitInit("", 0);
9abd1514 297 return 1;
43ae1d95 298}
299
b50e327b
AJ
300/**
301 * Checks the anonymizer (header_access) configuration.
4f4611bf 302 *
b50e327b
AJ
303 * \retval 0 Header is explicitly blocked for removal
304 * \retval 1 Header is explicitly allowed
305 * \retval 1 Header has been replaced, the current version can be used.
306 * \retval 1 Header has no access controls to test
6bccf575 307 */
2d72d4fd 308static int
8c01ada0 309httpHdrMangle(HttpHeaderEntry * e, HttpRequest * request, int req_or_rep)
6bccf575 310{
e9b1e111 311 int retval;
312
6bccf575 313 /* check with anonymizer tables */
3b07476b 314 HeaderManglers *hms = NULL;
6bccf575 315 assert(e);
8c01ada0 316
317 if (ROR_REQUEST == req_or_rep) {
3b07476b 318 hms = Config.request_header_access;
8c01ada0 319 } else if (ROR_REPLY == req_or_rep) {
3b07476b 320 hms = Config.reply_header_access;
8c01ada0 321 } else {
322 /* error. But let's call it "request". */
3b07476b 323 hms = Config.request_header_access;
8c01ada0 324 }
325
3b07476b
CT
326 /* manglers are not configured for this message kind */
327 if (!hms)
328 return 1;
329
330 const header_mangler *hm = hms->find(*e);
331
b50e327b 332 /* mangler or checklist went away. default allow */
af6a12ee 333 if (!hm || !hm->access_list) {
b50e327b
AJ
334 return 1;
335 }
336
c0941a6a 337 ACLFilledChecklist checklist(hm->access_list, request, NULL);
62e76326 338
2efeb0b7 339 if (checklist.fastCheck() == ACCESS_ALLOWED) {
b50e327b 340 /* aclCheckFast returns true for allow. */
62e76326 341 retval = 1;
a453662f 342 } else if (NULL == hm->replacement) {
62e76326 343 /* It was denied, and we don't have any replacement */
344 retval = 0;
a453662f 345 } else {
62e76326 346 /* It was denied, but we have a replacement. Replace the
347 * header on the fly, and return that the new header
348 * is allowed.
349 */
350 e->value = hm->replacement;
351 retval = 1;
a453662f 352 }
e9b1e111 353
e9b1e111 354 return retval;
6bccf575 355}
356
b50e327b 357/** Mangles headers for a list of headers. */
6bccf575 358void
8c01ada0 359httpHdrMangleList(HttpHeader * l, HttpRequest * request, int req_or_rep)
6bccf575 360{
361 HttpHeaderEntry *e;
362 HttpHeaderPos p = HttpHeaderInitPos;
62e76326 363
ba9fb01d 364 int headers_deleted = 0;
a9925b40 365 while ((e = l->getEntry(&p)))
8c01ada0 366 if (0 == httpHdrMangle(e, request, req_or_rep))
ba9fb01d 367 l->delAt(p, headers_deleted);
368
369 if (headers_deleted)
370 l->refreshMask();
6bccf575 371}
5967c0bf 372
3b07476b
CT
373static
374void header_mangler_clean(header_mangler &m)
375{
376 aclDestroyAccessList(&m.access_list);
377 safe_free(m.replacement);
378}
379
380static
381void header_mangler_dump_access(StoreEntry * entry, const char *option,
d3abcb0d 382 const header_mangler &m, const char *name)
3b07476b
CT
383{
384 if (m.access_list != NULL) {
385 storeAppendPrintf(entry, "%s ", option);
386 dump_acl_access(entry, name, m.access_list);
387 }
388}
389
390static
391void header_mangler_dump_replacement(StoreEntry * entry, const char *option,
d3abcb0d 392 const header_mangler &m, const char *name)
3b07476b
CT
393{
394 if (m.replacement)
395 storeAppendPrintf(entry, "%s %s %s\n", option, name, m.replacement);
396}
397
398HeaderManglers::HeaderManglers()
399{
400 memset(known, 0, sizeof(known));
401 memset(&all, 0, sizeof(all));
402}
403
404HeaderManglers::~HeaderManglers()
405{
95dc7ff4 406 for (int i = 0; i < HDR_ENUM_END; ++i)
3b07476b
CT
407 header_mangler_clean(known[i]);
408
409 typedef ManglersByName::iterator MBNI;
410 for (MBNI i = custom.begin(); i != custom.end(); ++i)
411 header_mangler_clean(i->second);
412
413 header_mangler_clean(all);
414}
415
416void
417HeaderManglers::dumpAccess(StoreEntry * entry, const char *name) const
5967c0bf 418{
95dc7ff4 419 for (int i = 0; i < HDR_ENUM_END; ++i) {
3b07476b
CT
420 header_mangler_dump_access(entry, name, known[i],
421 httpHeaderNameById(i));
5967c0bf 422 }
423
3b07476b
CT
424 typedef ManglersByName::const_iterator MBNCI;
425 for (MBNCI i = custom.begin(); i != custom.end(); ++i)
426 header_mangler_dump_access(entry, name, i->second, i->first.c_str());
427
428 header_mangler_dump_access(entry, name, all, "All");
5967c0bf 429}
3b07476b
CT
430
431void
432HeaderManglers::dumpReplacement(StoreEntry * entry, const char *name) const
433{
95dc7ff4 434 for (int i = 0; i < HDR_ENUM_END; ++i) {
3b07476b
CT
435 header_mangler_dump_replacement(entry, name, known[i],
436 httpHeaderNameById(i));
437 }
438
439 typedef ManglersByName::const_iterator MBNCI;
440 for (MBNCI i = custom.begin(); i != custom.end(); ++i) {
441 header_mangler_dump_replacement(entry, name, i->second,
442 i->first.c_str());
443 }
444
445 header_mangler_dump_replacement(entry, name, all, "All");
446}
447
448header_mangler *
449HeaderManglers::track(const char *name)
450{
451 int id = httpHeaderIdByNameDef(name, strlen(name));
452
453 if (id == HDR_BAD_HDR) { // special keyword or a custom header
454 if (strcmp(name, "All") == 0)
455 id = HDR_ENUM_END;
456 else if (strcmp(name, "Other") == 0)
457 id = HDR_OTHER;
458 }
459
460 header_mangler *m = NULL;
461 if (id == HDR_ENUM_END) {
462 m = &all;
d3abcb0d 463 } else if (id == HDR_BAD_HDR) {
3b07476b
CT
464 m = &custom[name];
465 } else {
466 m = &known[id]; // including HDR_OTHER
467 }
468
469 assert(m);
470 return m;
471}
472
473void
474HeaderManglers::setReplacement(const char *name, const char *value)
475{
476 // for backword compatibility, we allow replacements to be configured
477 // for headers w/o access rules, but such replacements are ignored
478 header_mangler *m = track(name);
479
480 safe_free(m->replacement); // overwrite old value if any
481 m->replacement = xstrdup(value);
482}
483
484const header_mangler *
485HeaderManglers::find(const HttpHeaderEntry &e) const
486{
487 // a known header with a configured ACL list
488 if (e.id != HDR_OTHER && 0 <= e.id && e.id < HDR_ENUM_END &&
d3abcb0d 489 known[e.id].access_list)
3b07476b
CT
490 return &known[e.id];
491
492 // a custom header
493 if (e.id == HDR_OTHER) {
494 // does it have an ACL list configured?
495 // Optimize: use a name type that we do not need to convert to here
496 const ManglersByName::const_iterator i = custom.find(e.name.termedBuf());
497 if (i != custom.end())
498 return &i->second;
499 }
500
501 // Next-to-last resort: "Other" rules match any custom header
502 if (e.id == HDR_OTHER && known[HDR_OTHER].access_list)
503 return &known[HDR_OTHER];
504
505 // Last resort: "All" rules match any header
506 if (all.access_list)
507 return &all;
508
509 return NULL;
510}
511
f4698e0b 512void
4bf68cfa 513httpHdrAdd(HttpHeader *heads, HttpRequest *request, const AccessLogEntryPointer &al, HeaderWithAclList &headersAdd)
f4698e0b
CT
514{
515 ACLFilledChecklist checklist(NULL, request, NULL);
516
517 for (HeaderWithAclList::const_iterator hwa = headersAdd.begin(); hwa != headersAdd.end(); ++hwa) {
518 if (!hwa->aclList || checklist.fastCheck(hwa->aclList) == ACCESS_ALLOWED) {
519 const char *fieldValue = NULL;
520 MemBuf mb;
521 if (hwa->quoted) {
4bf68cfa 522 if (al != NULL) {
f4698e0b 523 mb.init();
4bf68cfa 524 hwa->valueFormat->assemble(mb, al, 0);
f4698e0b
CT
525 fieldValue = mb.content();
526 }
527 } else {
528 fieldValue = hwa->fieldValue.c_str();
529 }
530
531 if (!fieldValue || fieldValue[0] == '\0')
532 fieldValue = "-";
533
999aa5d2
A
534 HttpHeaderEntry *e = new HttpHeaderEntry(hwa->fieldId, hwa->fieldName.c_str(),
535 fieldValue);
f4698e0b
CT
536 heads->addEntry(e);
537 }
538 }
539}