1 #include <openssl/md5.h>
2 #include <otsdaq-utilities/ECLWriter/ECLConnection.h>
7 #include "otsdaq-core/Macros/CoutMacros.h"
8 #include "otsdaq-core/MessageFacility/MessageFacility.h"
10 ECLConnection::ECLConnection(std::string user, std::string pwd, std::string url)
19 size_t ECLConnection::WriteMemoryCallback(
char* data,
28 buffer->append(data, size * nmemb);
29 realsize = size * nmemb;
35 bool ECLConnection::Get(std::string s, std::string& response)
39 char errorBuffer[CURL_ERROR_SIZE];
43 curl_global_init(CURL_GLOBAL_ALL);
46 curl_handle = curl_easy_init();
48 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorBuffer);
50 std::string fullURL = _url + s;
53 curl_easy_setopt(curl_handle, CURLOPT_URL, fullURL.c_str());
54 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
58 curl_handle, CURLOPT_WRITEFUNCTION, ECLConnection::WriteMemoryCallback);
61 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &buffer);
65 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
"libcurl-agent/1.0");
68 CURLcode result = curl_easy_perform(curl_handle);
71 curl_easy_cleanup(curl_handle);
73 if(result == CURLE_OK)
77 std::cerr <<
"Error: [" << result <<
"] - " << errorBuffer << std::endl;
81 curl_global_cleanup();
86 bool ECLConnection::Search(std::string s) {
return false; }
88 std::string ECLConnection::MakeSaltString()
90 std::string rndString =
"";
93 "abcdefghijklmnopqrstuvwxyz"
96 for(
int i = 0; i < 10; ++i)
98 rndString += chars[rand() % chars.size()];
106 std::string safe_url;
107 if(!Get(
"/secureURL", safe_url))
110 std::string rndString = MakeSaltString();
112 std::string myURL =
"/E/xml_post?";
113 std::string mySalt =
"salt=" + rndString;
114 std::string fullURL = _url + myURL + mySalt;
116 std::string myData = mySalt +
":" + _pwd +
":";
119 std::ostringstream oss;
121 std::string eclString = oss.str();
122 __COUT__ <<
"ECL XML is: " << eclString << std::endl;
124 eclString = eclString.substr(eclString.find_first_of(
">") + 2);
126 while(eclString.find(
'\n') != std::string::npos)
128 eclString = eclString.erase(eclString.find(
'\n'), 1);
130 while(eclString.find(
'\r') != std::string::npos)
132 eclString = eclString.erase(eclString.find(
'\r'), 1);
134 while(eclString.find(
" <") != std::string::npos)
136 eclString = eclString.erase(eclString.find(
" <"), 1);
138 boost::trim(eclString);
140 __COUT__ <<
"ECL Hash string is: " << myData << std::endl;
141 unsigned char resultMD5[MD5_DIGEST_LENGTH];
142 MD5((
unsigned char*)myData.c_str(), myData.size(), resultMD5);
146 for(
auto i = 0; i < MD5_DIGEST_LENGTH; i++)
148 sprintf(buf,
"%02x", resultMD5[i]);
151 __COUT__ <<
"ECL MD5 Signature is: " << xSig << std::endl;
154 char errorBuffer[CURL_ERROR_SIZE];
156 curl_global_init(CURL_GLOBAL_ALL);
160 curl_handle = curl_easy_init();
162 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorBuffer);
166 struct curl_slist* headers = NULL;
167 std::string buff =
"X-User: " + _user;
168 headers = curl_slist_append(headers, buff.c_str());
169 headers = curl_slist_append(headers,
"Content-type: text/xml");
170 headers = curl_slist_append(headers,
"X-Signature-Method: md5");
171 buff =
"X-Signature: " + xSig;
172 headers = curl_slist_append(headers, buff.c_str());
174 const char* estr = eclString.c_str();
176 __COUT__ <<
"ECL Setting message headers" << std::endl;
177 curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, estr);
178 curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
179 curl_easy_setopt(curl_handle, CURLOPT_URL, fullURL.c_str());
184 __COUT__ <<
"ECL Posting message" << std::endl;
185 CURLcode result = curl_easy_perform(curl_handle);
187 if(result != CURLE_OK)
189 std::cerr <<
"Error: [" << result <<
"] - " << errorBuffer << std::endl;
193 __COUT__ <<
"ECL Cleanup" << std::endl;
195 curl_easy_cleanup(curl_handle);
196 curl_slist_free_all(headers);
198 curl_global_cleanup();
203 Attachment_t ECLConnection::MakeAttachmentImage(std::string
const& imageFileName)
206 std::string fileNameShort = imageFileName;
207 if(fileNameShort.rfind(
'/') != std::string::npos)
209 fileNameShort = fileNameShort.substr(imageFileName.rfind(
'/'));
211 std::ifstream fin(imageFileName, std::ios::in | std::ios::binary);
212 fin.seekg(0, std::ios::end);
213 std::streamsize size = fin.tellg();
214 fin.seekg(0, std::ios::beg);
215 std::vector<char> buffer(size);
216 if(!fin.read(buffer.data(), size))
218 __COUT__ <<
"ECLConnection: Error reading file: " << imageFileName << std::endl;
224 ::xml_schema::base64_binary(&buffer[0], size),
"image", fileNameShort);
229 Attachment_t ECLConnection::MakeAttachmentFile(std::string
const& fileName)
232 std::string fileNameShort = fileName;
233 if(fileNameShort.rfind(
'/') != std::string::npos)
235 fileNameShort = fileNameShort.substr(fileName.rfind(
'/'));
237 std::ifstream fin(fileName, std::ios::in | std::ios::binary);
238 fin.seekg(0, std::ios::end);
239 std::streamsize size = fin.tellg();
240 fin.seekg(0, std::ios::beg);
242 std::vector<char> buffer(size);
243 if(!fin.read(buffer.data(), size))
245 __COUT__ <<
"ECLConnection: Error reading file: " << fileName;
251 ::xml_schema::base64_binary(&buffer[0], size),
"file", fileNameShort);