parser/parse_retry_after.c
1c388906
 /* 
  * Copyright (C) 2007 iptelorg GmbH
  *
07ca8c05
  * This file is part of Kamailio, a free SIP server.
1c388906
  *
07ca8c05
  * Kamailio is free software; you can redistribute it and/or modify
1c388906
  * 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
  *
07ca8c05
  * Kamailio is distributed in the hope that it will be useful,
1c388906
  * 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 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
1c388906
  * 
  */
 
1eb01c66
 /*! \file
  * \brief Parser :: Retry-After: header parser
  *
  * \ingroup parser
  */
  
1c388906
 
 #include "../comp_defs.h"
 #include "parse_retry_after.h"
 #include "parser_f.h"  /* eat_space_end and so on */
 #include "parse_def.h"
 #include "../dprint.h"
 #include "../mem/mem.h"
 
1eb01c66
 /*! \brief Parse the Retry-after header field */
f7b97e8a
 char* parse_retry_after(char* const buf, const char* const end, unsigned* const after, int* const err)
1c388906
 {
 	char *t;
 	int i;
 	unsigned val;
 	
 	val=0;
 	t=buf;
 	
 	t=eat_lws_end(t, end);
 	if (t>=end) goto error;
 	for (i=0; t<end; i++,t++){
 		if ((*t >= '0') && (*t <= '9')){
 			val=val*10+(*t-'0');
 		}else{
 			switch(*t){
 				/* for now we don't care about retry-after params or comment*/
 				case ' ':
 				case '\t':
 				case ';':
 				case '\r':
 				case '\n':
accfbe0a
 				case '(':
1c388906
 					goto found;
 				default:
 					/* invalid char */
 					goto error;
 			}
 		}
 	}
 	goto error_nocrlf; /* end reached without encountering cr or lf */
 found:
 	if (i>10 || i==0) /* too many  or too few digits */
 		goto error;
 	*after=val;
 	/* find the end of header */
 	for (; t<end; t++){
 		if (*t=='\n'){
 			if (((t+1)<end) && (*(t+1)=='\r'))
 				t++;
 			if (((t+1)<end) && (*(t+1)==' ' || *(t+1)=='\t')){
 				t++;
 				continue; /* line folding ... */
 			}
 			*err=0;
 			return t+1;
 		}
 	}
 error_nocrlf:
 	LOG(L_ERR, "ERROR: parse_retry_after: strange EoHF\n");
 	goto error;
 error:
 	LOG(L_ERR, "ERROR: parse_retry_after: bad Retry-After header \n");
 	*err=1;
 	return t;
 }