2
0

parse_content.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright (C) 2001-2003 Fhg Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser 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. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <sys/types.h>
  31. #include <unistd.h>
  32. #include "../dprint.h"
  33. #include "../str.h"
  34. #include "../ut.h"
  35. #include "parse_content.h"
  36. typedef struct type_node_s {
  37. char c;
  38. unsigned char final;
  39. unsigned char nr_sons;
  40. int next;
  41. }type_node_t;
  42. char* parse_content_length( char* buffer, char* end, int* length)
  43. {
  44. int number;
  45. char *p;
  46. int size;
  47. p = buffer;
  48. /* search the begining of the number */
  49. while ( p<end && (*p==' ' || *p=='\t' ||
  50. (*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
  51. p++;
  52. if (p==end)
  53. goto error;
  54. /* parse the number */
  55. size = 0;
  56. number = 0;
  57. while (p<end && *p>='0' && *p<='9') {
  58. number = number*10 + (*p)-'0';
  59. size ++;
  60. p++;
  61. }
  62. if (p==end || size==0)
  63. goto error;
  64. /* now we should have only spaces at the end */
  65. while ( p<end && (*p==' ' || *p=='\t' ||
  66. (*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
  67. p++;
  68. if (p==end)
  69. goto error;
  70. /* the header ends proper? */
  71. if ( (*(p++)!='\n') && (*(p-1)!='\r' || *(p++)!='\n' ) )
  72. goto error;
  73. *length = number;
  74. return p;
  75. error:
  76. LOG(L_ERR,"ERROR:parse_content_length: parse error near char [%d][%c]\n",
  77. *p,*p);
  78. return 0;
  79. }
  80. char* parse_content_type( char* buffer, char* end, int* type)
  81. {
  82. static type_node_t type_tree[] = {
  83. {'t',-1,1,4}, {'e',-1,1,-1}, {'x',-1,1,-1}, {'t',0,0,-1},
  84. {'m',-1,1,11}, {'e',-1,1,-1}, {'s',-1,1,-1}, {'s',-1,1,-1},
  85. {'a',-1,1,-1},{'g',-1,1,-1}, {'e',5,0,-1},
  86. {'a',-1,1,-1}, {'p',-1,1,-1}, {'p',-1,1,-1}, {'l',-1,1,-1},
  87. {'i',-1,1,-1},{'c',-1,1,-1},{'a',-1,1,-1},{'t',-1,1,-1},
  88. {'i',-1,1,-1},{'o',-1,1,-1},{'n',9,0,-1}
  89. };
  90. static type_node_t subtype_tree[] = {
  91. {'p',0,1,5}, {'l',0,1,-1}, {'a',0,1,-1}, {'i',0,1,-1},
  92. {'n',CONTENT_TYPE_TEXT_PLAIN,0,-1},
  93. {'c',0,1,9}, {'p',0,1,-1}, {'i',0,1,-1},
  94. {'m',CONTENT_TYPE_MESSAGE_CPIM,0,-1},
  95. {'s',0,1,-1}, {'d',0,1,-1},
  96. {'p',CONTENT_TYPE_APPLICATION_SDP,0,-1},
  97. };
  98. int node;
  99. int mime;
  100. char *p;
  101. p = buffer;
  102. mime = CONTENT_TYPE_UNKNOWN;
  103. /* search the begining of the type */
  104. while ( p<end && (*p==' ' || *p=='\t' ||
  105. (*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
  106. p++;
  107. if (p==end)
  108. goto error;
  109. /* parse the type */
  110. node = 0;
  111. while (p<end && ((*p>='a' && *p<='z') || (*p>='A' && *p<='Z')) ) {
  112. while ( node!=-1 && type_tree[node].c!=*p && type_tree[node].c+32!=*p){
  113. node = type_tree[node].next;
  114. }
  115. if (node!=-1 && type_tree[node].nr_sons)
  116. node++;
  117. p++;
  118. }
  119. if (p==end || node==0)
  120. goto error;
  121. if (node!=-1)
  122. node = type_tree[node].final;
  123. /* search the '/' separator */
  124. while ( p<end && (*p==' ' || *p=='\t' ||
  125. (*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
  126. p++;
  127. if ( p==end || *(p++)!='/')
  128. goto error;
  129. /* search the begining of the sub-type */
  130. while ( p<end && (*p==' ' || *p=='\t' ||
  131. (*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
  132. p++;
  133. if (p==end)
  134. goto error;
  135. /* parse the sub-type */
  136. while (p<end && ((*p>='a' && *p<='z') || (*p>='A' && *p<='Z')) ) {
  137. while(node!=-1&&subtype_tree[node].c!=*p&&subtype_tree[node].c+32!=*p)
  138. node = subtype_tree[node].next;
  139. if (node!=-1 && subtype_tree[node].nr_sons)
  140. node++;
  141. p++;
  142. }
  143. if (p==end || node==0)
  144. goto error;
  145. if (node!=-1)
  146. mime = subtype_tree[node].final;
  147. /* now its possible to have some spaces */
  148. while ( p<end && (*p==' ' || *p=='\t' ||
  149. (*p=='\n' && (*(p+1)==' '||*(p+1)=='\t')) ))
  150. p++;
  151. if (p==end)
  152. goto error;
  153. /* if there are params, eat everything to the end */
  154. if (*p==';') {
  155. while ( p<end && (*p!='\n' || (*(p+1)==' '||*(p+1)=='\t')) )
  156. p++;
  157. if (p==end)
  158. goto error;
  159. }
  160. /* the header ends proper? */
  161. if ( (*(p++)!='\n') && (*(p-1)!='\r' || *(p++)!='\n' ) )
  162. goto error;
  163. *type = mime;
  164. return p;
  165. error:
  166. LOG(L_ERR,"ERROR:parse_content_type: parse error near char [%d][%c]\n",
  167. *p,*p);
  168. return 0;
  169. }