OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ossimCurlHttpRequest Class Reference

#include <ossimCurlHttpRequest.h>

Inheritance diagram for ossimCurlHttpRequest:
ossimHttpRequest ossimWebRequest ossimObject ossimReferenced

Public Member Functions

 ossimCurlHttpRequest ()
 
virtual ~ossimCurlHttpRequest ()
 
virtual ossimRefPtr< ossimWebResponsegetResponse ()
 
virtual bool supportsProtocol (const ossimString &protocol) const
 
ossim_int64 getContentLength () const
 
virtual bool loadState (const ossimKeywordlist &kwl, const char *prefix=0)
 
- Public Member Functions inherited from ossimHttpRequest
 ossimHttpRequest ()
 
bool set (const ossimUrl &url, const ossimKeywordlist &headerOptions, HttpMethodType methodType=HTTP_METHOD_GET)
 
virtual ~ossimHttpRequest ()
 
void setHeaderOptions (const ossimKeywordlist &options)
 
void addHeaderOption (const ossimString &name, const ossimString &value)
 
void clearHeaderOptions ()
 
ossimKeywordlistgetHeaderOptions ()
 
const ossimKeywordlistgetHeaderOptions () const
 
virtual ossimString getLastError () const
 
- Public Member Functions inherited from ossimWebRequest
 ossimWebRequest ()
 
 ossimWebRequest (const ossimUrl &url)
 
const ossimUrlgetUrl () const
 
virtual bool setUrl (const ossimUrl &url)
 
void clearLastError ()
 
void clearLastError () const
 
const ossimString getLastError ()
 
- Public Member Functions inherited from ossimObject
 ossimObject ()
 
virtual ~ossimObject ()
 
virtual ossimObjectdup () const
 
virtual ossimString getShortName () const
 
virtual ossimString getLongName () const
 
virtual ossimString getDescription () const
 
virtual ossimString getClassName () const
 
virtual RTTItypeid getType () const
 
virtual bool canCastTo (ossimObject *obj) const
 
virtual bool canCastTo (const RTTItypeid &id) const
 
virtual bool canCastTo (const ossimString &parentClassName) const
 
virtual bool saveState (ossimKeywordlist &kwl, const char *prefix=0) const
 
virtual std::ostream & print (std::ostream &out) const
 Generic print method. More...
 
virtual bool isEqualTo (const ossimObject &obj, ossimCompareType compareType=OSSIM_COMPARE_FULL) const
 
virtual void accept (ossimVisitor &visitor)
 
- Public Member Functions inherited from ossimReferenced
 ossimReferenced ()
 
 ossimReferenced (const ossimReferenced &)
 
ossimReferencedoperator= (const ossimReferenced &)
 
void ref () const
 increment the reference count by one, indicating that this object has another pointer which is referencing it. More...
 
void unref () const
 decrement the reference count by one, indicating that a pointer to this object is referencing it. More...
 
void unref_nodelete () const
 decrement the reference count by one, indicating that a pointer to this object is referencing it. More...
 
int referenceCount () const
 

Static Public Member Functions

static int curlWriteResponseBody (void *buffer, size_t size, size_t nmemb, void *stream)
 
static int curlWriteResponseHeader (void *buffer, size_t size, size_t nmemb, void *stream)
 

Protected Member Functions

void setDefaultSSL (CURL *curl) const
 
- Protected Member Functions inherited from ossimReferenced
virtual ~ossimReferenced ()
 

Protected Attributes

CURL * m_curl
 
ossimRefPtr< ossimCurlHttpResponsem_response
 
- Protected Attributes inherited from ossimHttpRequest
ossimKeywordlist m_headerOptions
 
HttpMethodType m_methodType
 
 TYPE_DATA
 
- Protected Attributes inherited from ossimWebRequest
ossimUrl m_url
 
ossimString m_lastError
 
 TYPE_DATA
 

Additional Inherited Members

- Public Types inherited from ossimHttpRequest
enum  HttpMethodType { HTTP_METHOD_UNKNOWN = 0, HTTP_METHOD_GET = 1, HTTP_METHOD_POST = 2 }
 

Detailed Description

Definition at line 13 of file ossimCurlHttpRequest.h.

Constructor & Destructor Documentation

◆ ossimCurlHttpRequest()

ossimCurlHttpRequest::ossimCurlHttpRequest ( )
inline

Definition at line 16 of file ossimCurlHttpRequest.h.

