From 20cb5e16fcc7cb22a2af0bd07cf72f089c2fa3d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Wed, 11 Nov 2020 13:56:52 -0500
Subject: [PATCH] account: use string_view, cleanup

Change-Id: I65e8d0270bfcd852745a6babd1c713f4309032de
---
 src/call.h                  |  2 +-
 src/ip_utils.cpp            |  4 ++--
 src/ip_utils.h              |  2 +-
 src/jamidht/jamiaccount.cpp |  6 +++---
 src/jamidht/jamiaccount.h   | 23 +---------------------
 src/sip/sipaccount.cpp      | 37 ++++++++++++++++++------------------
 src/sip/sipaccount.h        | 15 +++++++--------
 src/sip/sipaccountbase.h    |  2 +-
 src/sip/sipvoiplink.cpp     | 38 +++++++++++++++++++------------------
 src/sip/sipvoiplink.h       |  6 +++---
 10 files changed, 57 insertions(+), 78 deletions(-)

diff --git a/src/call.h b/src/call.h
index 4a866f996d..247783b18b 100644
--- a/src/call.h
+++ b/src/call.h
@@ -145,7 +145,7 @@ public:
      * not protected by mutex (when created)
      * @return std::string The peer number
      */
-    std::string getPeerNumber() const { return peerNumber_; }
+    const std::string& getPeerNumber() const { return peerNumber_; }
     /**
      * Set the display name (caller in ingoing)
      * not protected by mutex (when created)
diff --git a/src/ip_utils.cpp b/src/ip_utils.cpp
index 6be7ea75e9..c5e77ee5e6 100644
--- a/src/ip_utils.cpp
+++ b/src/ip_utils.cpp
@@ -217,7 +217,7 @@ ip_utils::getDeviceName()
 }
 
 std::vector<IpAddr>
-ip_utils::getAddrList(const std::string& name, pj_uint16_t family)
+ip_utils::getAddrList(std::string_view name, pj_uint16_t family)
 {
     std::vector<IpAddr> ipList;
     if (name.empty())
@@ -235,7 +235,7 @@ ip_utils::getAddrList(const std::string& name, pj_uint16_t family)
     if (status != PJ_SUCCESS) {
         JAMI_ERR("Error resolving %.*s : %s",
                  (int) name.size(),
-                 name.c_str(),
+                 name.data(),
                  sip_utils::sip_strerror(status).c_str());
         return ipList;
     }
diff --git a/src/ip_utils.h b/src/ip_utils.h
index 3f014e319a..0705d13c8b 100644
--- a/src/ip_utils.h
+++ b/src/ip_utils.h
@@ -350,7 +350,7 @@ std::vector<std::string> getAllIpInterfaceByName();
  */
 std::vector<std::string> getAllIpInterface();
 
-std::vector<IpAddr> getAddrList(const std::string& name, pj_uint16_t family = pj_AF_UNSPEC());
+std::vector<IpAddr> getAddrList(std::string_view name, pj_uint16_t family = pj_AF_UNSPEC());
 
 bool haveCommonAddr(const std::vector<IpAddr>& a, const std::vector<IpAddr>& b);
 
diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 141f690e96..dcf0a8fa2d 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -734,7 +734,7 @@ JamiAccount::SIPStartCall(SIPCall& call, IpAddr target)
     call.setupLocalSDPFromIce();
     std::string toUri(
         getToUri(call.getPeerNumber() + "@"
-                 + target.toString(true).c_str())); // expecting a fully well formed sip uri
+                 + target.toString(true))); // expecting a fully well formed sip uri
 
     pj_str_t pjTo = sip_utils::CONST_PJ_STR(toUri);
 
@@ -2834,7 +2834,7 @@ JamiAccount::generateDhParams()
 }
 
 MatchRank
