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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
|
/*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of ser, a free SIP server.
*
* ser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* ser 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* 2003-08-04 parse_content_type_hdr separates type from subtype inside
* the mime type (bogdan)
* 2003-08-04 CPL subtype added (bogdan)
* 2003-08-05 parse_accept_hdr function added (bogdan)
* 2008-05-23 reset the type/subtype to unknown, if the end of
* tree has reached, but the type/subtype has still
* some remaining characters (Miklos)
*/
/*! \file
* \brief Parser :: Content part
*
* \ingroup parser
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "../mem/mem.h"
#include "../dprint.h"
#include "../str.h"
#include "../ut.h"
#include "parse_content.h"
#define is_mime_char(_c_) \
(isalpha((int)_c_) || (_c_)=='-' || (_c_)=='+' || (_c_)=='.')
#define is_char_equal(_c_,_cs_) \
( (isalpha((int)_c_)?(((_c_)|0x20)==(_cs_)):((_c_)==(_cs_)))==1 )
/*! \brief
* Node of the type's tree; this tree contains all the known types;
*/
typedef struct type_node_s {
char c; /*!< char contained by this node */
unsigned char final; /*!< says what mime type/subtype was detected
*!< if string ends at this node */
unsigned char nr_sons; /*!< the number of sub-nodes */
int next; /*!< the next sibling node */
}type_node_t;
static type_node_t type_tree[] = {
{'t',TYPE_UNKNOWN,1,4}, /* 0 */
{'e',TYPE_UNKNOWN,1,-1},
{'x',TYPE_UNKNOWN,1,-1},
{'t',TYPE_TEXT,0,-1},
{'m',TYPE_UNKNOWN,2,19}, /* 4 */
{'e',TYPE_UNKNOWN,1,11}, /* 5 */
{'s',TYPE_UNKNOWN,1,-1},
{'s',TYPE_UNKNOWN,1,-1},
{'a',TYPE_UNKNOWN,1,-1},
{'g',TYPE_UNKNOWN,1,-1},
{'e',TYPE_MESSAGE,0,-1},
{'u',TYPE_UNKNOWN,1,-1}, /* 11 */
{'l',TYPE_UNKNOWN,1,-1},
{'t',TYPE_UNKNOWN,1,-1},
{'i',TYPE_UNKNOWN,1,-1},
{'p',TYPE_UNKNOWN,1,-1},
{'a',TYPE_UNKNOWN,1,-1},
{'r',TYPE_UNKNOWN,1,-1},
{'t',TYPE_MULTIPART,0,-1},
{'a',TYPE_UNKNOWN,1,-1}, /* 19 */
{'p',TYPE_UNKNOWN,1,-1},
{'p',TYPE_UNKNOWN,1,-1},
{'l',TYPE_UNKNOWN,1,-1},
{'i',TYPE_UNKNOWN,1,-1},
{'c',TYPE_UNKNOWN,1,-1},
{'a',TYPE_UNKNOWN,1,-1},
{'t',TYPE_UNKNOWN,1,-1},
{'i',TYPE_UNKNOWN,1,-1},
{'o',TYPE_UNKNOWN,1,-1},
{'n',TYPE_APPLICATION,0,-1},
};
static type_node_t subtype_tree[] = {
{'p',SUBTYPE_UNKNOWN,2,13},
{'l',SUBTYPE_UNKNOWN,1,5},
{'a',SUBTYPE_UNKNOWN,1,-1},
{'i',SUBTYPE_UNKNOWN,1,-1},
{'n',SUBTYPE_PLAIN,0,-1},
{'i',SUBTYPE_UNKNOWN,1,-1}, /* 5 */
{'d',SUBTYPE_UNKNOWN,1,-1},
{'f',SUBTYPE_UNKNOWN,1,-1},
{'+',TYPE_UNKNOWN,1,-1},
{'x',TYPE_UNKNOWN,1,-1},
{'m',TYPE_UNKNOWN,1,-1},
{'l',SUBTYPE_PIDFXML,0,-1},
{'l',SUBTYPE_PIDFXML,0,-1},
{'s',SUBTYPE_UNKNOWN,1,16}, /* 13 */
{'d',SUBTYPE_UNKNOWN,1,-1},
{'p',SUBTYPE_SDP,0,-1},
{'c',SUBTYPE_UNKNOWN,1,34}, /* 16 */
{'p',SUBTYPE_UNKNOWN,2,-1},
{'i',SUBTYPE_UNKNOWN,1,29},
{'m',SUBTYPE_CPIM,1,-1},
{'-',SUBTYPE_UNKNOWN,1,-1},
{'p',SUBTYPE_UNKNOWN,1,-1},
{'i',SUBTYPE_UNKNOWN,1,-1},
{'d',SUBTYPE_UNKNOWN,1,-1},
{'f',SUBTYPE_UNKNOWN,1,-1},
{'+',SUBTYPE_UNKNOWN,1,-1},
{'x',SUBTYPE_UNKNOWN,1,-1},
{'m',SUBTYPE_UNKNOWN,1,-1},
{'l',SUBTYPE_CPIM_PIDFXML,0,-1},
{'l',SUBTYPE_UNKNOWN,1,-1}, /* 29 */
{'+',TYPE_UNKNOWN,1,-1},
{'x',TYPE_UNKNOWN,1,-1},
{'m',TYPE_UNKNOWN,1,-1},
{'l',SUBTYPE_CPLXML,0,-1},
{'r',SUBTYPE_UNKNOWN,2,48}, /* 34 */
{'l',SUBTYPE_UNKNOWN,1,42},/* 35 */
{'m',SUBTYPE_UNKNOWN,1,-1},
{'i',SUBTYPE_UNKNOWN,1,-1},
{'+',TYPE_UNKNOWN,1,-1},
{'x',TYPE_UNKNOWN,1,-1},
{'m',TYPE_UNKNOWN,1,-1},
{'l',SUBTYPE_RLMIXML,0,-1},
{'e',SUBTYPE_UNKNOWN,1,-1}, /* 42 */
{'l',SUBTYPE_UNKNOWN,1,-1},
{'a',SUBTYPE_UNKNOWN,1,-1},
{'t',SUBTYPE_UNKNOWN,1,-1},
{'e',SUBTYPE_UNKNOWN,1,-1},
{'d',SUBTYPE_RELATED,0,-1},
{'l',SUBTYPE_UNKNOWN,1,57}, /* 48 */
{'p',SUBTYPE_UNKNOWN,1,-1},
{'i',SUBTYPE_UNKNOWN,1,-1},
{'d',SUBTYPE_UNKNOWN,1,-1},
{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SUBTYPE_UNKNOWN,1,-1},
{'d',SUBTYPE_UNKNOWN,1,-1},
{'f',SUBTYPE_XML_MSRTC_PIDF,0,-1},
{'e',SUBTYPE_UNKNOWN,1,107}, /* 94 */
{'x',SUBTYPE_UNKNOWN,1,-1},
{'t',SUBTYPE_UNKNOWN,1,-1},
{'e',SUBTYPE_UNKNOWN,1,-1},
{'r',SUBTYPE_UNKNOWN,1,-1},
{'n',SUBTYPE_UNKNOWN,1,-1},
{'a',SUBTYPE_UNKNOWN,1,-1},
{'l',SUBTYPE_UNKNOWN,1,-1},
{'-',SUBTYPE_UNKNOWN,1,-1},
{'b',SUBTYPE_UNKNOWN,1,-1},
{'o',SUBTYPE_UNKNOWN,1,-1},
{'d',SUBTYPE_UNKNOWN,1,-1},
{'y',SUBTYPE_EXTERNAL_BODY,0,-1},
{'m',SUBTYPE_UNKNOWN,1,112}, /* 107 */
{'i',SUBTYPE_UNKNOWN,1,-1},
{'x',SUBTYPE_UNKNOWN,1,-1},
{'e',SUBTYPE_UNKNOWN,1,-1},
{'d',SUBTYPE_MIXED,0,-1},
{'i',SUBTYPE_UNKNOWN,1,-1}, /* 112 */
{'s',SUBTYPE_UNKNOWN,1,-1},
{'u',SUBTYPE_UNKNOWN,1,-1},
{'p',SUBTYPE_ISUP,0,-1},
};
char* parse_content_length(char* const buffer, const char* const end, int* const length)
{
int number;
char *p;
int size;
p = buffer;
/* search the begining of the number */
while ( p<end && (*p==' ' || *p=='\t' ||
(*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
p++;
if (p==end)
goto error;
/* parse the number */
size = 0;
number = 0;
while (p<end && *p>='0' && *p<='9') {
number = number*10 + (*p)-'0';
size ++;
p++;
}
if (p==end || size==0)
goto error;
/* now we should have only spaces at the end */
while ( p<end && (*p==' ' || *p=='\t' ||
(*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
p++;
if (p==end)
goto error;
/* the header ends proper? */
if ( (*(p++)!='\n') && (*(p-1)!='\r' || *(p++)!='\n' ) )
goto error;
*length = number;
return p;
error:
LOG(L_ERR,"ERROR:parse_content_length: parse error near char [%d][%c]\n",
*p,*p);
return 0;
}
char* decode_mime_type(char* const start, const char* const end, unsigned int* const mime_type)
{
int node;
char *mark;
char *p;
unsigned int type_candidate;
p = start;
/* search the begining of the type */
while ( p<end && (*p==' ' || *p=='\t' ||
(*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
p++;
if (p==end)
goto error;
/* parse the type */
if (*p=='*') {
*mime_type = TYPE_ALL<<16;
p++;
} else {
node = 0;
mark = p;
type_candidate = TYPE_UNKNOWN;
while (p<end && is_mime_char(*p) ) {
while ( node!=-1 && !is_char_equal(*p,type_tree[node].c) ){
node = type_tree[node].next;
}
if (node!=-1) {
type_candidate = type_tree[node].final;
if (type_tree[node].nr_sons)
node++;
else
node = -1;
} else {
/* end of the type tree has reached,
but the type has still some remaining
characters (Miklos) */
type_candidate = TYPE_UNKNOWN;
}
p++;
}
if (p==end || mark==p)
goto error;
*mime_type = type_candidate<<16;
}
/* search the '/' separator */
while ( p<end && (*p==' ' || *p=='\t' ||
(*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
p++;
if ( p==end || *(p++)!='/')
goto error;
/* search the begining of the sub-type */
while ( p<end && (*p==' ' || *p=='\t' ||
(*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
p++;
if (p==end)
goto error;
/* parse the sub-type */
if (*p=='*') {
*mime_type |= SUBTYPE_ALL;
p++;
} else {
node = 0;
mark = p;
type_candidate = SUBTYPE_UNKNOWN;
while (p<end && is_mime_char(*p) ) {
while(node!=-1 && !is_char_equal(*p,subtype_tree[node].c) )
node = subtype_tree[node].next;
if (node!=-1) {
type_candidate = subtype_tree[node].final;
if (subtype_tree[node].nr_sons)
node++;
else
node = -1;
} else {
/* end of the subtype tree has reached,
but the subtype has still some remaining
characters (Miklos) */
type_candidate = SUBTYPE_UNKNOWN;
}
p++;
}
if (p==mark)
goto error;
*mime_type |= type_candidate;;
}
/* now its possible to have some spaces */
while ( p<end && (*p==' ' || *p=='\t' ||
(*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
p++;
/* if there are params, ignore them!! -> eat everything to
* the end or to the first ',' */
if ( p<end && *p==';' )
for(p++; p<end && *p!=','; p++);
/* is this the correct end? */
if (p!=end && *p!=',' )
goto error;
/* check the format of the decoded mime */
if ((*mime_type)>>16==TYPE_ALL && ((*mime_type)&0x00ff)!=SUBTYPE_ALL) {
LOG(L_ERR,"ERROR:decode_mime_type: invalid mime format found "
" <*/submime> in [%.*s]!!\n", (int)(end-start),start);
return 0;
}
return p;
error:
LOG(L_ERR,"ERROR:decode_mime_type: parse error near in [%.*s] char"
"[%d][%c] offset=%d\n", (int)(end-start),start,*p,*p,(int)(p-start));
return 0;
}
/*! \brief
* \return
* - > 0 mime found
* - = 0 hdr not found
* - =-1 error */
int parse_content_type_hdr(struct sip_msg* const msg)
{
char *end;
const char *ret;
unsigned int mime;
/* is the header already found? */
if ( msg->content_type==0 ) {
/* if not, found it */
if ( parse_headers(msg, HDR_CONTENTTYPE_F, 0)==-1)
goto error;
if ( msg->content_type==0 ) {
DBG("DEBUG:parse_content_type_hdr: missing Content-Type"
"header\n");
return 0;
}
}
/* maybe the header is already parsed! */
if ( msg->content_type->parsed!=0)
return get_content_type(msg);
/* it seams we have to parse it! :-( */
end = msg->content_type->body.s + msg->content_type->body.len;
ret = decode_mime_type(msg->content_type->body.s, end , &mime);
if (ret==0)
goto error;
if (ret!=end) {
LOG(L_ERR,"ERROR:parse_content_type_hdr: CONTENT_TYPE hdr contains "
"more then one mime type :-(!\n");
goto error;
}
if ((mime&0x00ff)==SUBTYPE_ALL || (mime>>16)==TYPE_ALL) {
LOG(L_ERR,"ERROR:parse_content_type_hdr: invalid mime with wildcard "
"'*' in Content-Type hdr!\n");
goto error;
}
msg->content_type->parsed = (void*)(unsigned long)mime;
return mime;
error:
return -1;
}
int parse_accept_body(struct hdr_field* const hdr)
{
static unsigned int mimes[MAX_MIMES_NR];
int nr_mimes;
unsigned int mime;
char *end;
char *ret;
if (!hdr) return -1;
/* maybe the header is already parsed! */
if (hdr->parsed!=0) return 1;
/* it seams we have to parse it! :-( */
ret = hdr->body.s;
end = ret + hdr->body.len;
nr_mimes = 0;
while (1){
ret = decode_mime_type(ret, end , &mime);
if (ret==0)
goto error;
/* a new mime was found -> put it into array */
if (nr_mimes==MAX_MIMES_NR) {
LOG(L_ERR,"ERROR:parse_accept_hdr: Accept hdr contains more than"
" %d mime type -> buffer overflow!!\n",MAX_MIMES_NR);
goto error;
}
mimes[nr_mimes++] = mime;
/* is another mime following? */
if (ret==end )
break;
/* parse the mime separator ',' */
if (*ret!=',' || ret+1==end) {
LOG(L_ERR,"ERROR:parse_accept_hdr: parse error between mimes at "
"char <%x> (offset=%d) in <%.*s>!\n",
*ret, (int)(ret-hdr->body.s),
hdr->body.len, hdr->body.s);
goto error;
}
/* skip the ',' */
ret++;
}
/* copy and link the mime buffer into the message */
hdr->parsed = (void*)pkg_malloc((nr_mimes+1)*sizeof(int));
if (hdr->parsed==0) {
LOG(L_ERR,"ERROR:parse_accept: no more pkg memory\n");
goto error;
}
memcpy(hdr->parsed,mimes,nr_mimes*sizeof(int));
/* make the buffer null terminated */
((int*)hdr->parsed)[nr_mimes] = 0;
return 1;
error:
return -1;
}
/*! \brief
* returns: > 0 ok
* = 0 hdr not found
* = -1 error */
int parse_accept_hdr(struct sip_msg* const msg)
{
static unsigned int mimes[MAX_MIMES_NR];
int nr_mimes;
unsigned int mime;
char *end;
char *ret;
/* is the header already found? */
if ( msg->accept==0 ) {
/* if not, found it */
if ( parse_headers(msg, HDR_ACCEPT_F, 0)==-1)
goto error;
if ( msg->accept==0 ) {
DBG("DEBUG:parse_accept_hdr: missing Accept header\n");
return 0;
}
}
/* maybe the header is already parsed! */
if ( msg->accept->parsed!=0)
return 1;
/* it seams we have to parse it! :-( */
ret = msg->accept->body.s;
end = ret + msg->accept->body.len;
nr_mimes = 0;
while (1){
ret = decode_mime_type(ret, end , &mime);
if (ret==0)
goto error;
/* a new mime was found -> put it into array */
if (nr_mimes==MAX_MIMES_NR) {
LOG(L_ERR,"ERROR:parse_accept_hdr: Accept hdr contains more than"
" %d mime type -> buffer overflow!!\n",MAX_MIMES_NR);
goto error;
}
mimes[nr_mimes++] = mime;
/* is another mime following? */
if (ret==end )
break;
/* parse the mime separator ',' */
if (*ret!=',' || ret+1==end) {
LOG(L_ERR,"ERROR:parse_accept_hdr: parse error between mimes at "
"char <%x> (offset=%d) in <%.*s>!\n",
*ret, (int)(ret-msg->accept->body.s),
msg->accept->body.len, msg->accept->body.s);
goto error;
}
/* skip the ',' */
ret++;
}
/* copy and link the mime buffer into the message */
msg->accept->parsed = (void*)pkg_malloc((nr_mimes+1)*sizeof(int));
if (msg->accept->parsed==0) {
LOG(L_ERR,"ERROR:parse_accept_hdr: no more pkg memory\n");
goto error;
}
memcpy(msg->accept->parsed,mimes,nr_mimes*sizeof(int));
/* make the buffer null terminated */
((int*)msg->accept->parsed)[nr_mimes] = 0;
return 1;
error:
return -1;
}
|