Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 2009

## 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 - 2005, 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  * \note For now this code only supports 8 bit characters, not unicode,
    00024          which we ultimately will need to support.
    00025  *
    00026  */
    00027
    00028 #include <stdlib.h>
    00029 #include <stdio.h>
    00030 #include <string.h>
    00031 #include <sys/types.h>
    00032
    00033 #include "asterisk.h"
    00034
    00035 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $") */
    00036
    00037 #include "asterisk/channel.h"
    00038 #include "asterisk/pbx.h"
    00039 #include "asterisk/logger.h"
    00040 #include "asterisk/utils.h"
    00041 #include "asterisk/app.h"
    00042 #include "asterisk/module.h"
    00043
    00044 /*! \brief builtin_function_uriencode: Encode URL according to RFC 2396 */
    00045 static char *builtin_function_uriencode(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
    00046 {
    00047    char uri[BUFSIZ];
    00048
    00049    if (ast_strlen_zero(data)) {
    00050       ast_log(LOG_WARNING, "Syntax: URIENCODE(<data>) - missing argument!\n");
    00051       return NULL;
    00052    }
    00053
    00054    ast_uri_encode(data, uri, sizeof(uri), 1);
    00055    ast_copy_string(buf, uri, len);
    00056
    00057    return buf;
    00058 }
    00059
    00060 /*!\brief builtin_function_uridecode: Decode URI according to RFC 2396 */
    00061 static char *builtin_function_uridecode(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
    00062 {
    00063    if (ast_strlen_zero(data)) {
    00064       ast_log(LOG_WARNING, "Syntax: URIDECODE(<data>) - missing argument!\n");
    00065       return NULL;
    00066    }
    00067
    00068
    00069    ast_copy_string(buf, data, len);
    00070    ast_uri_decode(buf);
    00071    return buf;
    00072 }
    00073
    00074 #ifndef BUILTIN_FUNC
    00075 static
    00076 #endif
    00077 struct ast_custom_function urldecode_function = {
    00078    .name = "URIDECODE",
    00079    .synopsis = "Decodes an URI-encoded string.",
    00080    .syntax = "URIDECODE(<data>)",
    00081    .read = builtin_function_uridecode,
    00082 };
    00083
    00084 #ifndef BUILTIN_FUNC
    00085 static
    00086 #endif
    00087 struct ast_custom_function urlencode_function = {
    00088    .name = "URIENCODE",
    00089    .synopsis = "Encodes a string to URI-safe encoding.",
    00090    .syntax = "URIENCODE(<data>)",
    00091    .read = builtin_function_uriencode,
    00092 };
    00093
    00094 #ifndef BUILTIN_FUNC
    00095 static char *tdesc = "URI encode/decode functions";
    00096
    00097 int unload_module(void)
    00098 {
    00099         return ast_custom_function_unregister(&urldecode_function) || ast_custom_function_unregister(&urlencode_function);
    00100 }
    00101
    00102 int load_module(void)
    00103 {
    00104         return ast_custom_function_register(&urldecode_function) || ast_custom_function_register(&urlencode_function);
    00105 }
    00106
    00107 char *description(void)
    00108 {
    00109    return tdesc;
    00110 }
    00111
    00112 int usecount(void)
    00113 {
    00114    return 0;
    00115 }
    00116
    00117 char *key()
    00118 {
    00119    return ASTERISK_GPL_KEY;
    00120 }
    00121 #endif /* BUILTIN_FUNC */