Samchon Framework for CPP  1.0.0
URLVariables.hpp
1 #pragma once
2 #include <samchon/HashMap.hpp>
3 
4 #include <initializer_list>
5 #include <samchon/WeakString.hpp>
6 
7 namespace samchon
8 {
9 namespace library
10 {
29  : public HashMap<std::string, std::string>
30  {
31  private:
33 
34  public:
35  /* ------------------------------------------------------------
36  CONSTRUCTORS
37  ------------------------------------------------------------ */
41  URLVariables() : super()
42  {
43  };
44 
53  URLVariables(const WeakString &flashVars) : super()
54  {
55  std::vector<WeakString> &items = flashVars.split("&");
56  for (size_t i = 0; i < items.size(); i++)
57  {
58  WeakString &item = items[i];
59  size_t index = item.find("=");
60 
61  if (index == std::string::npos)
62  continue;
63 
64  std::string &key = item.substr(0, index).str();
65  std::string &value = decode(item.substr(index + 1));
66 
67  set(key, value);
68  }
69  };
70 
71  public:
79  static auto encode(const WeakString &wstr) -> std::string
80  {
81  std::string res;
82  res.reserve(wstr.size() * 3);
83 
84  for (size_t i = 0; i < wstr.size(); i++)
85  {
86  unsigned char ch = wstr[i];
87 
88  if
89  (
90  ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')
91  || ('0' <= ch && ch <= '9')
92  || ch == '-' || ch == '_' || ch == '.' || ch == '~'
93  || ch == '@' || ch == ':' || ch == '/' || ch == '\\'
94  )
95  res.push_back(ch);
96  else if (ch == ' ')
97  res.append("%20");
98  else
99  res.append
100  ({
101  '%',
102  toHex(ch >> 4),
103  toHex(ch & 0x0F)
104  });
105  }
106  return res;
107  };
108 
116  static auto decode(const WeakString &wstr) -> std::string
117  {
118  std::string res;
119  res.reserve(wstr.size());
120 
121  for (size_t i = 0; i < wstr.size(); i++)
122  {
123  const char ch = wstr[i];
124 
125  if (ch == '%' && wstr.size() > i + 2)
126  {
127  char ch1 = fromHex(wstr[i + 1]);
128  char ch2 = fromHex(wstr[i + 2]);
129  char decoded = (ch1 << 4) | ch2;
130 
131  res.append({ decoded });
132  i += 2;
133  }
134  else
135  res.append({ ch });
136  }
137  return res;
138  };
139 
140  private:
141  /* ------------------------------------------------------------
142  URI ENCODING & DECONDING
143  ------------------------------------------------------------ */
144  static auto toHex(unsigned char ch) -> char
145  {
146  static const std::string lookup = "0123456789ABCDEF";
147 
148  return lookup[ch];
149  };
150 
151  static auto fromHex(unsigned char ch) -> char
152  {
153  if (ch <= '9' && ch >= '0')
154  ch -= '0';
155  else if (ch <= 'f' && ch >= 'a')
156  ch -= 'a' - 10;
157  else if (ch <= 'F' && ch >= 'A')
158  ch -= 'A' - 10;
159  else
160  ch = 0;
161 
162  return ch;
163  };
164 
165  public:
166  /* ------------------------------------------------------------
167  TO_STRING
168  ------------------------------------------------------------ */
177  auto toString() const -> std::string
178  {
179  std::string str = "";
180  for (const_iterator it = begin(); it != end(); it++)
181  {
182  if (it != begin())
183  str.append("&");
184 
185  str.append(it->first);
186  str.append("=");
187  str.append(encode(it->second));
188  }
189  return str;
190  };
191  };
192 };
193 };
auto toString() const -> std::string
Get the string representing URLVariables.
static auto encode(const WeakString &wstr) -> std::string
Encode a string into a valid URI.
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
Definition: WeakString.hpp:273
static auto decode(const WeakString &wstr) -> std::string
Decode a URI string.
URLVariables()
Default Constructor.
URLVariables(const WeakString &flashVars)
Constructor by a string representing encoded properties.
auto substr(size_t startIndex, size_t size=SIZE_MAX) const -> WeakString
Generates a substring.
Definition: WeakString.hpp:419
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
Definition: WeakString.hpp:502
Customized std::unordered_map.
Definition: HashMap.hpp:103
A string class only references characeters, reference only.
Definition: WeakString.hpp:35
URLVariables class is for representing variables of HTTP.