| @@ -4,9 +4,10 @@ | |||||
| namespace rack { | namespace rack { | ||||
| namespace network { | |||||
| enum RequestMethod { | |||||
| enum Method { | |||||
| METHOD_GET, | METHOD_GET, | ||||
| METHOD_POST, | METHOD_POST, | ||||
| METHOD_PUT, | METHOD_PUT, | ||||
| @@ -16,13 +17,14 @@ enum RequestMethod { | |||||
| /** Requests a JSON API URL over HTTP(S), using the data as the query (GET) or the body (POST, etc) | /** Requests a JSON API URL over HTTP(S), using the data as the query (GET) or the body (POST, etc) | ||||
| Caller must json_decref(). | Caller must json_decref(). | ||||
| */ | */ | ||||
| json_t *requestJson(RequestMethod method, std::string url, json_t *dataJ); | |||||
| json_t *requestJson(Method method, std::string url, json_t *dataJ); | |||||
| /** Returns true if downloaded successfully */ | /** Returns true if downloaded successfully */ | ||||
| bool requestDownload(std::string url, std::string filename, float *progress); | bool requestDownload(std::string url, std::string filename, float *progress); | ||||
| /** URL-encodes `s` */ | /** URL-encodes `s` */ | ||||
| std::string requestEscape(std::string s); | |||||
| std::string encodeUrl(std::string s); | |||||
| /** Computes the SHA256 of the file at `filename` */ | /** Computes the SHA256 of the file at `filename` */ | ||||
| std::string requestSHA256File(std::string filename); | |||||
| std::string computeSHA256File(std::string filename); | |||||
| } // namespace network | |||||
| } // namespace rack | } // namespace rack | ||||
| @@ -1,6 +1,5 @@ | |||||
| #include "app.hpp" | #include "app.hpp" | ||||
| #include "window.hpp" | #include "window.hpp" | ||||
| #include "util/request.hpp" | |||||
| #include "osdialog.h" | #include "osdialog.h" | ||||
| #include <string.h> | #include <string.h> | ||||
| #include <thread> | #include <thread> | ||||
| @@ -1,5 +1,5 @@ | |||||
| #include "app.hpp" | #include "app.hpp" | ||||
| #include "util/request.hpp" | |||||
| #include "network.hpp" | |||||
| #include <thread> | #include <thread> | ||||
| @@ -20,7 +20,7 @@ RackScene *gRackScene = NULL; | |||||
| static void checkVersion() { | static void checkVersion() { | ||||
| json_t *resJ = requestJson(METHOD_GET, gApiHost + "/version", NULL); | |||||
| json_t *resJ = network::requestJson(network::METHOD_GET, gApiHost + "/version", NULL); | |||||
| if (resJ) { | if (resJ) { | ||||
| json_t *versionJ = json_object_get(resJ, "version"); | json_t *versionJ = json_object_get(resJ, "version"); | ||||
| @@ -1,11 +1,14 @@ | |||||
| #include "util/common.hpp" | |||||
| #include "util/request.hpp" | |||||
| #define CURL_STATICLIB | #define CURL_STATICLIB | ||||
| #include <curl/curl.h> | #include <curl/curl.h> | ||||
| #include <openssl/sha.h> | #include <openssl/sha.h> | ||||
| #include "util/common.hpp" | |||||
| #include "network.hpp" | |||||
| namespace rack { | namespace rack { | ||||
| namespace network { | |||||
| static size_t writeStringCallback(char *ptr, size_t size, size_t nmemb, void *userdata) { | static size_t writeStringCallback(char *ptr, size_t size, size_t nmemb, void *userdata) { | ||||
| std::string *str = (std::string*) userdata; | std::string *str = (std::string*) userdata; | ||||
| @@ -15,7 +18,7 @@ static size_t writeStringCallback(char *ptr, size_t size, size_t nmemb, void *us | |||||
| } | } | ||||
| json_t *requestJson(RequestMethod method, std::string url, json_t *dataJ) { | |||||
| json_t *requestJson(Method method, std::string url, json_t *dataJ) { | |||||
| CURL *curl = curl_easy_init(); | CURL *curl = curl_easy_init(); | ||||
| assert(curl); | assert(curl); | ||||
| @@ -149,7 +152,7 @@ bool requestDownload(std::string url, std::string filename, float *progress) { | |||||
| return res == CURLE_OK; | return res == CURLE_OK; | ||||
| } | } | ||||
| std::string requestEscape(std::string s) { | |||||
| std::string encodeUrl(std::string s) { | |||||
| CURL *curl = curl_easy_init(); | CURL *curl = curl_easy_init(); | ||||
| assert(curl); | assert(curl); | ||||
| char *escaped = curl_easy_escape(curl, s.c_str(), s.size()); | char *escaped = curl_easy_escape(curl, s.c_str(), s.size()); | ||||
| @@ -159,7 +162,7 @@ std::string requestEscape(std::string s) { | |||||
| return ret; | return ret; | ||||
| } | } | ||||
| std::string requestSHA256File(std::string filename) { | |||||
| std::string computeSHA256File(std::string filename) { | |||||
| FILE *f = fopen(filename.c_str(), "rb"); | FILE *f = fopen(filename.c_str(), "rb"); | ||||
| if (!f) | if (!f) | ||||
| return ""; | return ""; | ||||
| @@ -191,4 +194,5 @@ std::string requestSHA256File(std::string filename) { | |||||
| } | } | ||||
| } // namespace network | |||||
| } // namespace rack | } // namespace rack | ||||
| @@ -1,7 +1,7 @@ | |||||
| #include "plugin.hpp" | #include "plugin.hpp" | ||||
| #include "app.hpp" | #include "app.hpp" | ||||
| #include "asset.hpp" | #include "asset.hpp" | ||||
| #include "util/request.hpp" | |||||
| #include "network.hpp" | |||||
| #include "osdialog.h" | #include "osdialog.h" | ||||
| #include <stdio.h> | #include <stdio.h> | ||||
| @@ -175,14 +175,14 @@ static bool syncPlugin(std::string slug, json_t *manifestJ, bool dryRun) { | |||||
| if (dryRun) { | if (dryRun) { | ||||
| downloadUrl += "/available"; | downloadUrl += "/available"; | ||||
| } | } | ||||
| downloadUrl += "?token=" + requestEscape(gToken); | |||||
| downloadUrl += "&slug=" + requestEscape(slug); | |||||
| downloadUrl += "&version=" + requestEscape(latestVersion); | |||||
| downloadUrl += "&arch=" + requestEscape(arch); | |||||
| downloadUrl += "?token=" + network::encodeUrl(gToken); | |||||
| downloadUrl += "&slug=" + network::encodeUrl(slug); | |||||
| downloadUrl += "&version=" + network::encodeUrl(latestVersion); | |||||
| downloadUrl += "&arch=" + network::encodeUrl(arch); | |||||
| if (dryRun) { | if (dryRun) { | ||||
| // Check if available | // Check if available | ||||
| json_t *availableResJ = requestJson(METHOD_GET, downloadUrl, NULL); | |||||
| json_t *availableResJ = network::requestJson(network::METHOD_GET, downloadUrl, NULL); | |||||
| if (!availableResJ) { | if (!availableResJ) { | ||||
| warn("Could not check whether download is available"); | warn("Could not check whether download is available"); | ||||
| return false; | return false; | ||||
| @@ -200,7 +200,7 @@ static bool syncPlugin(std::string slug, json_t *manifestJ, bool dryRun) { | |||||
| // Download zip | // Download zip | ||||
| std::string pluginDest = asset::local("plugins/" + slug + ".zip"); | std::string pluginDest = asset::local("plugins/" + slug + ".zip"); | ||||
| if (!requestDownload(downloadUrl, pluginDest, &downloadProgress)) { | |||||
| if (!network::requestDownload(downloadUrl, pluginDest, &downloadProgress)) { | |||||
| warn("Plugin %s download was unsuccessful", slug.c_str()); | warn("Plugin %s download was unsuccessful", slug.c_str()); | ||||
| return false; | return false; | ||||
| } | } | ||||
| @@ -385,7 +385,7 @@ bool pluginSync(bool dryRun) { | |||||
| // Get user's plugins list | // Get user's plugins list | ||||
| json_t *pluginsReqJ = json_object(); | json_t *pluginsReqJ = json_object(); | ||||
| json_object_set(pluginsReqJ, "token", json_string(gToken.c_str())); | json_object_set(pluginsReqJ, "token", json_string(gToken.c_str())); | ||||
| json_t *pluginsResJ = requestJson(METHOD_GET, gApiHost + "/plugins", pluginsReqJ); | |||||
| json_t *pluginsResJ = network::requestJson(network::METHOD_GET, gApiHost + "/plugins", pluginsReqJ); | |||||
| json_decref(pluginsReqJ); | json_decref(pluginsReqJ); | ||||
| if (!pluginsResJ) { | if (!pluginsResJ) { | ||||
| warn("Request for user's plugins failed"); | warn("Request for user's plugins failed"); | ||||
| @@ -402,7 +402,7 @@ bool pluginSync(bool dryRun) { | |||||
| } | } | ||||
| // Get community manifests | // Get community manifests | ||||
| json_t *manifestsResJ = requestJson(METHOD_GET, gApiHost + "/community/manifests", NULL); | |||||
| json_t *manifestsResJ = network::requestJson(network::METHOD_GET, gApiHost + "/community/manifests", NULL); | |||||
| if (!manifestsResJ) { | if (!manifestsResJ) { | ||||
| warn("Request for community manifests failed"); | warn("Request for community manifests failed"); | ||||
| return false; | return false; | ||||
| @@ -450,7 +450,7 @@ void pluginLogIn(std::string email, std::string password) { | |||||
| json_t *reqJ = json_object(); | json_t *reqJ = json_object(); | ||||
| json_object_set(reqJ, "email", json_string(email.c_str())); | json_object_set(reqJ, "email", json_string(email.c_str())); | ||||
| json_object_set(reqJ, "password", json_string(password.c_str())); | json_object_set(reqJ, "password", json_string(password.c_str())); | ||||
| json_t *resJ = requestJson(METHOD_POST, gApiHost + "/token", reqJ); | |||||
| json_t *resJ = network::requestJson(network::METHOD_POST, gApiHost + "/token", reqJ); | |||||
| json_decref(reqJ); | json_decref(reqJ); | ||||
| if (resJ) { | if (resJ) { | ||||