]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/HttpHeaderData.cc
initial version of libsbuf
[thirdparty/squid.git] / src / acl / HttpHeaderData.cc
1 /*
2 * Copyright (C) 1996-2016 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 /* DEBUG: section 28 Access Control */
10
11 #include "squid.h"
12 #include "acl/Acl.h"
13 #include "acl/Checklist.h"
14 #include "acl/HttpHeaderData.h"
15 #include "acl/RegexData.h"
16 #include "base/RegexPattern.h"
17 #include "ConfigParser.h"
18 #include "Debug.h"
19 #include "HttpHeaderTools.h"
20 #include "sbuf/SBuf.h"
21
22 /* Construct an ACLHTTPHeaderData that uses an ACLRegex rule with the value of the
23 * selected header from a given request.
24 *
25 * TODO: This can be generalised by making the type of the regex_rule into a
26 * template parameter - so that we can use different rules types in future.
27 */
28 ACLHTTPHeaderData::ACLHTTPHeaderData() : hdrId(Http::HdrType::BAD_HDR), regex_rule(new ACLRegexData)
29 {}
30
31 ACLHTTPHeaderData::~ACLHTTPHeaderData()
32 {
33 delete regex_rule;
34 }
35
36 bool
37 ACLHTTPHeaderData::match(HttpHeader* hdr)
38 {
39 if (hdr == NULL)
40 return false;
41
42 debugs(28, 3, "aclHeaderData::match: checking '" << hdrName << "'");
43
44 String value;
45 if (hdrId != Http::HdrType::BAD_HDR) {
46 if (!hdr->has(hdrId))
47 return false;
48 value = hdr->getStrOrList(hdrId);
49 } else {
50 if (!hdr->getByNameIfPresent(hdrName, value))
51 return false;
52 }
53
54 SBuf cvalue(value);
55 return regex_rule->match(cvalue.c_str());
56 }
57
58 SBufList
59 ACLHTTPHeaderData::dump() const
60 {
61 SBufList sl;
62 sl.push_back(SBuf(hdrName));
63 #if __cplusplus >= 201103L
64 sl.splice(sl.end(), regex_rule->dump());
65 #else
66 // temp is needed until c++11 move-constructor
67 SBufList temp = regex_rule->dump();
68 sl.splice(sl.end(), temp);
69 #endif
70 return sl;
71 }
72
73 void
74 ACLHTTPHeaderData::parse()
75 {
76 char* t = ConfigParser::strtokFile();
77 assert (t != NULL);
78 hdrName = t;
79 hdrId = Http::HeaderLookupTable.lookup(hdrName).id;
80 regex_rule->parse();
81 }
82
83 bool
84 ACLHTTPHeaderData::empty() const
85 {
86 return (hdrId == Http::HdrType::BAD_HDR && hdrName.isEmpty()) || regex_rule->empty();
87 }
88
89 ACLData<HttpHeader*> *
90 ACLHTTPHeaderData::clone() const
91 {
92 /* Header's don't clone yet. */
93 ACLHTTPHeaderData * result = new ACLHTTPHeaderData;
94 result->regex_rule = regex_rule->clone();
95 result->hdrId = hdrId;
96 result->hdrName = hdrName;
97 return result;
98 }
99