$treeview $search $mathjax $extrastylesheet
otsdaq_utilities
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include <openssl/md5.h> 00002 #include <otsdaq-utilities/ECLWriter/ECLConnection.h> 00003 #include <cstring> 00004 #include <fstream> 00005 #include <iomanip> 00006 #include <sstream> 00007 #include "otsdaq-core/Macros/CoutMacros.h" 00008 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00009 00010 ECLConnection::ECLConnection(std::string user, std::string pwd, std::string url) 00011 { 00012 _user = user; 00013 _pwd = pwd; 00014 _url = url; 00015 00016 srand(time(NULL)); 00017 } 00018 00019 size_t ECLConnection::WriteMemoryCallback(char* data, 00020 size_t size, 00021 size_t nmemb, 00022 std::string* buffer) 00023 { 00024 size_t realsize = 0; 00025 00026 if(buffer != NULL) 00027 { 00028 buffer->append(data, size * nmemb); 00029 realsize = size * nmemb; 00030 } 00031 00032 return realsize; 00033 } 00034 00035 bool ECLConnection::Get(std::string s, std::string& response) 00036 { 00037 response = "NULL"; 00038 00039 char errorBuffer[CURL_ERROR_SIZE]; 00040 std::string buffer; 00041 CURL* curl_handle; 00042 00043 curl_global_init(CURL_GLOBAL_ALL); 00044 00045 /* init the curl session */ 00046 curl_handle = curl_easy_init(); 00047 00048 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorBuffer); 00049 00050 std::string fullURL = _url + s; 00051 00052 /* specify URL to get */ 00053 curl_easy_setopt(curl_handle, CURLOPT_URL, fullURL.c_str()); 00054 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1); 00055 00056 /* send all data to this function */ 00057 curl_easy_setopt( 00058 curl_handle, CURLOPT_WRITEFUNCTION, ECLConnection::WriteMemoryCallback); 00059 00060 /* we pass our 'chunk' struct to the callback function */ 00061 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &buffer); 00062 00063 /* some servers don't like requests that are made without a user-agent 00064 field, so we provide one */ 00065 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 00066 00067 /* get it! */ 00068 CURLcode result = curl_easy_perform(curl_handle); 00069 00070 /* cleanup curl stuff */ 00071 curl_easy_cleanup(curl_handle); 00072 00073 if(result == CURLE_OK) 00074 response = buffer; 00075 else 00076 { 00077 std::cerr << "Error: [" << result << "] - " << errorBuffer << std::endl; 00078 return false; 00079 } 00080 00081 curl_global_cleanup(); 00082 00083 return true; 00084 } 00085 00086 bool ECLConnection::Search(std::string s) { return false; } 00087 00088 std::string ECLConnection::MakeSaltString() 00089 { 00090 std::string rndString = ""; 00091 00092 std::string chars( 00093 "abcdefghijklmnopqrstuvwxyz" 00094 // "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 00095 "1234567890"); 00096 for(int i = 0; i < 10; ++i) 00097 { 00098 rndString += chars[rand() % chars.size()]; 00099 } 00100 00101 return rndString; 00102 } 00103 00104 bool ECLConnection::Post(ECLEntry_t& e) 00105 { 00106 std::string safe_url; 00107 if(!Get("/secureURL", safe_url)) 00108 return false; 00109 00110 std::string rndString = MakeSaltString(); 00111 00112 std::string myURL = "/E/xml_post?"; 00113 std::string mySalt = "salt=" + rndString; 00114 std::string fullURL = _url + myURL + mySalt; 00115 00116 std::string myData = mySalt + ":" + _pwd + ":"; 00117 00118 // create text from xml form, but need to remove all \n's 00119 std::ostringstream oss; 00120 entry(oss, e); 00121 std::string eclString = oss.str(); 00122 __COUT__ << "ECL XML is: " << eclString << std::endl; 00123 // std::string eclString = e.entry(); 00124 eclString = eclString.substr(eclString.find_first_of(">") + 2); 00125 00126 while(eclString.find('\n') != std::string::npos) 00127 { 00128 eclString = eclString.erase(eclString.find('\n'), 1); 00129 } 00130 while(eclString.find('\r') != std::string::npos) 00131 { 00132 eclString = eclString.erase(eclString.find('\r'), 1); 00133 } 00134 while(eclString.find(" <") != std::string::npos) 00135 { 00136 eclString = eclString.erase(eclString.find(" <"), 1); 00137 } 00138 boost::trim(eclString); 00139 myData += eclString; 00140 __COUT__ << "ECL Hash string is: " << myData << std::endl; 00141 unsigned char resultMD5[MD5_DIGEST_LENGTH]; 00142 MD5((unsigned char*)myData.c_str(), myData.size(), resultMD5); 00143 00144 std::string xSig; 00145 char buf[3]; 00146 for(auto i = 0; i < MD5_DIGEST_LENGTH; i++) 00147 { 00148 sprintf(buf, "%02x", resultMD5[i]); 00149 xSig.append(buf); 00150 } 00151 __COUT__ << "ECL MD5 Signature is: " << xSig << std::endl; 00152 00153 CURL* curl_handle; 00154 char errorBuffer[CURL_ERROR_SIZE]; 00155 00156 curl_global_init(CURL_GLOBAL_ALL); 00157 00158 /* init the curl session */ 00159 00160 curl_handle = curl_easy_init(); 00161 00162 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorBuffer); 00163 00164 /* specify URL to get */ 00165 00166 struct curl_slist* headers = NULL; 00167 std::string buff = "X-User: " + _user; 00168 headers = curl_slist_append(headers, buff.c_str()); 00169 headers = curl_slist_append(headers, "Content-type: text/xml"); 00170 headers = curl_slist_append(headers, "X-Signature-Method: md5"); 00171 buff = "X-Signature: " + xSig; 00172 headers = curl_slist_append(headers, buff.c_str()); 00173 00174 const char* estr = eclString.c_str(); 00175 00176 __COUT__ << "ECL Setting message headers" << std::endl; 00177 curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, estr); 00178 curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers); 00179 curl_easy_setopt(curl_handle, CURLOPT_URL, fullURL.c_str()); 00180 // curl_easy_setopt(curl_handle, CURLOPT_VERBOSE,1); 00181 00182 // post it! 00183 00184 __COUT__ << "ECL Posting message" << std::endl; 00185 CURLcode result = curl_easy_perform(curl_handle); 00186 00187 if(result != CURLE_OK) 00188 { 00189 std::cerr << "Error: [" << result << "] - " << errorBuffer << std::endl; 00190 return false; 00191 } 00192 00193 __COUT__ << "ECL Cleanup" << std::endl; 00194 // cleanup curl stuff 00195 curl_easy_cleanup(curl_handle); 00196 curl_slist_free_all(headers); 00197 00198 curl_global_cleanup(); 00199 00200 return true; 00201 } 00202 00203 Attachment_t ECLConnection::MakeAttachmentImage(std::string const& imageFileName) 00204 { 00205 Attachment_t attachment; 00206 std::string fileNameShort = imageFileName; 00207 if(fileNameShort.rfind('/') != std::string::npos) 00208 { 00209 fileNameShort = fileNameShort.substr(imageFileName.rfind('/')); 00210 } 00211 std::ifstream fin(imageFileName, std::ios::in | std::ios::binary); 00212 fin.seekg(0, std::ios::end); 00213 std::streamsize size = fin.tellg(); 00214 fin.seekg(0, std::ios::beg); 00215 std::vector<char> buffer(size); 00216 if(!fin.read(buffer.data(), size)) 00217 { 00218 __COUT__ << "ECLConnection: Error reading file: " << imageFileName << std::endl; 00219 attachment = Attachment_t("Image=none", fileNameShort); 00220 } 00221 else 00222 { 00223 attachment = Attachment_t( 00224 ::xml_schema::base64_binary(&buffer[0], size), "image", fileNameShort); 00225 } 00226 return attachment; 00227 } 00228 00229 Attachment_t ECLConnection::MakeAttachmentFile(std::string const& fileName) 00230 { 00231 Attachment_t attachment; 00232 std::string fileNameShort = fileName; 00233 if(fileNameShort.rfind('/') != std::string::npos) 00234 { 00235 fileNameShort = fileNameShort.substr(fileName.rfind('/')); 00236 } 00237 std::ifstream fin(fileName, std::ios::in | std::ios::binary); 00238 fin.seekg(0, std::ios::end); 00239 std::streamsize size = fin.tellg(); 00240 fin.seekg(0, std::ios::beg); 00241 00242 std::vector<char> buffer(size); 00243 if(!fin.read(buffer.data(), size)) 00244 { 00245 __COUT__ << "ECLConnection: Error reading file: " << fileName; 00246 attachment = Attachment_t("File=none", fileNameShort); 00247 } 00248 else 00249 { 00250 attachment = Attachment_t( 00251 ::xml_schema::base64_binary(&buffer[0], size), "file", fileNameShort); 00252 } 00253 return attachment; 00254 }