CMS 3D CMS Logo

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>

List of all members.

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.

{
  CRYPTO_set_id_callback(0);
  CRYPTO_set_locking_callback(0);

  for (int i = 0; i < CRYPTO_num_locks(); ++i)
    pthread_mutex_destroy(&mutexes_[i]);
  free(mutexes_);
  mutexes_ = NULL;
  
  curl_global_cleanup();
}
CurlInterface::CurlInterface ( ) [private]

Definition at line 13 of file CurlInterface.cc.

References Exception, i, mutexes_, sslIdFunction(), and sslLockingFunction().

Referenced by getInterface().

{
  curl_global_init(CURL_GLOBAL_ALL);
  const int cryptoNumLocks = CRYPTO_num_locks();

  //setup array to store all of the mutexes available to OpenSSL.
  mutexes_ = (pthread_mutex_t*)malloc( cryptoNumLocks * sizeof(pthread_mutex_t) );
  if ( ! mutexes_ )
    XCEPT_RAISE(stor::exception::Exception, "Failed to allocate memory for SSL mutexes");

  for (int i = 0; i < cryptoNumLocks; ++i)
    pthread_mutex_init(&mutexes_[i],0);

  CRYPTO_set_id_callback(sslIdFunction);
  CRYPTO_set_locking_callback(sslLockingFunction);
}

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().

{
  char errorBuffer[CURL_ERROR_SIZE]; 

  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 4); // seconds
  curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // do not send any signals
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, stor::CurlInterface::writeToString);  
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);  
  curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); 
  curl_easy_setopt(curl, CURLOPT_FAILONERROR,1); 
  
  const CURLcode returnCode = curl_easy_perform(curl);
  
  curl_easy_cleanup(curl);
  
  if (returnCode != CURLE_OK)
  {
    size_t i = 0;
    content.clear();
    while ( errorBuffer[i] != '\0' )
    {
      content.push_back( errorBuffer[i] );
      ++i;
    }
    content.push_back('\0');
  }

  return returnCode;
}
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.

{
  CURL* curl = curl_easy_init();
  if ( ! curl ) return CURLE_FAILED_INIT;

  curl_easy_setopt(curl, CURLOPT_USERPWD, user.c_str());

  return do_curl(curl, url, 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().

{
  if (interface_.get() == 0)
  {
    interface_.reset( new CurlInterface() );
  }

  return interface_;
}
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.

{
  CURL* curl = curl_easy_init();
  if ( ! curl ) return CURLE_FAILED_INIT;

  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, size);

  struct curl_slist *headers=NULL;
  headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
  headers = curl_slist_append(headers, "Content-Transfer-Encoding: binary");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

  const CURLcode status = do_curl(curl, url, content);
  curl_slist_free_all(headers);

  return status;
}
unsigned long CurlInterface::sslIdFunction ( void  ) [static, private]

Definition at line 149 of file CurlInterface.cc.

Referenced by CurlInterface().

{
  return ( (unsigned long)pthread_self() );
}
void CurlInterface::sslLockingFunction ( int  mode,
int  n,
const char *  file,
int  line 
) [static, private]

Definition at line 140 of file CurlInterface.cc.

References mutexes_.

Referenced by CurlInterface().

{
  if (mode & CRYPTO_LOCK)
    pthread_mutex_lock(&mutexes_[n]);
  else
    pthread_mutex_unlock(&mutexes_[n]);
}
size_t CurlInterface::writeToString ( char *  data,
size_t  size,
size_t  nmemb,
Content buffer 
) [static, private]

Definition at line 130 of file CurlInterface.cc.

References AlCaHLTBitMon_QueryRunRegistry::data, and NULL.

Referenced by do_curl().

{
  if (buffer == NULL) return 0;

  const size_t length = size * nmemb;
  buffer->insert(buffer->end(), data, data+length);
  return length;
}

Member Data Documentation

Definition at line 67 of file CurlInterface.h.

Referenced by getInterface().

pthread_mutex_t * CurlInterface::mutexes_ = 0 [static, private]

Definition at line 68 of file CurlInterface.h.

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