parse_expires.c

Go to the documentation of this file.
00001 /*
00002  * $Id: parse_expires.c 4720 2008-08-23 10:56:15Z henningw $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00023 /*!
00024  * \file
00025  * \brief Expires header field body parser
00026  * \ingroup parser
00027  */
00028 
00029 #include "parse_expires.h"
00030 #include <stdio.h>          /* printf */
00031 #include "../mem/mem.h"     /* pkg_malloc, pkg_free */
00032 #include "../dprint.h"
00033 #include "../trim.h"        /* trim_leading */
00034 #include <string.h>         /* memset */
00035 #include "../ut.h"
00036 
00037 
00038 static inline int expires_parser(char* _s, int _l, exp_body_t* _e)
00039 {
00040    int i;
00041    str tmp;
00042    
00043    tmp.s = _s;
00044    tmp.len = _l;
00045 
00046    trim_leading(&tmp);
00047 
00048    if (tmp.len == 0) {
00049       LM_ERR("empty body\n");
00050       _e->valid = 0;
00051       return -1;
00052    }
00053 
00054    _e->text.s = tmp.s;
00055 
00056    for(i = 0; i < tmp.len; i++) {
00057       if ((tmp.s[i] >= '0') && (tmp.s[i] <= '9')) {
00058          _e->val *= 10;
00059          _e->val += tmp.s[i] - '0';
00060       } else {
00061          switch(tmp.s[i]) {
00062          case ' ':
00063          case '\t':
00064          case '\r':
00065          case '\n':
00066             _e->text.len = i;
00067             _e->valid = 1;
00068             return 0;
00069 
00070          default:
00071                  /* Exit normally here, we want to be backwards compatible with
00072                   * RFC2543 entities that can put absolute time here
00073                   */
00074                  /*
00075             LM_ERR("invalid character\n");
00076             return -2;
00077                  */
00078             _e->valid = 0;
00079             return 0;
00080          }
00081       }
00082    }
00083 
00084    _e->text.len = _l;
00085    _e->valid = 1;
00086    return 0;
00087 }
00088 
00089 
00090 /*!
00091  * Parse expires header field body
00092  */
00093 int parse_expires(struct hdr_field* _h)
00094 {
00095    exp_body_t* e;
00096 
00097    if (_h->parsed) {
00098       return 0;  /* Already parsed */
00099    }
00100 
00101    e = (exp_body_t*)pkg_malloc(sizeof(exp_body_t));
00102    if (e == 0) {
00103       LM_ERR("no pkg memory left\n");
00104       return -1;
00105    }
00106    
00107    memset(e, 0, sizeof(exp_body_t));
00108 
00109    if (expires_parser(_h->body.s, _h->body.len, e) < 0) {
00110       LM_ERR("failed to parse\n");
00111       pkg_free(e);
00112       return -2;
00113    }
00114    
00115    _h->parsed = (void*)e;
00116    return 0;
00117 }
00118 
00119 
00120 /*!
00121  * Free all memory associated with exp_body_t
00122  */
00123 void free_expires(exp_body_t** _e)
00124 {
00125    pkg_free(*_e);
00126    *_e = 0;
00127 }
00128 
00129 
00130 /*!
00131  * Print exp_body_t content, for debugging only
00132  */
00133 void print_expires(exp_body_t* _e)
00134 {
00135    printf("===Expires===\n");
00136    printf("text: \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
00137    printf("val : %d\n", _e->val);
00138    printf("===/Expires===\n");
00139 }

Generated on Thu May 24 00:00:28 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6