mardi 24 décembre 2019

using custom deleter (curl_formfree()) in unique_ptr<>

I have the below code to post a file using libcurl.

bool FileUploadDownload::upload(const std::string& filename, const std::string& url) {

    CURLcode res;
    CURLFORMcode form_res;
    curl_off_t speed_upload = 0;
    struct curl_httppost* form_post = nullptr;
    struct curl_httppost* last_ptr = nullptr;
    struct curl_slist* header_list = nullptr;
    static const char buf[] = "Expect: 100-continue";

    std::string pure_fname = filename.substr(filename.find_last_of("/\\") + 1);

    res = curl_global_init(CURL_GLOBAL_ALL);
    if (res != CURLE_OK) {
        m_logger->errorf("curl_global_init(CURL_GLOBAL_ALL) failed: %s", curl_easy_strerror(res));
        return false;
    }

    form_res = curl_formadd(&form_post,
                            &last_ptr,
                            CURLFORM_COPYNAME, "file",
                            CURLFORM_FILENAME, pure_fname.c_str(),
                            CURLFORM_FILE, filename.c_str(),
                            CURLFORM_CONTENTTYPE, "text/plain",
                            CURLFORM_END);
    if (form_res != 0) {
        m_logger->errorf("curl_formadd() failed: %s", curl_easy_strerror((CURLcode)form_res));
        curl_formfree(form_post);
        return false;
    }

    form_res = curl_formadd(&form_post,
                            &last_ptr,
                            CURLFORM_COPYNAME, "submit",
                            CURLFORM_COPYCONTENTS, "send",
                            CURLFORM_END);
    if (form_res != 0) {
        m_logger->errorf("curl_formadd() failed: %s", curl_easy_strerror((CURLcode)form_res));
        curl_formfree(form_post);
        return false;
    }

    if (m_curl) {

        header_list = curl_slist_append(header_list, buf);
        if(nullptr == header_list){
            return false;
        }

        res = curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_setopt(CURLOPT_URL) failed: %s", curl_easy_strerror(res));
            curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, header_list);
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_setopt(CURLOPT_HTTPHEADER) failed: %s", curl_easy_strerror(res));
            curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, form_post);
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_setopt(CURLOPT_HTTPPOST) failed: %s", curl_easy_strerror(res));
            curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_setopt(CURLOPT_VERBOSE) failed: %s", curl_easy_strerror(res));
            curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_perform(m_curl);
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_perform() failed: %s", curl_easy_strerror(res));
            curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_getinfo(m_curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_getinfo(CURLINFO_SPEED_UPLOAD) failed: %s", curl_easy_strerror(res));
            curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }
        m_logger->infof("Average upload speed: %" CURL_FORMAT_CURL_OFF_T
                            " megabyte/sec.\n", speed_upload / (1024 * 1024));
        curl_formfree(form_post);
        curl_slist_free_all(header_list);
        return true;
    }
    return false;
}

There are a lot of stituations where the above function fails and returns false. In all those cases, I need to call curl_formfree() and curl_slist_free_all().

I want to optimize this using std::unique_ptr<>

I have re-written the above function as below.

bool FileUploadDownload::upload(const std::string& filename, const std::string& url) {

    CURLcode res;
    CURLFORMcode form_res;
    curl_off_t speed_upload = 0;
    auto form_delete = [&](curl_httppost * p) {
        curl_formfree(p);
    };
    //struct curl_httppost* form_post = nullptr;
    //struct curl_httppost* last_ptr = nullptr;
    std::unique_ptr<curl_httppost*, decltype(form_delete)> form_post;
    std::unique_ptr<curl_httppost*, decltype(form_delete)> last_ptr;

    struct curl_slist* header_list = nullptr;
    static const char buf[] = "Expect: 100-continue";

    std::string pure_fname = filename.substr(filename.find_last_of("/\\") + 1);

    res = curl_global_init(CURL_GLOBAL_ALL);
    if (res != CURLE_OK) {
        m_logger->errorf("curl_global_init(CURL_GLOBAL_ALL) failed: %s", curl_easy_strerror(res));
        return false;
    }

    form_res = curl_formadd(form_post.get(),
                            last_ptr.get(),
                            CURLFORM_COPYNAME, "file",
                            CURLFORM_FILENAME, pure_fname.c_str(),
                            CURLFORM_FILE, filename.c_str(),
                            CURLFORM_CONTENTTYPE, "text/plain",
                            CURLFORM_END);
    if (form_res != 0) {
        m_logger->errorf("curl_formadd() failed: %s", curl_easy_strerror((CURLcode)form_res));
        //curl_formfree(form_post);
        return false;
    }

    form_res = curl_formadd(form_post.get(),
                            last_ptr.get(),
                            CURLFORM_COPYNAME, "submit",
                            CURLFORM_COPYCONTENTS, "send",
                            CURLFORM_END);
    if (form_res != 0) {
        m_logger->errorf("curl_formadd() failed: %s", curl_easy_strerror((CURLcode)form_res));
        //curl_formfree(form_post);
        return false;
    }

    if (m_curl) {

        header_list = curl_slist_append(header_list, buf);
        if(nullptr == header_list){
            return false;
        }

        res = curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_setopt(CURLOPT_URL) failed: %s", curl_easy_strerror(res));
            //curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, header_list);
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_setopt(CURLOPT_HTTPHEADER) failed: %s", curl_easy_strerror(res));
            //curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, form_post.get());
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_setopt(CURLOPT_HTTPPOST) failed: %s", curl_easy_strerror(res));
            //curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_setopt(CURLOPT_VERBOSE) failed: %s", curl_easy_strerror(res));
            //curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_perform(m_curl);
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_perform() failed: %s", curl_easy_strerror(res));
            //curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }

        res = curl_easy_getinfo(m_curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
        if (res != CURLE_OK) {
            m_logger->errorf("curl_easy_getinfo(CURLINFO_SPEED_UPLOAD) failed: %s", curl_easy_strerror(res));
            //curl_formfree(form_post);
            curl_slist_free_all(header_list);
            return false;
        }
        m_logger->infof("Average upload speed: %" CURL_FORMAT_CURL_OFF_T
                            " megabyte/sec.\n", speed_upload / (1024 * 1024));
        //curl_formfree(form_post);
        curl_slist_free_all(header_list);
        return true;
    }
    return false;
}

I am getting the following compilation error.

error: no match for call to ‘(std::unique_ptr<curl_httppost*, FileUploadDownload::upload(const string&, const string&)::<lambda(curl_httppost*)> >::deleter_type {aka FileUploadDownload::upload(const string&, const string&)::<lambda(curl_httppost*)>}) (curl_httppost**&)’
    get_deleter()(__ptr);

Aucun commentaire:

Enregistrer un commentaire