| 831 | |
| 832 | |
| 833 | Try<string> decode(const string& s) |
| 834 | { |
| 835 | ostringstream out; |
| 836 | |
| 837 | for (size_t i = 0; i < s.length(); ++i) { |
| 838 | if (s[i] != '%') { |
| 839 | out << (s[i] == '+' ? ' ' : s[i]); |
| 840 | continue; |
| 841 | } |
| 842 | |
| 843 | // We now expect two more characters: "% HEXDIG HEXDIG" |
| 844 | if (i + 2 >= s.length() || !isxdigit(s[i+1]) || !isxdigit(s[i+2])) { |
| 845 | return Error( |
| 846 | "Malformed % escape in '" + s + "': '" + s.substr(i, 3) + "'"); |
| 847 | } |
| 848 | |
| 849 | // Convert from HEXDIG HEXDIG to char value. |
| 850 | istringstream in(s.substr(i + 1, 2)); |
| 851 | unsigned long l; |
| 852 | in >> std::hex >> l; |
| 853 | if (l > UCHAR_MAX) { |
| 854 | ABORT("Unexpected conversion from hex string: " + s.substr(i + 1, 2) + |
| 855 | " to unsigned long: " + stringify(l)); |
| 856 | } |
| 857 | out << static_cast<unsigned char>(l); |
| 858 | |
| 859 | i += 2; |
| 860 | } |
| 861 | |
| 862 | return out.str(); |
| 863 | } |
| 864 | |
| 865 | |
| 866 | Try<vector<Response>> decodeResponses(const string& s) |