]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/Tree.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / acl / Tree.cc
1 /*
2 * Copyright (C) 1996-2015 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 "acl/Checklist.h"
11 #include "acl/Tree.h"
12 #include "wordlist.h"
13
14 CBDATA_NAMESPACED_CLASS_INIT(Acl, Tree);
15
16 allow_t
17 Acl::Tree::winningAction() const
18 {
19 return actionAt(lastMatch_ - nodes.begin());
20 }
21
22 allow_t
23 Acl::Tree::lastAction() const
24 {
25 if (actions.empty())
26 return ACCESS_DUNNO;
27 return actions.back();
28 }
29
30 /// computes action that corresponds to the position of the matched rule
31 allow_t
32 Acl::Tree::actionAt(const Nodes::size_type pos) const
33 {
34 assert(pos < nodes.size());
35 if (actions.size()) {
36 assert(actions.size() == nodes.size());
37 return actions[pos];
38 }
39 // default for matched rules in trees without actions
40 return ACCESS_ALLOWED;
41 }
42
43 void
44 Acl::Tree::add(ACL *rule, const allow_t &action)
45 {
46 // either all rules have actions or none
47 assert(nodes.size() == actions.size());
48 InnerNode::add(rule);
49 actions.push_back(action);
50 }
51
52 void
53 Acl::Tree::add(ACL *rule)
54 {
55 // either all rules have actions or none
56 assert(actions.empty());
57 InnerNode::add(rule);
58 }
59
60 SBufList
61 Acl::Tree::treeDump(const char *prefix, const ActionToString &convert) const
62 {
63 SBufList text;
64 Actions::const_iterator action = actions.begin();
65 typedef Nodes::const_iterator NCI;
66 for (NCI node = nodes.begin(); node != nodes.end(); ++node) {
67
68 text.push_back(SBuf(prefix));
69
70 if (action != actions.end()) {
71 const char *act = convert ? convert[action->kind] :
72 (*action == ACCESS_ALLOWED ? "allow" : "deny");
73 text.push_back(act?SBuf(act):SBuf("???"));
74 ++action;
75 }
76
77 #if __cplusplus >= 201103L
78 text.splice(text.end(), (*node)->dump());
79 #else
80 // temp is needed until c++11 move constructor
81 SBufList temp = (*node)->dump();
82 text.splice(text.end(), temp);
83 #endif
84 text.push_back(SBuf("\n"));
85 }
86 return text;
87 }
88
89 bool
90 Acl::Tree::bannedAction(ACLChecklist *checklist, Nodes::const_iterator node) const
91 {
92 if (actions.size()) {
93 assert(actions.size() == nodes.size());
94 const Nodes::size_type pos = node - nodes.begin();
95 return checklist->bannedAction(actions.at(pos));
96 }
97 return false;
98 }
99