blob: b7223bd4d6ecf088b746992670c1ced7763ff37c [file] [log] [blame]
Chris Sosa45d9f102014-03-24 11:18:54 -07001// 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
11namespace chromeos {
12namespace data_encoding {
13
Alex Vakulenkob645cc92014-04-15 11:34:35 -070014typedef std::vector<std::pair<std::string, std::string>> WebParamList;
15
Chris Sosa45d9f102014-03-24 11:18:54 -070016// 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 Vakulenkob8ba5952014-04-17 11:35:56 -070019std::string UrlEncode(const char* data, bool encodeSpaceAsPlus);
Chris Sosa45d9f102014-03-24 11:18:54 -070020
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070021inline std::string UrlEncode(const char* data) {
Chris Sosa45d9f102014-03-24 11:18:54 -070022 return UrlEncode(data, true);
23}
24
25// Decodes/unescapes a URL. Replaces all %XX sequences with actual characters.
26// Also replaces '+' with spaces.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070027std::string UrlDecode(const char* data);
Chris Sosa45d9f102014-03-24 11:18:54 -070028
29// Converts a list of key-value pairs into a string compatible with
30// 'application/x-www-form-urlencoded' content encoding.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070031std::string WebParamsEncode(const WebParamList& params, bool encodeSpaceAsPlus);
Chris Sosa45d9f102014-03-24 11:18:54 -070032
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070033inline std::string WebParamsEncode(const WebParamList& params) {
Chris Sosa45d9f102014-03-24 11:18:54 -070034 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 Vakulenkob8ba5952014-04-17 11:35:56 -070040WebParamList WebParamsDecode(const std::string& data);
Chris Sosa45d9f102014-03-24 11:18:54 -070041
42} // namespace data_encoding
43} // namespace chromeos
44
45#endif // BUFFET_DATA_ENCODING__H_