1 #include <otsdaq-utilities/ECLWriter/ECLConnection.h>
3 #include <openssl/md5.h>
7 #include "otsdaq-core/MessageFacility/MessageFacility.h"
8 #include "otsdaq-core/Macros/CoutMacros.h"
10 ECLConnection::ECLConnection(std::string user,
22 size_t ECLConnection::WriteMemoryCallback(
char *data,
size_t size,
23 size_t nmemb, std::string* buffer)
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);
57 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, ECLConnection::WriteMemoryCallback);
60 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &buffer);
64 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
"libcurl-agent/1.0");
67 CURLcode result = curl_easy_perform(curl_handle);
70 curl_easy_cleanup(curl_handle);
72 if (result == CURLE_OK)
75 std::cerr <<
"Error: [" << result <<
"] - " << errorBuffer << std::endl;
79 curl_global_cleanup();
84 bool ECLConnection::Search(std::string s)
89 std::string ECLConnection::MakeSaltString()
91 std::string rndString =
"";
93 std::string chars(
"abcdefghijklmnopqrstuvwxyz"
96 for (
int i = 0; i < 10; ++i) {
97 rndString += chars[rand() % chars.size()];
106 std::string safe_url;
107 if (!Get(
"/secureURL", safe_url))
return false;
109 std::string rndString = MakeSaltString();
111 std::string myURL =
"/E/xml_post?";
112 std::string mySalt =
"salt=" + rndString;
113 std::string fullURL = _url + myURL + mySalt;
115 std::string myData = mySalt +
":" + _pwd +
":";
118 std::ostringstream oss;
120 std::string eclString = oss.str();
121 __COUT__ <<
"ECL XML is: " << eclString << std::endl;
123 eclString = eclString.substr(eclString.find_first_of(
">") + 2);
125 while (eclString.find(
'\n') != std::string::npos)
127 eclString = eclString.erase(eclString.find(
'\n'), 1);
129 while (eclString.find(
'\r') != std::string::npos)
131 eclString = eclString.erase(eclString.find(
'\r'), 1);
133 while (eclString.find(
" <") != std::string::npos)
135 eclString = eclString.erase(eclString.find(
" <"), 1);
137 boost::trim(eclString);
139 __COUT__ <<
"ECL Hash string is: " << myData << std::endl;
140 unsigned char resultMD5[MD5_DIGEST_LENGTH];
141 MD5((
unsigned char*)myData.c_str(), myData.size(), resultMD5);
145 for (
auto i = 0; i < MD5_DIGEST_LENGTH; i++) {
146 sprintf(buf,
"%02x", resultMD5[i]);
149 __COUT__ <<
"ECL MD5 Signature is: " << xSig << std::endl;
152 char errorBuffer[CURL_ERROR_SIZE];
154 curl_global_init(CURL_GLOBAL_ALL);
158 curl_handle = curl_easy_init();
160 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorBuffer);
164 struct curl_slist* headers = NULL;
165 std::string buff =
"X-User: " + _user;
166 headers = curl_slist_append(headers, buff.c_str());
167 headers = curl_slist_append(headers,
"Content-type: text/xml");
168 headers = curl_slist_append(headers,
"X-Signature-Method: md5");
169 buff =
"X-Signature: " + xSig;
170 headers = curl_slist_append(headers, buff.c_str());
172 const char* estr = eclString.c_str();
174 __COUT__ <<
"ECL Setting message headers" << std::endl;
175 curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, estr);
176 curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
177 curl_easy_setopt(curl_handle, CURLOPT_URL, fullURL.c_str());
182 __COUT__ <<
"ECL Posting message" << std::endl;
183 CURLcode result = curl_easy_perform(curl_handle);
185 if (result != CURLE_OK) {
186 std::cerr <<
"Error: [" << result <<
"] - " << errorBuffer << std::endl;
190 __COUT__ <<
"ECL Cleanup" << std::endl;
192 curl_easy_cleanup(curl_handle);
193 curl_slist_free_all(headers);
195 curl_global_cleanup();
202 Attachment_t ECLConnection::MakeAttachmentImage(std::string
const& imageFileName) {
204 std::string fileNameShort = imageFileName;
205 if (fileNameShort.rfind(
'/') != std::string::npos) {
206 fileNameShort = fileNameShort.substr(imageFileName.rfind(
'/'));
208 std::ifstream fin(imageFileName, std::ios::in | std::ios::binary);
209 fin.seekg(0, std::ios::end);
210 std::streamsize size = fin.tellg();
211 fin.seekg(0, std::ios::beg);
212 std::vector<char> buffer(size);
213 if (!fin.read(buffer.data(), size))
215 __COUT__ <<
"ECLConnection: Error reading file: " << imageFileName << std::endl;
219 attachment =
Attachment_t(::xml_schema::base64_binary(&buffer[0], size),
"image", fileNameShort);
225 Attachment_t ECLConnection::MakeAttachmentFile(std::string
const& fileName) {
227 std::string fileNameShort = fileName;
228 if (fileNameShort.rfind(
'/') != std::string::npos) {
229 fileNameShort = fileNameShort.substr(fileName.rfind(
'/'));
231 std::ifstream fin(fileName, std::ios::in | std::ios::binary);
232 fin.seekg(0, std::ios::end);
233 std::streamsize size = fin.tellg();
234 fin.seekg(0, std::ios::beg);
236 std::vector<char> buffer(size);
237 if (!fin.read(buffer.data(), size))
239 __COUT__ <<
"ECLConnection: Error reading file: " << fileName;
243 attachment =
Attachment_t(::xml_schema::base64_binary(&buffer[0], size),
"file", fileNameShort);