Merge "stasis: Allow filtering by formatter"
[asterisk/asterisk.git] / funcs / func_uri.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Created by Olle E. Johansson, Edvina.net
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief URI encoding / decoding
22  *
23  * \author Olle E. Johansson <oej@edvina.net>
24  *
25  * \note For now this code only supports 8 bit characters, not unicode,
26          which we ultimately will need to support.
27  *
28  * \ingroup functions
29  */
30
31 /*** MODULEINFO
32         <support_level>core</support_level>
33  ***/
34
35 #include "asterisk.h"
36
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/app.h"
42
43 /*** DOCUMENTATION
44         <function name="URIENCODE" language="en_US">
45                 <synopsis>
46                         Encodes a string to URI-safe encoding according to RFC 2396.
47                 </synopsis>
48                 <syntax>
49                         <parameter name="data" required="true">
50                                 <para>Input string to be encoded.</para>
51                         </parameter>
52                 </syntax>
53                 <description>
54                         <para>Returns the encoded string defined in <replaceable>data</replaceable>.</para>
55                 </description>
56         </function>
57         <function name="URIDECODE" language="en_US">
58                 <synopsis>
59                         Decodes a URI-encoded string according to RFC 2396.
60                 </synopsis>
61                 <syntax>
62                         <parameter name="data" required="true">
63                                 <para>Input string to be decoded.</para>
64                         </parameter>
65                 </syntax>
66                 <description>
67                         <para>Returns the decoded URI-encoded <replaceable>data</replaceable> string.</para>
68                 </description>
69         </function>
70  ***/
71
72 /*! \brief uriencode: Encode URL according to RFC 2396 */
73 static int uriencode(struct ast_channel *chan, const char *cmd, char *data,
74                      char *buf, size_t len)
75 {
76         if (ast_strlen_zero(data)) {
77                 buf[0] = '\0';
78                 return 0;
79         }
80
81         ast_uri_encode(data, buf, len, ast_uri_http);
82
83         return 0;
84 }
85
86 /*!\brief uridecode: Decode URI according to RFC 2396 */
87 static int uridecode(struct ast_channel *chan, const char *cmd, char *data,
88                      char *buf, size_t len)
89 {
90         if (ast_strlen_zero(data)) {
91                 buf[0] = '\0';
92                 return 0;
93         }
94
95         ast_copy_string(buf, data, len);
96         ast_uri_decode(buf, ast_uri_http);
97
98         return 0;
99 }
100
101 static struct ast_custom_function urldecode_function = {
102         .name = "URIDECODE",
103         .read = uridecode,
104 };
105
106 static struct ast_custom_function urlencode_function = {
107         .name = "URIENCODE",
108         .read = uriencode,
109 };
110
111 static int unload_module(void)
112 {
113         return ast_custom_function_unregister(&urldecode_function)
114                 || ast_custom_function_unregister(&urlencode_function);
115 }
116
117 static int load_module(void)
118 {
119         return ast_custom_function_register(&urldecode_function)
120                 || ast_custom_function_register(&urlencode_function);
121 }
122
123 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "URI encode/decode dialplan functions");