CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/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     curl_easy_setopt(han, CURLOPT_TIMEOUT, 5);
00090     curl_easy_setopt(han, CURLOPT_CONNECTTIMEOUT, 5);
00091         
00092     int success = curl_easy_perform(han);
00093     curl_formfree(post);
00094     curl_easy_cleanup(han);
00095     curl_slist_free_all(headers); /* free the header list */    
00096 
00097     if(success != 0)
00098       {
00099         std::ostringstream msg;
00100         msg <<  "could not post data to url " << url_ << " error #" 
00101             << success << " " << error;
00102         XCEPT_RAISE(evf::Exception,msg.str().c_str());
00103       }
00104 
00105   }
00106   void CurlPoster::postString(const char *content, size_t len, unsigned int run, 
00107                               mode m, const std::string &post_method)
00108   {
00109     if(!active_) return;
00110     post((unsigned char*)content,(unsigned int)len,run,m,post_method);
00111   }
00112   void CurlPoster::postBinary(const unsigned char *content, size_t len, unsigned int run,
00113                               const std::string &post_method)
00114   {
00115     if(!active_) return;
00116     post(content,len,run,bin,post_method);
00117   }
00118 
00119   bool CurlPoster::check(int run)
00120   {
00121     bool retVal = true;
00122     char ps[14];
00123     sprintf(ps,"run=%d",run);
00124     CURL* han = curl_easy_init();
00125     if(han==0)
00126       {
00127         active_ = false;
00128       }
00129     char error[CURL_ERROR_SIZE];
00130     std::string dummy;
00131 
00132     curl_easy_setopt(han, CURLOPT_URL, url_.c_str()           );
00133     curl_easy_setopt(han, CURLOPT_POSTFIELDS,ps               );    
00134     curl_easy_setopt(han, CURLOPT_WRITEFUNCTION, &write_data  );
00135     curl_easy_setopt(han, CURLOPT_WRITEDATA, &dummy           );
00136     curl_easy_setopt(han, CURLOPT_ERRORBUFFER, error          );
00137     curl_easy_setopt(han, CURLOPT_TIMEOUT, 5                  );
00138     curl_easy_setopt(han, CURLOPT_CONNECTTIMEOUT, 5           );
00139     int success = curl_easy_perform(han);
00140 
00141     curl_easy_cleanup(han);
00142     if(success != 0){
00143       std::cout << "curlposter failed check" << std::endl;
00144       retVal = false;
00145       active_ = false;
00146     }
00147     return retVal;
00148 
00149   }
00150 
00151 } //end