parse_retry_after.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2007 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * ser is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * History:
  21. * --------
  22. * 2007-07-27 created by andrei
  23. */
  24. /*! \file
  25. * \brief Parser :: Retry-After: header parser
  26. *
  27. * \ingroup parser
  28. */
  29. #include "../comp_defs.h"
  30. #include "parse_retry_after.h"
  31. #include "parser_f.h" /* eat_space_end and so on */
  32. #include "parse_def.h"
  33. #include "../dprint.h"
  34. #include "../mem/mem.h"
  35. /*! \brief Parse the Retry-after header field */
  36. char* parse_retry_after(char* const buf, const char* const end, unsigned* const after, int* const err)
  37. {
  38. char *t;
  39. int i;
  40. unsigned val;
  41. val=0;
  42. t=buf;
  43. t=eat_lws_end(t, end);
  44. if (t>=end) goto error;
  45. for (i=0; t<end; i++,t++){
  46. if ((*t >= '0') && (*t <= '9')){
  47. val=val*10+(*t-'0');
  48. }else{
  49. switch(*t){
  50. /* for now we don't care about retry-after params or comment*/
  51. case ' ':
  52. case '\t':
  53. case ';':
  54. case '\r':
  55. case '\n':
  56. case '(':
  57. goto found;
  58. default:
  59. /* invalid char */
  60. goto error;
  61. }
  62. }
  63. }
  64. goto error_nocrlf; /* end reached without encountering cr or lf */
  65. found:
  66. if (i>10 || i==0) /* too many or too few digits */
  67. goto error;
  68. *after=val;
  69. /* find the end of header */
  70. for (; t<end; t++){
  71. if (*t=='\n'){
  72. if (((t+1)<end) && (*(t+1)=='\r'))
  73. t++;
  74. if (((t+1)<end) && (*(t+1)==' ' || *(t+1)=='\t')){
  75. t++;
  76. continue; /* line folding ... */
  77. }
  78. *err=0;
  79. return t+1;
  80. }
  81. }
  82. error_nocrlf:
  83. LOG(L_ERR, "ERROR: parse_retry_after: strange EoHF\n");
  84. goto error;
  85. error:
  86. LOG(L_ERR, "ERROR: parse_retry_after: bad Retry-After header \n");
  87. *err=1;
  88. return t;
  89. }