CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/EventFilter/StorageManager/src/CurlInterface.cc

Go to the documentation of this file.
00001 // $Id: CurlInterface.cc,v 1.3 2011/03/07 15:31:32 mommsen Exp $
00003 
00004 #include "EventFilter/StorageManager/interface/CurlInterface.h"
00005 
00006 using namespace stor;
00007 
00008 
00009 CURLcode CurlInterface::getContent
00010 (
00011   const std::string& url,
00012   const std::string& user,
00013   Content& content
00014 )
00015 {
00016   CURL* curl = curl_easy_init();
00017   if ( ! curl ) return CURLE_FAILED_INIT;
00018 
00019   curl_easy_setopt(curl, CURLOPT_USERPWD, user.c_str());
00020 
00021   return do_curl(curl, url, content);
00022 }
00023 
00024 
00025 CURLcode CurlInterface::postBinaryMessage
00026 (
00027   const std::string& url,
00028   void* buf,
00029   size_t size,
00030   Content& content
00031 )
00032 {
00033   CURL* curl = curl_easy_init();
00034   if ( ! curl ) return CURLE_FAILED_INIT;
00035 
00036   curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);
00037   curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, size);
00038 
00039   struct curl_slist *headers=NULL;
00040   headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
00041   headers = curl_slist_append(headers, "Content-Transfer-Encoding: binary");
00042   curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
00043 
00044   CURLcode status = do_curl(curl, url, content);
00045   curl_slist_free_all(headers);
00046 
00047   return status;
00048 }
00049 
00050 
00051 CURLcode CurlInterface::do_curl(CURL* curl, const std::string& url, Content& content)
00052 {
00053   curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
00054   curl_easy_setopt(curl, CURLOPT_TIMEOUT, 4); // seconds
00055   curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // do not send any signals
00056   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, stor::CurlInterface::writeToString);  
00057   curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);  
00058   curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer_); 
00059   curl_easy_setopt(curl, CURLOPT_FAILONERROR,1); 
00060   
00061   CURLcode returnCode = curl_easy_perform(curl);
00062   
00063   curl_easy_cleanup(curl);
00064   
00065   if (returnCode != CURLE_OK)
00066   {
00067     size_t i = 0;
00068     content.clear();
00069     while ( errorBuffer_[i] != '\0' )
00070     {
00071       content.push_back( errorBuffer_[i] );
00072       ++i;
00073     }
00074     content.push_back('\0');
00075   }
00076 
00077   return returnCode;
00078 }
00079 
00080 
00081 size_t CurlInterface::writeToString(char *data, size_t size, size_t nmemb, Content* buffer)
00082 {
00083   if (buffer == NULL) return 0;
00084 
00085   const size_t length = size * nmemb;
00086   buffer->insert(buffer->end(), data, data+length);
00087   return length;
00088 }
00089 
00090