LIB-6 Release_2.1.0-RC28
authorVolker Birk <vb@pep-project.org>
Thu, 24 Sep 2020 13:41:49 +0200
changeset 36425a4d2b6d02f
parent 363 11650f95cca3
child 365 ddbb68006b7e
child 368 2e4ccd908609
LIB-6
message_cache.cc
message_cache.hh
     1.1 --- a/message_cache.cc	Sun Sep 06 23:42:47 2020 +0200
     1.2 +++ b/message_cache.cc	Thu Sep 24 13:41:49 2020 +0200
     1.3 @@ -80,7 +80,7 @@
     1.4                  extra, dst, enc_format, flags);
     1.5      }
     1.6  
     1.7 -    PEP_STATUS MessageCache::cache_release(const char *id)
     1.8 +    PEP_STATUS MessageCache::cache_release(std::string id)
     1.9      {
    1.10          message_cache.release(id);
    1.11          return PEP_STATUS_OK;
    1.12 @@ -297,13 +297,13 @@
    1.13              PEP_decrypt_flags_t *flags
    1.14          )
    1.15      {
    1.16 -        if (!src || emptystr(src->id))
    1.17 +        if (!src || cacheID(src) == "")
    1.18              return PEP_ILLEGAL_VALUE;
    1.19  
    1.20          ::message *_msg;
    1.21          {
    1.22              std::lock_guard<std::mutex> l(_mtx);
    1.23 -            _msg = message_cache._cache.at(src->id).src;
    1.24 +            _msg = message_cache._cache.at(cacheID(src)).src;
    1.25              swapContent(src, _msg);
    1.26          }
    1.27  
    1.28 @@ -318,8 +318,8 @@
    1.29          {
    1.30              std::lock_guard<std::mutex> l(_mtx);
    1.31              swapContent(_msg, src);
    1.32 -            ::free_message(message_cache._cache.at(src->id).dst);
    1.33 -            message_cache._cache.at(src->id).dst = _dst;
    1.34 +            ::free_message(message_cache._cache.at(cacheID(src)).dst);
    1.35 +            message_cache._cache.at(cacheID(src)).dst = _dst;
    1.36          }
    1.37          return status;
    1.38      }
    1.39 @@ -332,7 +332,7 @@
    1.40              bool has_pEp_msg_attachment     
    1.41          )
    1.42      {
    1.43 -        if (!msg || emptystr(msg->id))
    1.44 +        if (!msg || cacheID(msg) == "")
    1.45              return PEP_ILLEGAL_VALUE;
    1.46  
    1.47          if (one != msg_src && one != msg_dst)
    1.48 @@ -342,35 +342,99 @@
    1.49  
    1.50          if (one == msg_src) {
    1.51              std::lock_guard<std::mutex> l(_mtx);
    1.52 -            ::message *_src = _cache.at(std::string(msg->id)).src;
    1.53 +            ::message *_src = _cache.at(cacheID(msg)).src;
    1.54              swapContent(_msg, _src);
    1.55          }
    1.56          else /* msg_dst */ {
    1.57              std::lock_guard<std::mutex> l(_mtx);
    1.58 -            ::message *_dst = _cache.at(std::string(msg->id)).dst;
    1.59 +            ::message *_dst = _cache.at(cacheID(msg)).dst;
    1.60              swapContent(_msg, _dst);
    1.61          }
    1.62  
    1.63 +        removeCacheID(_msg);
    1.64          PEP_STATUS status = ::mime_encode_message(_msg, omit_fields, mimetext,
    1.65                  has_pEp_msg_attachment);
    1.66          ::free_message(_msg);
    1.67  
    1.68 -        cache_release(msg->id);
    1.69 +        cache_release(cacheID(msg));
    1.70  
    1.71          return status;
    1.72      }
    1.73  
    1.74 -    void MessageCache::generateMessageID(::message* msg)
    1.75 +    void MessageCache::generateCacheID(::message* msg)
    1.76      {
    1.77 -        if (msg && emptystr(msg->id)) {
    1.78 -            free(msg->id);
    1.79 -            std::string _range = std::to_string(id_range);
    1.80 -            std::string _id = std::to_string(next_id++);
    1.81 -            msg->id = strdup((std::string("pEp_auto_id_") + _range + _id).c_str());
    1.82 -            assert(msg->id);
    1.83 -            if (!msg->id)
    1.84 +        std::string _range = std::to_string(id_range);
    1.85 +        std::string _id = std::to_string(next_id++);
    1.86 +        std::string cid = _range + _id;
    1.87 +
    1.88 +        // if opt_fields is an empty list generate a new list
    1.89 +        if (!msg->opt_fields || !msg->opt_fields->value) {
    1.90 +            free_stringpair_list(msg->opt_fields);
    1.91 +            msg->opt_fields =
    1.92 +                ::new_stringpair_list(::new_stringpair("X-pEp-Adapter-Cache-ID",
    1.93 +                            cid.c_str()));
    1.94 +            if (!msg->opt_fields)
    1.95                  throw std::bad_alloc();
    1.96          }
    1.97 +        else {
    1.98 +            // add the cache ID as first field to an existing list
    1.99 +            auto spl = msg->opt_fields;
   1.100 +            msg->opt_fields =
   1.101 +                ::new_stringpair_list(::new_stringpair("X-pEp-Adapter-Cache-ID",
   1.102 +                            cid.c_str()));
   1.103 +            if (!msg->opt_fields) {
   1.104 +                msg->opt_fields = spl;
   1.105 +                throw std::bad_alloc();
   1.106 +            }
   1.107 +            msg->opt_fields->next = spl;
   1.108 +        }
   1.109 +    }
   1.110 +
   1.111 +    std::string MessageCache::cacheID(const ::message* msg)
   1.112 +    {
   1.113 +        for (auto spl = msg->opt_fields; spl && spl->value; spl = spl->next) {
   1.114 +            assert(spl->value->key);
   1.115 +            if (spl->value->key && std::string(spl->value->key) ==
   1.116 +                    "X-pEp-Adapter-Cache-ID") {
   1.117 +                assert(spl->value->value);
   1.118 +                if (spl->value->value)
   1.119 +                    return spl->value->value;
   1.120 +                else
   1.121 +                    return "";
   1.122 +            }
   1.123 +        }
   1.124 +        return "";
   1.125 +    }
   1.126 + 
   1.127 +    void MessageCache::removeCacheID(::message* msg)
   1.128 +    {
   1.129 +        // if the first element in the list is the cache ID then skip
   1.130 +        if (msg->opt_fields && msg->opt_fields->value &&
   1.131 +                msg->opt_fields->value->key &&
   1.132 +                std::string(msg->opt_fields->value->key) ==
   1.133 +                "X-pEp-Adapter-Cache-ID") {
   1.134 +            auto n = msg->opt_fields->next;
   1.135 +            msg->opt_fields->next = nullptr;
   1.136 +            ::free_stringpair_list(msg->opt_fields);
   1.137 +            msg->opt_fields = n;
   1.138 +        }
   1.139 +        else {
   1.140 +            // go through the list and remove
   1.141 +            ::stringpair_list_t *prev = nullptr;
   1.142 +            for (auto spl = msg->opt_fields; spl && spl->value; spl =
   1.143 +                    spl->next) {
   1.144 +                assert(spl->value->key);
   1.145 +                if (spl->value->key &&
   1.146 +                        std::string(spl->value->key) == "X-pEp-Adapter-Cache-ID") {
   1.147 +                    auto next = spl->next;
   1.148 +                    spl->next = nullptr;
   1.149 +                    ::free_stringpair_list(spl);
   1.150 +                    prev->next = next;
   1.151 +                    break;
   1.152 +                }
   1.153 +                prev = spl;
   1.154 +            }
   1.155 +        }
   1.156      }
   1.157  
   1.158      PEP_STATUS MessageCache::mime_decode_message(
   1.159 @@ -386,12 +450,11 @@
   1.160          if (status)
   1.161              return status;
   1.162  
   1.163 -        generateMessageID(_msg);
   1.164 -        *msg = empty_message_copy(_msg);
   1.165 +        generateCacheID(_msg); *msg = empty_message_copy(_msg);
   1.166  
   1.167          {
   1.168              std::lock_guard<std::mutex> l(_mtx);
   1.169 -            message_cache._cache.emplace(std::make_pair(std::string(_msg->id),
   1.170 +            message_cache._cache.emplace(std::make_pair(cacheID(_msg),
   1.171                          cache_entry(_msg, nullptr)));
   1.172          }
   1.173  
   1.174 @@ -409,21 +472,19 @@
   1.175      {
   1.176          ::message *_msg;
   1.177          {
   1.178 -            std::lock_guard<std::mutex> l(_mtx);
   1.179 -            _msg = message_cache._cache.at(src->id).src;
   1.180 -            swapContent(src, _msg);
   1.181 +            std::lock_guard<std::mutex> l(_mtx); _msg =
   1.182 +                message_cache._cache.at(cacheID(src)).src; swapContent(src, _msg);
   1.183          }
   1.184  
   1.185 -        ::message *_dst = nullptr;
   1.186 -        PEP_STATUS status = ::encrypt_message(session, src, extra, &_dst,
   1.187 -                enc_format, flags);
   1.188 +        ::message *_dst = nullptr; PEP_STATUS status =
   1.189 +            ::encrypt_message(session, src, extra, &_dst, enc_format, flags);
   1.190          *dst = empty_message_copy(_dst);
   1.191  
   1.192          {
   1.193              std::lock_guard<std::mutex> l(_mtx);
   1.194              swapContent(_msg, src);
   1.195 -            ::free_message(message_cache._cache.at(src->id).dst);
   1.196 -            message_cache._cache.at(src->id).dst = _dst;
   1.197 +            ::free_message(message_cache._cache.at(cacheID(src)).dst);
   1.198 +            message_cache._cache.at(cacheID(src)).dst = _dst;
   1.199          }
   1.200  
   1.201          return status;
   1.202 @@ -441,21 +502,20 @@
   1.203      {
   1.204          ::message *_msg;
   1.205          {
   1.206 -            std::lock_guard<std::mutex> l(_mtx);
   1.207 -            _msg = message_cache._cache.at(src->id).src;
   1.208 -            swapContent(src, _msg);
   1.209 +            std::lock_guard<std::mutex> l(_mtx); _msg =
   1.210 +                message_cache._cache.at(cacheID(src)).src; swapContent(src,
   1.211 +                        _msg);
   1.212          }
   1.213  
   1.214 -        ::message *_dst = nullptr;
   1.215 -        PEP_STATUS status = ::encrypt_message_for_self(session, target_id, src,
   1.216 -                extra, &_dst, enc_format, flags);
   1.217 -        *dst = empty_message_copy(_dst);
   1.218 +        ::message *_dst = nullptr; PEP_STATUS status =
   1.219 +            ::encrypt_message_for_self(session, target_id, src, extra, &_dst,
   1.220 +                    enc_format, flags); *dst = empty_message_copy(_dst);
   1.221  
   1.222          {
   1.223              std::lock_guard<std::mutex> l(_mtx);
   1.224              swapContent(_msg, src);
   1.225 -            ::free_message(message_cache._cache.at(src->id).dst);
   1.226 -            message_cache._cache.at(src->id).dst = _dst;
   1.227 +            ::free_message(message_cache._cache.at(cacheID(src)).dst);
   1.228 +            message_cache._cache.at(cacheID(src)).dst = _dst;
   1.229          }
   1.230  
   1.231          return status;
     2.1 --- a/message_cache.hh	Sun Sep 06 23:42:47 2020 +0200
     2.2 +++ b/message_cache.hh	Thu Sep 24 13:41:49 2020 +0200
     2.3 @@ -72,7 +72,7 @@
     2.4              );
     2.5  
     2.6  
     2.7 -        static PEP_STATUS cache_release(const char *id);
     2.8 +        static PEP_STATUS cache_release(std::string id);
     2.9  
    2.10      protected:
    2.11          void release(std::string id);
    2.12 @@ -120,7 +120,9 @@
    2.13                  PEP_encrypt_flags_t flags
    2.14              );
    2.15  
    2.16 -        void generateMessageID(::message* msg);
    2.17 +        void generateCacheID(::message* msg);
    2.18 +        static std::string cacheID(const ::message* msg);
    2.19 +        static void removeCacheID(::message* msg);
    2.20      };
    2.21  
    2.22      extern MessageCache message_cache;