parse_methods.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (c) 2004 Juha Heinanen
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * ser is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*! \file
  23. * \brief Parser :: Parse Methods
  24. *
  25. * \ingroup parser
  26. */
  27. #include <strings.h>
  28. #include "../dprint.h"
  29. #include "../trim.h"
  30. #include "parse_methods.h"
  31. /*! \brief
  32. * Check if argument is valid RFC3261 token character.
  33. */
  34. static int token_char(char _c)
  35. {
  36. return (_c >= 65 && _c <= 90) || /* upper alpha */
  37. (_c >= 97 && _c <= 122) || /* lower aplha */
  38. (_c >= 48 && _c <= 57) || /* digits */
  39. (_c == '-') || (_c == '.') || (_c == '!') || (_c == '%') ||
  40. (_c == '*') || (_c == '_') || (_c == '+') || (_c == '`') ||
  41. (_c == '\'') || (_c == '~') || (_c == '+') || (_c == '`');
  42. }
  43. /*! \brief Parse a string containing a method.
  44. *
  45. * Parse a method pointed by s & assign its enum bit to method. The string
  46. * _must_ contain _only_ the method (without trailing or heading whitespace).
  47. * \return 0 on success, -1 on error
  48. */
  49. int parse_method_name(str* s, enum request_method* method)
  50. {
  51. if (unlikely(!s || !method)) {
  52. LOG(L_ERR, "Invalid parameter value\n");
  53. return -1;
  54. }
  55. if (unlikely(!s->len || (s->s==0))) {
  56. DBG("No input\n");
  57. *method = METHOD_OTHER;
  58. return 0;
  59. }
  60. switch ((s->s)[0]) {
  61. /* ordered after probability of aparition on a normal proxy */
  62. case 'R':
  63. case 'r':
  64. if (likely((s->len == 8) &&
  65. !strncasecmp(s->s + 1, "egister", 7))) {
  66. *method = METHOD_REGISTER;
  67. return 0;
  68. }
  69. if (likely((s->len==5) && !strncasecmp(s->s + 1, "efer", 4))) {
  70. *method = METHOD_REFER;
  71. return 0;
  72. }
  73. break;
  74. case 'A':
  75. case 'a':
  76. if (likely((s->len==3) && !strncasecmp(s->s + 1, "ck", 2))) {
  77. *method = METHOD_ACK;
  78. return 0;
  79. }
  80. break;
  81. case 'I':
  82. case 'i':
  83. if (likely((s->len==6) && !strncasecmp(s->s + 1, "nvite", 5))){
  84. *method = METHOD_INVITE;
  85. return 0;
  86. }
  87. if (likely((s->len==4) && !strncasecmp(s->s + 1, "nfo", 3))) {
  88. *method = METHOD_INFO;
  89. return 0;
  90. }
  91. break;
  92. case 'P':
  93. case 'p':
  94. if (likely((s->len==5) && !strncasecmp(s->s + 1, "rack", 4))) {
  95. *method = METHOD_PRACK;
  96. return 0;
  97. }
  98. if (likely((s->len==7) && !strncasecmp(s->s + 1, "ublish", 6))) {
  99. *method = METHOD_PUBLISH;
  100. return 0;
  101. }
  102. break;
  103. case 'C':
  104. case 'c':
  105. if (likely((s->len==6) && !strncasecmp(s->s + 1, "ancel", 5))) {
  106. *method = METHOD_CANCEL;
  107. return 0;
  108. }
  109. break;
  110. case 'B':
  111. case 'b':
  112. if (likely((s->len==3) && !strncasecmp(s->s + 1, "ye", 2))) {
  113. *method = METHOD_BYE;
  114. return 0;
  115. }
  116. break;
  117. case 'M':
  118. case 'm':
  119. if (likely((s->len==7) && !strncasecmp(s->s + 1, "essage", 6))) {
  120. *method = METHOD_MESSAGE;
  121. return 0;
  122. }
  123. break;
  124. case 'O':
  125. case 'o':
  126. if (likely((s->len==7) && !strncasecmp(s->s + 1, "ptions", 6))) {
  127. *method = METHOD_OPTIONS;
  128. return 0;
  129. }
  130. break;
  131. case 'S':
  132. case 's':
  133. if (likely((s->len==9) && !strncasecmp(s->s + 1, "ubscribe", 8))) {
  134. *method = METHOD_SUBSCRIBE;
  135. return 0;
  136. }
  137. break;
  138. case 'N':
  139. case 'n':
  140. if (likely((s->len==6) && !strncasecmp(s->s + 1, "otify", 5))){
  141. *method = METHOD_NOTIFY;
  142. return 0;
  143. }
  144. break;
  145. case 'U':
  146. case 'u':
  147. if (likely((s->len==6) && !strncasecmp(s->s + 1, "pdate", 5))){
  148. *method = METHOD_UPDATE;
  149. return 0;
  150. }
  151. break;
  152. default:
  153. break;
  154. }
  155. /* unknown method */
  156. *method = METHOD_OTHER;
  157. return 0;
  158. }
  159. /*! \brief
  160. * Parse a method pointed by _next, assign its enum bit to _method, and update
  161. * _next past the method. Returns 1 if parse succeeded and 0 otherwise.
  162. */
  163. static int parse_method_advance(str* _next, enum request_method* _method)
  164. {
  165. char* end;
  166. if (unlikely(!_next || !_method)) {
  167. LOG(L_ERR, "Invalid parameter value\n");
  168. return 0;
  169. }
  170. if (unlikely(!_next->len || !_next->s)) {
  171. DBG("No input\n");
  172. *_method = METHOD_OTHER;
  173. return 1;
  174. }
  175. end=_next->s+_next->len;
  176. switch ((_next->s)[0]) {
  177. case 'A':
  178. case 'a':
  179. if ((_next->len > 2) && !strncasecmp(_next->s + 1, "ck", 2)) {
  180. *_method = METHOD_ACK;
  181. _next->len -= 3;
  182. _next->s += 3;
  183. goto found;
  184. } else {
  185. goto unknown;
  186. }
  187. case 'B':
  188. case 'b':
  189. if ((_next->len > 2) && !strncasecmp(_next->s + 1, "ye", 2)) {
  190. *_method = METHOD_BYE;
  191. _next->len -= 3;
  192. _next->s += 3;
  193. goto found;
  194. } else {
  195. goto unknown;
  196. }
  197. case 'C':
  198. case 'c':
  199. if ((_next->len > 5) && !strncasecmp(_next->s + 1, "ancel", 5)) {
  200. *_method = METHOD_CANCEL;
  201. _next->len -= 6;
  202. _next->s += 6;
  203. goto found;
  204. } else {
  205. goto unknown;
  206. }
  207. case 'I':
  208. case 'i':
  209. if ((_next->len > 3) &&
  210. ((*(_next->s + 1) == 'N') || (*(_next->s + 1) == 'n'))) {
  211. if (!strncasecmp(_next->s + 2, "fo", 2)) {
  212. *_method = METHOD_INFO;
  213. _next->len -= 4;
  214. _next->s += 4;
  215. goto found;
  216. }
  217. if ((_next->len > 5) && !strncasecmp(_next->s + 2, "vite", 4)) {
  218. *_method = METHOD_INVITE;
  219. _next->len -= 6;
  220. _next->s += 6;
  221. goto found;
  222. }
  223. }
  224. goto unknown;
  225. case 'M':
  226. case 'm':
  227. if ((_next->len > 6) && !strncasecmp(_next->s + 1, "essage", 6)) {
  228. *_method = METHOD_MESSAGE;
  229. _next->len -= 7;
  230. _next->s += 7;
  231. goto found;
  232. } else {
  233. goto unknown;
  234. }
  235. case 'N':
  236. case 'n':
  237. if ((_next->len > 5) && !strncasecmp(_next->s + 1, "otify", 5)) {
  238. *_method = METHOD_NOTIFY;
  239. _next->len -= 6;
  240. _next->s += 6;
  241. goto found;
  242. } else {
  243. goto unknown;
  244. }
  245. case 'O':
  246. case 'o':
  247. if ((_next->len > 6) && !strncasecmp(_next->s + 1, "ptions", 6)) {
  248. *_method = METHOD_OPTIONS;
  249. _next->len -= 7;
  250. _next->s += 7;
  251. goto found;
  252. } else {
  253. goto unknown;
  254. }
  255. case 'P':
  256. case 'p':
  257. if ((_next->len > 4) && !strncasecmp(_next->s + 1, "rack", 4)) {
  258. *_method = METHOD_PRACK;
  259. _next->len -= 5;
  260. _next->s += 5;
  261. goto found;
  262. }
  263. if ((_next->len > 6) && !strncasecmp(_next->s + 1, "ublish", 6)) {
  264. *_method = METHOD_PUBLISH;
  265. _next->len -= 7;
  266. _next->s += 7;
  267. goto found;
  268. }
  269. goto unknown;
  270. case 'R':
  271. case 'r':
  272. if ((_next->len > 4) &&
  273. ((*(_next->s + 1) == 'E') || (*(_next->s + 1) == 'e'))) {
  274. if (!strncasecmp(_next->s + 2, "fer", 3)) {
  275. *_method = METHOD_REFER;
  276. _next->len -= 5;
  277. _next->s += 5;
  278. goto found;
  279. }
  280. if ((_next->len > 7) && !strncasecmp(_next->s + 2, "gister", 6)) {
  281. *_method = METHOD_REGISTER;
  282. _next->len -= 8;
  283. _next->s += 8;
  284. goto found;
  285. }
  286. }
  287. goto unknown;
  288. case 'S':
  289. case 's':
  290. if ((_next->len > 8) && !strncasecmp(_next->s + 1, "ubscribe", 8)) {
  291. *_method = METHOD_SUBSCRIBE;
  292. _next->len -= 9;
  293. _next->s += 9;
  294. goto found;
  295. } else {
  296. goto unknown;
  297. }
  298. case 'U':
  299. case 'u':
  300. if ((_next->len > 5) && !strncasecmp(_next->s + 1, "pdate", 5)) {
  301. *_method = METHOD_UPDATE;
  302. _next->len -= 6;
  303. _next->s += 6;
  304. goto found;
  305. } else {
  306. goto unknown;
  307. }
  308. default:
  309. goto unknown;
  310. }
  311. unknown:
  312. if (token_char(*(_next->s))) {
  313. do {
  314. _next->s++;
  315. _next->len--;
  316. } while (_next->len && token_char(*(_next->s)));
  317. *_method = METHOD_OTHER;
  318. return 1;
  319. } else {
  320. return 0;
  321. }
  322. found:
  323. /* check if the method really ends here (if not return 0) */
  324. return (_next->s>=end) || (!token_char(*(_next->s)));
  325. }
  326. /*! \brief
  327. * Parse comma separated list of methods pointed by _body and assign their
  328. * enum bits to _methods. Returns 0 on success and -1 on failure.
  329. */
  330. int parse_methods(str* _body, unsigned int* _methods)
  331. {
  332. str next;
  333. unsigned int method;
  334. method=0; /* fixes silly gcc 4.x warning */
  335. if (!_body || !_methods) {
  336. LOG(L_ERR, "parse_methods: Invalid parameter value\n");
  337. return -1;
  338. }
  339. next.len = _body->len;
  340. next.s = _body->s;
  341. trim_leading(&next);
  342. *_methods = 0;
  343. if (next.len == 0) {
  344. return 0;
  345. }
  346. while (1) {
  347. if (parse_method_advance(&next, &method)) {
  348. *_methods |= method;
  349. } else {
  350. LOG(L_ERR, "ERROR: parse_methods: Invalid method\n");
  351. return -1;
  352. }
  353. trim_leading(&next);
  354. if (next.len) {
  355. if (next.s[0] == ',') {
  356. next.len--;
  357. next.s++;
  358. trim_leading(&next);
  359. if (next.len == 0) {
  360. LOG(L_ERR, "ERROR: parse_methods: Method expected\n");
  361. return 0;
  362. }
  363. } else {
  364. LOG(L_ERR, "ERROR: parse_methods: Comma expected\n");
  365. return -1;
  366. }
  367. } else {
  368. break;
  369. }
  370. }
  371. return 0;
  372. }