]> git.ipfire.org Git - thirdparty/squid.git/blame - src/mgr/QueryParams.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / mgr / QueryParams.cc
CommitLineData
b8151fa1 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
b8151fa1 3 *
bbc27441
AJ
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.
b8151fa1
CT
7 */
8
bbc27441
AJ
9/* DEBUG: section 16 Cache Manager API */
10
f7f3304a 11#include "squid.h"
b8151fa1
CT
12#include "base/TextException.h"
13#include "ipc/TypedMsgHdr.h"
14#include "mgr/IntParam.h"
b8151fa1 15#include "mgr/QueryParams.h"
602d9612 16#include "mgr/StringParam.h"
b8151fa1 17
b8151fa1 18Mgr::QueryParam::Pointer
22b5be72 19Mgr::QueryParams::get(const String& name) const
b8151fa1
CT
20{
21 Must(name.size() != 0);
22b5be72 22 Params::const_iterator pos = find(name);
b8151fa1
CT
23 return (pos == params.end() ? NULL : pos->second);
24}
25
26void
27Mgr::QueryParams::pack(Ipc::TypedMsgHdr& msg) const
28{
29 msg.putInt(params.size());
30 for (Params::const_iterator iter = params.begin(); iter != params.end(); ++iter) {
31 Must(iter->first.size() != 0);
32 msg.putString(iter->first);
33 Must(iter->second != NULL);
34 iter->second->pack(msg);
35 }
36}
37
38void
39Mgr::QueryParams::unpack(const Ipc::TypedMsgHdr& msg)
40{
41 int count = msg.getInt();
42 Must(count >= 0);
43 params.clear();
44 for ( ; count > 0; --count) {
45 String name;
46 msg.getString(name);
47 Must(name.size() != 0);
48 QueryParam::Type type;
49 msg.getPod(type);
50 QueryParam::Pointer value = CreateParam(type);
51 value->unpackValue(msg);
52 params.push_back(Param(name, value));
53 }
54}
55
22b5be72
CT
56Mgr::QueryParams::Params::const_iterator
57Mgr::QueryParams::find(const String& name) const
b8151fa1
CT
58{
59 Must(name.size() != 0);
22b5be72 60 Params::const_iterator iter = params.begin();
b8151fa1
CT
61 for ( ; iter != params.end(); ++iter) {
62 if (name.caseCmp(iter->first) == 0)
63 break;
64 }
65 return iter;
66}
67
68bool
69Mgr::QueryParams::ParseParam(const String& paramStr, Param& param)
70{
c2afddd8
AJ
71 bool parsed = false;
72 regmatch_t pmatch[3];
73 regex_t intExpr;
74 regcomp(&intExpr, "^([a-z][a-z0-9_]*)=([0-9]+((,[0-9]+))*)$", REG_EXTENDED | REG_ICASE);
75 regex_t stringExpr;
76 regcomp(&stringExpr, "^([a-z][a-z0-9_]*)=([^&= ]+)$", REG_EXTENDED | REG_ICASE);
77 if (regexec(&intExpr, paramStr.termedBuf(), 3, pmatch, 0) == 0) {
78 param.first = paramStr.substr(pmatch[1].rm_so, pmatch[1].rm_eo);
b8151fa1 79 std::vector<int> array;
c2afddd8
AJ
80 int n = pmatch[2].rm_so;
81 for (int i = n; i < pmatch[2].rm_eo; ++i) {
82 if (paramStr[i] == ',') {
83 array.push_back(atoi(paramStr.substr(n, i).termedBuf()));
84 n = i + 1;
85 }
b8151fa1 86 }
c2afddd8
AJ
87 if (n < pmatch[2].rm_eo)
88 array.push_back(atoi(paramStr.substr(n, pmatch[2].rm_eo).termedBuf()));
b8151fa1 89 param.second = new IntParam(array);
c2afddd8
AJ
90 parsed = true;
91 } else if (regexec(&stringExpr, paramStr.termedBuf(), 3, pmatch, 0) == 0) {
92 param.first = paramStr.substr(pmatch[1].rm_so, pmatch[1].rm_eo);
93 param.second = new StringParam(paramStr.substr(pmatch[2].rm_so, pmatch[2].rm_eo));
94 parsed = true;
b8151fa1 95 }
c2afddd8
AJ
96 regfree(&stringExpr);
97 regfree(&intExpr);
98 return parsed;
b8151fa1
CT
99}
100
101bool
102Mgr::QueryParams::Parse(const String& aParamsStr, QueryParams& aParams)
103{
104 if (aParamsStr.size() != 0) {
105 Param param;
106 size_t n = 0;
107 size_t len = aParamsStr.size();
108 for (size_t i = n; i < len; ++i) {
109 if (aParamsStr[i] == '&') {
110 if (!ParseParam(aParamsStr.substr(n, i), param))
111 return false;
112 aParams.params.push_back(param);
113 n = i + 1;
114 }
115 }
116 if (n < len) {
117 if (!ParseParam(aParamsStr.substr(n, len), param))
118 return false;
119 aParams.params.push_back(param);
120 }
121 }
122 return true;
123}
124
125Mgr::QueryParam::Pointer
126Mgr::QueryParams::CreateParam(QueryParam::Type aType)
127{
10c40d99 128 switch (aType) {
b8151fa1
CT
129 case QueryParam::ptInt:
130 return new IntParam();
131
132 case QueryParam::ptString:
133 return new StringParam();
134
135 default:
136 throw TexcHere("unknown parameter type");
137 break;
138 }
139 return NULL;
140}
f53969cc 141