Commit 5af50a06 authored by jehan's avatar jehan
Browse files

implement sip uri header management

parent 48d9aa44
......@@ -9,9 +9,11 @@
#define BELLE_SIP_URI_H_
#include "belle_sip_list.h"
typedef struct _belle_sip_uri belle_sip_uri;
belle_sip_uri* belle_sip_uri_new();
void belle_sip_uri_delete(belle_sip_uri* uri);
......@@ -26,7 +28,7 @@ typedef struct _belle_sip_uri belle_sip_uri;
* Returns an Iterator over the const char*names of all headers present in this SipURI.
*
*/
/*list<const char*>::iterator belle_sip_uri_get_header_names(SipUri* uri) ;*/
belle_sip_list* belle_sip_uri_get_header_names(belle_sip_uri* uri) ;
/**
* Returns the host part of this SipURI.
*
......
......@@ -6,7 +6,7 @@ options { language = C;}
an_sip_uri returns [belle_sip_uri* ret]
scope { belle_sip_uri* current; }
@init { $an_sip_uri::current = belle_sip_uri_new(); }
: sip_schema userinfo? hostport uri_parameters {$ret = $an_sip_uri::current;};
: sip_schema userinfo? hostport uri_parameters headers? {$ret = $an_sip_uri::current;};
sip_schema : ('sip' | is_sips='sips') ':' {if ($is_sips) belle_sip_uri_set_secure($an_sip_uri::current,1);};
userinfo : user ( ':' password )? '@' ;
user : ( unreserved | escaped | user_unreserved )+ {belle_sip_uri_set_user($an_sip_uri::current,(const char *)$text->chars);};
......@@ -61,6 +61,13 @@ paramchar
param_unreserved
: '[' | ']' | '/' | ':' | '&' | '+' | '$';
headers : '?' header ( '&' header )* ;
header : hname '=' hvalue;
hname : ( hnv_unreserved | unreserved | escaped )+;
hvalue : ( hnv_unreserved | unreserved | escaped )*;
fragment hnv_unreserved : '[' | ']' | '|' | '?' | ':' | '+' | '$' ;
fragment escaped : '%' HEXDIG HEXDIG;
fragment ttl : three_digit;
fragment three_digit: DIGIT DIGIT? DIGIT? ;
......
......@@ -11,6 +11,7 @@
#include <stdarg.h>
#include "belle_sip_uriParser.h"
#include "belle_sip_uriLexer.h"
#include "belle_sip_utils.h"
#define GET_SET_STRING(object_type,attribute) \
const char* object_type##_get_##attribute (object_type* obj) {\
......@@ -56,7 +57,39 @@ struct _belle_sip_uri {
unsigned int secure;
unsigned int lr_param;
const char* maddr_param;
belle_sip_list* header_list;
belle_sip_list* headernames_list;
};
void belle_sip_uri_delete(belle_sip_uri* uri) {
if (uri->user) free (uri->user);
if (uri->host) free (uri->host);
if (uri->transport_param) free (uri->transport_param);
if (uri->maddr_param) free (uri->maddr_param);
if (uri->header_list) belle_sip_list_free (uri->header_list);
if (uri->headernames_list) belle_sip_list_free (uri->headernames_list);
free(uri);
}
typedef struct _header_pair {
const char* name;
const char* value;
} header_pair;
static const header_pair* header_pair_new(const char* name,const char* value) {
header_pair* lPair = (header_pair*)malloc( sizeof(header_pair));
lPair->name=strdup(name);
lPair->value=strdup(value);
return lPair;
}
static void header_pair_delete(header_pair* pair) {
free(pair->name);
free(pair->value);
free (pair);
}
static int header_pair_comp_func(const header_pair *a, const char*b) {
return strcmp(a->name,b);
}
belle_sip_uri* belle_sip_uri_parse (const char* uri) {
pANTLR3_INPUT_STREAM input;
pbelle_sip_uriLexer lex;
......@@ -85,59 +118,11 @@ belle_sip_uri* belle_sip_uri_new () {
memset(lUri,0,sizeof(belle_sip_uri));
return lUri;
}
void belle_sip_uri_delete(belle_sip_uri* uri) {
free(uri);
}
static char * concat (const char *str, ...) {
va_list ap;
size_t allocated = 100;
char *result = (char *) malloc (allocated);
if (result != NULL)
{
char *newp;
char *wp;
const char* s;
va_start (ap, str);
wp = result;
for (s = str; s != NULL; s = va_arg (ap, const char *)) {
size_t len = strlen (s);
/* Resize the allocated memory if necessary. */
if (wp + len + 1 > result + allocated)
{
allocated = (allocated + len) * 2;
newp = (char *) realloc (result, allocated);
if (newp == NULL)
{
free (result);
return NULL;
}
wp = newp + (wp - result);
result = newp;
}
memcpy (wp, s, len);
wp +=len;
}
/* Terminate the result string. */
*wp++ = '\0';
/* Resize memory to the optimal size. */
newp = realloc (result, wp - result);
if (newp != NULL)
result = newp;
va_end (ap);
}
return result;
}
char* belle_sip_uri_to_string(belle_sip_uri* uri) {
return concat( "sip:"
return belle_sip_concat( "sip:"
,(uri->user?uri->user:"")
,(uri->user?"@":"")
,uri->host
......@@ -146,6 +131,42 @@ char* belle_sip_uri_to_string(belle_sip_uri* uri) {
,NULL);
}
const char* belle_sip_uri_get_header(belle_sip_uri* uri,const char* name) {
belle_sip_list * lResult = belle_sip_list_find_custom(uri->header_list, header_pair_comp_func, name);
if (lResult) {
return ((header_pair*)(lResult->data))->value;
}
else {
return NULL;
}
}
void belle_sip_uri_set_header(belle_sip_uri* uri,const char* name,const char* value) {
/*1 check if present*/
belle_sip_list * lResult = belle_sip_list_find_custom(uri->headernames_list, strcmp, name);
/* first remove from headersnames list*/
if (lResult) {
belle_sip_list_remove_link(uri->headernames_list,lResult);
}
/* next from header list*/
lResult = belle_sip_list_find_custom(uri->header_list, header_pair_comp_func, name);
if (lResult) {
header_pair_delete(lResult->data);
belle_sip_list_remove_link(uri->header_list,lResult);
}
/* 2 insert*/
header_pair* lNewpair = header_pair_new(name,value);
uri->header_list=belle_sip_list_append(uri->header_list,lNewpair);
uri->headernames_list=belle_sip_list_append(uri->headernames_list,lNewpair->name);
}
belle_sip_list* belle_sip_uri_get_header_names(belle_sip_uri* uri) {
return uri->headernames_list;
}
SIP_URI_GET_SET_STRING(user)
SIP_URI_GET_SET_STRING(host)
SIP_URI_GET_SET_STRING(transport_param)
......
......@@ -24,6 +24,7 @@ void testSIMPLEURI(void) {
CU_ASSERT_PTR_NULL(belle_sip_uri_get_user(L_uri));
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "titi.com");
CU_ASSERT_PTR_NULL(belle_sip_uri_get_transport_param(L_uri));
belle_sip_uri_delete(L_uri);
}
void testCOMPLEXURI(void) {
......@@ -32,6 +33,7 @@ void testCOMPLEXURI(void) {
CU_ASSERT_EQUAL(belle_sip_uri_get_port(L_uri), 5060);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "titi.com");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(L_uri), "tcp");
belle_sip_uri_delete(L_uri);
}
void testSIPSURI(void) {
belle_sip_uri* L_uri = belle_sip_uri_parse("sips:titi.com");
......@@ -39,22 +41,40 @@ void testSIPSURI(void) {
belle_sip_uri_delete(L_uri);
L_uri = belle_sip_uri_parse("sip:titi.com");
CU_ASSERT_EQUAL(belle_sip_uri_is_secure(L_uri), 0);
belle_sip_uri_delete(L_uri);
}
void test_ip_host(void) {
belle_sip_uri* L_uri = belle_sip_uri_parse("sip:192.168.0.1");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "192.168.0.1");
belle_sip_uri_delete(L_uri);
}
void test_lr(void) {
belle_sip_uri* L_uri = belle_sip_uri_parse("sip:192.168.0.1;lr");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "192.168.0.1");
CU_ASSERT_EQUAL(belle_sip_uri_has_lr_param(L_uri), 1);
belle_sip_uri_delete(L_uri);
}
void test_maddr(void) {
belle_sip_uri* L_uri = belle_sip_uri_parse("sip:192.168.0.1;lr;maddr=linphone.org");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_maddr_param(L_uri), "linphone.org");
belle_sip_uri_delete(L_uri);
}
void test_headers(void) {
belle_sip_uri* L_uri = belle_sip_uri_parse("sip:192.168.0.1?toto=titi");
CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_uri_get_header(L_uri,"toto"));
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_header(L_uri,"toto"), "titi");
CU_ASSERT_PTR_NULL(belle_sip_uri_get_header(L_uri,"bla"));
belle_sip_uri_delete(L_uri);
L_uri = belle_sip_uri_parse("sip:192.168.0.1?toto=titi&header2=popo");
CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_uri_get_header(L_uri,"toto"));
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_header(L_uri,"header2"), "popo");
}
int main (int argc, char *argv[]) {
......@@ -79,6 +99,8 @@ int main (int argc, char *argv[]) {
|| (NULL == CU_add_test(pSuite, "test of ip uri", test_ip_host))
|| (NULL == CU_add_test(pSuite, "test of lr uri", test_lr))
|| (NULL == CU_add_test(pSuite, "test of maddr uri", test_maddr))
|| (NULL == CU_add_test(pSuite, "test of headers", test_headers))
|| (NULL == CU_add_test(pSuite, "test of sips uri", testSIPSURI)))
{
CU_cleanup_registry();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment