func_uri.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Created by Olle E. Johansson, Edvina.net 
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief URI encoding / decoding
00022  *
00023  * \author Olle E. Johansson <oej@edvina.net>
00024  * 
00025  * \note For now this code only supports 8 bit characters, not unicode,
00026          which we ultimately will need to support.
00027  * 
00028  * \ingroup functions
00029  */
00030 
00031 /*** MODULEINFO
00032    <support_level>core</support_level>
00033  ***/
00034 
00035 #include "asterisk.h"
00036 
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 418654 $")
00038 
00039 #include "asterisk/module.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/utils.h"
00043 #include "asterisk/app.h"
00044 
00045 /*** DOCUMENTATION
00046    <function name="URIENCODE" language="en_US">
00047       <synopsis>
00048          Encodes a string to URI-safe encoding according to RFC 2396.
00049       </synopsis>
00050       <syntax>
00051          <parameter name="data" required="true">
00052             <para>Input string to be encoded.</para>
00053          </parameter>
00054       </syntax>
00055       <description>
00056          <para>Returns the encoded string defined in <replaceable>data</replaceable>.</para>
00057       </description>
00058    </function>
00059    <function name="URIDECODE" language="en_US">
00060       <synopsis>
00061          Decodes a URI-encoded string according to RFC 2396.
00062       </synopsis>
00063       <syntax>
00064          <parameter name="data" required="true">
00065             <para>Input string to be decoded.</para>
00066          </parameter>
00067       </syntax>
00068       <description>
00069          <para>Returns the decoded URI-encoded <replaceable>data</replaceable> string.</para>
00070       </description>
00071    </function>
00072  ***/
00073 
00074 /*! \brief uriencode: Encode URL according to RFC 2396 */
00075 static int uriencode(struct ast_channel *chan, const char *cmd, char *data,
00076            char *buf, size_t len)
00077 {
00078    if (ast_strlen_zero(data)) {
00079       buf[0] = '\0';
00080       return 0;
00081    }
00082 
00083    ast_uri_encode(data, buf, len, ast_uri_http);
00084 
00085    return 0;
00086 }
00087 
00088 /*!\brief uridecode: Decode URI according to RFC 2396 */
00089 static int uridecode(struct ast_channel *chan, const char *cmd, char *data,
00090            char *buf, size_t len)
00091 {
00092    if (ast_strlen_zero(data)) {
00093       buf[0] = '\0';
00094       return 0;
00095    }
00096 
00097    ast_copy_string(buf, data, len);
00098    ast_uri_decode(buf, ast_uri_http);
00099 
00100    return 0;
00101 }
00102 
00103 static struct ast_custom_function urldecode_function = {
00104    .name = "URIDECODE",
00105    .read = uridecode,
00106 };
00107 
00108 static struct ast_custom_function urlencode_function = {
00109    .name = "URIENCODE",
00110    .read = uriencode,
00111 };
00112 
00113 static int unload_module(void)
00114 {
00115    return ast_custom_function_unregister(&urldecode_function)
00116       || ast_custom_function_unregister(&urlencode_function);
00117 }
00118 
00119 static int load_module(void)
00120 {
00121    return ast_custom_function_register(&urldecode_function)
00122       || ast_custom_function_register(&urlencode_function);
00123 }
00124 
00125 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "URI encode/decode dialplan functions");

Generated on Thu Apr 16 06:27:36 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6