1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
10 * \file zypp/url/UrlUtils.h
12 #ifndef ZYPP_URL_URLUTILS_H
13 #define ZYPP_URL_URLUTILS_H
15 #include "zypp/url/UrlException.h"
21 /** Characters that are safe for URL without percent-encoding. */
22 #define URL_SAFE_CHARS ":/?#[]@!$&'()*+,;="
24 //////////////////////////////////////////////////////////////////////
26 { ////////////////////////////////////////////////////////////////////
28 ////////////////////////////////////////////////////////////////////
29 /** Url details namespace. */
31 { //////////////////////////////////////////////////////////////////
34 // ---------------------------------------------------------------
35 /** A parameter vector container.
36 * A string vector containing splited PathParam- or Query-String.
37 * Each string in the vector is allways URL percent encoded and
38 * usually contains a "key=value" pair.
40 typedef std::vector < std::string > ParamVec;
43 /** A parameter map container.
44 * A map containing key and value pairs parsed from a PathParam-
47 typedef std::map < std::string, std::string > ParamMap;
53 E_ENCODED, //!< Flag to request encoded string(s).
54 E_DECODED //!< Flag to request decoded string(s).
58 // ---------------------------------------------------------------
59 /** Encodes a string using URL percent encoding.
61 * By default, all characters except of "a-zA-Z0-9_.-" will be encoded.
62 * Additional characters from the set ":/?#[]@!$&'()*+,;=", that are
63 * safe for a URL compoent without encoding, can be specified in the
66 * If the \p eflag parameter is set to E_ENCODED, then already encoded
67 * substrings will be detected and not encoded a second time.
69 * The following function call will encode the "@" character as "%40",
70 * but skip encoding of the "%" character, because the \p eflag is set
71 * to E_ENCODED and "%ba" is detected as a valid encoded character.
73 * zypp::url::encode("foo%bar@localhost", "", E_ENCODED);
75 * With \p eflag set to E_DECODED, the "%" character would be encoded
76 * as well. The complete encoded string would be "foo%25bar%40localhost".
78 * \param str A string to encode (binary data).
79 * \param safe Characters safe to skip in encoding,
80 * e.g. "/" for path names.
81 * \param eflag If to detect and skip already encoded substrings.
82 * \return A percent encoded string.
85 encode(const std::string &str, const std::string &safe = "",
86 EEncoding eflag = E_DECODED);
89 // ---------------------------------------------------------------
90 /** Decodes a URL percent encoded string.
91 * Replaces all occurences of \c "%<hex><hex>" in the \p str string
92 * with the character encoded using the two hexadecimal digits that
93 * follows the "%" character.
95 * For example, the encoded string "%40%3F%3D%26%25" will be decoded
98 * \param str A string to decode.
99 * \param allowNUL A flag, if \c "%00" (encoded \c '\\0')
101 * \return A decoded strig (may contain binary data).
102 * \throws UrlDecodingException if \p allowNUL is false and
103 * a encoded NUL byte (\c "%00") was found in \p str.
106 decode(const std::string &str, bool allowNUL = false);
109 // ---------------------------------------------------------------
110 /** Encode one character.
112 * Encode the specified character \p c into its \c "%<hex><hex>"
115 * \param c A character to encode.
116 * \return A percent encoded representation of the character,
117 * e.g. %20 for a ' ' (space).
120 encode_octet(const unsigned char c);
123 // ---------------------------------------------------------------
124 /** Decode one character.
126 * Decode the \p hex parameter pointing to (at least) two hexadecimal
127 * digits into its character value and return it.
132 * char *pct = strchr(str, '%');
133 * int chr = pct ? decode_octet(pct+1) : -1;
134 * // chr is set to the '@' ASCII character now.
137 * \param hex Pointer to two hex characters representing
138 * the character value in percent-encoded strings.
139 * \return The value (0-255) encoded in the \p hex characters or -1
140 * if \p hex does not point to two hexadecimal characters.
143 decode_octet(const char *hex);
146 // ---------------------------------------------------------------
147 /** Split into a parameter vector.
149 * Splits a parameter string \p pstr into substrings using \p psep
150 * as separator and appends the resulting substrings to \p pvec.
152 * Usual parameter separators are \c '&' for Query- and \c ',' for
155 * \param pvec Reference to a result parameter vector.
156 * \param pstr Reference to the PathParam- or Query-String to split.
157 * \param psep Parameter separator character to split at.
158 * \throws UrlNotSupportedException if \p psep separator is empty.
161 split(ParamVec &pvec,
162 const std::string &pstr,
163 const std::string &psep);
166 // ---------------------------------------------------------------
167 /** Split into a parameter map.
169 * Splits a parameter string \p pstr into substrings using \p psep as
170 * separator and then, each substring into key and value pair using
171 * \p vsep as separator between parameter key and value and adds them
172 * to the parameter map \p pmap.
174 * If a parameter substring doesn't contain any value separator \p vsep,
175 * the substring is used as a parameter key and value is set to an empty
178 * Usual parameter separators are \c '&' for Query- and \c ',' for
179 * PathParam-Strings. A usual parameter-value separator is \c '=' for
180 * both, Query- and PathParam-Strings.
182 * If the encoding flag \p eflag is set to \p E_DECODED, then the key
183 * and values are dedcoded before they are stored in the map.
185 * \param pmap Reference to a result parameter map.
186 * \param pstr Reference to the PathParam- or Query-String to split.
187 * \param psep Separator character to split key-value pairs.
188 * \param vsep Separator character to split key and value.
189 * \param eflag Flag if the key and value strings should be URL percent
190 * decoded before they're stored in the map.
191 * \throws UrlNotSupportedException if \p psep or \p vsep separator
195 split(ParamMap &pmap,
196 const std::string &pstr,
197 const std::string &psep,
198 const std::string &vsep,
199 EEncoding eflag = E_ENCODED);
202 // ---------------------------------------------------------------
203 /** Join parameter vector to a string.
205 * Creates a string containing all substrings from the \p pvec separated
206 * by \p psep separator character. The substrings in \p pvec should be
207 * already URL percent encoded and should't contain \p psep characters.
209 * Usual parameter separators are \c '&' for Query- and \c ',' for
212 * \param pvec Reference to encoded parameter vector.
213 * \param psep Parameter separator character to use.
214 * \return A parameter string.
217 join(const ParamVec &pvec,
218 const std::string &psep);
221 // ---------------------------------------------------------------
222 /** Join parameter map to a string.
224 * Creates a string containing all parameter key-value pairs from the
225 * parameter map \p pmap, that will be joined using the \p psep character
226 * and the parameter key is separated from the parameter value using the
227 * \p vsep character. Both, key and value will be automatically encoded.
229 * Usual parameter separators are \c '&' for Query- and \c ',' for
230 * PathParam-Strings. A usual parameter-value separator is \c '=' for
231 * both, Query- and PathParam-Strings.
233 * See encode() function from details about the \p safe characters.
235 * \param pmap Reference to a parameter map.
236 * \param psep Separator character to use between key-value pairs.
237 * \param vsep Separator character to use between keys and values.
238 * \param safe List of characters to accept without encoding.
239 * \return A URL percent-encoded parameter string.
240 * \throws UrlNotSupportedException if \p psep or \p vsep separator
244 join(const ParamMap &pmap,
245 const std::string &psep,
246 const std::string &vsep,
247 const std::string &safe);
250 //////////////////////////////////////////////////////////////////
252 ////////////////////////////////////////////////////////////////////
254 ////////////////////////////////////////////////////////////////////
256 //////////////////////////////////////////////////////////////////////
258 #endif /* ZYPP_URL_URLUTILS_H */
260 ** vim: set ts=2 sts=2 sw=2 ai et: