Commit 993b7ae5 authored by jehan's avatar jehan

add uri user passwd support

parent 3bc4bd25
......@@ -886,6 +886,7 @@ belle_sip_refresher_t* belle_sip_refresher_new(belle_sip_client_transaction_t* t
int belle_sip_get_char (const char*a,int n,char*out);
/*return an escaped string*/
BELLESIP_INTERNAL_EXPORT char* belle_sip_uri_to_escaped_username(const char* buff) ;
BELLESIP_INTERNAL_EXPORT char* belle_sip_uri_to_escaped_userpasswd(const char* buff) ;
BELLESIP_INTERNAL_EXPORT char* belle_sip_uri_to_escaped_parameter(const char* buff) ;
BELLESIP_INTERNAL_EXPORT char* belle_sip_uri_to_escaped_header(const char* buff) ;
BELLESIP_INTERNAL_EXPORT char* belle_sip_to_unescaped_string(const char* buff) ;
......
......@@ -1312,7 +1312,12 @@ user : ( unreserved | escaped | user_unreserved )+ {
belle_sip_free(unescaped_username);
};
user_unreserved : '&' | EQUAL | '+' | '$' | COMMA | SEMI | '?' | SLASH;
password : ( unreserved |'&' | EQUAL | '+' | '$' | COMMA )*;
password : ( unreserved | escaped |'&' | EQUAL | '+' | '$' | COMMA )* {
char* unescaped_userpasswd;
unescaped_userpasswd=belle_sip_to_unescaped_string((const char *)$text->chars);
belle_sip_uri_set_user_password($userinfo::current,unescaped_userpasswd);
belle_sip_free(unescaped_userpasswd);
};
hostport[belle_sip_uri_t* uri]
scope { belle_sip_uri_t* current; }
@init {$hostport::current=uri;}
......
......@@ -46,6 +46,7 @@ struct _belle_sip_uri {
belle_sip_parameters_t params;
unsigned int secure;
char* user;
char* user_password;
char* host;
int port;
belle_sip_parameters_t * header_list;
......@@ -54,12 +55,14 @@ struct _belle_sip_uri {
static void belle_sip_uri_destroy(belle_sip_uri_t* uri) {
if (uri->user) belle_sip_free (uri->user);
if (uri->host) belle_sip_free (uri->host);
if (uri->user_password) belle_sip_free (uri->user_password);
belle_sip_object_unref(BELLE_SIP_OBJECT(uri->header_list));
}
static void belle_sip_uri_clone(belle_sip_uri_t* uri, const belle_sip_uri_t *orig){
uri->secure=orig->secure;
uri->user=orig->user?belle_sip_strdup(orig->user):NULL;
uri->user_password=orig->user_password?belle_sip_strdup(orig->user_password):NULL;
uri->host=orig->host?belle_sip_strdup(orig->host):NULL;
uri->port=orig->port;
if (orig->header_list){
......@@ -90,10 +93,21 @@ belle_sip_error_code belle_sip_uri_marshal(const belle_sip_uri_t* uri, char* buf
if (uri->user) {
char* escaped_username=belle_sip_uri_to_escaped_username(uri->user);
error=belle_sip_snprintf(buff,buff_size,offset,"%s@",escaped_username);
error=belle_sip_snprintf(buff,buff_size,offset,"%s",escaped_username);
belle_sip_free(escaped_username);
if (error!=BELLE_SIP_OK) return error;
if (uri->user_password) {
char* escaped_password=belle_sip_uri_to_escaped_userpasswd(uri->user_password);
error=belle_sip_snprintf(buff,buff_size,offset,":%s",escaped_password);
belle_sip_free(escaped_password);
if (error!=BELLE_SIP_OK) return error;
}
error=belle_sip_snprintf(buff,buff_size,offset,"@",NULL);
if (error!=BELLE_SIP_OK) return error;
}
if (uri->host) {
if (strchr(uri->host,':')) { /*ipv6*/
error=belle_sip_snprintf(buff,buff_size,offset,"[%s]",uri->host);
......@@ -200,6 +214,7 @@ void belle_sip_uri_fix(belle_sip_uri_t *uri){
SIP_URI_GET_SET_BOOL(secure)
SIP_URI_GET_SET_STRING(user)
SIP_URI_GET_SET_STRING(user_password)
SIP_URI_GET_SET_STRING(host)
SIP_URI_GET_SET_INT(port)
......
......@@ -836,6 +836,24 @@ static const char *get_uri_username_noescapes() {
}
return noescapes;
}
/*
*
* password = *( unreserved / escaped /
"&" / "=" / "+" / "$" / "," )
* */
static const char *get_uri_userpasswd_noescapes() {
static char noescapes[BELLE_SIP_NO_ESCAPES_SIZE] = {0};
if (noescapes[BELLE_SIP_NO_ESCAPES_SIZE-1] == 0) {
// unreserved
noescapes_add_alfanums(noescapes);
noescapes_add_list(noescapes, "-_.!~*'()");
noescapes_add_list(noescapes, "&=+$,");
noescapes[BELLE_SIP_NO_ESCAPES_SIZE-1] = 1; // initialized
}
return noescapes;
}
static const char *get_uri_parameter_noescapes() {
static char noescapes[BELLE_SIP_NO_ESCAPES_SIZE] = {0};
......@@ -916,7 +934,9 @@ static char* belle_sip_escape(const char* buff, const char *noescapes) {
char* belle_sip_uri_to_escaped_username(const char* buff) {
return belle_sip_escape(buff, get_uri_username_noescapes());
}
char* belle_sip_uri_to_escaped_userpasswd(const char* buff) {
return belle_sip_escape(buff, get_uri_userpasswd_noescapes());
}
char* belle_sip_uri_to_escaped_parameter(const char* buff) {
return belle_sip_escape(buff, get_uri_parameter_noescapes());
}
......
......@@ -128,6 +128,18 @@ static void test_maddr(void) {
}
static void test_user_passwd(void) {
belle_sip_uri_t * L_uri = belle_sip_uri_parse("sip:toto:tata@bla;");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_uri = belle_sip_uri_parse(l_raw_uri);
belle_sip_free(l_raw_uri);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_user_password(L_uri), "tata");
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
}
static void test_uri_parameters (void) {
char* l_raw_uri;
belle_sip_uri_t* L_tmp;
......@@ -205,6 +217,23 @@ static void test_escaped_username(void) {
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
}
static void test_escaped_passwd(void) {
belle_sip_uri_t* L_tmp;
belle_sip_uri_t * L_uri = belle_sip_uri_parse("sips:%22jehan%22%20%3cjehan%40sip2.linphone.org:544%3e@sip.linphone.org");
char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
L_tmp = belle_sip_uri_parse(l_raw_uri);
L_uri = BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(L_tmp)));
belle_sip_object_unref(BELLE_SIP_OBJECT(L_tmp));
belle_sip_free(l_raw_uri);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_user(L_uri), "\"jehan\" <jehan@sip2.linphone.org");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "sip.linphone.org");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_user_password(L_uri), "544>");
belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
}
static void test_escaped_parameter(void) {
belle_sip_uri_t* L_tmp;
......@@ -390,6 +419,8 @@ test_t uri_tests[] = {
{ "Complex URI", testCOMPLEXURI },
{ "Escaped username", test_escaped_username },
{ "Escaped parameter", test_escaped_parameter },
{ "Escaped passwd", test_escaped_passwd},
{ "User passwd", test_user_passwd},
{ "IP host", test_ip_host },
{ "lr", test_lr },
{ "maddr", test_maddr },
......
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