CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_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 legaux:
00053       {
00054         headers = curl_slist_append(headers, "Content-Type: text/plain");
00055         method = "legendaAux";
00056         break;
00057       }
00058     case bin:
00059       {
00060         headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
00061         //      headers = curl_slist_append(headers, "Content-Transfer-Encoding: base64");
00062         headers = curl_slist_append(headers, "Expect:");
00063         method = "trp";
00064         break;
00065       }
00066     default:
00067       {
00068         headers = curl_slist_append(headers, "Content-Type: application/xml");
00069       }
00070     }
00071     struct curl_httppost *post=NULL;
00072     struct curl_httppost *last=NULL;
00073     char error[CURL_ERROR_SIZE];
00074     
00075     curl_easy_setopt(han, CURLOPT_URL, urlp.c_str());
00076     //    curl_easy_setopt(han, CURLOPT_VERBOSE,"");
00077     curl_easy_setopt(han, CURLOPT_NOSIGNAL,"");
00078     curl_easy_setopt(han, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
00079     //  curl_easy_setopt(han, CURLOPT_TIMEOUT, 60.0L);
00080     curl_formadd(&post, &last,
00081                  CURLFORM_COPYNAME, "name",
00082                  CURLFORM_COPYCONTENTS, buf_->nodename, CURLFORM_END);
00083     curl_formadd(&post, &last,
00084                  CURLFORM_COPYNAME, "run",
00085                  CURLFORM_COPYCONTENTS, srun, CURLFORM_END);
00086     int retval = curl_formadd(&post, &last,
00087                               CURLFORM_COPYNAME, method.c_str(),
00088                               CURLFORM_COPYCONTENTS, content,
00089                               CURLFORM_CONTENTSLENGTH, len,
00090                               CURLFORM_CONTENTHEADER, headers,
00091                               CURLFORM_END);
00092     if(retval != 0) std::cout << "Error in formadd " << retval << std::endl;
00093     curl_easy_setopt(han, CURLOPT_HTTPPOST, post);
00094     curl_easy_setopt(han, CURLOPT_ERRORBUFFER, error);
00095     curl_easy_setopt(han, CURLOPT_TIMEOUT, 5);
00096     curl_easy_setopt(han, CURLOPT_CONNECTTIMEOUT, 5);
00097         
00098     int success = curl_easy_perform(han);
00099     curl_formfree(post);
00100     curl_easy_cleanup(han);
00101     curl_slist_free_all(headers); /* free the header list */    
00102 
00103     if(success != 0)
00104       {
00105         std::ostringstream msg;
00106         msg <<  "could not post data to url " << url_ << " error #" 
00107             << success << " " << error;
00108         XCEPT_RAISE(evf::Exception,msg.str().c_str());
00109       }
00110 
00111   }
00112   void CurlPoster::postString(const char *content, size_t len, unsigned int run, 
00113                               mode m, const std::string &post_method)
00114   {
00115     if(!active_) return;
00116     post((unsigned char*)content,(unsigned int)len,run,m,post_method);
00117   }
00118   void CurlPoster::postBinary(const unsigned char *content, size_t len, unsigned int run,
00119                               const std::string &post_method)
00120   {
00121     if(!active_) return;
00122     post(content,len,run,bin,post_method);
00123   }
00124 
00125   bool CurlPoster::check(int run)
00126   {
00127     bool retVal = true;
00128     char ps[14];
00129     sprintf(ps,"run=%d",run);
00130     CURL* han = curl_easy_init();
00131     if(han==0)
00132       {
00133         active_ = false;
00134       }
00135     char error[CURL_ERROR_SIZE];
00136     std::string dummy;
00137 
00138     curl_easy_setopt(han, CURLOPT_URL, url_.c_str()           );
00139     curl_easy_setopt(han, CURLOPT_POSTFIELDS,ps               );    
00140     curl_easy_setopt(han, CURLOPT_WRITEFUNCTION, &write_data  );
00141     curl_easy_setopt(han, CURLOPT_WRITEDATA, &dummy           );
00142     curl_easy_setopt(han, CURLOPT_ERRORBUFFER, error          );
00143     curl_easy_setopt(han, CURLOPT_TIMEOUT, 5                  );
00144     curl_easy_setopt(han, CURLOPT_CONNECTTIMEOUT, 5           );
00145     int success = curl_easy_perform(han);
00146 
00147     curl_easy_cleanup(han);
00148     if(success != 0){
00149       std::cout << "curlposter failed check" << std::endl;
00150       retVal = false;
00151       active_ = false;
00152     }
00153     return retVal;
00154 
00155   }
00156 
00157 } //end