References m_curl.

17  :m_curl(0)
18  {
19  m_curl = curl_easy_init();
20  }

◆ ~ossimCurlHttpRequest()

virtual ossimCurlHttpRequest::~ossimCurlHttpRequest ( )
inlinevirtual

Definition at line 21 of file ossimCurlHttpRequest.h.

References m_curl.

22  {
23  if(m_curl)
24  {
25  curl_easy_cleanup(m_curl);
26  m_curl = 0;
27  }
28  }

Member Function Documentation

◆ curlWriteResponseBody()

int ossimCurlHttpRequest::curlWriteResponseBody ( void *  buffer,
size_t  size,
size_t  nmemb,
void *  stream 
)
static

Definition at line 96 of file ossimCurlHttpRequest.cpp.

References ossimHttpResponse::bodyStream(), and size.

Referenced by getContentLength(), and getResponse().

97 {
98  ossimHttpResponse* cStream = static_cast<ossimHttpResponse*>(stream);
99 
100  if(cStream)
101  {
102  cStream->bodyStream().write((char*)buffer, nmemb*size);
103  return nmemb*size;
104  }
105 
106  return 0;
107 }
yy_size_t size
std::iostream & bodyStream()

◆ curlWriteResponseHeader()

int ossimCurlHttpRequest::curlWriteResponseHeader ( void *  buffer,
size_t  size,
size_t  nmemb,
void *  stream 
)
static

Definition at line 109 of file ossimCurlHttpRequest.cpp.

References ossimHttpResponse::headerStream(), and size.

Referenced by getContentLength(), and getResponse().

110 {
111  ossimHttpResponse* cStream = static_cast<ossimHttpResponse*>(stream);
112  if(cStream)
113  {
114  cStream->headerStream().write((char*)buffer, nmemb*size);
115  return nmemb*size;
116  }
117 
118  return 0;
119 }
std::iostream & headerStream()
yy_size_t size

◆ getContentLength()

ossim_int64 ossimCurlHttpRequest::getContentLength ( ) const

Definition at line 121 of file ossimCurlHttpRequest.cpp.

References ossimString::c_str(), ossimWebRequest::clearLastError(), ossimHttpResponse::convertHeaderStreamToKeywordlist(), curlWriteResponseBody(), curlWriteResponseHeader(), ossimRefPtr< T >::get(), ossimKeywordlist::getMap(), ossimUrl::getProtocol(), ossimWebRequest::getUrl(), m_curl, ossimHttpRequest::m_headerOptions, ossimWebRequest::m_lastError, setDefaultSSL(), and ossimUrl::toString().

122 {
123  double contentLength=-1;
124  curl_easy_reset(m_curl);
125  clearLastError();
126  ossimString urlString = getUrl().toString();
127  ossimString protocol = getUrl().getProtocol();
129 
130  curl_easy_setopt(m_curl, CURLOPT_HEADERFUNCTION, curlWriteResponseHeader);
131  curl_easy_setopt(m_curl, CURLOPT_HEADERDATA, (void*)response.get());
132  curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, curlWriteResponseBody);
133  curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, (void*)response.get());
134  curl_easy_setopt(m_curl, CURLOPT_HEADER, 0);
135  curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1);
136  curl_easy_setopt(m_curl, CURLOPT_RANGE, "");
137 
139  struct curl_slist *headers=0; /* init to NULL is important */
140 
141  ossimKeywordlist::KeywordMap::const_iterator iter = headerMap.begin();
142  while(iter != headerMap.end())
143  {
144 // std::cout << ((*iter).first + ":"+(*iter).second).c_str() << std::endl;
145  headers = curl_slist_append(headers, ((*iter).first + ":"+(*iter).second).c_str());
146  ++iter;
147  }
148  curl_easy_setopt(m_curl, CURLOPT_URL, urlString.c_str());
149  if(protocol == "https")
150  {
152  }
153  int rc = curl_easy_perform(m_curl);
154 
155  //rc = curl_easy_getinfo(m_curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLength);
156  if(rc == CURLE_SSL_CONNECT_ERROR)
157  {
158  // try default if an error for SSL connect ocurred
159  curl_easy_setopt(m_curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
160  rc = curl_easy_perform(m_curl);
161  }
162  bool result = (rc < 1);
163  if(result)
164  {
166  rc = curl_easy_getinfo(m_curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLength);
167  //if(rc>=1) contentLength = -1;
168  // response->convertHeaderStreamToKeywordlist();
169  //std::cout << response->headerKwl() << "\n";
170  }
171  else
172  {
173  m_lastError = curl_easy_strerror((CURLcode)rc);
174  //std::cout << curl_easy_strerror((CURLcode)rc) << std::endl;
175  }
176 
177  return static_cast<ossim_int64> (contentLength);
178 }
static int curlWriteResponseHeader(void *buffer, size_t size, size_t nmemb, void *stream)
ossimKeywordlist m_headerOptions
void setDefaultSSL(CURL *curl) const
ossimString toString() const
Definition: ossimUrl.cpp:106
std::map< std::string, std::string > KeywordMap
const ossimString & getProtocol() const
Definition: ossimUrl.h:17
const ossimUrl & getUrl() const
const ossimKeywordlist::KeywordMap & getMap() const
ossimString m_lastError
long long ossim_int64
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
static int curlWriteResponseBody(void *buffer, size_t size, size_t nmemb, void *stream)
void convertHeaderStreamToKeywordlist()
This will parse out the response code from the status line and initialize the header variables into a...

