httpapitest.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * http_client API test Module
  3. * Copyright (C) 2015-2016 Edvina AB, Olle E. Johansson
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /*! \file
  23. * \brief Kamailio http_client :: The test module
  24. * Not compiled by default
  25. * \ingroup http_client
  26. */
  27. #include "../../modules/http_client/curl_api.h"
  28. #include "../../mod_fix.h"
  29. #include "../../sr_module.h"
  30. #include "../../ut.h"
  31. #include "../../resolve.h"
  32. #include "../../locking.h"
  33. #include "../../config.h"
  34. #include "../../lvalue.h"
  35. MODULE_VERSION
  36. /* Module parameter variables */
  37. str default_http_conn = STR_NULL; /*!< Default connection to test */
  38. /* Module management function prototypes */
  39. static int mod_init(void);
  40. static int child_init(int);
  41. static void destroy(void);
  42. /* Fixup functions to be defined later */
  43. static int fixup_testcurl_connect(void** param, int param_no);
  44. static int fixup_free_testcurl_connect(void** param, int param_no);
  45. static int fixup_testcurl_connect_post(void** param, int param_no);
  46. static int fixup_free_testcurl_connect_post(void** param, int param_no);
  47. static int w_testcurl_connect(struct sip_msg* _m, char* _con, char * _url, char* _result);
  48. static httpc_api_t httpapi;
  49. /* Exported functions */
  50. static cmd_export_t cmds[] = {
  51. /* Test_http_connect(connection, <URL>, <result pvar>) - HTTP GET */
  52. {"test_http_connect", (cmd_function)w_testcurl_connect, 3, fixup_testcurl_connect,
  53. fixup_free_testcurl_connect,
  54. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  55. };
  56. /* Exported parameters */
  57. static param_export_t params[] = {
  58. {"test_connection", PARAM_INT, &default_connection_timeout},
  59. {0, 0, 0}
  60. };
  61. /* Module interface */
  62. struct module_exports exports = {
  63. "httpapitest",
  64. DEFAULT_DLFLAGS, /* dlopen flags */
  65. cmds, /* Exported functions */
  66. params, /* Exported parameters */
  67. 0, /* exported statistics */
  68. 0, /* exported MI functions */
  69. 0, /* exported pseudo-variables */
  70. 0, /* extra processes */
  71. mod_init, /* module initialization function */
  72. 0, /* response function*/
  73. destroy, /* destroy function */
  74. child_init /* per-child init function */
  75. };
  76. /* Module initialization function */
  77. static int mod_init(void)
  78. {
  79. LM_DBG("init httpapitest module\n");
  80. if (httpc_load_api(&httpapi) != 0) {
  81. LM_ERR("Can not bind to http_client API \n");
  82. return -1;
  83. }
  84. LM_DBG("**** init httpapitest module done.\n");
  85. return 0;
  86. }
  87. /* Child initialization function */
  88. static int child_init(int rank)
  89. {
  90. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN) {
  91. return 0; /* do nothing for the main process */
  92. }
  93. return 0;
  94. }
  95. static void destroy(void)
  96. {
  97. ;
  98. }
  99. /* Fixup functions */
  100. /*
  101. * Fix test_curl_connect params:
  102. * 1. connection(string/pvar)
  103. * 2. url (string that may contain pvars) and
  104. * 3. result (writable pvar).
  105. */
  106. static int fixup_testcurl_connect(void** param, int param_no)
  107. {
  108. /* 1. Connection */
  109. if (param_no == 1) {
  110. /* We want char * strings */
  111. return 0;
  112. }
  113. /* 2. URL and data may contain pvar */
  114. if (param_no == 2) {
  115. return fixup_spve_null(param, 1);
  116. }
  117. /* 3. PVAR for result */
  118. if (param_no == 3) {
  119. if (fixup_pvar_null(param, 1) != 0) {
  120. LM_ERR("failed to fixup result pvar\n");
  121. return -1;
  122. }
  123. if (((pv_spec_t *)(*param))->setf == NULL) {
  124. LM_ERR("result pvar is not writeble\n");
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. LM_ERR("invalid parameter number <%d>\n", param_no);
  130. return -1;
  131. }
  132. /*
  133. * Free testcurl_connect params.
  134. */
  135. static int fixup_free_testcurl_connect(void** param, int param_no)
  136. {
  137. if (param_no == 1) {
  138. /* Char strings don't need freeing */
  139. return 0;
  140. }
  141. if (param_no == 2) {
  142. return fixup_free_spve_null(param, 1);
  143. }
  144. if (param_no == 3) {
  145. return fixup_free_pvar_null(param, 1);
  146. }
  147. LM_ERR("invalid parameter number <%d>\n", param_no);
  148. return -1;
  149. }
  150. /*
  151. * Wrapper for Curl_connect (GET)
  152. */
  153. static int w_testcurl_connect(struct sip_msg* _m, char* _con, char * _url, char* _result) {
  154. str con = {NULL,0};
  155. str url = {NULL,0};
  156. str result = {NULL,0};
  157. pv_spec_t *dst;
  158. pv_value_t val;
  159. int ret = 0;
  160. if (_con == NULL || _url == NULL || _result == NULL) {
  161. LM_ERR("Invalid parameter\n");
  162. return -1;
  163. }
  164. con.s = _con;
  165. con.len = strlen(con.s);
  166. if (get_str_fparam(&url, _m, (gparam_p)_url) != 0) {
  167. LM_ERR("_url has no value\n");
  168. return -1;
  169. }
  170. LM_DBG("**** Curl Connection %s URL %s Result var %s\n", _con, _url, _result);
  171. /* API http_connect(msg, connection, url, result, content_type, post) */
  172. ret = httpapi.http_connect(_m, &con, &url, &result, NULL, NULL);
  173. val.rs = result;
  174. val.flags = PV_VAL_STR;
  175. dst = (pv_spec_t *)_result;
  176. dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
  177. if (result.s != NULL)
  178. pkg_free(result.s);
  179. return ret;
  180. }