QueryParams.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 16 Cache Manager API */
10 
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "ipc/TypedMsgHdr.h"
14 #include "mgr/IntParam.h"
15 #include "mgr/QueryParams.h"
16 #include "mgr/StringParam.h"
17 
19 Mgr::QueryParams::get(const String& name) const
20 {
21  Must(name.size() != 0);
22  Params::const_iterator pos = find(name);
23  return (pos == params.end() ? NULL : pos->second);
24 }
25 
26 void
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 
38 void
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);
49  msg.getPod(type);
50  QueryParam::Pointer value = CreateParam(type);
51  value->unpackValue(msg);
52  params.push_back(Param(name, value));
53  }
54 }
55 
56 Mgr::QueryParams::Params::const_iterator
57 Mgr::QueryParams::find(const String& name) const
58 {
59  Must(name.size() != 0);
60  Params::const_iterator iter = params.begin();
61  for ( ; iter != params.end(); ++iter) {
62  if (name.caseCmp(iter->first) == 0)
63  break;
64  }
65  return iter;
66 }
67 
68 bool
69 Mgr::QueryParams::ParseParam(const String& paramStr, Param& param)
70 {
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);
79  std::vector<int> array;
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  }
86  }
87  if (n < pmatch[2].rm_eo)
88  array.push_back(atoi(paramStr.substr(n, pmatch[2].rm_eo).termedBuf()));
89  param.second = new IntParam(array);
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;
95  }
96  regfree(&stringExpr);
97  regfree(&intExpr);
98  return parsed;
99 }
100 
101 bool
102 Mgr::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 
127 {
128  switch (aType) {
129  case QueryParam::ptInt:
130  return new IntParam();
131 
133  return new StringParam();
134 
135  default:
136  throw TexcHere("unknown parameter type");
137  break;
138  }
139  return NULL;
140 }
141 
void pack(Ipc::TypedMsgHdr &msg) const
store params into msg
Definition: QueryParams.cc:27
int regcomp(regex_t *preg, const char *pattern, int cflags)
Definition: GnuRegex.c:4120
int caseCmp(char const *) const
Definition: String.cc:299
int type
Definition: errorpage.cc:79
void unpack(const Ipc::TypedMsgHdr &msg)
Definition: QueryParams.cc:39
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:109
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:50
int i
Definition: membanger.c:49
regoff_t rm_eo
Definition: GnuRegex.h:382
void regfree(regex_t *preg)
Definition: GnuRegex.c:4283
size_type size() const
Definition: SquidString.h:71
static bool Parse(const String &aParamsStr, QueryParams &aParams)
parses the query string parameters
Definition: QueryParams.cc:102
#define REG_EXTENDED
Definition: GnuRegex.h:226
#define REG_ICASE
Definition: GnuRegex.h:230
static bool ParseParam(const String &paramStr, Param &param)
parses string like "param=value"; returns true if success
Definition: QueryParams.cc:69
String substr(size_type from, size_type to) const
Definition: String.cc:223
char const * termedBuf() const
Definition: SquidString.h:90
int getInt() const
load an integer
Definition: TypedMsgHdr.cc:101
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
#define Must(cond)
Definition: TextException.h:89
std::pair< String, QueryParam::Pointer > Param
Definition: QueryParams.h:26
void putString(const String &s)
store variable-length string
Definition: TypedMsgHdr.cc:133
int regexec(regex_t *preg, const char *string, size_t nmatch, pmatch, int eflags) const
Definition: GnuRegex.c:4191
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:31
regoff_t rm_so
Definition: GnuRegex.h:381
static QueryParam::Pointer CreateParam(QueryParam::Type aType)
creates a parameter of the specified type
Definition: QueryParams.cc:126
Params::const_iterator find(const String &name) const
find query parameter by name
Definition: QueryParams.cc:57
#define TexcHere(msg)
Definition: TextException.h:81
void getString(String &s) const
load variable-length string
Definition: TypedMsgHdr.cc:115
QueryParam::Pointer get(const String &name) const
returns query parameter by name
Definition: QueryParams.cc:19
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors