parse_subscription_state.c

Go to the documentation of this file.
00001 
00007 #include "parse_subscription_state.h"
00008 #include "../dprint.h"
00009 #include "../trim.h"
00010 #include "../mem/mem.h"
00011 #include "../ut.h"
00012 #include "parser_f.h"
00013 #include "parse_param.h"
00014 #include <string.h>
00015 
00016 void free_subscription_state(subscription_state_t**ss)
00017 {
00018         if (ss) {
00019                 if (*ss) pkg_free(*ss);
00020                 *ss = 0;
00021         }
00022 }
00023 
00024 static inline int str_cmp(const str *a, const str *b)
00025 {
00026         int i;
00027         
00028         if (a->len != b->len) return 1;
00029         
00030         for (i = 0; i < a->len; i++) 
00031                 if (a->s[i] != b->s[i]) return 1;
00032         return 0;
00033 }
00034 
00035 static int ss_parse(str *src, subscription_state_t *ss)
00036 {
00037         static str active = STR_STATIC_INIT("active");
00038         static str pending = STR_STATIC_INIT("pending");
00039         static str terminated = STR_STATIC_INIT("terminated");
00040         
00041         int res = 0;
00042         param_hooks_t ph;
00043         param_t *params;
00044         str s = *src;
00045         str state;
00046         char *c;
00047         
00048         /* initialization */
00049         ss->expires_set = 0;
00050         ss->expires = 0;
00051         
00052         trim_leading(&s);
00053                 
00054         state = s;
00055         
00056         c = find_not_quoted(&s, ';');
00057         if (c) {
00058                 /* first parameter starts after c */
00059                 state.len = c - state.s;
00060                 s.len = s.len - (c - s.s) - 1;
00061                 s.s = c + 1;
00062         }
00063         else {
00064                 s.len = 0;
00065         }
00066 
00067         /* set state value */
00068         trim(&state);
00069         if (str_cmp(&state, &active) == 0) {
00070                 ss->value = ss_active;
00071         }
00072         else if (str_cmp(&state, &pending) == 0) {
00073                 ss->value = ss_pending;
00074         }
00075         else if (str_cmp(&state, &terminated) == 0) {
00076                 ss->value = ss_terminated;
00077         }
00078         else { 
00079                 /* INFO("unknown subscription-State value :%.*s\n",
00080                                         state.len, state.s); */
00081                 ss->value = ss_extension;
00082         }
00083 
00084         /* explore parameters */
00085         
00086         trim_leading(&s);
00087         if (s.len > 0) {
00088                 params = NULL;
00089                 if (parse_params(&s, CLASS_CONTACT, &ph, &params) < 0) {
00090                         ERR("can't parse params\n");
00091                         res = -1;
00092                 }
00093                 else {
00094                         if (ph.contact.expires) {
00095                                 ss->expires_set = 1;
00096                                 res = str2int(&ph.contact.expires->body, &ss->expires);
00097                                 if (res != 0) 
00098                                         ERR("invalid expires value: \'%.*s\'\n", 
00099                                                 ph.contact.expires->body.len,
00100                                                 ph.contact.expires->body.s);
00101                         }
00102                         if (params) free_params(params);
00103                 }
00104         }
00105         /*
00106         ss->value = ss_active;
00107         ss->expires = 0;*/
00108 
00109 
00110         return res;
00111 }
00112 
00113 int parse_subscription_state(struct hdr_field *h)
00114 {
00115         subscription_state_t *ss;
00116         if (h->parsed) return 0;
00117 
00118         ss = (subscription_state_t*)pkg_malloc(sizeof(*ss));
00119         if (!ss) {
00120                 ERR("No memory left\n");
00121                 return -1;
00122         }
00123 
00124         memset(ss, 0, sizeof(*ss));
00125 
00126         if (ss_parse(&h->body, ss) < 0) {
00127                 ERR("Can't parse Subscription-State\n");
00128                 pkg_free(ss);
00129                 return -2;
00130         }
00131 
00132         h->parsed = (void*)ss;
00133         
00134         return 0;
00135 }