func_uri.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /*! \file
  19. *
  20. * \brief URI encoding / decoding
  21. *
  22. * \author Olle E. Johansson <oej@edvina.net>
  23. *
  24. * \note For now this code only supports 8 bit characters, not unicode,
  25. which we ultimately will need to support.
  26. *
  27. * \ingroup functions
  28. */
  29. /*** MODULEINFO
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/module.h"
  35. #include "asterisk/channel.h"
  36. #include "asterisk/pbx.h"
  37. #include "asterisk/utils.h"
  38. #include "asterisk/app.h"
  39. /*** DOCUMENTATION
  40. <function name="URIENCODE" language="en_US">
  41. <synopsis>
  42. Encodes a string to URI-safe encoding according to RFC 2396.
  43. </synopsis>
  44. <syntax>
  45. <parameter name="data" required="true">
  46. <para>Input string to be encoded.</para>
  47. </parameter>
  48. </syntax>
  49. <description>
  50. <para>Returns the encoded string defined in <replaceable>data</replaceable>.</para>
  51. </description>
  52. </function>
  53. <function name="URIDECODE" language="en_US">
  54. <synopsis>
  55. Decodes a URI-encoded string according to RFC 2396.
  56. </synopsis>
  57. <syntax>
  58. <parameter name="data" required="true">
  59. <para>Input string to be decoded.</para>
  60. </parameter>
  61. </syntax>
  62. <description>
  63. <para>Returns the decoded URI-encoded <replaceable>data</replaceable> string.</para>
  64. </description>
  65. </function>
  66. ***/
  67. /*! \brief uriencode: Encode URL according to RFC 2396 */
  68. static int uriencode(struct ast_channel *chan, const char *cmd, char *data,
  69. char *buf, size_t len)
  70. {
  71. if (ast_strlen_zero(data)) {
  72. buf[0] = '\0';
  73. return 0;
  74. }
  75. ast_uri_encode(data, buf, len, ast_uri_http);
  76. return 0;
  77. }
  78. /*!\brief uridecode: Decode URI according to RFC 2396 */
  79. static int uridecode(struct ast_channel *chan, const char *cmd, char *data,
  80. char *buf, size_t len)
  81. {
  82. if (ast_strlen_zero(data)) {
  83. buf[0] = '\0';
  84. return 0;
  85. }
  86. ast_copy_string(buf, data, len);
  87. ast_uri_decode(buf, ast_uri_http);
  88. return 0;
  89. }
  90. static struct ast_custom_function urldecode_function = {
  91. .name = "URIDECODE",
  92. .read = uridecode,
  93. };
  94. static struct ast_custom_function urlencode_function = {
  95. .name = "URIENCODE",
  96. .read = uriencode,
  97. };
  98. static int unload_module(void)
  99. {
  100. return ast_custom_function_unregister(&urldecode_function)
  101. || ast_custom_function_unregister(&urlencode_function);
  102. }
  103. static int load_module(void)
  104. {
  105. return ast_custom_function_register(&urldecode_function)
  106. || ast_custom_function_register(&urlencode_function);
  107. }
  108. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "URI encode/decode dialplan functions");