functions.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * script functions of utils module
  3. *
  4. * Copyright (C) 2008 Juha Heinanen
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. /*!
  24. * \file
  25. * \brief SIP-router utils ::
  26. * \ingroup utils
  27. * Module: \ref utils
  28. */
  29. #include <curl/curl.h>
  30. #include "../../mod_fix.h"
  31. #include "../../pvar.h"
  32. #include "../../route_struct.h"
  33. #include "../../ut.h"
  34. #include "../../mem/mem.h"
  35. #include "../../parser/msg_parser.h"
  36. #include "../../lvalue.h"
  37. #include "utils.h"
  38. /*
  39. * curl write function that saves received data as zero terminated
  40. * to stream. Returns the amount of data taken care of.
  41. */
  42. size_t write_function( void *ptr, size_t size, size_t nmemb, void *stream)
  43. {
  44. /* Allocate memory and copy */
  45. char* data;
  46. data = (char*)malloc((size* nmemb) + 1);
  47. if (data == NULL) {
  48. LM_ERR("cannot allocate memory for stream\n");
  49. return CURLE_WRITE_ERROR;
  50. }
  51. memcpy(data, (char*)ptr, size* nmemb);
  52. data[nmemb] = '\0';
  53. *((char**) stream) = data;
  54. return size* nmemb;
  55. }
  56. /*
  57. * Performs http_query and saves possible result (first body line of reply)
  58. * to pvar.
  59. */
  60. int http_query(struct sip_msg* _m, char* _url, char* _dst)
  61. {
  62. CURL *curl;
  63. CURLcode res;
  64. str value;
  65. char *url, *at;
  66. char* stream;
  67. long stat;
  68. pv_spec_t *dst;
  69. pv_value_t val;
  70. double download_size;
  71. if (fixup_get_svalue(_m, (gparam_p)_url, &value) != 0) {
  72. LM_ERR("cannot get page value\n");
  73. return -1;
  74. }
  75. curl = curl_easy_init();
  76. if (curl == NULL) {
  77. LM_ERR("failed to initialize curl\n");
  78. return -1;
  79. }
  80. url = pkg_malloc(value.len + 1);
  81. if (url == NULL) {
  82. curl_easy_cleanup(curl);
  83. LM_ERR("cannot allocate pkg memory for url\n");
  84. return -1;
  85. }
  86. memcpy(url, value.s, value.len);
  87. *(url + value.len) = (char)0;
  88. curl_easy_setopt(curl, CURLOPT_URL, url);
  89. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, (long)1);
  90. curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)http_query_timeout);
  91. stream = NULL;
  92. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function);
  93. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);
  94. res = curl_easy_perform(curl);
  95. pkg_free(url);
  96. curl_easy_cleanup(curl);
  97. if (res != CURLE_OK) {
  98. LM_ERR("failed to perform curl\n");
  99. return -1;
  100. }
  101. curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &stat);
  102. if ((stat >= 200) && (stat < 400)) {
  103. curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &download_size);
  104. LM_DBG("http_query download size: %u\n", (unsigned int)download_size);
  105. /* search for line feed */
  106. at = memchr(stream, (char)10, download_size);
  107. if (at == NULL) {
  108. /* not found: use whole stream */
  109. at = stream + (unsigned int)download_size;
  110. }
  111. val.rs.s = stream;
  112. val.rs.len = at - stream;
  113. LM_DBG("http)query result: %.*s\n", val.rs.len, val.rs.s);
  114. val.flags = PV_VAL_STR;
  115. dst = (pv_spec_t *)_dst;
  116. dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
  117. }
  118. return stat;
  119. }