Samchon Framework for CPP  1.0.0
URLVariables.cpp
1 #include <samchon/library/URLVariables.hpp>
2 #include <initializer_list>
3 
4 using namespace std;
5 using namespace samchon;
6 using namespace samchon::library;
7 
8 /* ------------------------------------------------------------
9  CONSTRUCTORS
10 ------------------------------------------------------------ */
11 URLVariables::URLVariables()
12  : super()
13 {
14 }
16  : super()
17 {
18  vector<WeakString> &items = flashVars.split("&");
19  for (size_t i = 0; i < items.size(); i++)
20  {
21  WeakString &item = items[i];
22  size_t index = item.find("=");
23 
24  if(index == string::npos)
25  continue;
26 
27  string &key = item.substr(0, index).str();
28  string &value = decode(item.substr(index + 1));
29 
30  set(key, value);
31  }
32 }
33 
34 /* ------------------------------------------------------------
35  URL ENCODING & DECONDING
36 ------------------------------------------------------------ */
37 auto URLVariables::encode(const WeakString &wstr) -> string
38 {
39  string res;
40  res.reserve(wstr.size() * 3);
41 
42  for (size_t i = 0; i < wstr.size(); i++)
43  {
44  unsigned char ch = wstr[i];
45 
46  if
47  (
48  ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')
49  || ('0' <= ch && ch <= '9')
50  || ch == '-' || ch == '_' || ch == '.' || ch == '~'
51  || ch == '@' || ch == ':' || ch == '/' || ch == '\\'
52  )
53  res.push_back(ch);
54  else if (ch == ' ')
55  res.append("%20");
56  else
57  res.append
58  ({
59  '%',
60  toHex( ch >> 4 ),
61  toHex( ch & 0x0F )
62  });
63  }
64  return res;
65 }
66 auto URLVariables::decode(const WeakString &wstr) -> string
67 {
68  string res;
69  res.reserve(wstr.size());
70 
71  for (size_t i = 0; i < wstr.size(); i++)
72  {
73  const char ch = wstr[i];
74 
75  /*if (ch == '+')
76  {
77  res.append({' '});
78  }
79  else */
80  if (ch == '%' && wstr.size() > i + 2)
81  {
82  char ch1 = fromHex(wstr[i + 1]);
83  char ch2 = fromHex(wstr[i + 2]);
84  char decoded = (ch1 << 4) | ch2;
85 
86  res.append({ decoded });
87  i += 2;
88  }
89  else
90  res.append({ch});
91  }
92  return res;
93 }
94 
95 auto URLVariables::toHex(unsigned char ch) -> char
96 {
97  static const char lookup[] = "0123456789ABCDEF";
98 
99  return lookup[ch];
100 }
101 auto URLVariables::fromHex(unsigned char ch) -> char
102 {
103  if (ch <= '9' && ch >= '0')
104  ch -= '0';
105  else if (ch <= 'f' && ch >= 'a')
106  ch -= 'a' - 10;
107  else if (ch <= 'F' && ch >= 'A')
108  ch -= 'A' - 10;
109  else
110  ch = 0;
111 
112  return ch;
113 }
114 
115 /* ------------------------------------------------------------
116  TO_STRING
117 ------------------------------------------------------------ */
118 auto URLVariables::toString() const -> string
119 {
120  string str = "";
121  for(const_iterator it = begin(); it != end(); it++)
122  {
123  if(it != begin())
124  str.append("&");
125 
126  str.append(it->first);
127  str.append("=");
128  str.append(encode(it->second));
129  }
130  return str;
131 }
auto substr(size_t startIndex, size_t endIndex=SIZE_MAX) const -> WeakString
Generates a substring.
Definition: WeakString.cpp:174
Definition: RWMutex.hpp:4
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
Definition: WeakString.cpp:212
Package of libraries.
Definition: library.hpp:84
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
Definition: WeakString.cpp:91
static auto decode(const WeakString &) -> std::string
Decode a URI string.
static auto encode(const WeakString &) -> std::string
Encode a string into a valid URI.
auto toString() const -> std::string
Get the string representing URLVariables.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
A string class only references characeters, reference only.
Definition: WeakString.hpp:32
URLVariables()
Default Constructor.