Thu Apr 28 2011 17:13:33

Asterisk developer's documentation


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 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 153365 $")
00034 
00035 #include "asterisk/module.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/app.h"
00040 
00041 /*** DOCUMENTATION
00042    <function name="URIENCODE" language="en_US">
00043       <synopsis>
00044          Encodes a string to URI-safe encoding according to RFC 2396.
00045       </synopsis>
00046       <syntax>
00047          <parameter name="data" required="true">
00048             <para>Input string to be encoded.</para>
00049          </parameter>
00050       </syntax>
00051       <description>
00052          <para>Returns the encoded string defined in <replaceable>data</replaceable>.</para>
00053       </description>
00054    </function>
00055    <function name="URIDECODE" language="en_US">
00056       <synopsis>
00057          Decodes a URI-encoded string according to RFC 2396.
00058       </synopsis>
00059       <syntax>
00060          <parameter name="data" required="true">
00061             <para>Input string to be decoded.</para>
00062          </parameter>
00063       </syntax>
00064       <description>
00065          <para>Returns the decoded URI-encoded <replaceable>data</replaceable> string.</para>
00066       </description>
00067    </function>
00068  ***/
00069 
00070 /*! \brief uriencode: Encode URL according to RFC 2396 */
00071 static int uriencode(struct ast_channel *chan, const char *cmd, char *data,
00072            char *buf, size_t len)
00073 {
00074    if (ast_strlen_zero(data)) {
00075       ast_log(LOG_WARNING, "Syntax: URIENCODE(<data>) - missing argument!\n");
00076       return -1;
00077    }
00078 
00079    ast_uri_encode(data, buf, len, 1);
00080 
00081    return 0;
00082 }
00083 
00084 /*!\brief uridecode: Decode URI according to RFC 2396 */
00085 static int uridecode(struct ast_channel *chan, const char *cmd, char *data,
00086            char *buf, size_t len)
00087 {
00088    if (ast_strlen_zero(data)) {
00089       ast_log(LOG_WARNING, "Syntax: URIDECODE(<data>) - missing argument!\n");
00090       return -1;
00091    }
00092 
00093    ast_copy_string(buf, data, len);
00094    ast_uri_decode(buf);
00095 
00096    return 0;
00097 }
00098 
00099 static struct ast_custom_function urldecode_function = {
00100    .name = "URIDECODE",
00101    .read = uridecode,
00102 };
00103 
00104 static struct ast_custom_function urlencode_function = {
00105    .name = "URIENCODE",
00106    .read = uriencode,
00107 };
00108 
00109 static int unload_module(void)
00110 {
00111    return ast_custom_function_unregister(&urldecode_function)
00112       || ast_custom_function_unregister(&urlencode_function);
00113 }
00114 
00115 static int load_module(void)
00116 {
00117    return ast_custom_function_register(&urldecode_function)
00118       || ast_custom_function_register(&urlencode_function);
00119 }
00120 
00121 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "URI encode/decode dialplan functions");