Adapted from http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/ example/http/server3/request_handler.cpp See http://www.boost.org/LICENSE_1_0.txt for license for this method.
| 98 | // example/http/server3/request_handler.cpp |
| 99 | // See http://www.boost.org/LICENSE_1_0.txt for license for this method. |
| 100 | bool UrlDecode(const string& in, string* out, bool hive_compat) { |
| 101 | out->clear(); |
| 102 | out->reserve(in.size()); |
| 103 | for (size_t i = 0; i < in.size(); ++i) { |
| 104 | if (in[i] == '%') { |
| 105 | if (i + 3 <= in.size()) { |
| 106 | int value = 0; |
| 107 | istringstream is(in.substr(i + 1, 2)); |
| 108 | if (is >> hex >> value) { |
| 109 | (*out) += static_cast<char>(value); |
| 110 | i += 2; |
| 111 | } else { |
| 112 | return false; |
| 113 | } |
| 114 | } else { |
| 115 | return false; |
| 116 | } |
| 117 | } else if (!hive_compat && in[i] == '+') { // Hive does not encode ' ' as '+' |
| 118 | (*out) += ' '; |
| 119 | } else { |
| 120 | (*out) += in[i]; |
| 121 | } |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool Base64EncodeBufLen(int64_t in_len, int64_t* out_max) { |
| 127 | // Base64 encoding turns every 3 bytes into 4 characters. If the length is not |