CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Static Private Attributes
stor::CurlInterface Class Reference

#include <CurlInterface.h>

Public Types

typedef std::vector< char > Content
 

Public Member Functions

CURLcode getContent (const std::string &url, const std::string &user, Content &content)
 
CURLcode postBinaryMessage (const std::string &url, void *buf, size_t size, Content &content)
 
 ~CurlInterface ()
 

Static Public Member Functions

static boost::shared_ptr
< CurlInterface
getInterface ()
 

Private Member Functions

 CurlInterface ()
 
CURLcode do_curl (CURL *, const std::string &url, Content &content)
 

Static Private Member Functions

static unsigned long sslIdFunction ()
 
static void sslLockingFunction (int mode, int n, const char *file, int line)
 
static size_t writeToString (char *data, size_t size, size_t nmemb, Content *buffer)
 

Static Private Attributes

static boost::shared_ptr
< CurlInterface
interface_
 
static pthread_mutex_t * mutexes_ = 0
 

Detailed Description

Helper class to interact with curl

Author:
mommsen
Revision:
1.4
Date:
2011/11/16 14:32:22

Definition at line 26 of file CurlInterface.h.

Member Typedef Documentation

typedef std::vector<char> stor::CurlInterface::Content

Definition at line 31 of file CurlInterface.h.

Constructor & Destructor Documentation

CurlInterface::~CurlInterface ( )

Definition at line 31 of file CurlInterface.cc.

References i, mutexes_, and NULL.

32 {
33  CRYPTO_set_id_callback(0);
34  CRYPTO_set_locking_callback(0);
35 
36  for (int i = 0; i < CRYPTO_num_locks(); ++i)
37  pthread_mutex_destroy(&mutexes_[i]);
38  free(mutexes_);
39  mutexes_ = NULL;
40 
41  curl_global_cleanup();
42 }
int i
Definition: DBlmapReader.cc:9
#define NULL
Definition: scimark2.h:8
static pthread_mutex_t * mutexes_
Definition: CurlInterface.h:68
CurlInterface::CurlInterface ( )
private

Definition at line 13 of file CurlInterface.cc.

References edm::hlt::Exception, i, mutexes_, sslIdFunction(), and sslLockingFunction().

Referenced by getInterface().

14 {
15  curl_global_init(CURL_GLOBAL_ALL);
16  const int cryptoNumLocks = CRYPTO_num_locks();
17 
18  //setup array to store all of the mutexes available to OpenSSL.
19  mutexes_ = (pthread_mutex_t*)malloc( cryptoNumLocks * sizeof(pthread_mutex_t) );
20  if ( ! mutexes_ )
21  XCEPT_RAISE(stor::exception::Exception, "Failed to allocate memory for SSL mutexes");
22 
23  for (int i = 0; i < cryptoNumLocks; ++i)
24  pthread_mutex_init(&mutexes_[i],0);
25 
26  CRYPTO_set_id_callback(sslIdFunction);
27  CRYPTO_set_locking_callback(sslLockingFunction);
28 }
static void sslLockingFunction(int mode, int n, const char *file, int line)
int i
Definition: DBlmapReader.cc:9
static unsigned long sslIdFunction()
static pthread_mutex_t * mutexes_
Definition: CurlInterface.h:68

Member Function Documentation

CURLcode CurlInterface::do_curl ( CURL *  curl,
const std::string &  url,
Content content 
)
private

Definition at line 98 of file CurlInterface.cc.

References i, runEdmFileComparison::returnCode, and writeToString().

99 {
100  char errorBuffer[CURL_ERROR_SIZE];
101 
102  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
103  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 4); // seconds
104  curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // do not send any signals
105  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, stor::CurlInterface::writeToString);
106  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
107  curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
108  curl_easy_setopt(curl, CURLOPT_FAILONERROR,1);
109 
110  const CURLcode returnCode = curl_easy_perform(curl);
111 
112  curl_easy_cleanup(curl);
113 
114  if (returnCode != CURLE_OK)
115  {
116  size_t i = 0;
117  content.clear();
118  while ( errorBuffer[i] != '\0' )
119  {
120  content.push_back( errorBuffer[i] );
121  ++i;
122  }
123  content.push_back('\0');
124  }
125 
126  return returnCode;
127 }
int i
Definition: DBlmapReader.cc:9
static size_t writeToString(char *data, size_t size, size_t nmemb, Content *buffer)
CURLcode CurlInterface::getContent ( const std::string &  url,
const std::string &  user,
Content content 
)

