/src/osip/src/osipparser2/osip_accept.c
Line | Count | Source |
1 | | /* |
2 | | The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-) |
3 | | Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com |
4 | | |
5 | | This library is free software; you can redistribute it and/or |
6 | | modify it under the terms of the GNU Lesser General Public |
7 | | License as published by the Free Software Foundation; either |
8 | | version 2.1 of the License, or (at your option) any later version. |
9 | | |
10 | | This library is distributed in the hope that it will be useful, |
11 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | Lesser General Public License for more details. |
14 | | |
15 | | You should have received a copy of the GNU Lesser General Public |
16 | | License along with this library; if not, write to the Free Software |
17 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | | */ |
19 | | |
20 | | #include <osipparser2/internal.h> |
21 | | |
22 | | #include <osipparser2/osip_port.h> |
23 | | #include <osipparser2/osip_message.h> |
24 | | #include <osipparser2/osip_parser.h> |
25 | | |
26 | | #ifndef MINISIZE |
27 | | |
28 | | /* adds the accept header to message. */ |
29 | | /* INPUT : char *hvalue | value of header. */ |
30 | | /* OUTPUT: osip_message_t *sip | structure to save results. */ |
31 | | /* returns -1 on error. */ |
32 | 2.53k | int osip_message_set_accept(osip_message_t *sip, const char *hvalue) { |
33 | 2.53k | osip_accept_t *accept; |
34 | 2.53k | int i; |
35 | | |
36 | 2.53k | i = accept_init(&accept); |
37 | | |
38 | 2.53k | if (i != 0) |
39 | 0 | return i; |
40 | | |
41 | 2.53k | i = osip_accept_parse(accept, hvalue); |
42 | | |
43 | 2.53k | if (i != 0) { |
44 | 1.04k | osip_accept_free(accept); |
45 | 1.04k | return i; |
46 | 1.04k | } |
47 | | |
48 | 1.48k | sip->message_property = 2; |
49 | | |
50 | 1.48k | osip_list_add(&sip->accepts, accept, -1); |
51 | 1.48k | return OSIP_SUCCESS; |
52 | 2.53k | } |
53 | | |
54 | 0 | int osip_message_get_accept(const osip_message_t *sip, int pos, osip_accept_t **dest) { |
55 | 0 | osip_accept_t *accept; |
56 | |
|
57 | 0 | *dest = NULL; |
58 | |
|
59 | 0 | if (osip_list_size(&sip->accepts) <= pos) |
60 | 0 | return OSIP_UNDEFINED_ERROR; /* does not exist */ |
61 | | |
62 | 0 | accept = (osip_accept_t *) osip_list_get(&sip->accepts, pos); |
63 | 0 | *dest = accept; |
64 | 0 | return pos; |
65 | 0 | } |
66 | | |
67 | | /* returns the content_type header as a string. */ |
68 | | /* INPUT : osip_content_type_t *content_type | content_type header. */ |
69 | | /* returns null on error. */ |
70 | 1.26k | int osip_accept_to_str(const osip_accept_t *accept, char **dest) { |
71 | 1.26k | char *buf; |
72 | 1.26k | char *tmp; |
73 | 1.26k | size_t len = 0; |
74 | | |
75 | 1.26k | *dest = NULL; |
76 | | |
77 | 1.26k | if (accept == NULL) |
78 | 0 | return OSIP_BADPARAMETER; |
79 | | |
80 | 1.26k | if (accept->type != NULL) |
81 | 287 | len += strlen(accept->type); |
82 | | |
83 | 1.26k | if (accept->subtype != NULL) |
84 | 287 | len += strlen(accept->subtype); |
85 | | |
86 | 1.26k | if (len == 0) { |
87 | | /* Empty header ! */ |
88 | 978 | buf = (char *) osip_malloc(2); |
89 | | |
90 | 978 | if (buf == NULL) |
91 | 0 | return OSIP_NOMEM; |
92 | | |
93 | 978 | buf[0] = ' '; |
94 | 978 | buf[1] = '\0'; |
95 | 978 | *dest = buf; |
96 | 978 | return OSIP_SUCCESS; |
97 | 978 | } |
98 | | |
99 | | /* try to guess a long enough length */ |
100 | 287 | len += 4 /* for '/', ' ', ';' and '\0' */ |
101 | 287 | + 10 * osip_list_size(&accept->gen_params); |
102 | | |
103 | 287 | buf = (char *) osip_malloc(len); |
104 | | |
105 | 287 | if (buf == NULL) |
106 | 0 | return OSIP_NOMEM; |
107 | | |
108 | 287 | tmp = buf; |
109 | | |
110 | 287 | sprintf(tmp, "%s/%s", accept->type, accept->subtype); |
111 | | |
112 | 287 | tmp = tmp + strlen(tmp); |
113 | 287 | { |
114 | 287 | osip_list_iterator_t it; |
115 | 287 | osip_generic_param_t *u_param = (osip_generic_param_t *) osip_list_get_first(&accept->gen_params, &it); |
116 | | |
117 | 919 | while (u_param != OSIP_SUCCESS) { |
118 | 662 | size_t tmp_len; |
119 | | |
120 | 662 | if (u_param->gvalue == NULL) { |
121 | 30 | osip_free(buf); |
122 | 30 | return OSIP_SYNTAXERROR; |
123 | 30 | } |
124 | | |
125 | 632 | tmp_len = strlen(buf) + 4 + strlen(u_param->gname) + strlen(u_param->gvalue) + 1; |
126 | | |
127 | 632 | if (len < tmp_len) { |
128 | 232 | buf = osip_realloc(buf, tmp_len); |
129 | 232 | len = tmp_len; |
130 | 232 | tmp = buf + strlen(buf); |
131 | 232 | } |
132 | | |
133 | 632 | snprintf(tmp, len - (tmp - buf), "; %s=%s", u_param->gname, u_param->gvalue); |
134 | 632 | tmp = tmp + strlen(tmp); |
135 | 632 | u_param = (osip_generic_param_t *) osip_list_get_next(&it); |
136 | 632 | } |
137 | 287 | } |
138 | 257 | *dest = buf; |
139 | 257 | return OSIP_SUCCESS; |
140 | 287 | } |
141 | | |
142 | | #endif |