◆ getResponse()

ossimRefPtr< ossimWebResponse > ossimCurlHttpRequest::getResponse ( )
virtual

Reimplemented from ossimWebRequest.

Definition at line 4 of file ossimCurlHttpRequest.cpp.

References ossimString::c_str(), ossimWebRequest::clearLastError(), ossimHttpResponse::convertHeaderStreamToKeywordlist(), curlWriteResponseBody(), curlWriteResponseHeader(), ossimKeywordlist::find(), ossimRefPtr< T >::get(), ossimUrl::getProtocol(), ossimWebRequest::getUrl(), ossimHttpRequest::HTTP_METHOD_GET, m_curl, ossimHttpRequest::m_headerOptions, ossimWebRequest::m_lastError, ossimHttpRequest::m_methodType, setDefaultSSL(), ossimString::substitute(), supportsProtocol(), and ossimUrl::toString().

5 {
7  ossimString protocol = getUrl().getProtocol();
8  if(!supportsProtocol(protocol))
9  {
10  return 0;
11  }
12  curl_easy_reset(m_curl);
14  switch (m_methodType)
15  {
17  {
18  ossimCurlHttpResponse* curlResponse = new ossimCurlHttpResponse();
19  response = curlResponse;
20 
21  curl_easy_setopt(m_curl, CURLOPT_HEADERFUNCTION, curlWriteResponseHeader);
22  curl_easy_setopt(m_curl, CURLOPT_HEADERDATA, (void*)response.get());
23  curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, curlWriteResponseBody);
24  curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, (void*)response.get());
25  curl_easy_setopt(m_curl, CURLOPT_HEADER, 0);
26  curl_easy_setopt(m_curl, CURLOPT_NOBODY, 0);
27  //curl_easy_setopt(m_curl, CURLOPT_CONNECT_ONLY, 1);
28  // ossimKeywordlist::KeywordMap& headerMap = m_headerOptions.getMap();
29  // struct curl_slist *headers=0; /* init to NULL is important */
30  ossimString range = m_headerOptions.find("Range");
31  range=range.substitute("bytes=", "");
32  curl_easy_setopt(m_curl, CURLOPT_RANGE, range.c_str());
33  // ossimKeywordlist::KeywordMap::iterator iter = headerMap.begin();
34  // while(iter != headerMap.end())
35  // {
36  // std::cout << ((*iter).first + ":"+(*iter).second).c_str() << std::endl;
37  // headers = curl_slist_append(headers, ((*iter).first + ":"+(*iter).second).c_str());
38  // ++iter;
39  // }
40  ossimString urlString = getUrl().toString();
41  curl_easy_setopt(m_curl, CURLOPT_URL, urlString.c_str());
42 
43 
44  if(protocol == "https")
45  {
47  }
48 
49  int rc = curl_easy_perform(m_curl);
50 
51  if(rc == CURLE_SSL_CONNECT_ERROR)
52  {
53  // try default if an error for SSL connect ocurred
54  curl_easy_setopt(m_curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
55  rc = curl_easy_perform(m_curl);
56  }
57  bool result = (rc < 1);
58  if(result)
59  {
60  curlResponse->convertHeaderStreamToKeywordlist();
61  }
62  else
63  {
64  m_lastError = curl_easy_strerror((CURLcode)rc);
65  //std::cout << curl_easy_strerror((CURLcode)rc) << std::endl;
66  response = 0;
67  }
68  break;
69  }
70 
71  default:
72  break;
73  }
74 
75  return response;
76 }
ossimString substitute(const ossimString &searchKey, const ossimString &replacementValue, bool replaceAll=false) const
Substitutes searchKey string with replacementValue and returns a string.
const char * find(const char *key) const
virtual bool supportsProtocol(const ossimString &protocol) const
static int curlWriteResponseHeader(void *buffer, size_t size, size_t nmemb, void *stream)
ossimKeywordlist m_headerOptions
void setDefaultSSL(CURL *curl) const
ossimString toString() const
Definition: ossimUrl.cpp:106
HttpMethodType m_methodType
const ossimString & getProtocol() const
Definition: ossimUrl.h:17
const ossimUrl & getUrl() const
ossimString m_lastError
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
static int curlWriteResponseBody(void *buffer, size_t size, size_t nmemb, void *stream)
void convertHeaderStreamToKeywordlist()
This will parse out the response code from the status line and initialize the header variables into a...

