OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimCurlHttpRequest.cpp
Go to the documentation of this file.
1 #include "ossimCurlHttpRequest.h"
2 #include "CurlStreamDefaults.h"
3 
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 }
77 
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 }
95 
96 int ossimCurlHttpRequest::curlWriteResponseBody(void *buffer, size_t size, size_t nmemb, void *stream)
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 }
108 
109 int ossimCurlHttpRequest::curlWriteResponseHeader(void *buffer, size_t size, size_t nmemb, void *stream)
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 }
120 
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 }
179 
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 }
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
ossim_int64 getContentLength() const
static int curlWriteResponseHeader(void *buffer, size_t size, size_t nmemb, void *stream)
ossimKeywordlist m_headerOptions
virtual ossimRefPtr< ossimWebResponse > getResponse()
std::iostream & headerStream()
void setDefaultSSL(CURL *curl) const
ossimString toString() const
Definition: ossimUrl.cpp:106
yy_size_t size
std::map< std::string, std::string > KeywordMap
HttpMethodType m_methodType
const ossimString & getProtocol() const
Definition: ossimUrl.h:17
static ossimFilename m_cacert
static ossimFilename m_clientKey
const ossimUrl & getUrl() const
std::iostream & bodyStream()
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 ossimString m_clientCertType
static ossimString m_clientKeyPassword
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...
static ossimFilename m_clientCert