File: hdr_acceptencoding.c

package info (click to toggle)
libosip 0.9.7-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,852 kB
  • ctags: 1,812
  • sloc: ansic: 20,266; sh: 9,218; makefile: 215
file content (238 lines) | stat: -rw-r--r-- 5,855 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
  The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
  Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdlib.h>
#include <stdio.h>

#include <osip/port.h>
#include <osip/smsg.h>
#include "msg.h"


/* Accept-Encoding = token
   token possible values are gzip,compress,deflate,identity
*/
int
msg_setaccept_encoding (sip_t * sip, char *hvalue)
{
  accept_encoding_t *accept_encoding;
  int i;

  i = accept_encoding_init (&accept_encoding);
  if (i != 0)
    return -1;
  i = accept_encoding_parse (accept_encoding, hvalue);
  if (i != 0)
    {
      accept_encoding_free (accept_encoding);
      sfree (accept_encoding);
      return -1;
    }
#ifdef USE_TMP_BUFFER
  sip->message_property = 2;
#endif
  list_add (sip->accept_encodings, accept_encoding, -1);
  return 0;
}

int
msg_getaccept_encoding (sip_t * sip, int pos, accept_encoding_t ** dest)
{
  accept_encoding_t *accept_encoding;

  *dest = NULL;
  if (list_size (sip->accept_encodings) <= pos)
    return -1;			/* does not exist */
  accept_encoding =
    (accept_encoding_t *) list_get (sip->accept_encodings, pos);
  *dest = accept_encoding;
  return pos;
}

int
accept_encoding_init (accept_encoding_t ** accept_encoding)
{
  *accept_encoding =
    (accept_encoding_t *) smalloc (sizeof (accept_encoding_t));
  if (*accept_encoding == NULL)
    return -1;
  (*accept_encoding)->element = NULL;

  (*accept_encoding)->gen_params = (list_t *) smalloc (sizeof (list_t));
  if ((*accept_encoding)->gen_params == NULL)
    {
      sfree (*accept_encoding);
      *accept_encoding = NULL;
      return -1;
    }
  list_init ((*accept_encoding)->gen_params);

  return 0;
}

int
accept_encoding_parse (accept_encoding_t * accept_encoding, char *hvalue)
{
  char *accept_encoding_params;

  accept_encoding_params = strchr (hvalue, ';');

  if (accept_encoding_params != NULL)
    {
      if (generic_param_parseall (accept_encoding->gen_params,
				  accept_encoding_params) == -1)
	return -1;
    }
  else
    accept_encoding_params = hvalue + strlen (hvalue);

  if (accept_encoding_params - hvalue + 1 < 2)
    return -1;
  accept_encoding->element =
    (char *) smalloc (accept_encoding_params - hvalue + 1);
  if (accept_encoding->element == NULL)
    return -1;
  sstrncpy (accept_encoding->element, hvalue,
	    accept_encoding_params - hvalue);
  sclrspace (accept_encoding->element);

  return 0;
}

/* returns the accept_encoding header as a string.  */
/* INPUT : accept_encoding_t *accept_encoding | accept_encoding header.   */
/* returns null on error. */
int
accept_encoding_2char (accept_encoding_t * accept_encoding, char **dest)
{
  char *buf;
  char *tmp;
  int len;

  *dest = NULL;
  if ((accept_encoding == NULL) || (accept_encoding->element == NULL))
    return -1;

  len = strlen (accept_encoding->element) + 2;
  buf = (char *) smalloc (len);
  if (buf == NULL)
    return -1;

  sprintf (buf, "%s", accept_encoding->element);
  {
    int pos = 0;
    int plen;
    generic_param_t *u_param;

    while (!list_eol (accept_encoding->gen_params, pos))
      {
	u_param =
	  (generic_param_t *) list_get (accept_encoding->gen_params, pos);
	if (u_param->gvalue == NULL)
	  plen = strlen (u_param->gname) + 2;
	else
	  plen = strlen (u_param->gname) + strlen (u_param->gvalue) + 3;
	len = len + plen;
	buf = (char *) realloc (buf, len);
	tmp = buf;
	tmp = tmp + strlen (tmp);
	if (u_param->gvalue == NULL)
	  sprintf (tmp, ";%s", u_param->gname);
	else
	  sprintf (tmp, ";%s=%s", u_param->gname, u_param->gvalue);
	pos++;
      }
  }
  *dest = buf;
  return 0;
}

/* deallocates a accept_encoding_t structure.  */
/* INPUT : accept_encoding_t *accept_encoding | accept_encoding. */
void
accept_encoding_free (accept_encoding_t * accept_encoding)
{
  if (accept_encoding == NULL)
    return;
  sfree (accept_encoding->element);

  generic_param_freelist (accept_encoding->gen_params);
  sfree (accept_encoding->gen_params);

  accept_encoding->element = NULL;
  accept_encoding->gen_params = NULL;
}

int
accept_encoding_clone (accept_encoding_t * ctt, accept_encoding_t ** dest)
{
  int i;
  accept_encoding_t *ct;

  *dest = NULL;
  if (ctt == NULL)
    return -1;
  if (ctt->element == NULL)
    return -1;

  i = accept_encoding_init (&ct);
  if (i != 0)			/* allocation failed */
    return -1;
  ct->element = sgetcopy (ctt->element);
  if (ctt->element != NULL && ct->element == NULL)
    {
      accept_encoding_free (ct);
      sfree (ct);
      return -1;
    }
  {
    int pos = 0;
    generic_param_t *u_param;
    generic_param_t *dest_param;

    while (!list_eol (ctt->gen_params, pos))
      {
	u_param = (generic_param_t *) list_get (ctt->gen_params, pos);
	i = generic_param_clone (u_param, &dest_param);
	if (i != 0)
	  {
	    accept_encoding_free (ct);
	    sfree (ct);
	    return -1;
	  }
	list_add (ct->gen_params, dest_param, -1);
	pos++;
      }
  }
  *dest = ct;
  return 0;
}


char *
accept_encoding_getelement (accept_encoding_t * ae)
{
  return ae->element;
}

void
accept_encoding_setelement (accept_encoding_t * ae, char *element)
{
  ae->element = element;
}