Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BUFFET_DATA_ENCODING__H_ |
| 6 | #define BUFFET_DATA_ENCODING__H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | #include <string> |
| 10 | |
| 11 | namespace chromeos { |
| 12 | namespace data_encoding { |
| 13 | |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 14 | typedef std::vector<std::pair<std::string, std::string>> WebParamList; |
| 15 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 16 | // Encode/escape string to be used in the query portion of a URL. |
| 17 | // If |encodeSpaceAsPlus| is set to true, spaces are encoded as '+' instead |
| 18 | // of "%20" |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 19 | std::string UrlEncode(const char* data, bool encodeSpaceAsPlus); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 20 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 21 | inline std::string UrlEncode(const char* data) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 22 | return UrlEncode(data, true); |
| 23 | } |
| 24 | |
| 25 | // Decodes/unescapes a URL. Replaces all %XX sequences with actual characters. |
| 26 | // Also replaces '+' with spaces. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 27 | std::string UrlDecode(const char* data); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 28 | |
| 29 | // Converts a list of key-value pairs into a string compatible with |
| 30 | // 'application/x-www-form-urlencoded' content encoding. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 31 | std::string WebParamsEncode(const WebParamList& params, bool encodeSpaceAsPlus); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 32 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 33 | inline std::string WebParamsEncode(const WebParamList& params) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 34 | return WebParamsEncode(params, true); |
| 35 | } |
| 36 | |
| 37 | // Parses a string of '&'-delimited key-value pairs (separated by '=') and |
| 38 | // encoded in a way compatible with 'application/x-www-form-urlencoded' |
| 39 | // content encoding. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 40 | WebParamList WebParamsDecode(const std::string& data); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 41 | |
| 42 | } // namespace data_encoding |
| 43 | } // namespace chromeos |
| 44 | |
| 45 | #endif // BUFFET_DATA_ENCODING__H_ |