blob: e493e360c3529a7001ad12688eecd2b2b3fc8b17 [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
Alex Vakulenkoaf23b322014-05-08 16:25:45 -07005#ifndef BUFFET_DATA_ENCODING_H_
6#define BUFFET_DATA_ENCODING_H_
Chris Sosa45d9f102014-03-24 11:18:54 -07007
Chris Sosa45d9f102014-03-24 11:18:54 -07008#include <string>
Alex Vakulenkoaf23b322014-05-08 16:25:45 -07009#include <utility>
10#include <vector>
Chris Sosa45d9f102014-03-24 11:18:54 -070011
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070012namespace buffet {
Chris Sosa45d9f102014-03-24 11:18:54 -070013namespace data_encoding {
14
Alex Vakulenkob645cc92014-04-15 11:34:35 -070015typedef std::vector<std::pair<std::string, std::string>> WebParamList;
16
Chris Sosa45d9f102014-03-24 11:18:54 -070017// Encode/escape string to be used in the query portion of a URL.
18// If |encodeSpaceAsPlus| is set to true, spaces are encoded as '+' instead
19// of "%20"
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070020std::string UrlEncode(const char* data, bool encodeSpaceAsPlus);
Chris Sosa45d9f102014-03-24 11:18:54 -070021
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070022inline std::string UrlEncode(const char* data) {
Chris Sosa45d9f102014-03-24 11:18:54 -070023 return UrlEncode(data, true);
24}
25
26// Decodes/unescapes a URL. Replaces all %XX sequences with actual characters.
27// Also replaces '+' with spaces.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070028std::string UrlDecode(const char* data);
Chris Sosa45d9f102014-03-24 11:18:54 -070029
30// Converts a list of key-value pairs into a string compatible with
31// 'application/x-www-form-urlencoded' content encoding.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070032std::string WebParamsEncode(const WebParamList& params, bool encodeSpaceAsPlus);
Chris Sosa45d9f102014-03-24 11:18:54 -070033
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070034inline std::string WebParamsEncode(const WebParamList& params) {
Chris Sosa45d9f102014-03-24 11:18:54 -070035 return WebParamsEncode(params, true);
36}
37
38// Parses a string of '&'-delimited key-value pairs (separated by '=') and
39// encoded in a way compatible with 'application/x-www-form-urlencoded'
40// content encoding.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070041WebParamList WebParamsDecode(const std::string& data);
Chris Sosa45d9f102014-03-24 11:18:54 -070042
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070043} // namespace data_encoding
44} // namespace buffet
Chris Sosa45d9f102014-03-24 11:18:54 -070045
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070046#endif // BUFFET_DATA_ENCODING_H_