diff --git a/src/sip/sip_utils.cpp b/src/sip/sip_utils.cpp index 509b6b7ea9a3e2abb0da81dd044e579510a9c9df..8fc22e8889155ae87f6927fcff2f965b087fae8c 100644 --- a/src/sip/sip_utils.cpp +++ b/src/sip/sip_utils.cpp @@ -42,6 +42,8 @@ #include <vector> #include <algorithm> +using namespace std::literals; + namespace jami { namespace sip_utils { @@ -131,29 +133,30 @@ parseDisplayName(const pjsip_contact_hdr* header) return parseDisplayName(reinterpret_cast<pjsip_name_addr*>(header->uri)); } -void -stripSipUriPrefix(std::string& sipUri) +std::string_view +stripSipUriPrefix(std::string_view sipUri) { // Remove sip: prefix - static const char SIP_PREFIX[] = "sip:"; + static constexpr auto SIP_PREFIX = "sip:"sv; size_t found = sipUri.find(SIP_PREFIX); - if (found != std::string::npos) - sipUri.erase(found, (sizeof SIP_PREFIX) - 1); + if (found != std::string_view::npos) + sipUri = sipUri.substr(found + SIP_PREFIX.size()); // URI may or may not be between brackets found = sipUri.find('<'); - if (found != std::string::npos) - sipUri.erase(found, 1); + if (found != std::string_view::npos) + sipUri = sipUri.substr(found + 1); found = sipUri.find('@'); - - if (found != std::string::npos) - sipUri.erase(found); + if (found != std::string_view::npos) + sipUri = sipUri.substr(0, found); found = sipUri.find('>'); - if (found != std::string::npos) - sipUri.erase(found, 1); + if (found != std::string_view::npos) + sipUri = sipUri.substr(0, found); + + return sipUri; } std::string diff --git a/src/sip/sip_utils.h b/src/sip/sip_utils.h index a4980c0247e68456fd552590e793cd12618e9fac..78608ea050f61022798c6bd03769c6c3ea7269f4 100644 --- a/src/sip/sip_utils.h +++ b/src/sip/sip_utils.h @@ -90,7 +90,7 @@ std::string fetchHeaderValue(pjsip_msg* msg, const std::string& field); pjsip_route_hdr* createRouteSet(const std::string& route, pj_pool_t* hdr_pool); -void stripSipUriPrefix(std::string& sipUri); +std::string_view stripSipUriPrefix(std::string_view sipUri); std::string parseDisplayName(const pjsip_name_addr* sip_name_addr); std::string parseDisplayName(const pjsip_from_hdr* header); diff --git a/src/sip/sipvoiplink.cpp b/src/sip/sipvoiplink.cpp index 6c53dfb149b9ea25c3d2fe74f5c31479294ec559..73bb067a00f8d1e611b487e42e5cb482ff02afc6 100644 --- a/src/sip/sipvoiplink.cpp +++ b/src/sip/sipvoiplink.cpp @@ -204,12 +204,14 @@ transaction_request_cb(pjsip_rx_data* rdata) std::string_view viaHostname(sip_via.host.ptr, sip_via.host.slen); const std::string_view remote_user(sip_from_uri->user.ptr, sip_from_uri->user.slen); const std::string_view remote_hostname(sip_from_uri->host.ptr, sip_from_uri->host.slen); - char tmp[PJSIP_MAX_URL_SIZE]; - size_t length = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, sip_from_uri, tmp, PJSIP_MAX_URL_SIZE); - std::string peerNumber(tmp, length); - sip_utils::stripSipUriPrefix(peerNumber); + std::string peerNumber; if (not remote_user.empty() and not remote_hostname.empty()) peerNumber = remote_user + "@" + remote_hostname; + else { + char tmp[PJSIP_MAX_URL_SIZE]; + size_t length = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, sip_from_uri, tmp, PJSIP_MAX_URL_SIZE); + peerNumber = sip_utils::stripSipUriPrefix(std::string_view(tmp, length)); + } auto account( Manager::instance().sipVoIPLink().guessAccount(toUsername, viaHostname, remote_hostname)); diff --git a/test/sip/test_SIP.cpp b/test/sip/test_SIP.cpp index dd13878095be1cb6e07b25bd731fbe15f6072198..aab9ab5c91a13e36dc52d4e5a916a884299d11f5 100644 --- a/test/sip/test_SIP.cpp +++ b/test/sip/test_SIP.cpp @@ -32,6 +32,7 @@ #include "call_const.h" using namespace jami; +using namespace std::literals; static pthread_mutex_t count_mutex; static pthread_cond_t count_nb_thread; @@ -329,7 +330,6 @@ void test_SIP::testSIPURI() { std::cout << ">>>> test SIPURI <<<< " << '\n'; - std::string foo("<sip:17771234567@callcentric.com>"); - sip_utils::stripSipUriPrefix(foo); - CPPUNIT_ASSERT(foo == "17771234567"); + auto foo = sip_utils::stripSipUriPrefix("<sip:17771234567@callcentric.com>"sv); + CPPUNIT_ASSERT_EQUAL("17771234567"sv, foo); }