parse_expires.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "parse_expires.h"
00030 #include <stdio.h>
00031 #include "../mem/mem.h"
00032 #include "../dprint.h"
00033 #include "../trim.h"
00034 #include <string.h>
00035 #include "../ut.h"
00036
00037
00038 static inline int expires_parser(char* _s, int _l, exp_body_t* _e)
00039 {
00040 int i;
00041 str tmp;
00042
00043 tmp.s = _s;
00044 tmp.len = _l;
00045
00046 trim_leading(&tmp);
00047
00048 if (tmp.len == 0) {
00049 LM_ERR("empty body\n");
00050 _e->valid = 0;
00051 return -1;
00052 }
00053
00054 _e->text.s = tmp.s;
00055
00056 for(i = 0; i < tmp.len; i++) {
00057 if ((tmp.s[i] >= '0') && (tmp.s[i] <= '9')) {
00058 _e->val *= 10;
00059 _e->val += tmp.s[i] - '0';
00060 } else {
00061 switch(tmp.s[i]) {
00062 case ' ':
00063 case '\t':
00064 case '\r':
00065 case '\n':
00066 _e->text.len = i;
00067 _e->valid = 1;
00068 return 0;
00069
00070 default:
00071
00072
00073
00074
00075
00076
00077
00078 _e->valid = 0;
00079 return 0;
00080 }
00081 }
00082 }
00083
00084 _e->text.len = _l;
00085 _e->valid = 1;
00086 return 0;
00087 }
00088
00089
00090
00091
00092
00093 int parse_expires(struct hdr_field* _h)
00094 {
00095 exp_body_t* e;
00096
00097 if (_h->parsed) {
00098 return 0;
00099 }
00100
00101 e = (exp_body_t*)pkg_malloc(sizeof(exp_body_t));
00102 if (e == 0) {
00103 LM_ERR("no pkg memory left\n");
00104 return -1;
00105 }
00106
00107 memset(e, 0, sizeof(exp_body_t));
00108
00109 if (expires_parser(_h->body.s, _h->body.len, e) < 0) {
00110 LM_ERR("failed to parse\n");
00111 pkg_free(e);
00112 return -2;
00113 }
00114
00115 _h->parsed = (void*)e;
00116 return 0;
00117 }
00118
00119
00120
00121
00122
00123 void free_expires(exp_body_t** _e)
00124 {
00125 pkg_free(*_e);
00126 *_e = 0;
00127 }
00128
00129
00130
00131
00132
00133 void print_expires(exp_body_t* _e)
00134 {
00135 printf("===Expires===\n");
00136 printf("text: \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
00137 printf("val : %d\n", _e->val);
00138 printf("===/Expires===\n");
00139 }