-JamiAccount::matches(const std::string& userName, const std::string& server) const
+JamiAccount::matches(std::string_view userName, std::string_view server) const
 {
     if (not accountManager_ or not accountManager_->getInfo())
         return MatchRank::NONE;
@@ -2842,7 +2842,7 @@ JamiAccount::matches(const std::string& userName, const std::string& server) con
     if (userName == accountManager_->getInfo()->accountId
         || server == accountManager_->getInfo()->accountId
         || userName == accountManager_->getInfo()->deviceId) {
-        JAMI_DBG("Matching account id in request with username %s", userName.c_str());
+        JAMI_DBG("Matching account id in request with username %.*s", (int)userName.size(), userName.data());
         return MatchRank::FULL;
     } else {
         return MatchRank::NONE;
diff --git a/src/jamidht/jamiaccount.h b/src/jamidht/jamiaccount.h
index 1195892aca..887ae866cc 100644
--- a/src/jamidht/jamiaccount.h
+++ b/src/jamidht/jamiaccount.h
@@ -221,19 +221,8 @@ public:
      */
     pj_str_t getContactHeader(pjsip_transport* = nullptr) override;
 
-    void setReceivedParameter(const std::string& received)
-    {
-        receivedParameter_ = received;
-        via_addr_.host.ptr = (char*) receivedParameter_.c_str();
-        via_addr_.host.slen = receivedParameter_.size();
-    }
-
-    std::string getReceivedParameter() const { return receivedParameter_; }
-
-    pjsip_host_port* getViaAddr() { return &via_addr_; }
-
     /* Returns true if the username and/or hostname match this account */
-    MatchRank matches(const std::string& username, const std::string& hostname) const override;
+    MatchRank matches(std::string_view username, std::string_view hostname) const override;
 
     /**
      * Implementation of Account::newOutgoingCall()
@@ -670,19 +659,9 @@ private:
     bool allowPeersFromContact_ {true};
     bool allowPeersFromTrusted_ {true};
 
-    /**
-     * Optional: "received" parameter from VIA header
-     */
-    std::string receivedParameter_ {};
-
     std::string managerUri_ {};
     std::string managerUsername_ {};
 
-    /**
-     * Optional: "rport" parameter from VIA header
-     */
-    int rPort_ {-1};
-
     /**
      * Optional: via_addr construct from received parameters
      */
diff --git a/src/sip/sipaccount.cpp b/src/sip/sipaccount.cpp
index 621976cd96..7e4c807adb 100644
--- a/src/sip/sipaccount.cpp
+++ b/src/sip/sipaccount.cpp
@@ -353,13 +353,12 @@ SIPAccount::SIPStartCall(std::shared_ptr<SIPCall>& call)
     // Add Ice headers to local SDP if ice transport exist
     call->setupLocalSDPFromIce();
 
-    std::string toUri(call->getPeerNumber()); // expecting a fully well formed sip uri
-
-    pj_str_t pjTo = pj_str((char*) toUri.c_str());
+    const std::string& toUri(call->getPeerNumber()); // expecting a fully well formed sip uri
+    pj_str_t pjTo = sip_utils::CONST_PJ_STR(toUri);
 
     // Create the from header
     std::string from(getFromUri());
-    pj_str_t pjFrom = pj_str((char*) from.c_str());
+    pj_str_t pjFrom = sip_utils::CONST_PJ_STR(from);
 
     auto transport = call->getTransport();
     if (!transport) {
@@ -1015,10 +1014,10 @@ SIPAccount::sendRegister()
 
     // Generate the FROM header
     std::string from(getFromUri());
-    pj_str_t pjFrom {(char*) from.data(), (pj_ssize_t) from.size()};
+    pj_str_t pjFrom(sip_utils::CONST_PJ_STR(from));
 
     // Get the received header
-    std::string received(getReceivedParameter());
+    const std::string& received(getReceivedParameter());
 
     // Get the contact header
     const pj_str_t pjContact(getContactHeader());
@@ -1353,19 +1352,19 @@ SIPAccount::loadConfig()
 }
 
 bool
-SIPAccount::fullMatch(const std::string& username, const std::string& hostname) const
+SIPAccount::fullMatch(std::string_view username, std::string_view hostname) const
 {
     return userMatch(username) and hostnameMatch(hostname);
 }
 
 bool
-SIPAccount::userMatch(const std::string& username) const
+SIPAccount::userMatch(std::string_view username) const
 {
     return !username.empty() and username == username_;
 }
 
 bool
-SIPAccount::hostnameMatch(const std::string& hostname) const
+SIPAccount::hostnameMatch(std::string_view hostname) const
 {
     if (hostname == hostname_)
         return true;
@@ -1375,7 +1374,7 @@ SIPAccount::hostnameMatch(const std::string& hostname) const
 }
 
 bool
-SIPAccount::proxyMatch(const std::string& hostname) const
+SIPAccount::proxyMatch(std::string_view hostname) const
 {
     if (hostname == serviceRoute_)
         return true;
@@ -1804,21 +1803,21 @@ SIPAccount::supportPresence(int function, bool enabled)
 }
 
 MatchRank
-SIPAccount::matches(const std::string& userName, const std::string& server) const
+SIPAccount::matches(std::string_view userName, std::string_view server) const
 {
     if (fullMatch(userName, server)) {
-        JAMI_DBG("Matching account id in request is a fullmatch %s@%s",
-                 userName.c_str(),
-                 server.c_str());
+        JAMI_DBG("Matching account id in request is a fullmatch %.*s@%.*s",
+                 (int)userName.size(), userName.data(),
+                 (int)server.size(), server.data());
         return MatchRank::FULL;
     } else if (hostnameMatch(server)) {
-        JAMI_DBG("Matching account id in request with hostname %s", server.c_str());
+        JAMI_DBG("Matching account id in request with hostname %.*s", (int)server.size(), server.data());
         return MatchRank::PARTIAL;
     } else if (userMatch(userName)) {
-        JAMI_DBG("Matching account id in request with username %s", userName.c_str());
+        JAMI_DBG("Matching account id in request with username %.*s", (int)userName.size(), userName.data());
         return MatchRank::PARTIAL;
     } else if (proxyMatch(server)) {
-        JAMI_DBG("Matching account id in request with proxy %s", server.c_str());
+        JAMI_DBG("Matching account id in request with proxy %.*s", (int)server.size(), server.data());
         return MatchRank::PARTIAL;
     } else {
         return MatchRank::NONE;
@@ -2132,8 +2131,8 @@ SIPAccount::sendTextMessage(const std::string& to,
 
     constexpr pjsip_method msg_method = {PJSIP_OTHER_METHOD, CONST_PJ_STR("MESSAGE")};
     std::string from(getFromUri());
-    pj_str_t pjFrom = pj_str((char*) from.c_str());
-    pj_str_t pjTo = pj_str((char*) toUri.c_str());
+    pj_str_t pjFrom = sip_utils::CONST_PJ_STR(from);
+    pj_str_t pjTo = sip_utils::CONST_PJ_STR(toUri);
 
     /* Create request. */
     pjsip_tx_data* tdata;
diff --git a/src/sip/sipaccount.h b/src/sip/sipaccount.h
index c754d1dd21..f959612ef7 100644
--- a/src/sip/sipaccount.h
+++ b/src/sip/sipaccount.h
@@ -385,11 +385,10 @@ public:
     void setReceivedParameter(const std::string& received)
     {
         receivedParameter_ = received;
-        via_addr_.host.ptr = (char*) receivedParameter_.c_str();
-        via_addr_.host.slen = receivedParameter_.size();
+        via_addr_.host = sip_utils::CONST_PJ_STR(receivedParameter_);
     }
 
-    std::string getReceivedParameter() const { return receivedParameter_; }
+    const std::string& getReceivedParameter() const { return receivedParameter_; }
 
     pjsip_host_port* getViaAddr() { return &via_addr_; }
 
@@ -427,7 +426,7 @@ public:
     pjsip_tpselector getTransportSelector();
 
     /* Returns true if the username and/or hostname match this account */
-    MatchRank matches(const std::string& username, const std::string& hostname) const override;
+    MatchRank matches(std::string_view username, std::string_view hostname) const override;
 
     /**
      * Presence management
@@ -528,10 +527,10 @@ private:
 
     void usePublishedAddressPortInVIA();
     void useUPnPAddressPortInVIA();
-    bool fullMatch(const std::string& username, const std::string& hostname) const;
-    bool userMatch(const std::string& username) const;
-    bool hostnameMatch(const std::string& hostname) const;
-    bool proxyMatch(const std::string& hostname) const;
+    bool fullMatch(std::string_view username, std::string_view hostname) const;
+    bool userMatch(std::string_view username) const;
+    bool hostnameMatch(std::string_view hostname) const;
+    bool proxyMatch(std::string_view hostname) const;
 
     bool isSrtpEnabled() const { return srtpKeyExchange_ != sip_utils::KeyExchangeProtocol::NONE; }
 
diff --git a/src/sip/sipaccountbase.h b/src/sip/sipaccountbase.h
index 79fc2c78a5..efad4167b1 100644
--- a/src/sip/sipaccountbase.h
+++ b/src/sip/sipaccountbase.h
@@ -274,7 +274,7 @@ public:
                                const std::map<std::string, std::string>& payloads);
 
     /* Returns true if the username and/or hostname match this account */
-    virtual MatchRank matches(const std::string& username, const std::string& hostname) const = 0;
+    virtual MatchRank matches(std::string_view username, std::string_view hostname) const = 0;
 
     void connectivityChanged() override {};
 
diff --git a/src/sip/sipvoiplink.cpp b/src/sip/sipvoiplink.cpp
index 00e7dffb77..0f618d4198 100644
--- a/src/sip/sipvoiplink.cpp
+++ b/src/sip/sipvoiplink.cpp
@@ -199,11 +199,11 @@ transaction_request_cb(pjsip_rx_data* rdata)
         return PJ_FALSE;
     }
 
-    std::string toUsername(sip_to_uri->user.ptr, sip_to_uri->user.slen);
-    std::string toHost(sip_to_uri->host.ptr, sip_to_uri->host.slen);
-    std::string viaHostname(sip_via.host.ptr, sip_via.host.slen);
-    const std::string remote_user(sip_from_uri->user.ptr, sip_from_uri->user.slen);
-    const std::string remote_hostname(sip_from_uri->host.ptr, sip_from_uri->host.slen);
+    std::string_view toUsername(sip_to_uri->user.ptr, sip_to_uri->user.slen);
+    std::string_view toHost(sip_to_uri->host.ptr, sip_to_uri->host.slen);
+    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);
@@ -223,15 +223,15 @@ transaction_request_cb(pjsip_rx_data* rdata)
 
     if (method->id == PJSIP_OTHER_METHOD) {
         pj_str_t* str = &method->name;
-        std::string request(str->ptr, str->slen);
+        std::string_view request(str->ptr, str->slen);
 
-        if (request.find("NOTIFY") != std::string::npos) {
+        if (request.find("NOTIFY") != std::string_view::npos) {
             if (body and body->data) {
                 int newCount {0};
                 int oldCount {0};
                 int urgentCount {0};
 
-                std::string sp = std::string(static_cast<char*>(body->data));
+                std::string sp(static_cast<char*>(body->data), body->len);
                 auto pos = sp.find("Voice-Message: ");
                 sp = sp.substr(pos);
 
@@ -249,7 +249,7 @@ transaction_request_cb(pjsip_rx_data* rdata)
                                                                    oldCount,
                                                                    urgentCount);
             }
-        } else if (request.find("MESSAGE") != std::string::npos) {
+        } else if (request.find("MESSAGE") != std::string_view::npos) {
             // Reply 200 immediately (RFC 3428, ch. 7)
             try_respond_stateless(endpt_, rdata, PJSIP_SC_OK, nullptr, nullptr, nullptr);
             // Process message content in case of multi-part body
@@ -325,7 +325,7 @@ transaction_request_cb(pjsip_rx_data* rdata)
 
     bool hasVideo = false;
     if (r_sdp) {
-        auto pj_str_video = pj_str((char*) "video");
+        auto pj_str_video = sip_utils::CONST_PJ_STR("video");
         for (decltype(r_sdp->media_count) i = 0; i < r_sdp->media_count; i++) {
             if (pj_strcmp(&r_sdp->media[i]->desc.media, &pj_str_video) == 0)
                 hasVideo = true;
@@ -334,7 +334,7 @@ transaction_request_cb(pjsip_rx_data* rdata)
 
     auto transport = Manager::instance().sipVoIPLink().sipTransportBroker->addTransport(
         rdata->tp_info.transport);
-    auto call = account->newIncomingCall(remote_user,
+    auto call = account->newIncomingCall(std::string(remote_user),
                                          {{"AUDIO_ONLY", (hasVideo ? "false" : "true")}},
                                          transport);
     if (!call) {
@@ -721,14 +721,14 @@ SIPVoIPLink::shutdown()
 }
 
 std::shared_ptr<SIPAccountBase>
-SIPVoIPLink::guessAccount(const std::string& userName,
-                          const std::string& server,
-                          const std::string& fromUri) const
+SIPVoIPLink::guessAccount(std::string_view userName,
+                          std::string_view server,
+                          std::string_view fromUri) const
 {
-    JAMI_DBG("username = %s, server = %s, from = %s",
-             userName.c_str(),
-             server.c_str(),
-             fromUri.c_str());
+    JAMI_DBG("username = %.*s, server = %.*s, from = %.*s",
+             (int)userName.size(), userName.data(),
+             (int)server.size(), server.data(),
+             (int)fromUri.size(), fromUri.data());
     // Try to find the account id from username and server name by full match
 
     std::shared_ptr<SIPAccountBase> result;
@@ -1084,7 +1084,9 @@ handleMediaControl(SIPCall& call, pjsip_msg_body* body)
                     while (rotation > 180)
                         rotation -= 360;
                     JAMI_WARN("Rotate video %d deg.", rotation);
+#ifdef ENABLE_VIDEO
                     call.getVideoRtp().setRotation(rotation);
+#endif
                 } catch (const std::exception& e) {
                     JAMI_WARN("Error parsing angle: %s", e.what());
                 }
diff --git a/src/sip/sipvoiplink.h b/src/sip/sipvoiplink.h
index ea2c67300f..5a055c503e 100644
--- a/src/sip/sipvoiplink.h
+++ b/src/sip/sipvoiplink.h
@@ -113,9 +113,9 @@ public:
     /**
      * Guess the account related to an incoming SIP call.
      */
-    std::shared_ptr<SIPAccountBase> guessAccount(const std::string& userName,
-                                                 const std::string& server,
-                                                 const std::string& fromUri) const;
+    std::shared_ptr<SIPAccountBase> guessAccount(std::string_view userName,
+                                                 std::string_view server,
+                                                 std::string_view fromUri) const;
 
     int getModId();
     pjsip_endpoint* getEndpoint();
-- 
GitLab