| 1 |
/* |
| 2 |
* Copyright (C) 2005-2021 Team Kodi (https://kodi.tv) |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
* See LICENSE.md for more information. |
| 6 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include <string> |
| 11 |
|
| 12 |
namespace uri |
| 13 |
{ |
| 14 |
/// Char class. |
| 15 |
typedef enum char_class_e : signed char |
| 16 |
{ |
| 17 |
CINV = -2, ///< invalid |
| 18 |
CEND = -1, ///< end delimitor |
| 19 |
CVAL = 0, ///< valid any position |
| 20 |
CVA2 = 1, ///< valid anywhere but 1st position |
| 21 |
} char_class_e_type; |
| 22 |
|
| 23 |
/// Traits used for parsing and encoding components. |
| 24 |
struct traits |
| 25 |
{ |
| 26 |
const char* begin_cstring; ///< begin cstring (or 0 if none) |
| 27 |
const char begin_char; ///< begin char (or 0 if none) |
| 28 |
const char end_char; ///< end char (or 0 if none) |
| 29 |
const char_class_e_type char_class[256]; ///< map of char to class |
| 30 |
}; |
| 31 |
|
| 32 |
/** |
| 33 |
* \brief Encode the URI (sub) component. |
| 34 |
* Note that this should be used on the subcomponents before appending to |
| 35 |
* subdelimiter chars, if any. |
| 36 |
* |
| 37 |
* From the RFC: URI producing applications should percent-encode data octets |
| 38 |
* are specifically allowed by the URI scheme to represent data in that |
| 39 |
* component. If a reserved character is found in a URI component and |
| 40 |
* no delimiting role is known for that character, then it must be |
| 41 |
* interpreted as representing the data octet corresponding to that |
| 42 |
* character's encoding in US-ASCII. |
| 43 |
* @see http://tools.ietf.org/html/rfc3986 |
| 44 |
* @see decode std::string encode(const traits& ts, const std::string& comp); |
| 45 |
*/ |
| 46 |
std::string encode(const traits& ts, const std::string& comp); |
| 47 |
/** |
| 48 |
* Decode the pct-encoded (hex) sequences, if any, return success. |
| 49 |
* Does not change string on error. |
| 50 |
* @see http://tools.ietf.org/html/rfc3986#section-2.1 |
| 51 |
* @see encode |
| 52 |
* \param s A reference to the std::string to decode |
| 53 |
*/ |
| 54 |
bool decode(std::string& s); |
| 55 |
|
| 56 |
extern const char ENCODE_BEGIN_CHAR; ///< encode begin char ('\%') |
| 57 |
extern const traits SCHEME_TRAITS; ///< scheme traits |
| 58 |
extern const traits AUTHORITY_TRAITS; ///< authority traits |
| 59 |
extern const traits PATH_TRAITS; ///< path traits |
| 60 |
extern const traits QUERY_TRAITS; ///< query traits |
| 61 |
extern const traits FRAGMENT_TRAITS; ///< fragment traits |
| 62 |
} |