2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) 2017 - ESI-Group - Cedric Delamarre
6 * This file is hereby licensed under the terms of the GNU GPL v2.0,
7 * pursuant to article 5.3.4 of the CeCILL v.2.1.
8 * This file was originally licensed under the terms of the CeCILL v2.1,
9 * and continues to be available under such terms.
10 * For more information, see the COPYING file which you should have received
11 * along with this program.
15 #include "sciCurl.hxx"
21 #include "getScilabPreference.h"
22 #include "freeArrayOfString.h"
27 SciCurl* SciCurl::me = nullptr;
28 std::string SciCurl::data;
29 bool SciCurl::useFile = false;
31 SciCurl* SciCurl::getInstance(void)
41 void SciCurl::destroyInstance(void)
52 curl_global_init(CURL_GLOBAL_ALL);
57 curl_global_cleanup();
60 void SciCurl::getResultAsObject(CURL* curl)
62 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, SciCurl::write_result);
66 void SciCurl::getResultAsFile(CURL* curl, FILE* fd)
69 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, SciCurl::write_result);
70 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fd);
73 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
74 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fd);
78 types::InternalType* SciCurl::getResult()
80 types::InternalType* res = fromJSON(data);
83 res = new types::String(data.c_str());
96 int SciCurl::write_result(char* pcInput, size_t size, size_t nmemb, void* output)
101 FILE* fd = (FILE*)output;
102 fwrite(pcInput, size, nmemb, fd);
103 return static_cast<int>(size*nmemb);
107 std::string d(pcInput, size * nmemb);
109 return static_cast<int>(size*nmemb);
112 // Proxy is configured in scilab preferences (internet tab)
113 int SciCurl::setProxy(CURL* curl)
115 char* proxyUserPwd = NULL;
116 const char* attrs[] = {"enabled", "host", "port", "user", "password"};
117 const unsigned int N = sizeof(attrs) / sizeof(char*);
118 char** values = getPrefAttributesValues("//web/body/proxy", attrs, N);
122 // no proxy configured
126 // proxy is configured and not enabled
127 if (stricmp(values[0]/*enabled*/, "false") == 0)
129 freeArrayOfString(values, N);
133 const unsigned int host_len = (const unsigned int)strlen(values[1]);
134 const unsigned int port_len = (const unsigned int)strlen(values[2]);
135 const unsigned int user_len = (const unsigned int)strlen(values[3]);
136 const unsigned int pwd_len = (const unsigned int)strlen(values[4]);
138 if(host_len == 0 || port_len == 0 || user_len == 0)
140 freeArrayOfString(values, N);
146 proxyUserPwd = values[3]; //user
150 proxyUserPwd = (char *)MALLOC((user_len + 1 + pwd_len + 1) * sizeof(char));
151 sprintf(proxyUserPwd, "%s:%s", values[3]/*user*/, values[4]/*password*/);
152 proxyUserPwd[user_len + 1 + pwd_len] = '\0';
156 if(curl_easy_setopt(curl, CURLOPT_PROXY, values[1]) != CURLE_OK) //host
159 freeArrayOfString(values, N);
163 if(curl_easy_setopt(curl, CURLOPT_PROXYPORT, strtol(values[2], NULL, 10)) != CURLE_OK) //port
166 freeArrayOfString(values, N);
170 if(curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxyUserPwd) != CURLE_OK) //port
173 freeArrayOfString(values, N);
178 freeArrayOfString(values, N);
182 void SciCurl::setCommonHeaders(CURL* curl)
184 char* OperatingSystem = getOSFullName();
185 char* Release = getOSRelease();
188 std::string pcUserAgent = "Scilab/" + std::to_string(SCI_VERSION_MAJOR)+"."+ std::to_string(SCI_VERSION_MINOR)+"."+ std::to_string(SCI_VERSION_MAINTENANCE);
190 pcUserAgent += " (" + std::string(OperatingSystem) + " " + std::string(Release) + ")";
191 // set user agent header
192 curl_easy_setopt(curl, CURLOPT_USERAGENT, pcUserAgent.data());
194 FREE(OperatingSystem);