HttpStatus.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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/FilledChecklist.h"
13 #include "acl/HttpStatus.h"
14 #include "acl/SplayInserter.h"
15 #include "debug/Stream.h"
16 #include "HttpReply.h"
17 
18 #include <algorithm>
19 #include <climits>
20 
22 static int aclHTTPStatusCompare(acl_httpstatus_data * const &a, acl_httpstatus_data * const &b);
24 
25 acl_httpstatus_data::acl_httpstatus_data(int x) : status1(x), status2(x) { ; }
26 
27 acl_httpstatus_data::acl_httpstatus_data(int x, int y) : status1(x), status2(y) { ; }
28 
29 SBuf
31 {
32  SBuf rv;
33  if (status2 == INT_MAX)
34  rv.Printf("%d-", status1);
35  else if (status1 == status2)
36  rv.Printf("%d", status1);
37  else
38  rv.Printf("%d-%d", status1, status2);
39  return rv;
40 }
41 
42 template <>
43 int
45 {
46  return aclHTTPStatusCompare(a, b);
47 }
48 
49 template <>
50 bool
52 {
53  return b->status1 <= a->status1 && a->status2 <= b->status2;
54 }
55 
56 template <>
59 {
60  const auto minLeft = std::min(a->status1, b->status1);
61  const auto maxRight = std::max(a->status2, b->status2);
62  return new acl_httpstatus_data(minLeft, maxRight);
63 }
64 
66 static std::ostream &
67 operator <<(std::ostream &os, const acl_httpstatus_data *status)
68 {
69  if (status)
70  os << status->toStr();
71  return os;
72 }
73 
74 ACLHTTPStatus::ACLHTTPStatus (char const *theClass) : data(nullptr), class_ (theClass)
75 {}
76 
78 {
79  if (data) {
80  data->destroy();
81  delete data;
82  }
83 }
84 
85 char const *
87 {
88  return class_;
89 }
90 
91 bool
93 {
94  return data->empty();
95 }
96 
97 static acl_httpstatus_data*
98 aclParseHTTPStatusData(const char *t)
99 {
100  int status;
101  status = atoi(t);
102  t = strchr(t, '-');
103 
104  if (!t)
105  return new acl_httpstatus_data(status);
106 
107  if (*(++t))
108  return new acl_httpstatus_data(status, atoi(t));
109 
110  return new acl_httpstatus_data(status, INT_MAX);
111 }
112 
113 void
115 {
116  if (!data)
118 
120 }
121 
122 void
124 {
125  while (char *t = ConfigParser::strtokFile()) {
127  Acl::SplayInserter<acl_httpstatus_data*>::Merge(**curlist, std::move(q));
128  }
129 }
130 
131 int
133 {
134  return aclMatchHTTPStatus(&data, Filled(checklist)->reply().sline.status());
135 }
136 
137 int
139 {
140  acl_httpstatus_data X(status);
141  const acl_httpstatus_data * const * result = (*dataptr)->find(&X, aclHTTPStatusCompare);
142 
143  debugs(28, 3, "aclMatchHTTPStatus: '" << status << "' " << (result ? "found" : "NOT found"));
144  return (result != nullptr);
145 }
146 
147 static int
149 {
150  if (a->status2 < b->status1)
151  return 1; // the entire range a is to the left of range b
152 
153  if (a->status1 > b->status2)
154  return -1; // the entire range a is to the right of range b
155 
156  return 0; // equal or partially overlapping ranges
157 }
158 
162  contents.push_back(node->toStr());
163  }
164 };
165 
166 SBufList
168 {
169  HttpStatusAclDumpVisitor visitor;
170  data->visit(visitor);
171  return visitor.contents;
172 }
173 
Definition: parse.c:104
static int Compare(const Value &a, const Value &b)
void visit(ValueVisitor &) const
left-to-right visit of all stored Values
static char * strtokFile()
Definition: ConfigParser.cc:65
bool empty() const
Definition: splay.h:78
SBuf toStr() const
Definition: HttpStatus.cc:30
const char * class_
Definition: HttpStatus.h:42
std::list< SBuf > SBufList
Definition: forward.h:22
Definition: SBuf.h:93
const A & max(A const &lhs, A const &rhs)
bool empty() const override
Definition: HttpStatus.cc:92
StatusCode
Definition: StatusCode.h:20
SBufList dump() const override
Definition: HttpStatus.cc:167
void parse() override
parses node representation in squid.conf; dies on failures
Definition: HttpStatus.cc:114
static Value MakeCombinedValue(const Value &a, const Value &b)
static void Merge(Splay< Value > &, Value &&)
Definition: SplayInserter.h:68
static int aclMatchHTTPStatus(Splay< acl_httpstatus_data * > **dataptr, Http::StatusCode status)
Definition: HttpStatus.cc:138
static int aclHTTPStatusCompare(acl_httpstatus_data *const &a, acl_httpstatus_data *const &b)
Definition: HttpStatus.cc:148
ACLFilledChecklist * Filled(ACLChecklist *checklist)
convenience and safety wrapper for dynamic_cast<ACLFilledChecklist*>
~ACLHTTPStatus() override
Definition: HttpStatus.cc:77
#define INT_MAX
Definition: types.h:70
SBuf & Printf(const char *fmt,...) PRINTF_FORMAT_ARG2
Definition: SBuf.cc:214
void operator()(const acl_httpstatus_data *node)
Definition: HttpStatus.cc:161
const char * typeString() const override
Definition: HttpStatus.cc:86
static std::ostream & operator<<(std::ostream &os, const acl_httpstatus_data *status)
reports acl_httpstatus_data using squid.conf http_status ACL value format
Definition: HttpStatus.cc:67
static acl_httpstatus_data * aclParseHTTPStatusData(const char *t)
Definition: HttpStatus.cc:98
void destroy(SPLAYFREE *=DefaultFree)
Definition: splay.h:369
ACLHTTPStatus(char const *)
Definition: HttpStatus.cc:74
int match(ACLChecklist *checklist) override
Matches the actual data in checklist against this Acl::Node.
Definition: HttpStatus.cc:132
static void aclParseHTTPStatusList(Splay< acl_httpstatus_data * > **curlist)
Definition: HttpStatus.cc:123
static bool IsSubset(const Value &a, const Value &b)
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
const A & min(A const &lhs, A const &rhs)
Splay< acl_httpstatus_data * > * data
Definition: HttpStatus.h:41

 

Introduction

Documentation

Support

Miscellaneous