解析
示例:url解析
#include <osipparser2/osip_port.h>
#include <cstdlib>
#include <cstring>
#include <osipparser2/osip_uri.h>
int osip_uri_test_accessor_api(osip_uri_t *url);
void osip_uri_parse(char *a_url);
int main(int argc, char **argv) {
osip_uri_parse("sip:j.d;oe:secret?@big.com;to[to?r+s=foo:&.bar;qs=dr$/def");
return 0;
}
void osip_uri_parse(char *a_url){
if(a_url == NULL || *a_url == 0){
return;
}
int errcode;
char *dest;
osip_uri_t *url;
osip_uri_init(&url);
printf("=================================================\n");
printf("URL TO PARSE: |%s|\n", a_url);
errcode = osip_uri_parse(url, a_url);
if (errcode != -1) {
if (osip_uri_to_str(url, &dest) != -1) {
printf("result: |%s|\n", dest);
osip_uri_test_accessor_api(url);
osip_free(dest);
}
}
osip_uri_free(url);
printf("=================================================\n");
}
int osip_uri_test_accessor_api(osip_uri_t *url) {
if (url->scheme != NULL)
fprintf(stdout, "url->scheme: %s\n", url->scheme);
if (url->string != NULL) {
fprintf(stdout, "url->string: %s\n", url->string);
return 0;
}
if (url->username != NULL)
fprintf(stdout, "url->username: %s\n", url->username);
if ((url->password != NULL) )
fprintf(stdout, "url->passwor: %s\n", url->password);
if (url->host != NULL) {
fprintf(stdout, "url->host: %s\n", url->host);
}
if (url->port != NULL)
fprintf(stdout, "url->port: %s\n", url->port);
fprintf(stdout, "\nuri-params\n");
{
int pos = 0;
osip_uri_param_t *u_param;
while (!osip_list_eol(&url->url_params, pos)) {
u_param = (osip_uri_param_t *) osip_list_get(&url->url_params, pos);
if (u_param->gvalue != NULL)
fprintf(stdout, "\t%s=%s\n", u_param->gname, u_param->gvalue);
else
fprintf(stdout, "\t%s\n", u_param->gname);
pos++;
}
}
fprintf(stdout, "\nheaders\n");
{
int pos = 0;
osip_uri_header_t *u_header;
while (!osip_list_eol(&url->url_headers, pos)) {
u_header = (osip_uri_header_t *) osip_list_get(&url->url_headers, pos);
if (pos == 0)
fprintf(stdout, "\t%s=%s\n", u_header->gname, u_header->gvalue);
else
fprintf(stdout, "\t%s=%s\n", u_header->gname, u_header->gvalue);
pos++;
}
}
fprintf(stdout, "\n");
return 0;
}
示例:via解析
#include <cstdlib>
#include <cstring>
#include <osipparser2/osip_port.h>
#include <osipparser2/osip_message.h>
void osip_via_parse(char *a_via){
if(a_via == NULL || *a_via == 0){
return;
}
int errcode;
char *dest;
osip_via_t *via;
printf("=================================================\n");
printf("VIA TO PARSE: |%s|\n", a_via);
osip_via_init(&via);
errcode = osip_via_parse(via, a_via);
if (errcode != -1) {
if (osip_via_to_str(via, &dest) != -1) {
printf("result: |%s|\n", dest);
osip_free(dest);
}
} else
printf("Bad via format: %s\n", a_via);
osip_via_free(via);
printf("=================================================\n");
}
int main(int argc, char **argv) {
osip_via_parse("SIP/2.0/UDP my.ex.fr:4000;ttl=15;joe=tokenabc123-.!%* ; bob=");
osip_via_parse(" SIP/2.0/UDP my.ex.fr");
osip_via_parse(" SIP/2.0/UDP my.ex.fr:4054");
osip_via_parse(" SIP/2.0/UDP my.ex.fr:4054");
osip_via_parse(" SIP/ 2.0/UDP my.ex.fr:4054");
osip_via_parse(" SIP / 2.0 / UDP my.ex.fr");
return 0;
}
示例:wwwa解析
#include <cstdlib>
#include <cstring>
#include <osipparser2/osip_port.h>
#include <osipparser2/osip_message.h>
void osip_wwwa_parse(const char *a_wwwauthenticate){
if(a_wwwauthenticate == NULL || *a_wwwauthenticate == 0){
return;
}
printf("=================================================\n");
printf("WWWAUTHENTICATE TO PARSE: |%s|\n", a_wwwauthenticate);
char *dest;
int errcode;
osip_www_authenticate_t *wwwauthenticate;
osip_www_authenticate_init(&wwwauthenticate);
errcode = osip_www_authenticate_parse(wwwauthenticate, a_wwwauthenticate);
if (errcode != -1) {
if (osip_www_authenticate_to_str(wwwauthenticate, &dest) != -1) {
printf("result: |%s|\n", dest);
osip_free(dest);
}
} else
printf("Bad wwwauthenticate format: %s\n", a_wwwauthenticate);
osip_www_authenticate_free(wwwauthenticate);
printf("=================================================\n");
}
#include <string>
int main(int argc, char **argv) {
osip_wwwa_parse(R"(Basic realm="jobs", nonce="??????????")");
osip_wwwa_parse(R"(Digest realm="testrealm@host.com", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41")");
osip_wwwa_parse(R"(Digest realm="studentlev1", nonce="??????????", stale=true, algorithm=MD5)");
osip_wwwa_parse(R"(Digest realm="studentlev1", nonce="??????????", stale=true)");
osip_wwwa_parse(R"(Digest realm="%$s..c,\"", nonce="??????????")");
osip_wwwa_parse(R"(Digest realm="%$s..c,\\\"", nonce="??????????")");
osip_wwwa_parse(R"(Basic realm="jobs" , nonce="??????????")");
osip_wwwa_parse(R"(Digest realm= "studentlev1" , nonce="??????????", stale=true , algorithm= MD5)");
osip_wwwa_parse(R"(Digest realm="%$s..c,\"", nonce ="??????????")");
osip_wwwa_parse(R"(Digest realm="%$s..c,\\\"", nonce= "??????????")");
return 0;
}