CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CurlInterface.cc
Go to the documentation of this file.
1 // $Id: CurlInterface.cc,v 1.4 2011/11/16 14:32:22 mommsen Exp $
3 
6 
7 using namespace stor;
8 
10 pthread_mutex_t* CurlInterface::mutexes_ = 0;
11 
12 
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 }
29 
30 
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 }
43 
44 
45 boost::shared_ptr<CurlInterface> CurlInterface::getInterface()
46 {
47  if (interface_.get() == 0)
48  {
49  interface_.reset( new CurlInterface() );
50  }
51 
52  return interface_;
53 }
54 
55 
57 (
58  const std::string& url,
59  const std::string& user,
61 )
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 }
70 
71 
73 (
74  const std::string& url,
75  void* buf,
76  size_t size,
77  Content& content
78 )
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 }
96 
97 
98 CURLcode CurlInterface::do_curl(CURL* curl, const std::string& url, Content& content)
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 }
128 
129 
130 size_t CurlInterface::writeToString(char *data, size_t size, size_t nmemb, Content* buffer)
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 }
138 
139 
140 void CurlInterface::sslLockingFunction(int mode, int n, const char* file, int line)
141 {
142  if (mode & CRYPTO_LOCK)
143  pthread_mutex_lock(&mutexes_[n]);
144  else
145  pthread_mutex_unlock(&mutexes_[n]);
146 }
147 
148 
149 unsigned long CurlInterface::sslIdFunction(void)
150 {
151  return ( (unsigned long)pthread_self() );
152 }
153 
154 
static void sslLockingFunction(int mode, int n, const char *file, int line)
int i
Definition: DBlmapReader.cc:9
static unsigned long sslIdFunction()
#define NULL
Definition: scimark2.h:8
static boost::shared_ptr< CurlInterface > getInterface()
static boost::shared_ptr< CurlInterface > interface_
Definition: CurlInterface.h:67
CURLcode do_curl(CURL *, const std::string &url, Content &content)
CURLcode getContent(const std::string &url, const std::string &user, Content &content)
boost::shared_ptr< CurlInterface > CurlInterfacePtr
Definition: CurlInterface.h:71
static size_t writeToString(char *data, size_t size, size_t nmemb, Content *buffer)
static pthread_mutex_t * mutexes_
Definition: CurlInterface.h:68
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
CURLcode postBinaryMessage(const std::string &url, void *buf, size_t size, Content &content)
tuple status
Definition: ntuplemaker.py:245
tuple size
Write out results.
std::vector< char > Content
Definition: CurlInterface.h:31