◆ loadState()

virtual bool ossimCurlHttpRequest::loadState ( const ossimKeywordlist kwl,
const char *  prefix = 0 
)
inlinevirtual

Method to the load (recreate) the state of the object from a keyword list. Return true if ok or false on error.

Reimplemented from ossimHttpRequest.

Definition at line 34 of file ossimCurlHttpRequest.h.

References ossimHttpRequest::loadState(), and m_response.

35  {
36  m_response = 0;
37  return ossimHttpRequest::loadState(kwl, prefix);
38  }
ossimRefPtr< ossimCurlHttpResponse > m_response
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)

◆ setDefaultSSL()

void ossimCurlHttpRequest::setDefaultSSL ( CURL *  curl) const
protected

Definition at line 180 of file ossimCurlHttpRequest.cpp.

References ossim::CurlStreamDefaults::m_cacert, ossim::CurlStreamDefaults::m_clientCert, ossim::CurlStreamDefaults::m_clientCertType, ossim::CurlStreamDefaults::m_clientKey, and ossim::CurlStreamDefaults::m_clientKeyPassword.

Referenced by getContentLength(), and getResponse().

181 {
182  curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT);//);
184  {
185  curl_easy_setopt(curl, CURLOPT_CAINFO, ossim::CurlStreamDefaults::m_cacert.c_str());
186  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
187  }
188  else
189  {
190  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
191  }
193  {
194  curl_easy_setopt(curl, CURLOPT_SSLCERT, ossim::CurlStreamDefaults::m_clientCert.c_str());
195  }
197  {
198  curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, ossim::CurlStreamDefaults::m_clientCertType.c_str());
199  }
201  {
202  curl_easy_setopt(curl, CURLOPT_SSLKEYPASSWD, ossim::CurlStreamDefaults::m_clientKeyPassword.c_str());
203  }
205  {
206  curl_easy_setopt(curl, CURLOPT_SSLKEY, ossim::CurlStreamDefaults::m_clientKey.c_str());
207  }
208 }
static ossimFilename m_cacert
static ossimFilename m_clientKey
static ossimString m_clientCertType
static ossimString m_clientKeyPassword
static ossimFilename m_clientCert

◆ supportsProtocol()

bool ossimCurlHttpRequest::supportsProtocol ( const ossimString protocol) const
virtual

Definition at line 78 of file ossimCurlHttpRequest.cpp.

Referenced by ossimWebPluginRequestFactory::create(), and getResponse().

79 {
80  bool result = false;
81  if(protocol == "http")
82  {
83  result = true;
84  }
85  else if(protocol == "https")
86  {
87  curl_version_info_data * vinfo = curl_version_info( CURLVERSION_NOW );
88  if( vinfo->features & CURL_VERSION_SSL )
89  {
90  result = true;
91  }
92  }
93  return result;
94 }

Member Data Documentation

◆ m_curl

CURL* ossimCurlHttpRequest::m_curl
protected

◆ m_response

ossimRefPtr<ossimCurlHttpResponse> ossimCurlHttpRequest::m_response
mutableprotected

Definition at line 42 of file ossimCurlHttpRequest.h.

Referenced by loadState().


The documentation for this class was generated from the following files: