Commit 0eb8a5d8 authored by jehan's avatar jehan

better parser error handling

parent 84bd7a0c
......@@ -945,7 +945,10 @@ belle_sip_header_extension_t* belle_sip_header_extension_parse (const char* valu
tokens ->free(tokens);
lex ->free(lex);
input ->close(input);
if (l_parsed_object.ret == NULL) belle_sip_error("Parser error for [%s]",value);\
if (l_parsed_object.ret == NULL) {\
belle_sip_error("Parser error for [%s]",value);\
return NULL;\
} else \
return BELLE_SIP_HEADER_EXTENSION(l_parsed_object.ret);
}
GET_SET_STRING(belle_sip_header_extension,value);
......
......@@ -433,7 +433,7 @@ scope { belle_sip_header_contact_t* current; belle_sip_header_contact_t* first;
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
if ($header_contact::current) belle_sip_object_unref($header_contact::current);
if ($ret) belle_sip_object_unref($ret);
$ret=NULL;
}
......@@ -452,17 +452,6 @@ scope { belle_sip_header_contact_t* prev;}
: (name_addr[BELLE_SIP_HEADER_ADDRESS($header_contact::current)]
| paramless_addr_spec[BELLE_SIP_HEADER_ADDRESS($header_contact::current)]) (semi contact_params)*;
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
if ( $contact_param::prev == NULL) {
$header_contact::first=NULL;
} else {
belle_sip_header_set_next(BELLE_SIP_HEADER($contact_param::prev),NULL);
}
belle_sip_object_unref($header_contact::current);
$header_contact::current=$contact_param::prev;
}
header_address returns [belle_sip_header_address_t* ret]
@init { $ret=NULL; }
......@@ -840,7 +829,7 @@ scope { belle_sip_header_service_route_t* current; belle_sip_header_service_rout
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
belle_sip_object_unref($header_service_route::current);
if ($ret) belle_sip_object_unref($ret);
$ret=NULL;
}
srv_route
......@@ -855,17 +844,7 @@ scope { belle_sip_header_service_route_t* prev;}
}
}
: name_addr[BELLE_SIP_HEADER_ADDRESS($header_service_route::current)] ( semi sr_param )*;
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
if ( $srv_route::prev == NULL) {
$header_service_route::first=NULL;
} else {
belle_sip_header_set_next(BELLE_SIP_HEADER($srv_route::prev),NULL);
}
belle_sip_object_unref($header_service_route::current);
$header_service_route::current=$srv_route::prev;
}
sr_param
: generic_param[BELLE_SIP_PARAMETERS($header_service_route::current)];
......@@ -878,7 +857,7 @@ scope { belle_sip_header_record_route_t* current; belle_sip_header_record_route_
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
belle_sip_object_unref($header_record_route::current);
if ($ret) belle_sip_object_unref($ret);
$ret=NULL;
}
rec_route
......@@ -893,17 +872,7 @@ scope { belle_sip_header_record_route_t* prev;}
}
}
: name_addr[BELLE_SIP_HEADER_ADDRESS($header_record_route::current)] ( semi rr_param )*;
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
if ( $rec_route::prev == NULL) {
$header_record_route::first=NULL;
} else {
belle_sip_header_set_next(BELLE_SIP_HEADER($rec_route::prev),NULL);
}
belle_sip_object_unref($header_record_route::current);
$header_record_route::current=$rec_route::prev;
}
rr_param
: generic_param[BELLE_SIP_PARAMETERS($header_record_route::current)];
......@@ -934,6 +903,12 @@ header_route returns [belle_sip_header_route_t* ret]
scope { belle_sip_header_route_t* current;belle_sip_header_route_t* first; }
@init { $header_route::current = NULL; }
: route_token /*'Route'*/ hcolon route_param (comma route_param)*{$ret = $header_route::first;};
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
if ($ret) belle_sip_object_unref($ret);
$ret=NULL;
}
route_param
scope { belle_sip_header_route_t* prev;}
@init { if ($header_route::current == NULL) {
......@@ -946,17 +921,7 @@ scope { belle_sip_header_route_t* prev;}
}
}
: name_addr[BELLE_SIP_HEADER_ADDRESS($header_route::current)] ( semi r_param )*;
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
if ( $route_param::prev == NULL) {
$header_route::first=NULL;
} else {
belle_sip_header_set_next(BELLE_SIP_HEADER($route_param::prev),NULL);
}
belle_sip_object_unref($header_record_route::current);
$header_route::current=$route_param::prev;
}
r_param
: generic_param[BELLE_SIP_PARAMETERS($header_route::current)];
/*
......@@ -1052,7 +1017,12 @@ scope { belle_sip_header_via_t* current; belle_sip_header_via_t* first; }
@init { $header_via::current = NULL;$ret = NULL;}
: via_token/* ( 'via' | 'v' )*/ hcolon via_parm (comma via_parm)* {$ret = $header_via::first;} ;
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
if ($ret) belle_sip_object_unref($ret);
$ret=NULL;
}
via_parm
scope { belle_sip_header_via_t* prev;}
@init { if ($header_via::current == NULL) {
......@@ -1066,17 +1036,6 @@ scope { belle_sip_header_via_t* prev;}
}
}
: sent_protocol LWS sent_by ( semi via_params )*;
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
if ( $via_parm::prev == NULL) {
$header_via::first=NULL;
} else {
belle_sip_header_set_next(BELLE_SIP_HEADER($via_parm::prev),NULL);
}
belle_sip_object_unref($header_via::current);
$header_via::current=$via_parm::prev;
}
via_params
: /*via_ttl | via_maddr
......@@ -1201,6 +1160,7 @@ privacy_val: token {belle_sip_header_privacy_add_privacy($header_privacy::curren
//********************************************************************************************//
header_extension[ANTLR3_BOOLEAN check_for_known_header] returns [belle_sip_header_t* ret]
@init {$ret=NULL;}
: header_name
hcolon
header_value {if (check_for_known_header && STRCASECMP_HEADER_NAMED(BELLE_SIP_CONTACT,"m",(const char*)$header_name.text->chars)) {
......@@ -1261,6 +1221,12 @@ header_extension[ANTLR3_BOOLEAN check_for_known_header] returns [belle_sip_head
belle_sip_header_set_name($ret,(const char*)$header_name.text->chars);
}
} ;
catch [ANTLR3_MISMATCHED_TOKEN_EXCEPTION]
{
belle_sip_message("[\%s] reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
if ($ret) belle_sip_object_unref($ret);
$ret=NULL;
}
header_name
: token;
......
This diff is collapsed.
......@@ -562,6 +562,35 @@ static void testUrisComponentsForRequest(void) {
CU_ASSERT_NOT_EQUAL(belle_sip_client_transaction_send_request(t),0);
}
static void testGenericMessage(void) {
const char* raw_message = "SIP/2.0 180 Ringing\r\n"
"Via: SIP/2.0/UDP 192.168.1.73:5060;branch=z9hG4bK.hhdJx4~kD;rport\r\n"
"Record-Route: <sip:91.121.209.194;lr>\r\n"
"Record-Route: <sip:siproxd@192.168.1.254:5060;lr>\r\n"
"From: <sip:granny2@sip.linphone.org>;tag=5DuaoDRru\r\n"
"To: <sip:chmac@sip.linphone.org>;tag=PelIhu0\r\n"
"Call-ID: e-2Q~fxwNs\r\n"
"CSeq: 21 INVITE\r\n"
"user-agent: Linphone/3.6.99 (belle-sip/1.2.4)\r\n"
"supported: replaces\r\n"
"supported: outbound\r\n"
"Content-Length: 0\r\n"
"\r\n";
belle_sip_response_t* response;
belle_sip_message_t* message = belle_sip_message_parse(raw_message);
char* encoded_message = belle_sip_object_to_string(BELLE_SIP_OBJECT(message));
belle_sip_object_unref(BELLE_SIP_OBJECT(message));
message = belle_sip_message_parse(encoded_message);
response = BELLE_SIP_RESPONSE(message);
CU_ASSERT_EQUAL(belle_sip_response_get_status_code(response),180);
/* CU_ASSERT_STRING_EQUAL(belle_sip_response_get_reason_phrase(response),"Unauthorized");
CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"WWW-Authenticate"));
check_uri_and_headers(message);*/
belle_sip_object_unref(message);
belle_sip_free(encoded_message);
}
/* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
test_t message_tests[] = {
{ "REGISTER", testRegisterMessage },
......@@ -578,7 +607,8 @@ test_t message_tests[] = {
{ "Channel parser malformed start", channel_parser_malformed_start},
{ "RFC2543 compatibility", testRFC2543Compat},
{ "Uri headers in sip INVITE",testUriHeadersInInvite},
{ "Uris components in request",testUrisComponentsForRequest}
{ "Uris components in request",testUrisComponentsForRequest},
{ "Generic message test",testGenericMessage}
};
test_suite_t message_test_suite = {
......
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