Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
otb
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Youssefi
otb
Commits
041827bf
Commit
041827bf
authored
13 years ago
by
Otmane Lahlou
Browse files
Options
Downloads
Patches
Plain Diff
ENH : Replace Macro with a class to handle the eventual curl errors
parent
570d8f97
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Code/UtilitiesAdapters/CurlAdapters/otbCurlHelper.cxx
+52
-59
52 additions, 59 deletions
Code/UtilitiesAdapters/CurlAdapters/otbCurlHelper.cxx
with
52 additions
and
59 deletions
Code/UtilitiesAdapters/CurlAdapters/otbCurlHelper.cxx
+
52
−
59
View file @
041827bf
...
...
@@ -30,42 +30,35 @@
#include
<cstdio>
#include
<itkLightObject.h>
/**
* Macro to handle the curl code errors, and return a string
* describing the code error
*/
#define otbCurlCall(command) \
{ \
{ \
CURLcode curlCode; \
curlCode = command; \
if(curlCode != CURLE_OK) \
{ \
itkExceptionMacro(<<" Curl Error : "<< curl_easy_strerror(curlCode)) \
} \
} \
}
namespace
otb
{
#ifdef OTB_USE_CURL
/**
* Macro to handle the multi curl code errors, and return a string
* describing the code error
* Class to handle the CURLcode and CURLMCode and throw exceptions when needed
*/
#define otbCurlMultiCall(command) \
{ \
{ \
CURLMcode curlmultiCode; \
curlmultiCode = command; \
if(curlmultiCode != CURLM_OK) \
{ \
itkExceptionMacro(<<" CurlM Error : "<< curl_multi_strerror(curlmultiCode))\
} \
} \
}
class
CurlHandleError
{
namespace
otb
{
public:
/** Processing CURLcode */
static
void
ProcessCURLcode
(
CURLcode
curlCode
)
{
if
(
curlCode
!=
CURLE_OK
)
{
itkGenericExceptionMacro
(
<<
" Curl Error : "
<<
curl_easy_strerror
(
curlCode
));
}
}
#ifdef OTB_USE_CURL
/** Processing CURLMcode */
static
void
ProcessCURLcode
(
CURLMcode
curlMCode
)
{
if
(
curlMCode
!=
CURLM_OK
)
{
itkGenericExceptionMacro
(
<<
" CurlM Error : "
<<
curl_multi_strerror
(
curlMCode
));
}
}
};
/**
* Resource class that create and clean the curl environment proprely
...
...
@@ -213,13 +206,13 @@ bool CurlHelper::TestUrlAvailability(const std::string& url) const
#ifdef OTB_USE_CURL
// Set up a curl resource
CurlResource
::
Pointer
curlResource
=
CurlResource
::
New
();
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_USERAGENT
,
m_Browser
.
data
()));
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_URL
,
url
.
data
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_USERAGENT
,
m_Browser
.
data
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_URL
,
url
.
data
()));
// Set the dummy write function
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
&
Self
::
CallbackWriteDataDummy
));
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_MAXFILESIZE
,
1
));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_MAXFILESIZE
,
1
));
// Perform requet
CURLcode
easyPerformResult
=
curl_easy_perform
(
curlResource
->
GetCurlResource
());
...
...
@@ -251,13 +244,13 @@ bool CurlHelper::IsCurlReturnHttpError(const std::string& url) const
// Set up a curl resource
CurlResource
::
Pointer
curlResource
=
CurlResource
::
New
();
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_USERAGENT
,
m_Browser
.
data
()));
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_URL
,
url
.
data
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_USERAGENT
,
m_Browser
.
data
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_URL
,
url
.
data
()));
// Set the dummy write function
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
&
Self
::
CallbackWriteDataDummy
));
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_MAXFILESIZE
,
1
));
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_FAILONERROR
,
1
));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_MAXFILESIZE
,
1
));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_FAILONERROR
,
1
));
// Perform requet
CURLcode
easyPerformResult
=
curl_easy_perform
(
curlResource
->
GetCurlResource
());
...
...
@@ -280,22 +273,22 @@ int CurlHelper::RetrieveUrlInMemory(const std::string& url, std::string& output)
CURLcode
res
=
CURLE_OK
;
CurlResource
::
Pointer
curlResource
=
CurlResource
::
New
();
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_URL
,
url
.
c_str
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_URL
,
url
.
c_str
()));
// Set 5s timeout
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_TIMEOUT
,
10
));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_TIMEOUT
,
10
));
// Use our writing static function to avoid file descriptor
// pointer crash on windows
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
&
Self
::
CallbackWriteDataToStringStream
));
// Say the file where to write the received data
std
::
ostringstream
*
outputStream
=
new
std
::
ostringstream
;
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEDATA
,
(
void
*
)
outputStream
));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEDATA
,
(
void
*
)
outputStream
));
// Perform request
otb
Curl
Call
(
curl_easy_perform
(
curlResource
->
GetCurlResource
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_perform
(
curlResource
->
GetCurlResource
()));
// Save output
output
=
outputStream
->
str
();
...
...
@@ -332,20 +325,20 @@ int CurlHelper::RetrieveFile(const std::string& urlString, std::string filename)
char
url
[
256
];
strcpy
(
url
,
urlString
.
c_str
());
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_URL
,
url
));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_URL
,
url
));
// Set 10s timeout
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_TIMEOUT
,
10
));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_TIMEOUT
,
10
));
// Use our writing static function to avoid file descriptor
// pointer crash on windows
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
&
Self
::
CallbackWriteDataToFile
));
// Say the file where to write the received data
otb
Curl
Call
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEDATA
,
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
curlResource
->
GetCurlResource
(),
CURLOPT_WRITEDATA
,
(
void
*
)
output_file
->
GetFileResource
()));
otb
Curl
Call
(
curl_easy_perform
(
curlResource
->
GetCurlResource
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_perform
(
curlResource
->
GetCurlResource
()));
otbMsgDevMacro
(
<<
" -> "
<<
res
);
return
res
;
...
...
@@ -393,15 +386,15 @@ int CurlHelper::RetrieveFileMulti(const std::vector<std::string>& listURLs,
CurlResource
::
Pointer
lEasyHandle
=
CurlResource
::
New
();
// Param easy handle
otb
Curl
Call
(
curl_easy_setopt
(
lEasyHandle
->
GetCurlResource
(),
CURLOPT_USERAGENT
,
m_Browser
.
data
()));
otb
Curl
Call
(
curl_easy_setopt
(
lEasyHandle
->
GetCurlResource
(),
CURLOPT_URL
,
(
*
url
).
data
()));
otb
Curl
Call
(
curl_easy_setopt
(
lEasyHandle
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
lEasyHandle
->
GetCurlResource
(),
CURLOPT_USERAGENT
,
m_Browser
.
data
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
lEasyHandle
->
GetCurlResource
(),
CURLOPT_URL
,
(
*
url
).
data
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
lEasyHandle
->
GetCurlResource
(),
CURLOPT_WRITEFUNCTION
,
&
Self
::
CallbackWriteDataToFile
));
otb
Curl
Call
(
curl_easy_setopt
(
lEasyHandle
->
GetCurlResource
(),
CURLOPT_WRITEDATA
,
Curl
HandleError
::
ProcessCURLcode
(
curl_easy_setopt
(
lEasyHandle
->
GetCurlResource
(),
CURLOPT_WRITEDATA
,
(
void
*
)
(
*
file
)
->
GetFileResource
()));
// Add easy handle to multi handle
otb
Curl
MultiCall
(
curl_multi_add_handle
(
multiHandle
->
GetCurlMultiResource
(),
lEasyHandle
->
GetCurlResource
()));
Curl
HandleError
::
ProcessCURLcode
(
curl_multi_add_handle
(
multiHandle
->
GetCurlMultiResource
(),
lEasyHandle
->
GetCurlResource
()));
// Add hanle to vector
listCurlHandles
.
push_back
(
lEasyHandle
);
...
...
@@ -411,8 +404,8 @@ int CurlHelper::RetrieveFileMulti(const std::vector<std::string>& listURLs,
//fetch tiles
// Configure multi handle - set the maximum connections
otb
Curl
MultiCall
(
curl_multi_setopt
(
multiHandle
->
GetCurlMultiResource
(),
CURLMOPT_MAXCONNECTS
,
maxConnect
));
otb
Curl
MultiCall
(
curl_multi_setopt
(
multiHandle
->
GetCurlMultiResource
(),
CURLMOPT_PIPELINING
,
0
));
Curl
HandleError
::
ProcessCURLcode
(
curl_multi_setopt
(
multiHandle
->
GetCurlMultiResource
(),
CURLMOPT_MAXCONNECTS
,
maxConnect
));
Curl
HandleError
::
ProcessCURLcode
(
curl_multi_setopt
(
multiHandle
->
GetCurlMultiResource
(),
CURLMOPT_PIPELINING
,
0
));
// Perform
int
lStillRunning
;
...
...
@@ -439,7 +432,7 @@ int CurlHelper::RetrieveFileMulti(const std::vector<std::string>& listURLs,
timeout
.
tv_usec
=
1
;
/* get file descriptors from the transfers */
otb
Curl
MultiCall
(
curl_multi_fdset
(
multiHandle
->
GetCurlMultiResource
(),
&
fdread
,
&
fdwrite
,
&
fdexcep
,
&
maxfd
));
Curl
HandleError
::
ProcessCURLcode
(
curl_multi_fdset
(
multiHandle
->
GetCurlMultiResource
(),
&
fdread
,
&
fdwrite
,
&
fdexcep
,
&
maxfd
));
rc
=
select
(
maxfd
+
1
,
&
fdread
,
&
fdwrite
,
&
fdexcep
,
&
timeout
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment