7 #include <unordered_map>
19 int qs_strncmp(
const char* s,
const char* qs,
size_t n);
26 size_t qs_parse(
char* qs,
char* qs_kv[],
size_t qs_kv_size,
bool parse_url);
30 int qs_decode(
char * qs);
37 char * qs_k2v(
const char * key,
char *
const * qs_kv,
size_t qs_kv_size,
int nth);
42 char * qs_scanvalue(
const char * key,
const char * qs,
char * val,
size_t val_len);
48 #define CROW_QS_ISHEX(x) ((((x)>='0'&&(x)<='9') || ((x)>='A'&&(x)<='F') || ((x)>='a'&&(x)<='f')) ? 1 : 0)
49 #define CROW_QS_HEX2DEC(x) (((x)>='0'&&(x)<='9') ? (x)-48 : ((x)>='A'&&(x)<='F') ? (x)-55 : ((x)>='a'&&(x)<='f') ? (x)-87 : 0)
50 #define CROW_QS_ISQSCHR(x) ((((x)=='=')||((x)=='#')||((x)=='&')||((x)=='\0')) ? 0 : 1)
52 inline int qs_strncmp(
const char * s,
const char * qs,
size_t n)
54 unsigned char u1, u2, unyb, lnyb;
58 u1 =
static_cast<unsigned char>(*s++);
59 u2 =
static_cast<unsigned char>(*qs++);
61 if ( ! CROW_QS_ISQSCHR(u1) ) { u1 =
'\0'; }
62 if ( ! CROW_QS_ISQSCHR(u2) ) { u2 =
'\0'; }
64 if ( u1 ==
'+' ) { u1 =
' '; }
67 unyb =
static_cast<unsigned char>(*s++);
68 lnyb =
static_cast<unsigned char>(*s++);
69 if ( CROW_QS_ISHEX(unyb) && CROW_QS_ISHEX(lnyb) )
70 u1 = (CROW_QS_HEX2DEC(unyb) * 16) + CROW_QS_HEX2DEC(lnyb);
75 if ( u2 ==
'+' ) { u2 =
' '; }
78 unyb =
static_cast<unsigned char>(*qs++);
79 lnyb =
static_cast<unsigned char>(*qs++);
80 if ( CROW_QS_ISHEX(unyb) && CROW_QS_ISHEX(lnyb) )
81 u2 = (CROW_QS_HEX2DEC(unyb) * 16) + CROW_QS_HEX2DEC(lnyb);
91 if ( CROW_QS_ISQSCHR(*qs) )
98 inline size_t qs_parse(
char* qs,
char* qs_kv[],
size_t qs_kv_size,
bool parse_url =
true)
103 for(i=0; i<qs_kv_size; i++) qs_kv[i] = NULL;
106 substr_ptr = parse_url ? qs + strcspn(qs,
"?#") : qs;
109 if (substr_ptr[0] !=
'\0')
118 qs_kv[i] = substr_ptr;
119 j = strcspn(substr_ptr,
"&");
120 if ( substr_ptr[j] ==
'\0' ) { i++;
break; }
129 substr_ptr = qs_kv[j] + strcspn(qs_kv[j],
"=&#");
130 if ( substr_ptr[0] ==
'&' || substr_ptr[0] ==
'\0')
131 substr_ptr[0] =
'\0';
133 qs_decode(++substr_ptr);
144 inline int qs_decode(
char * qs)
148 while( CROW_QS_ISQSCHR(qs[j]) )
150 if ( qs[j] ==
'+' ) { qs[i] =
' '; }
151 else if ( qs[j] ==
'%' )
153 if ( ! CROW_QS_ISHEX(qs[j+1]) || ! CROW_QS_ISHEX(qs[j+2]) )
158 qs[i] = (CROW_QS_HEX2DEC(qs[j+1]) * 16) + CROW_QS_HEX2DEC(qs[j+2]);
173 inline char * qs_k2v(
const char * key,
char *
const * qs_kv,
size_t qs_kv_size,
int nth = 0)
176 size_t key_len, skip;
178 key_len = strlen(key);
183 for(i=0; i<qs_kv_size; i++)
186 if ( qs_strncmp(key, qs_kv[i], key_len) == 0 )
188 skip = strcspn(qs_kv[i],
"=");
189 if ( qs_kv[i][skip] ==
'=' )
193 return qs_kv[i] + skip;
203 inline std::unique_ptr<std::pair<std::string, std::string>> qs_dict_name2kv(
const char * dict_name,
char *
const * qs_kv,
size_t qs_kv_size,
int nth = 0)
206 size_t name_len, skip_to_eq, skip_to_brace_open, skip_to_brace_close;
208 name_len = strlen(dict_name);
213 for(i=0; i<qs_kv_size; i++)
215 if ( strncmp(dict_name, qs_kv[i], name_len) == 0 )
217 skip_to_eq = strcspn(qs_kv[i],
"=");
218 if ( qs_kv[i][skip_to_eq] ==
'=' )
220 skip_to_brace_open = strcspn(qs_kv[i],
"[");
221 if ( qs_kv[i][skip_to_brace_open] ==
'[' )
222 skip_to_brace_open++;
223 skip_to_brace_close = strcspn(qs_kv[i],
"]");
225 if ( skip_to_brace_open <= skip_to_brace_close &&
226 skip_to_brace_open > 0 &&
227 skip_to_brace_close > 0 &&
230 auto key = std::string(qs_kv[i] + skip_to_brace_open, skip_to_brace_close - skip_to_brace_open);
231 auto value = std::string(qs_kv[i] + skip_to_eq);
232 return std::unique_ptr<std::pair<std::string, std::string>>(
new std::pair<std::string, std::string>(key, value));
246 inline char * qs_scanvalue(
const char * key,
const char * qs,
char * val,
size_t val_len)
252 if ( (tmp = strchr(qs,
'?')) != NULL )
255 key_len = strlen(key);
256 while(qs[0] !=
'#' && qs[0] !=
'\0')
258 if ( qs_strncmp(key, qs, key_len) == 0 )
260 qs += strcspn(qs,
"&") + 1;
263 if ( qs[0] ==
'\0' )
return NULL;
265 qs += strcspn(qs,
"=&#");
269 i = strcspn(qs,
"&=#");
271 strncpy_s(val, val_len, qs, (val_len - 1)<(i + 1) ? (val_len - 1) : (i + 1));
273 strncpy(val, qs, (val_len - 1)<(i + 1) ? (val_len - 1) : (i + 1));
296 static const int MAX_KEY_VALUE_PAIRS_COUNT = 256;
303 for (
auto p : qs.key_value_pairs_)
305 key_value_pairs_.push_back((
char*)(p - qs.url_.c_str() + url_.c_str()));
312 key_value_pairs_.clear();
313 for (
auto p : qs.key_value_pairs_)
315 key_value_pairs_.push_back((
char*)(p - qs.url_.c_str() + url_.c_str()));
322 key_value_pairs_ = std::move(qs.key_value_pairs_);
323 char* old_data = (
char*)qs.url_.c_str();
324 url_ = std::move(qs.url_);
325 for (
auto& p : key_value_pairs_)
327 p += (
char*)url_.c_str() - old_data;
334 url_(std::move(params))
339 key_value_pairs_.resize(MAX_KEY_VALUE_PAIRS_COUNT);
340 size_t count = qs_parse(&url_[0], &key_value_pairs_[0], MAX_KEY_VALUE_PAIRS_COUNT, url);
342 key_value_pairs_.resize(count);
343 key_value_pairs_.shrink_to_fit();
348 key_value_pairs_.clear();
352 friend std::ostream& operator<<(std::ostream& os,
const query_string& qs)
355 for (
size_t i = 0; i < qs.key_value_pairs_.size(); ++i)
359 os << qs.key_value_pairs_[i];
369 char*
get(
const std::string& name)
const
371 char* ret = qs_k2v(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size());
376 char*
pop(
const std::string& name)
378 char* ret =
get(name);
381 for (
unsigned int i = 0; i < key_value_pairs_.size(); i++)
383 std::string str_item(key_value_pairs_[i]);
384 if (str_item.substr(0, name.size() + 1) == name +
'=')
386 key_value_pairs_.erase(key_value_pairs_.begin() + i);
398 std::vector<char*>
get_list(
const std::string& name,
bool use_brackets =
true)
const
400 std::vector<char*> ret;
401 std::string plus = name + (use_brackets ?
"[]" :
"");
402 char* element =
nullptr;
407 element = qs_k2v(plus.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++);
410 ret.push_back(element);
416 std::vector<char*>
pop_list(
const std::string& name,
bool use_brackets =
true)
418 std::vector<char*> ret =
get_list(name, use_brackets);
421 for (
unsigned int i = 0; i < key_value_pairs_.size(); i++)
423 std::string str_item(key_value_pairs_[i]);
424 if ((use_brackets ? (str_item.substr(0, name.size() + 3) == name +
"[]=") : (str_item.substr(0, name.size() + 1) == name +
'=')))
426 key_value_pairs_.erase(key_value_pairs_.begin() + i--);
439 std::unordered_map<std::string, std::string>
get_dict(
const std::string& name)
const
441 std::unordered_map<std::string, std::string> ret;
446 if (
auto element = qs_dict_name2kv(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++))
447 ret.insert(*element);
455 std::unordered_map<std::string, std::string>
pop_dict(
const std::string& name)
457 std::unordered_map<std::string, std::string> ret =
get_dict(name);
460 for (
unsigned int i = 0; i < key_value_pairs_.size(); i++)
462 std::string str_item(key_value_pairs_[i]);
463 if (str_item.substr(0, name.size() + 1) == name +
'[')
465 key_value_pairs_.erase(key_value_pairs_.begin() + i--);
472 std::vector<std::string> keys()
const
474 std::vector<std::string> keys;
475 keys.reserve(key_value_pairs_.size());
477 for (
const char*
const element : key_value_pairs_)
479 const char* delimiter = strchr(element,
'=');
481 keys.emplace_back(element, delimiter);
483 keys.emplace_back(element);
491 std::vector<char*> key_value_pairs_;
A class to represent any data coming after the ? in the request URL into key-value pairs.
Definition: query_string.h:294
std::unordered_map< std::string, std::string > pop_dict(const std::string &name)
Works the same as get_dict() but removes the values from the query string.
Definition: query_string.h:455
std::vector< char * > get_list(const std::string &name, bool use_brackets=true) const
Returns a list of values, passed as ?name[]=value1&name[]=value2&...name[]=valuen with n being the si...
Definition: query_string.h:398
char * get(const std::string &name) const
Get a value from a name, used for ?name=value.
Definition: query_string.h:369
std::vector< char * > pop_list(const std::string &name, bool use_brackets=true)
Similar to get_list() but it removes the.
Definition: query_string.h:416
char * pop(const std::string &name)
Works similar to get() except it removes the item from the query string.
Definition: query_string.h:376
std::unordered_map< std::string, std::string > get_dict(const std::string &name) const
Works similar to get_list() except the brackets are mandatory must not be empty.
Definition: query_string.h:439
The main namespace of the library. In this namespace is defined the most important classes and functi...