parse_subscription_state.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*! \file
  2. * \brief Parser :: Parse subscription-state in NOTIFY
  3. *
  4. * \ingroup parser
  5. */
  6. #include "parse_subscription_state.h"
  7. #include "../dprint.h"
  8. #include "../trim.h"
  9. #include "../mem/mem.h"
  10. #include "../ut.h"
  11. #include "parser_f.h"
  12. #include "parse_param.h"
  13. #include <string.h>
  14. void free_subscription_state(subscription_state_t**ss)
  15. {
  16. if (ss) {
  17. if (*ss) pkg_free(*ss);
  18. *ss = 0;
  19. }
  20. }
  21. static inline int str_cmp(const str *a, const str *b)
  22. {
  23. int i;
  24. if (a->len != b->len) return 1;
  25. for (i = 0; i < a->len; i++)
  26. if (a->s[i] != b->s[i]) return 1;
  27. return 0;
  28. }
  29. static int ss_parse(str *src, subscription_state_t *ss)
  30. {
  31. static str active = STR_STATIC_INIT("active");
  32. static str pending = STR_STATIC_INIT("pending");
  33. static str terminated = STR_STATIC_INIT("terminated");
  34. int res = 0;
  35. param_hooks_t ph;
  36. param_t *params;
  37. str s = *src;
  38. str state;
  39. char *c, *end;
  40. /* initialization */
  41. ss->expires_set = 0;
  42. ss->expires = 0;
  43. trim_leading(&s);
  44. end = s.s + s.len;
  45. state = s;
  46. c = find_not_quoted(&s, ';');
  47. if (c) {
  48. /* first parameter starts after c */
  49. state.len = c - state.s;
  50. s.len = s.len - (c - s.s) - 1;
  51. s.s = c + 1;
  52. }
  53. else {
  54. s.len = 0;
  55. }
  56. /* set state value */
  57. trim(&state);
  58. if (str_cmp(&state, &active) == 0) {
  59. ss->value = ss_active;
  60. }
  61. else if (str_cmp(&state, &pending) == 0) {
  62. ss->value = ss_pending;
  63. }
  64. else if (str_cmp(&state, &terminated) == 0) {
  65. ss->value = ss_terminated;
  66. }
  67. else {
  68. /* INFO("unknown subscription-State value :%.*s\n",
  69. state.len, state.s); */
  70. ss->value = ss_extension;
  71. }
  72. /* explore parameters */
  73. trim_leading(&s);
  74. if (s.len > 0) {
  75. params = NULL;
  76. if (parse_params(&s, CLASS_CONTACT, &ph, &params) < 0) {
  77. ERR("can't parse params\n");
  78. res = -1;
  79. }
  80. else {
  81. if (ph.contact.expires) {
  82. ss->expires_set = 1;
  83. res = str2int(&ph.contact.expires->body, &ss->expires);
  84. if (res != 0)
  85. ERR("invalid expires value: \'%.*s\'\n",
  86. ph.contact.expires->body.len,
  87. ph.contact.expires->body.s);
  88. }
  89. if (params) free_params(params);
  90. }
  91. }
  92. /*
  93. ss->value = ss_active;
  94. ss->expires = 0;*/
  95. return res;
  96. }
  97. int parse_subscription_state(struct hdr_field *h)
  98. {
  99. subscription_state_t *ss;
  100. if (h->parsed) return 0;
  101. ss = (subscription_state_t*)pkg_malloc(sizeof(*ss));
  102. if (!ss) {
  103. ERR("No memory left\n");
  104. return -1;
  105. }
  106. memset(ss, 0, sizeof(*ss));
  107. if (ss_parse(&h->body, ss) < 0) {
  108. ERR("Can't parse Subscription-State\n");
  109. pkg_free(ss);
  110. return -2;
  111. }
  112. h->parsed = (void*)ss;
  113. return 0;
  114. }