Permalink
Browse files

curl code simplification

  • Loading branch information...
Turupawn committed Oct 17, 2018
1 parent 15d4f0a commit 06b2f9182120455b33d30c3e1fae3ddd4aefe83e
Showing with 220 additions and 228 deletions.
  1. +2 −1 .vscode/settings.json
  2. +18 −16 include/wrappers/CurlUtility.h
  3. +13 −12 src/ModUtility.cpp
  4. +6 −7 src/ModioUtility.cpp
  5. +64 −44 src/wrappers/CurlUtility.cpp
  6. +117 −148 src/wrappers/CurlWrapper.cpp
@@ -74,6 +74,7 @@
"deque": "cpp",
"cinttypes": "cpp",
"stdio.h": "c",
"set": "cpp"
"set": "cpp",
"unordered_set": "cpp"
}
}
@@ -34,20 +34,20 @@ class JsonResponseHandler
std::string response;
std::map<std::string, std::string> headers;
struct curl_slist *slist = NULL;
char* post_fields;
#ifdef MODIO_WINDOWS_DETECTED
curl_mime *mime_form = NULL;
#elif defined(MODIO_OSX_DETECTED) || defined(MODIO_LINUX_DETECTED)
struct curl_httppost *formpost = NULL;
#endif
char *post_fields;
#ifdef MODIO_WINDOWS_DETECTED
curl_mime *mime_form = NULL;
#elif defined(MODIO_OSX_DETECTED) || defined(MODIO_LINUX_DETECTED)
struct curl_httppost *formpost = NULL;
#endif
std::function<void(u32 call_number, u32 response_code, nlohmann::json response_json)> callback;
#ifdef MODIO_WINDOWS_DETECTED
JsonResponseHandler(u32 call_number, struct curl_slist * slist, char* post_fields, curl_mime *curl_mime, std::function<void(u32 call_number, u32 response_code, nlohmann::json response_json)> callback);
#elif defined(MODIO_OSX_DETECTED) || defined(MODIO_LINUX_DETECTED)
JsonResponseHandler(u32 call_number, struct curl_slist * slist, char* post_fields, struct curl_httppost *formpost, std::function<void(u32 call_number, u32 response_code, nlohmann::json response_json)> callback);
#endif
#ifdef MODIO_WINDOWS_DETECTED
JsonResponseHandler(u32 call_number, struct curl_slist *slist, char *post_fields, curl_mime *curl_mime, std::function<void(u32 call_number, u32 response_code, nlohmann::json response_json)> callback);
#elif defined(MODIO_OSX_DETECTED) || defined(MODIO_LINUX_DETECTED)
JsonResponseHandler(u32 call_number, struct curl_slist *slist, char *post_fields, struct curl_httppost *formpost, std::function<void(u32 call_number, u32 response_code, nlohmann::json response_json)> callback);
#endif
~JsonResponseHandler();
};
@@ -58,7 +58,7 @@ class OngoingDownload
std::string url;
struct curl_slist *slist = NULL;
std::function<void(u32 call_number, u32 response_code)> callback;
OngoingDownload(u32 call_number, std::string url, struct curl_slist * slist, std::function<void(u32 call_number, u32 response_code)> callback);
OngoingDownload(u32 call_number, std::string url, struct curl_slist *slist, std::function<void(u32 call_number, u32 response_code)> callback);
~OngoingDownload();
};
@@ -84,7 +84,7 @@ extern CURL *current_modfile_upload_curl_handle;
extern struct curl_slist *current_mod_download_slist;
extern struct curl_slist *current_modfile_upload_slist;
extern struct curl_httppost *current_modfile_upload_httppost;
extern struct curl_httppost *current_modfile_upload_httppost;
extern QueuedModDownload *current_queued_mod_download;
extern QueuedModfileUpload *current_queued_modfile_upload;
@@ -105,7 +105,9 @@ void downloadNextQueuedMod();
void setHeaders(std::vector<std::string> headers, CURL *curl);
void setVerifies(CURL *curl);
void setJsonResponseWrite(CURL *curl);
}
}
std::string mapDataToUrlString(std::map<std::string, std::string> data);
std::string multimapDataToUrlString(std::multimap<std::string, std::string> data);
} // namespace curlwrapper
} // namespace modio
#endif
@@ -55,9 +55,9 @@ std::string getCallFileFromCache(std::string url, u32 max_age_seconds)
void addToInstalledModsJson(u32 mod_id, std::string path, u32 modfile_id, u32 date_updated)
{
bool mod_reinstalled = false;
for(auto &installed_mod : modio::installed_mods)
for (auto &installed_mod : modio::installed_mods)
{
if(installed_mod["mod_id"] == mod_id && installed_mod["path"] == path)
if (installed_mod["mod_id"] == mod_id && installed_mod["path"] == path)
{
installed_mod["modfile_id"] = modfile_id;
installed_mod["date_updated"] = date_updated;
@@ -66,7 +66,7 @@ void addToInstalledModsJson(u32 mod_id, std::string path, u32 modfile_id, u32 da
}
}
if(!mod_reinstalled)
if (!mod_reinstalled)
{
nlohmann::json installed_mod_json;
installed_mod_json["path"] = path;
@@ -103,12 +103,12 @@ void updateInstalledModsJson()
modio::installed_mods.clear();
// Migrating from v0.9.0
if(modio::hasKey(stored_installed_mods, "mods"))
if (modio::hasKey(stored_installed_mods, "mods"))
{
stored_installed_mods = stored_installed_mods["mods"];
}
for(auto stored_installed_mod : stored_installed_mods)
for (auto stored_installed_mod : stored_installed_mods)
{
if (checkIfModIsStillInstalled(stored_installed_mod["path"], stored_installed_mod["mod_id"]))
{
@@ -126,20 +126,21 @@ void clearOldCache()
u32 current_time = modio::getCurrentTime();
nlohmann::json cache_json = modio::openJson(modio::getModIODirectory() + "cache.json");
nlohmann::json resulting_cache_json;
for(auto cache_file_json : cache_json)
for (auto cache_file_json : cache_json)
{
if(modio::hasKey(cache_file_json,"datetime"))
if (modio::hasKey(cache_file_json, "datetime"))
{
u32 cache_time = cache_file_json["datetime"];
u32 time_difference = current_time - cache_time;
if(time_difference > MAX_CACHE_TIME)
if (time_difference > MAX_CACHE_TIME)
{
std::string cache_file_to_delete_path = modio::getModIODirectory() + "cache/";
std::string cache_filename = cache_file_json["file"];
cache_file_to_delete_path += cache_filename;
modio::removeFile(cache_file_to_delete_path);
}else
}
else
{
resulting_cache_json.push_back(cache_file_json);
}
@@ -151,13 +152,13 @@ void clearOldCache()
std::string getInstalledModPath(u32 mod_id)
{
for(auto installed_mod_json : modio::installed_mods)
for (auto installed_mod_json : modio::installed_mods)
{
if(installed_mod_json["mod_id"] == mod_id)
if (installed_mod_json["mod_id"] == mod_id)
{
return installed_mod_json["path"];
}
}
return "";
}
}
} // namespace modio
@@ -48,7 +48,6 @@ void addModsToDownloadQueue(std::vector<u32> mod_ids)
{
modioAddFilterInField(&filter, (char *)"id", (char *)modio::toString(mod_id).c_str());
}
writeLogLine("XX0",MODIO_DEBUGLEVEL_LOG);
modioGetAllMods(NULL, filter, &modio::onAddModsToDownloadQueue);
modioFreeFilter(&filter);
}
@@ -73,9 +72,9 @@ void onGetAllEventsPoll(void *object, ModioResponse response, ModioEvent *events
case MODIO_EVENT_MODFILE_CHANGED:
{
bool reinstall = true;
for(auto installed_mod : modio::installed_mods)
for (auto installed_mod : modio::installed_mods)
{
if(installed_mod["mod_id"] == events_array[i].mod_id &&
if (installed_mod["mod_id"] == events_array[i].mod_id &&
installed_mod["date_updated"] >= events_array[i].date_added)
{
modio::writeLogLine("Modfile changed event detected but you already have a newer version installed, the modfile will not be downloaded. Mod id: " + modio::toString(events_array[i].mod_id), MODIO_DEBUGLEVEL_LOG);
@@ -112,7 +111,7 @@ void onGetAllEventsPoll(void *object, ModioResponse response, ModioEvent *events
updateModsCache(mod_edited_ids);
if (mod_to_download_queue_ids.size() > 0)
addModsToDownloadQueue(mod_to_download_queue_ids);
nlohmann::json event_polling_json = modio::openJson(modio::getModIODirectory() + "event_polling.json");
event_polling_json["last_mod_event_poll"] = modio::LAST_MOD_EVENT_POLL;
modio::writeJson(modio::getModIODirectory() + "event_polling.json", event_polling_json);
@@ -197,10 +196,10 @@ void pollEvents()
modioAddFilterMinField(&filter, (char *)"date_added", (char *)modio::toString(modio::LAST_MOD_EVENT_POLL).c_str());
modioAddFilterSmallerThanField(&filter, (char *)"date_added", (char *)modio::toString(current_time).c_str());
for(auto installed_mod : modio::installed_mods)
for (auto installed_mod : modio::installed_mods)
{
if(modio::hasKey(installed_mod, "mod_id"))
modioAddFilterInField(&filter, (char *)"mod_id", (char*)modio::toString((u32)installed_mod["mod_id"]).c_str());
if (modio::hasKey(installed_mod, "mod_id"))
modioAddFilterInField(&filter, (char *)"mod_id", (char *)modio::toString((u32)installed_mod["mod_id"]).c_str());
}
modioGetAllEvents(NULL, filter, &onGetAllEventsPoll);
@@ -39,40 +39,40 @@ std::list<QueuedModfileUpload *> getModfileUploadQueue()
}
#ifdef MODIO_WINDOWS_DETECTED
JsonResponseHandler::JsonResponseHandler(u32 call_number, struct curl_slist * slist, char* post_fields, curl_mime *mime_form, std::function<void(u32 call_number, u32 response_code, nlohmann::json response_json)> callback)
{
this->response = "";
this->call_number = call_number;
this->slist = slist;
this->post_fields = post_fields;
this->mime_form = mime_form;
this->callback = callback;
}
JsonResponseHandler::JsonResponseHandler(u32 call_number, struct curl_slist *slist, char *post_fields, curl_mime *mime_form, std::function<void(u32 call_number, u32 response_code, nlohmann::json response_json)> callback)
{
this->response = "";
this->call_number = call_number;
this->slist = slist;
this->post_fields = post_fields;
this->mime_form = mime_form;
this->callback = callback;
}
#elif defined(MODIO_OSX_DETECTED) || defined(MODIO_LINUX_DETECTED)
JsonResponseHandler::JsonResponseHandler(u32 call_number, struct curl_slist * slist, char* post_fields, struct curl_httppost *formpost, std::function<void(u32 call_number, u32 response_code, nlohmann::json response_json)> callback)
{
this->response = "";
this->call_number = call_number;
this->slist = slist;
this->post_fields = post_fields;
this->formpost = formpost;
this->callback = callback;
}
JsonResponseHandler::JsonResponseHandler(u32 call_number, struct curl_slist *slist, char *post_fields, struct curl_httppost *formpost, std::function<void(u32 call_number, u32 response_code, nlohmann::json response_json)> callback)
{
this->response = "";
this->call_number = call_number;
this->slist = slist;
this->post_fields = post_fields;
this->formpost = formpost;
this->callback = callback;
}
#endif
JsonResponseHandler::~JsonResponseHandler()
{
curl_slist_free_all(this->slist);
if(post_fields)
if (post_fields)
delete[] post_fields;
#ifdef MODIO_WINDOWS_DETECTED
curl_mime_free(this->mime_form);
#elif defined(MODIO_OSX_DETECTED) || defined(MODIO_LINUX_DETECTED)
curl_formfree(this->formpost);
#endif
#ifdef MODIO_WINDOWS_DETECTED
curl_mime_free(this->mime_form);
#elif defined(MODIO_OSX_DETECTED) || defined(MODIO_LINUX_DETECTED)
curl_formfree(this->formpost);
#endif
}
OngoingDownload::OngoingDownload(u32 call_number, std::string url, struct curl_slist * slist, std::function<void(u32 call_number, u32 response_code)> callback)
OngoingDownload::OngoingDownload(u32 call_number, std::string url, struct curl_slist *slist, std::function<void(u32 call_number, u32 response_code)> callback)
{
this->url = url;
this->call_number = call_number;
@@ -89,17 +89,17 @@ void updateModDownloadQueue()
{
nlohmann::json mod_download_queue_json = openJson(modio::getModIODirectory() + "mod_download_queue.json");
for(auto &queued_mod_download : modio::curlwrapper::mod_download_queue)
for (auto &queued_mod_download : modio::curlwrapper::mod_download_queue)
{
delete queued_mod_download;
}
modio::curlwrapper::mod_download_queue.clear();
for(auto &queued_mod_download_json : mod_download_queue_json)
for (auto &queued_mod_download_json : mod_download_queue_json)
{
ModioQueuedModDownload modio_queued_mod_download;
modioInitQueuedModDownload(&modio_queued_mod_download, queued_mod_download_json);
QueuedModDownload* queued_mod_download = new QueuedModDownload();
QueuedModDownload *queued_mod_download = new QueuedModDownload();
queued_mod_download->initialize(modio_queued_mod_download);
modio::curlwrapper::mod_download_queue.push_back(queued_mod_download);
modioFreeQueuedModDownload(&modio_queued_mod_download);
@@ -110,7 +110,7 @@ void updateModDownloadQueueFile()
{
modio::writeLogLine("Updating mod download queue file...", MODIO_DEBUGLEVEL_LOG);
nlohmann::json mod_download_queue_json;
for(auto &queued_mod_download : modio::curlwrapper::mod_download_queue)
for (auto &queued_mod_download : modio::curlwrapper::mod_download_queue)
{
mod_download_queue_json.push_back(modio::toJson(*queued_mod_download));
}
@@ -121,41 +121,37 @@ void updateModDownloadQueueFile()
void updateModUploadQueueFile()
{
nlohmann::json mod_upload_queue_json;
for(auto &queued_mod_upload : modfile_upload_queue)
for (auto &queued_mod_upload : modfile_upload_queue)
{
mod_upload_queue_json.push_back(modio::toJson(*queued_mod_upload));
}
writeJson(modio::getModIODirectory() + "mod_upload_queue.json",mod_upload_queue_json);
writeJson(modio::getModIODirectory() + "mod_upload_queue.json", mod_upload_queue_json);
}
void prioritizeModDownload(u32 mod_id)
{
nlohmann::json result_json;
nlohmann::json mod_download_queue_json = openJson(modio::getModIODirectory() + "mod_download_queue.json");
for(auto &queued_mod_download_json : mod_download_queue_json)
for (auto &queued_mod_download_json : mod_download_queue_json)
{
if(modio::hasKey(queued_mod_download_json,"mod")
&& modio::hasKey(queued_mod_download_json["mod"],"id")
&& queued_mod_download_json["mod"]["id"] == mod_id)
if (modio::hasKey(queued_mod_download_json, "mod") && modio::hasKey(queued_mod_download_json["mod"], "id") && queued_mod_download_json["mod"]["id"] == mod_id)
{
result_json.push_back(queued_mod_download_json);
result_json.push_back(queued_mod_download_json);
}
}
for(auto &queued_mod_download_json : mod_download_queue_json)
for (auto &queued_mod_download_json : mod_download_queue_json)
{
if(!modio::hasKey(queued_mod_download_json,"mod")
|| !modio::hasKey(queued_mod_download_json["mod"],"id")
|| queued_mod_download_json["mod"]["id"] != mod_id)
if (!modio::hasKey(queued_mod_download_json, "mod") || !modio::hasKey(queued_mod_download_json["mod"], "id") || queued_mod_download_json["mod"]["id"] != mod_id)
{
result_json.push_back(queued_mod_download_json);
result_json.push_back(queued_mod_download_json);
}
}
writeJson(modio::getModIODirectory() + "mod_download_queue.json",result_json);
writeJson(modio::getModIODirectory() + "mod_download_queue.json", result_json);
if(current_queued_mod_download)
if (current_queued_mod_download)
current_queued_mod_download->state = MODIO_PRIORITIZING_OTHER_DOWNLOAD;
}
@@ -190,5 +186,29 @@ void setJsonResponseWrite(CURL *curl)
curl_easy_setopt(curl, CURLOPT_HEADERDATA, curl);
}
std::string mapDataToUrlString(std::map<std::string, std::string> data)
{
std::string url_string = "";
for (std::map<std::string, std::string>::iterator i = data.begin(); i != data.end(); i++)
{
if (url_string != "")
url_string += "&";
url_string += (*i).first + "=" + (*i).second;
}
return url_string;
}
}
std::string multimapDataToUrlString(std::multimap<std::string, std::string> data)
{
std::string url_string = "";
for (std::map<std::string, std::string>::iterator i = data.begin(); i != data.end(); i++)
{
if (url_string != "")
url_string += "&";
url_string += (*i).first + "=" + (*i).second;
}
return url_string;
}
} // namespace curlwrapper
} // namespace modio
Oops, something went wrong.

0 comments on commit 06b2f91

Please sign in to comment.