netpgp : curl : shorter curl easy context life time, different for send and recv
authorEdouard Tisserant
Wed, 24 Jun 2015 16:42:31 +0200
changeset 322978c746d7d2c
parent 321 814faa0be78b
child 323 f2d4f0173818
netpgp : curl : shorter curl easy context life time, different for send and recv
src/pgp_netpgp.c
src/pgp_netpgp_internal.h
test/pEpEngineTest.cc
     1.1 --- a/src/pgp_netpgp.c	Wed Jun 24 15:10:46 2015 +0200
     1.2 +++ b/src/pgp_netpgp.c	Wed Jun 24 16:42:31 2015 +0200
     1.3 @@ -85,7 +85,6 @@
     1.4  }
     1.5  
     1.6  static PEP_STATUS init_curl(
     1.7 -    CURL **curl,
     1.8      pthread_mutex_t *curl_mutex,
     1.9      bool in_first)
    1.10  {
    1.11 @@ -104,6 +103,33 @@
    1.12          curl_global_init(CURL_GLOBAL_DEFAULT);
    1.13      }
    1.14  
    1.15 +    pthread_mutex_unlock(curl_mutex);
    1.16 +    return status;
    1.17 +}
    1.18 +
    1.19 +static void release_curl(
    1.20 +    pthread_mutex_t *curl_mutex, 
    1.21 +    bool out_last)
    1.22 +{
    1.23 +    if(pthread_mutex_lock(curl_mutex)){
    1.24 +        return;
    1.25 +    }
    1.26 +
    1.27 +    if(out_last){
    1.28 +        curl_global_cleanup();
    1.29 +    }
    1.30 +
    1.31 +    pthread_mutex_destroy(curl_mutex);
    1.32 +
    1.33 +    return;
    1.34 +}
    1.35 +
    1.36 +static PEP_STATUS curl_get_ctx(
    1.37 +    CURL **curl)
    1.38 +{
    1.39 +    PEP_STATUS status = PEP_STATUS_OK;
    1.40 +    struct curl_slist *headers=NULL;
    1.41 +
    1.42      if ((*curl = curl_easy_init()) == NULL) {
    1.43          return PEP_OUT_OF_MEMORY;
    1.44      }
    1.45 @@ -117,40 +143,24 @@
    1.46  
    1.47      if(!headers)
    1.48      {
    1.49 -        status = PEP_OUT_OF_MEMORY;
    1.50 -        goto unlock_curl;
    1.51 +        return PEP_OUT_OF_MEMORY;
    1.52      }
    1.53  
    1.54      curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);
    1.55      curl_slist_free_all(headers);
    1.56  
    1.57      // TODO curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
    1.58 -
    1.59 -unlock_curl:
    1.60 -    pthread_mutex_unlock(curl_mutex);
    1.61      return status;
    1.62  }
    1.63  
    1.64 -static void release_curl(
    1.65 -    CURL **curl,
    1.66 -    pthread_mutex_t *curl_mutex, 
    1.67 -    bool out_last)
    1.68 +static void curl_release_ctx(
    1.69 +    CURL **curl)
    1.70  {
    1.71 -    if(pthread_mutex_lock(curl_mutex)){
    1.72 -        return;
    1.73 -    }
    1.74 -
    1.75      if(*curl)
    1.76          curl_easy_cleanup(*curl);
    1.77  
    1.78      *curl = NULL;
    1.79  
    1.80 -    if(out_last){
    1.81 -        curl_global_cleanup();
    1.82 -    }
    1.83 -
    1.84 -    pthread_mutex_destroy(curl_mutex);
    1.85 -
    1.86      return;
    1.87  }
    1.88  
    1.89 @@ -167,7 +177,6 @@
    1.90      }
    1.91  
    1.92      if((status = init_curl(
    1.93 -                    &session->ctx.curl,
    1.94                      &session->ctx.curl_mutex,
    1.95                      in_first) != PEP_STATUS_OK)){
    1.96          if(in_first) release_netpgp();
    1.97 @@ -185,7 +194,7 @@
    1.98      if (out_last){
    1.99          release_netpgp();
   1.100      }
   1.101 -    release_curl(&session->ctx.curl, &session->ctx.curl_mutex, out_last);
   1.102 +    release_curl(&session->ctx.curl_mutex, out_last);
   1.103  }
   1.104  
   1.105  // return 1 if the file contains ascii-armoured text 
   1.106 @@ -1050,12 +1059,15 @@
   1.107          return PEP_UNKNOWN_ERROR;
   1.108      }
   1.109  
   1.110 -    curl = session->ctx.curl;
   1.111 +    result = curl_get_ctx(&curl);
   1.112 +    if(result != PEP_STATUS_OK){
   1.113 +        goto unlock_curl;
   1.114 +    }
   1.115  
   1.116      encoded_pattern = curl_easy_escape(curl, (char*)pattern, 0);
   1.117      if(!encoded_pattern){
   1.118          result = PEP_OUT_OF_MEMORY;
   1.119 -        goto unlock_curl;
   1.120 +        goto release_curl_ctx;
   1.121      }
   1.122  
   1.123      if((request = malloc(strlen(ks_cmd) + strlen(encoded_pattern) + 1))==NULL){
   1.124 @@ -1095,6 +1107,8 @@
   1.125      free(request);
   1.126  free_encoded_pattern:
   1.127      curl_free(encoded_pattern);
   1.128 +release_curl_ctx:
   1.129 +    curl_release_ctx(&curl);
   1.130  unlock_curl:
   1.131      pthread_mutex_unlock(&session->ctx.curl_mutex);
   1.132  
   1.133 @@ -1274,6 +1288,8 @@
   1.134  
   1.135      PEP_STATUS result;
   1.136  
   1.137 +    CURL *curl;
   1.138 +
   1.139      assert(session);
   1.140      assert(pattern);
   1.141  
   1.142 @@ -1287,23 +1303,30 @@
   1.143      }
   1.144  
   1.145      if(pthread_mutex_lock(&netpgp_mutex)){
   1.146 -        return PEP_UNKNOWN_ERROR;
   1.147 +        result = PEP_UNKNOWN_ERROR;
   1.148 +        goto free_encoded_keys;
   1.149      }
   1.150  
   1.151      result = find_keys_do(pattern, &send_key_cb, (void*)encoded_keys);
   1.152  
   1.153 -unlock_netpgp:
   1.154      pthread_mutex_unlock(&netpgp_mutex);
   1.155  
   1.156 +    if(result != PEP_STATUS_OK){
   1.157 +        goto free_encoded_keys;
   1.158 +    }
   1.159 +
   1.160      if(pthread_mutex_lock(&session->ctx.curl_mutex)){
   1.161 -        return PEP_UNKNOWN_ERROR;
   1.162 +        result = PEP_UNKNOWN_ERROR;
   1.163 +        goto free_encoded_keys;
   1.164 +    }
   1.165 +
   1.166 +    result = curl_get_ctx(&curl);
   1.167 +    if(result != PEP_STATUS_OK){
   1.168 +        goto unlock_curl;
   1.169      }
   1.170  
   1.171      if(result == PEP_STATUS_OK){
   1.172          CURLcode curlres;
   1.173 -        CURL *curl;
   1.174 -
   1.175 -        curl = session->ctx.curl;
   1.176  
   1.177          for (post = encoded_keys; post != NULL; post = post->next) {
   1.178              assert(post->value);
   1.179 @@ -1321,17 +1344,18 @@
   1.180              if(curlres != CURLE_OK) {
   1.181  
   1.182                  result = PEP_CANNOT_SEND_KEY;
   1.183 -                goto free_encoded_keys;
   1.184 +                goto release_curl_ctx;
   1.185              }
   1.186          }
   1.187      }
   1.188  
   1.189 +release_curl_ctx:
   1.190 +    curl_release_ctx(&curl);
   1.191 +unlock_curl:
   1.192 +    pthread_mutex_unlock(&session->ctx.curl_mutex);
   1.193  free_encoded_keys:
   1.194      free_stringlist(encoded_keys);
   1.195  
   1.196 -unlock_curl:
   1.197 -    pthread_mutex_unlock(&session->ctx.curl_mutex);
   1.198 -
   1.199      return result;
   1.200  }
   1.201  
     2.1 --- a/src/pgp_netpgp_internal.h	Wed Jun 24 15:10:46 2015 +0200
     2.2 +++ b/src/pgp_netpgp_internal.h	Wed Jun 24 16:42:31 2015 +0200
     2.3 @@ -4,6 +4,5 @@
     2.4  #include <pthread.h>
     2.5  
     2.6  typedef struct _pEpNetPGPSession {
     2.7 -    CURL *curl;
     2.8      pthread_mutex_t curl_mutex;
     2.9  } pEpNetPGPSession;
     3.1 --- a/test/pEpEngineTest.cc	Wed Jun 24 15:10:46 2015 +0200
     3.2 +++ b/test/pEpEngineTest.cc	Wed Jun 24 16:42:31 2015 +0200
     3.3 @@ -271,6 +271,7 @@
     3.4  
     3.5      cout << "sending vb@ulm.ccc.de to keyserver\n";
     3.6      PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
     3.7 +    cout << "send_key() exits with " << send_key_status << "\n";
     3.8      assert(send_key_status == PEP_STATUS_OK);
     3.9  
    3.10      PEP_comm_type tcomm_type;