/src/opensips/parser/parse_expires.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Expires header field body parser |
3 | | * |
4 | | * Copyright (C) 2001-2003 FhG Fokus |
5 | | * |
6 | | * |
7 | | * This file is part of opensips, a free SIP server. |
8 | | * |
9 | | * opensips is free software; you can redistribute it and/or modify |
10 | | * it under the terms of the GNU General Public License as published by |
11 | | * the Free Software Foundation; either version 2 of the License, or |
12 | | * (at your option) any later version |
13 | | * |
14 | | * opensips is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU General Public License |
20 | | * along with this program; if not, write to the Free Software |
21 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | | * |
23 | | * History: |
24 | | * -------- |
25 | | * 2003-04-26 ZSW (jiri) |
26 | | */ |
27 | | |
28 | | |
29 | | #include <stdio.h> /* printf */ |
30 | | #include <string.h> /* memset */ |
31 | | #include "../mem/mem.h" /* pkg_malloc, pkg_free */ |
32 | | #include "../dprint.h" |
33 | | #include "../trim.h" /* trim_leading */ |
34 | | #include "../ut.h" |
35 | | #include "../errinfo.h" |
36 | | #include "parse_expires.h" |
37 | | |
38 | | |
39 | | static inline int expires_parser(char* _s, int _l, exp_body_t* _e) |
40 | 0 | { |
41 | 0 | str tmp; |
42 | |
|
43 | 0 | tmp.s = _s; |
44 | 0 | tmp.len = _l; |
45 | |
|
46 | 0 | trim(&tmp); |
47 | |
|
48 | 0 | if (tmp.len == 0) { |
49 | 0 | LM_ERR("empty body\n"); |
50 | 0 | _e->valid = 0; |
51 | 0 | return -1; |
52 | 0 | } |
53 | | |
54 | 0 | if ( str2int( &tmp, (unsigned int*)&_e->val)!=0 ) { |
55 | 0 | LM_ERR("body is not a number <%.*s>\n",tmp.len,tmp.s); |
56 | 0 | _e->valid = 0; |
57 | 0 | return -2; |
58 | 0 | } |
59 | | |
60 | 0 | _e->text = tmp; |
61 | 0 | _e->valid = 1; |
62 | |
|
63 | 0 | return 0; |
64 | 0 | } |
65 | | |
66 | | |
67 | | /* |
68 | | * Parse expires header field body |
69 | | */ |
70 | | int parse_expires(struct hdr_field* _h) |
71 | 0 | { |
72 | 0 | exp_body_t* e; |
73 | |
|
74 | 0 | if (_h->parsed) { |
75 | 0 | return 0; /* Already parsed */ |
76 | 0 | } |
77 | | |
78 | 0 | e = (exp_body_t*)pkg_malloc(sizeof(exp_body_t)); |
79 | 0 | if (e == 0) { |
80 | 0 | LM_ERR("no pkg memory left\n"); |
81 | 0 | return -1; |
82 | 0 | } |
83 | | |
84 | 0 | memset(e, 0, sizeof(exp_body_t)); |
85 | |
|
86 | 0 | if (expires_parser(_h->body.s, _h->body.len, e) < 0) { |
87 | 0 | LM_ERR("failed to parse\n"); |
88 | 0 | set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, |
89 | 0 | "error parsing EXPIRE header"); |
90 | 0 | set_err_reply(400, "bad headers"); |
91 | 0 | pkg_free(e); |
92 | 0 | return -2; |
93 | 0 | } |
94 | | |
95 | 0 | _h->parsed = (void*)e; |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | | |
100 | | /* |
101 | | * Free all memory associated with exp_body_t |
102 | | */ |
103 | | void free_expires(exp_body_t** _e) |
104 | 0 | { |
105 | 0 | pkg_free(*_e); |
106 | 0 | *_e = 0; |
107 | 0 | } |
108 | | |
109 | | |
110 | | /* |
111 | | * Print exp_body_t content, for debugging only |
112 | | */ |
113 | | void print_expires(exp_body_t* _e) |
114 | 0 | { |
115 | 0 | printf("===Expires===\n"); |
116 | 0 | printf("text: \'%.*s\'\n", _e->text.len, ZSW(_e->text.s)); |
117 | 0 | printf("val : %d\n", _e->val); |
118 | 0 | printf("===/Expires===\n"); |
119 | 0 | } |