CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CurlPoster.cc
Go to the documentation of this file.
4 
5 #include "curl/curl.h"
6 #include <netdb.h>
7 #include <sys/socket.h> /* for AF_INET */
8 
9 #include <sstream>
10 
11 
12 
13 namespace evf{
14 
16 
17  //______________________________________________________________________________
18  void CurlPoster::post(const unsigned char *content,
19  size_t len,
20  unsigned int run,
21  mode m, const std::string &post_method)
22  {
23  std::string urlp = url_+post_method;
24  char srun[12];
25  sprintf(srun,"%d",run);
27  CURL* han = curl_easy_init();
28  if(han==0)
29  {
30  XCEPT_RAISE(evf::Exception,"could not create handle for curlPoster");
31  }
32  struct curl_slist *headers=NULL; /* init to NULL is important */
33  switch(m){
34  case text:
35  {
36  headers = curl_slist_append(headers, "Content-Type: text/plain");
37  method = "text";
38  break;
39  }
40  case stack:
41  {
42  headers = curl_slist_append(headers, "Content-Type: text/plain");
43  method = "stacktrace";
44  break;
45  }
46  case leg:
47  {
48  headers = curl_slist_append(headers, "Content-Type: text/plain");
49  method = "legenda";
50  break;
51  }
52  case legaux:
53  {
54  headers = curl_slist_append(headers, "Content-Type: text/plain");
55  method = "legendaAux";
56  break;
57  }
58  case bin:
59  {
60  headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
61  // headers = curl_slist_append(headers, "Content-Transfer-Encoding: base64");
62  headers = curl_slist_append(headers, "Expect:");
63  method = "trp";
64  break;
65  }
66  default:
67  {
68  headers = curl_slist_append(headers, "Content-Type: application/xml");
69  }
70  }
71  struct curl_httppost *post=NULL;
72  struct curl_httppost *last=NULL;
73  char error[CURL_ERROR_SIZE];
74 
75  curl_easy_setopt(han, CURLOPT_URL, urlp.c_str());
76  // curl_easy_setopt(han, CURLOPT_VERBOSE,"");
77  curl_easy_setopt(han, CURLOPT_NOSIGNAL,"");
78  curl_easy_setopt(han, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
79  // curl_easy_setopt(han, CURLOPT_TIMEOUT, 60.0L);
80  curl_formadd(&post, &last,
81  CURLFORM_COPYNAME, "name",
82  CURLFORM_COPYCONTENTS, buf_->nodename, CURLFORM_END);
83  curl_formadd(&post, &last,
84  CURLFORM_COPYNAME, "run",
85  CURLFORM_COPYCONTENTS, srun, CURLFORM_END);
86  int retval = curl_formadd(&post, &last,
87  CURLFORM_COPYNAME, method.c_str(),
88  CURLFORM_COPYCONTENTS, content,
89  CURLFORM_CONTENTSLENGTH, len,
90  CURLFORM_CONTENTHEADER, headers,
91  CURLFORM_END);
92  if(retval != 0) std::cout << "Error in formadd " << retval << std::endl;
93  curl_easy_setopt(han, CURLOPT_HTTPPOST, post);
94  curl_easy_setopt(han, CURLOPT_ERRORBUFFER, error);
95  curl_easy_setopt(han, CURLOPT_TIMEOUT, 5);
96  curl_easy_setopt(han, CURLOPT_CONNECTTIMEOUT, 5);
97 
98  int success = curl_easy_perform(han);
99  curl_formfree(post);
100  curl_easy_cleanup(han);
101  curl_slist_free_all(headers); /* free the header list */
102 
103  if(success != 0)
104  {
105  std::ostringstream msg;
106  msg << "could not post data to url " << url_ << " error #"
107  << success << " " << error;
108  XCEPT_RAISE(evf::Exception,msg.str().c_str());
109  }
110 
111  }
112  void CurlPoster::postString(const char *content, size_t len, unsigned int run,
113  mode m, const std::string &post_method)
114  {
115  if(!active_) return;
116  post((unsigned char*)content,(unsigned int)len,run,m,post_method);
117  }
118  void CurlPoster::postBinary(const unsigned char *content, size_t len, unsigned int run,
119  const std::string &post_method)
120  {
121  if(!active_) return;
122  post(content,len,run,bin,post_method);
123  }
124 
126  {
127  bool retVal = true;
128  char ps[14];
129  sprintf(ps,"run=%d",run);
130  CURL* han = curl_easy_init();
131  if(han==0)
132  {
133  active_ = false;
134  }
135  char error[CURL_ERROR_SIZE];
136  std::string dummy;
137 
138  curl_easy_setopt(han, CURLOPT_URL, url_.c_str() );
139  curl_easy_setopt(han, CURLOPT_POSTFIELDS,ps );
140  curl_easy_setopt(han, CURLOPT_WRITEFUNCTION, &write_data );
141  curl_easy_setopt(han, CURLOPT_WRITEDATA, &dummy );
142  curl_easy_setopt(han, CURLOPT_ERRORBUFFER, error );
143  curl_easy_setopt(han, CURLOPT_TIMEOUT, 5 );
144  curl_easy_setopt(han, CURLOPT_CONNECTTIMEOUT, 5 );
145  int success = curl_easy_perform(han);
146 
147  curl_easy_cleanup(han);
148  if(success != 0){
149  std::cout << "curlposter failed check" << std::endl;
150  retVal = false;
151  active_ = false;
152  }
153  return retVal;
154 
155  }
156 
157 } //end
bool check(int)
Definition: CurlPoster.cc:125
struct utsname * buf_
Definition: CurlPoster.h:31
#define NULL
Definition: scimark2.h:8
void postString(const char *, size_t, unsigned int, mode, const std::string &=standard_post_method_)
Definition: CurlPoster.cc:112
void postBinary(const unsigned char *, size_t, unsigned int, const std::string &=standard_post_method_)
Definition: CurlPoster.cc:118
static const std::string standard_post_method_
Definition: CurlPoster.h:32
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *pointer)
Definition: CurlUtils.h:12
std::string url_
Definition: CurlPoster.h:29
void post(const unsigned char *, size_t, unsigned int, mode, const std::string &)
Definition: CurlPoster.cc:18
tuple cout
Definition: gather_cfg.py:121