Get webpage content from specified URL using the user/password specified. If the return value is CURLE_OK, the webpage could be fetched and the content is in the content string. Otherwise, the content string contains the error message.

Definition at line 57 of file CurlInterface.cc.

62 {
63  CURL* curl = curl_easy_init();
64  if ( ! curl ) return CURLE_FAILED_INIT;
65 
66  curl_easy_setopt(curl, CURLOPT_USERPWD, user.c_str());
67 
68  return do_curl(curl, url, content);
69 }
CURLcode do_curl(CURL *, const std::string &url, Content &content)
boost::shared_ptr< CurlInterface > CurlInterface::getInterface ( )
static

Return a shared pointer to the singleton

Definition at line 45 of file CurlInterface.cc.

References CurlInterface(), and interface_.

Referenced by stor::ResourceMonitorCollection::checkSataDisks(), and stor::EventServerProxy< RegInfo >::getInitMsgFromEventServer().

46 {
47  if (interface_.get() == 0)
48  {
49  interface_.reset( new CurlInterface() );
50  }
51 
52  return interface_;
53 }
static boost::shared_ptr< CurlInterface > interface_
Definition: CurlInterface.h:67
CURLcode CurlInterface::postBinaryMessage ( const std::string &  url,
void *  buf,
size_t  size,
Content content 
)

Post message a message at the given location. If the return value is CURLE_OK, the post succeeded and the reply is in the content string. Otherwise, the content string contains the error message.

Definition at line 73 of file CurlInterface.cc.

References NULL, and ntuplemaker::status.

79 {
80  CURL* curl = curl_easy_init();
81  if ( ! curl ) return CURLE_FAILED_INIT;
82 
83  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);
84  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, size);
85 
86  struct curl_slist *headers=NULL;
87  headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
88  headers = curl_slist_append(headers, "Content-Transfer-Encoding: binary");
89  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
90 
91  const CURLcode status = do_curl(curl, url, content);
92  curl_slist_free_all(headers);
93 
94  return status;
95 }
#define NULL
Definition: scimark2.h:8
CURLcode do_curl(CURL *, const std::string &url, Content &content)
tuple status
Definition: ntuplemaker.py:245
tuple size
Write out results.
unsigned long CurlInterface::sslIdFunction ( void  )
staticprivate

Definition at line 149 of file CurlInterface.cc.

Referenced by CurlInterface().

150 {
151  return ( (unsigned long)pthread_self() );
152 }
void CurlInterface::sslLockingFunction ( int  mode,
int  n,
const char *  file,
int  line 
)
staticprivate

Definition at line 140 of file CurlInterface.cc.

References mutexes_.

Referenced by CurlInterface().

141 {
142  if (mode & CRYPTO_LOCK)
143  pthread_mutex_lock(&mutexes_[n]);
144  else
145  pthread_mutex_unlock(&mutexes_[n]);
146 }
static pthread_mutex_t * mutexes_
Definition: CurlInterface.h:68
size_t CurlInterface::writeToString ( char *  data,
size_t  size,
size_t  nmemb,
Content buffer 
)
staticprivate

Definition at line 130 of file CurlInterface.cc.

References data, and NULL.

Referenced by do_curl().

131 {
132  if (buffer == NULL) return 0;
133 
134  const size_t length = size * nmemb;
135  buffer->insert(buffer->end(), data, data+length);
136  return length;
137 }
#define NULL
Definition: scimark2.h:8
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
tuple size
Write out results.

Member Data Documentation

CurlInterfacePtr CurlInterface::interface_
staticprivate

Definition at line 67 of file CurlInterface.h.

Referenced by getInterface().

pthread_mutex_t * CurlInterface::mutexes_ = 0
staticprivate

Definition at line 68 of file CurlInterface.h.

Referenced by CurlInterface(), sslLockingFunction(), and ~CurlInterface().