Skip to content
Snippets Groups Projects
Commit cfe201f4 authored by Otmane Lahlou's avatar Otmane Lahlou
Browse files

ENH : curlHelper to avoid windows crash when passing a pointer on a file descriptor in libcurl

parent 0a025d46
No related branches found
No related tags found
No related merge requests found
...@@ -35,13 +35,9 @@ int CurlHelper::TestUrlAvailability(const std::string& url) const ...@@ -35,13 +35,9 @@ int CurlHelper::TestUrlAvailability(const std::string& url) const
CURLcode res = CURL_LAST; CURLcode res = CURL_LAST;
curl = curl_easy_init(); curl = curl_easy_init();
// Set up the browser
std::string browser =
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11";
if (curl) if (curl)
{ {
curl_easy_setopt(curl, CURLOPT_USERAGENT, browser.data()); curl_easy_setopt(curl, CURLOPT_USERAGENT, m_Browser.data());
curl_easy_setopt(curl, CURLOPT_URL, url.data()); curl_easy_setopt(curl, CURLOPT_URL, url.data());
// Set the dummy write function // Set the dummy write function
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Self::curlDummyWriteFunction); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Self::curlDummyWriteFunction);
...@@ -69,26 +65,32 @@ int CurlHelper::RetrieveFile(const std::string& urlString, std::string filename) ...@@ -69,26 +65,32 @@ int CurlHelper::RetrieveFile(const std::string& urlString, std::string filename)
CURLcode res = CURL_LAST; CURLcode res = CURL_LAST;
FILE* output_file = fopen(filename.c_str(), "w"); FILE* output_file = fopen(filename.c_str(), "w");
curl = curl_easy_init(); curl = curl_easy_init();
char url[256]; char url[256];
strcpy(url, urlString.data()); strcpy(url, urlString.c_str());
// std::cout << url << std::endl;
if (curl) if (curl)
{ {
std::vector<char> chunk;
curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_URL, url);
// Set 5s timeout // Set 5s timeout
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, output_file); // Use our writing static function to avoid file descriptor
// pointer crash on windows
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Self::write_data);
// Say the file where to write the received data
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)output_file);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
fclose(output_file);
/* always cleanup */ /* always cleanup */
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
fclose(output_file);
} }
return res; return res;
#else #else
...@@ -105,7 +107,7 @@ int CurlHelper::RetrieveFileMulti(const std::vector<std::string>& listURLs, ...@@ -105,7 +107,7 @@ int CurlHelper::RetrieveFileMulti(const std::vector<std::string>& listURLs,
#ifdef OTB_USE_CURL #ifdef OTB_USE_CURL
#ifdef OTB_CURL_MULTI_AVAILABLE #ifdef OTB_CURL_MULTI_AVAILABLE
otbMsgDevMacro(<< "Using curl multi"); otbMsgDevMacro(<< "Using curl multi");
std::string m_Browser = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"; //std::string m_Browser = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11";
CURLM * multiHandle; CURLM * multiHandle;
std::vector<CURL *> listCurlHandles; std::vector<CURL *> listCurlHandles;
...@@ -155,7 +157,8 @@ int CurlHelper::RetrieveFileMulti(const std::vector<std::string>& listURLs, ...@@ -155,7 +157,8 @@ int CurlHelper::RetrieveFileMulti(const std::vector<std::string>& listURLs,
// Param easy handle // Param easy handle
curl_easy_setopt(lEasyHandle, CURLOPT_USERAGENT, m_Browser.data()); curl_easy_setopt(lEasyHandle, CURLOPT_USERAGENT, m_Browser.data());
curl_easy_setopt(lEasyHandle, CURLOPT_URL, (*url).data()); curl_easy_setopt(lEasyHandle, CURLOPT_URL, (*url).data());
curl_easy_setopt(lEasyHandle, CURLOPT_WRITEDATA, *file); curl_easy_setopt(lEasyHandle, CURLOPT_WRITEFUNCTION, &Self::write_data);
curl_easy_setopt(lEasyHandle, CURLOPT_WRITEDATA, (void*)(*file));
// Add easy handle to multi handle // Add easy handle to multi handle
curl_multi_add_handle(multiHandle, lEasyHandle); curl_multi_add_handle(multiHandle, lEasyHandle);
...@@ -166,7 +169,7 @@ int CurlHelper::RetrieveFileMulti(const std::vector<std::string>& listURLs, ...@@ -166,7 +169,7 @@ int CurlHelper::RetrieveFileMulti(const std::vector<std::string>& listURLs,
++file; ++file;
} }
//fetch tiles //fetch tiles
// Configure multi handle - set the maximum connections // Configure multi handle - set the maximum connections
curl_multi_setopt(multiHandle, CURLMOPT_MAXCONNECTS, maxConnect); curl_multi_setopt(multiHandle, CURLMOPT_MAXCONNECTS, maxConnect);
curl_multi_setopt(multiHandle, CURLMOPT_PIPELINING, 0); curl_multi_setopt(multiHandle, CURLMOPT_PIPELINING, 0);
......
...@@ -49,7 +49,10 @@ public: ...@@ -49,7 +49,10 @@ public:
const std::vector<std::string>& listFiles, const std::vector<std::string>& listFiles,
int maxConnect) const; int maxConnect) const;
protected: protected:
CurlHelper() {} CurlHelper()
{
m_Browser = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11";
}
~CurlHelper() {} ~CurlHelper() {}
...@@ -61,7 +64,23 @@ private: ...@@ -61,7 +64,23 @@ private:
{ {
return nmemb; return nmemb;
} }
// Need to use our writing function to handle windows segfaults
// Need to be static cause the CURL_OPT is excpecting a pure C
// function or a static c++ method.
static size_t write_data(void* ptr, size_t size, size_t nmemb, void* data)
{
size_t written;
FILE * fDescriptor = (FILE *)(data);
written = fwrite(ptr,size,nmemb,fDescriptor);
return written;
}
// Browser Agent used
std::string m_Browser;
}; };
} }
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment