#include "common/pch.h" #include #include #include #include"StringUtil.h" #include"ProjectBuildNetworkApi.h" #include "JsonUtil.h" #include #include #include #include "md5.h" #include #include #include #include #include #include string ProjectBuildNetworkApi::get_rquest_id() { string requestIdStr; std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); // ½«µ±Ç°Ê±¼äµãת»»Îªtime_tÀàÐÍ std::time_t currentTime = std::chrono::system_clock::to_time_t(now); requestIdStr.append("_").append(std::to_string(currentTime)).append("_"); // Éú³ÉËæ»úÊý std::random_device rd; std::mt19937 rng(rd()); std::uniform_int_distribution intDist(1, 100000); // Éú³É·¶Î§ÔÚ1-100Ö®¼äµÄÕûÊý int randomInt = intDist(rng); requestIdStr.append(std::to_string(randomInt)); return requestIdStr; } string ProjectBuildNetworkApi::get_sign(rapidjson::Document& document) { //¼ÆËãÇ©Ãû std::list strList; for (rapidjson::Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr) { std::cout << "¼ü: " << itr->name.GetString() << ", Öµ: "; if (itr->value.IsObject() || itr->value.IsArray()) { //Èç¹ûÊǶÔÏó rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); itr->value.Accept(writer); std::string jsonString = buffer.GetString(); strList.push_back(string(itr->name.GetString()).append("=").append(jsonString)); } else { if (itr->value.IsString()) { strList.push_back(string(itr->name.GetString()).append("=").append(itr->value.GetString())); } else if (itr->value.IsInt()) { strList.push_back(string(itr->name.GetString()).append("=").append(std::to_string(itr->value.GetInt()))); } } } strList.sort(); strList.push_back("%Yeshi2014@#."); string src = ""; int index = 0; for (std::list::iterator el = strList.begin(); el != strList.end(); el++) { src.append(*el); index++; if (index != strList.size()) { src.append("&"); } } //MD5 //cout << "¼ÓÃÜǰ×Ö·û´®£º" < writer(buffer); document.Accept(writer); std::string jsonString = buffer.GetString(); //·¢ËÍÊý¾Ý std::stringstream ss; ss << std::setw(8) << std::setfill('0') << jsonString.length(); std::string length_str = string("##").append(ss.str()); std::string fstr = length_str.append(jsonString); return fstr; } static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) { ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; } std::string ProjectBuildNetworkApi::base_request_get(string ip, int port,string path, map params) { CURL* curl = curl_easy_init(); std::string response; if (curl) { string url = "http://"; url.append(ip).append(":").append(to_string(port)).append(path); int index = 0; if (!params.empty()) { url.append("?"); for (auto it = params.begin(); it != params.end(); ++it) { url.append(it->first).append("="); char* output = curl_easy_escape(curl, it->second.c_str(), it->second.length()); url.append(output); if (index != params.size() - 1) { url.append("&"); } index++; } } curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // ÉèÖÃURL curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); // ÉèÖûص÷ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);// ´«µÝ½ÓÊÕ»º³åÇø curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 600L);// Á¬½Ó³¬Ê± 600 Ãë curl_easy_setopt(curl, CURLOPT_TIMEOUT, 600L); // ´«Êä×ܳ¬Ê± 600 Ãë CURLcode res = curl_easy_perform(curl); // Ö´ÐÐÇëÇó if (res != CURLE_OK) { std::cerr << "cURL failed: " << curl_easy_strerror(res) << std::endl; throw; } else { long response_code; curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); std::cout << "Response code: " << response_code << std::endl; // ´òÓ¡ÏìÓ¦ÄÚÈÝ std::cout << "Response body:\n" << response << std::endl; } curl_easy_cleanup(curl); // ÇåÀí×ÊÔ´ return response; } throw; } string ProjectBuildNetworkApi::build(string ip, int port, string dir, string script, string output) { map params; params["dir"] = dir; params["script"] = script; params["output"] = output; return base_request_get(ip, port, "/build",params); }