xhttp_trans.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2013 Crocodile RCS Ltd
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "../../pvar.h"
  23. #include "../../str.h"
  24. #include "../../trim.h"
  25. #include "xhttp_trans.h"
  26. enum _tr_xhttp_type { TR_XHTTP_NONE = 0, TR_XHTTPURL, TR_XHTTPURLQUERYSTRING };
  27. enum _tr_xhttpurl_subtype { TR_XHTTPURL_NONE = 0, TR_XHTTPURL_PATH,
  28. TR_XHTTPURL_QUERYSTRING};
  29. enum _tr_xhttpquerystring_subtype { TR_XHTTPUTLQUERYSTRING_NONE = 0,
  30. TR_XHTTPURLQUERYSTRING_VALUE};
  31. static str _httpurl_str = {0, 0};
  32. static int _httpurl_querystring_pos = 0;
  33. int xhttp_tr_eval_xhttpurl(struct sip_msg *msg, tr_param_t *tp, int subtype,
  34. pv_value_t *val)
  35. {
  36. int pos = 0;
  37. if (val == NULL || val->flags & PV_VAL_NULL)
  38. return -1;
  39. if (!(val->flags & PV_VAL_STR))
  40. {
  41. val->rs.s = int2str(val->ri, &val->rs.len);
  42. val->flags = PV_VAL_STR;
  43. }
  44. if (_httpurl_str.len == 0 || _httpurl_str.len != val->rs.len
  45. || strncmp(_httpurl_str.s, val->rs.s, val->rs.len) != 0)
  46. {
  47. if (val->rs.len > _httpurl_str.len)
  48. {
  49. if (_httpurl_str.s) pkg_free(_httpurl_str.s);
  50. _httpurl_str.s = (char *) pkg_malloc(
  51. (val->rs.len + 1) * sizeof(char));
  52. if (_httpurl_str.s == NULL)
  53. {
  54. LM_ERR("allocating package memory\n");
  55. memset(&_httpurl_str.s, 0, sizeof(str));
  56. return -1;
  57. }
  58. }
  59. _httpurl_str.len = val->rs.len;
  60. memcpy(_httpurl_str.s, val->rs.s, val->rs.len);
  61. while (pos < val->rs.len && val->rs.s[pos] != '?') pos++;
  62. _httpurl_querystring_pos = (pos >= val->rs.len) ? 0 : pos + 1;
  63. }
  64. switch (subtype)
  65. {
  66. case TR_XHTTPURL_PATH:
  67. val->rs.len = (_httpurl_querystring_pos == 0)
  68. ? val->rs.len : _httpurl_querystring_pos - 1;
  69. break;
  70. case TR_XHTTPURL_QUERYSTRING:
  71. if (_httpurl_querystring_pos == 0)
  72. {
  73. val->rs.s[0] = '\0';
  74. val->rs.len = 0;
  75. break;
  76. }
  77. val->rs.s = &val->rs.s[_httpurl_querystring_pos];
  78. val->rs.len = val->rs.len - _httpurl_querystring_pos;
  79. break;
  80. default:
  81. LM_ERR("unknown subtype %d\n", subtype);
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. char *xhttp_tr_parse_url(str *in, trans_t *t)
  87. {
  88. char *p;
  89. str name;
  90. if (in == NULL || in->s == NULL || t == NULL)
  91. return NULL;
  92. p = in->s;
  93. name.s = in->s;
  94. t->type = TR_XHTTPURL;
  95. t->trf = xhttp_tr_eval_xhttpurl;
  96. /* find next token */
  97. while (is_in_str(p, in) && *p != TR_PARAM_MARKER && *p != TR_RBRACKET)
  98. {
  99. p++;
  100. }
  101. if (*p == '\0')
  102. {
  103. LM_ERR("invalid transformation: %.*s\n", in->len, in->s);
  104. goto error;
  105. }
  106. name.len = p - name.s;
  107. trim(&name);
  108. if (name.len == 4 && strncasecmp(name.s, "path", 4) == 0)
  109. {
  110. t->subtype = TR_XHTTPURL_PATH;
  111. goto done;
  112. }
  113. else if (name.len == 11 && strncasecmp(name.s, "querystring", 11) == 0)
  114. {
  115. t->subtype = TR_XHTTPURL_QUERYSTRING;
  116. goto done;
  117. }
  118. LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s,
  119. name.len, name.s, name.len);
  120. error:
  121. return NULL;
  122. done:
  123. t->name = name;
  124. return p;
  125. }