CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/EventFilter/Utilities/src/CurlPoster.cc

Go to the documentation of this file.
00001 #include "EventFilter/Utilities/interface/CurlPoster.h"
00002 #include "EventFilter/Utilities/interface/CurlUtils.h"
00003 #include "EventFilter/Utilities/interface/Exception.h"
00004 
00005 #include "curl/curl.h"
00006 #include <netdb.h>
00007 #include <sys/socket.h>        /* for AF_INET */
00008 
00009 #include <sstream>
00010 
00011 
00012 
00013 namespace evf{
00014 
00015   const std::string CurlPoster::standard_post_method_ = "/postEntry";
00016 
00017   //______________________________________________________________________________
00018   void CurlPoster::post(const unsigned char *content, 
00019                         size_t len, 
00020                         unsigned int run,
00021                         mode m, const std::string &post_method)
00022   {
00023     std::string urlp = url_+post_method;
00024     char srun[12];
00025     sprintf(srun,"%d",run);
00026     std::string method;
00027     CURL* han = curl_easy_init();
00028     if(han==0)
00029       {
00030         XCEPT_RAISE(evf::Exception,"could not create handle for curlPoster"); 
00031       }
00032     struct curl_slist *headers=NULL; /* init to NULL is important */
00033     switch(m){
00034     case text:
00035       {
00036         headers = curl_slist_append(headers, "Content-Type: text/plain");
00037         method = "text";
00038         break;
00039       }
00040     case stack:
00041       {
00042         headers = curl_slist_append(headers, "Content-Type: text/plain");
00043         method = "stacktrace";
00044         break;
00045       }
00046     case leg:
00047       {
00048         headers = curl_slist_append(headers, "Content-Type: text/plain");
00049         method = "legenda";
00050         break;
00051       }
00052     case bin:
00053       {
00054         headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
00055         //      headers = curl_slist_append(headers, "Content-Transfer-Encoding: base64");
00056         headers = curl_slist_append(headers, "Expect:");
00057         method = "trp";
00058         break;
00059       }
00060     default:
00061       {
00062         headers = curl_slist_append(headers, "Content-Type: application/xml");
00063       }
00064     }
00065     struct curl_httpspost *post=NULL;
00066     struct curl_httpspost *last=NULL;
00067     char error[CURL_ERROR_SIZE];
00068     
00069     curl_easy_setopt(han, CURLOPT_URL, urlp.c_str());
00070     //    curl_easy_setopt(han, CURLOPT_VERBOSE,"");
00071     curl_easy_setopt(han, CURLOPT_NOSIGNAL,"");
00072     curl_easy_setopt(han, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
00073     //  curl_easy_setopt(han, CURLOPT_TIMEOUT, 60.0L);
00074     curl_formadd(&post, &last,
00075                  CURLFORM_COPYNAME, "name",
00076                  CURLFORM_COPYCONTENTS, buf_->nodename, CURLFORM_END);
00077     curl_formadd(&post, &last,
00078                  CURLFORM_COPYNAME, "run",
00079                  CURLFORM_COPYCONTENTS, srun, CURLFORM_END);
00080     int retval = curl_formadd(&post, &last,
00081                               CURLFORM_COPYNAME, method.c_str(),
00082                               CURLFORM_COPYCONTENTS, content,
00083                               CURLFORM_CONTENTSLENGTH, len,
00084                               CURLFORM_CONTENTHEADER, headers,
00085                               CURLFORM_END);
00086     if(retval != 0) std::cout << "Error in formadd " << retval << std::endl;
00087     curl_easy_setopt(han, CURLOPT_HTTPPOST, post);
00088     curl_easy_setopt(han, CURLOPT_ERRORBUFFER, error);
00089         
00090     int success = curl_easy_perform(han);
00091     curl_formfree(post);
00092     curl_easy_cleanup(han);
00093     curl_slist_free_all(headers); /* free the header list */    
00094 
00095     if(success != 0)
00096       {
00097         std::ostringstream msg;
00098         msg <<  "could not post data to url " << url_ << " error #" 
00099             << success << " " << error;
00100         XCEPT_RAISE(evf::Exception,msg.str().c_str());
00101       }
00102 
00103   }
00104   void CurlPoster::postString(const char *content, size_t len, unsigned int run, 
00105                               mode m, const std::string &post_method)
00106   {
00107     if(!active_) return;
00108     post((unsigned char*)content,(unsigned int)len,run,m,post_method);
00109   }
00110   void CurlPoster::postBinary(const unsigned char *content, size_t len, unsigned int run,
00111                               const std::string &post_method)
00112   {
00113     if(!active_) return;
00114     post(content,len,run,bin,post_method);
00115   }
00116 
00117   bool CurlPoster::check(int run)
00118   {
00119     bool retVal = true;
00120     char ps[14];
00121     sprintf(ps,"run=%d",run);
00122     CURL* han = curl_easy_init();
00123     if(han==0)
00124       {
00125         active_ = false;
00126       }
00127     char error[CURL_ERROR_SIZE];
00128     std::string dummy;
00129 
00130     curl_easy_setopt(han, CURLOPT_URL, url_.c_str()           );
00131     curl_easy_setopt(han, CURLOPT_POSTFIELDS,ps               );    
00132     curl_easy_setopt(han, CURLOPT_WRITEFUNCTION, &write_data  );
00133     curl_easy_setopt(han, CURLOPT_WRITEDATA, &dummy           );
00134     curl_easy_setopt(han, CURLOPT_ERRORBUFFER, error          );
00135     int success = curl_easy_perform(han);
00136 
00137     curl_easy_cleanup(han);
00138     if(success != 0){
00139       std::cout << "curlposter failed check" << std::endl;
00140       retVal = false;
00141       active_ = false;
00142     }
00143     return retVal;
00144 
00145   }
00146 
00147 } //end