From 37b9ece904e4937b6d725d115619e4e3cd189489 Mon Sep 17 00:00:00 2001 From: Yuriy Chumak Date: Sun, 27 Jun 2021 04:07:44 -0700 Subject: [PATCH] UI: Add request type param to GetRemoteText --- UI/auth-oauth.cpp | 2 +- UI/auth-restream.cpp | 4 ++-- UI/auth-twitch.cpp | 4 ++-- UI/remote-text.cpp | 17 ++++++++++++++++- UI/remote-text.hpp | 2 +- UI/win-update/win-update.cpp | 6 +++--- 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/UI/auth-oauth.cpp b/UI/auth-oauth.cpp index a27c49c17..8c94a11fd 100644 --- a/UI/auth-oauth.cpp +++ b/UI/auth-oauth.cpp @@ -212,7 +212,7 @@ try { auto func = [&]() { success = GetRemoteFile(url, output, error, nullptr, - "application/x-www-form-urlencoded", + "application/x-www-form-urlencoded", "", post_data.c_str(), std::vector(), nullptr, 5); }; diff --git a/UI/auth-restream.cpp b/UI/auth-restream.cpp index ee6c8b009..1429bef6d 100644 --- a/UI/auth-restream.cpp +++ b/UI/auth-restream.cpp @@ -58,8 +58,8 @@ try { auto func = [&]() { success = GetRemoteFile(RESTREAM_STREAMKEY_URL, output, error, - nullptr, "application/json", nullptr, - headers, nullptr, 5); + nullptr, "application/json", "", + nullptr, headers, nullptr, 5); }; ExecThreadedWithoutBlocking( diff --git a/UI/auth-twitch.cpp b/UI/auth-twitch.cpp index d7a98c3c3..91bd6861a 100644 --- a/UI/auth-twitch.cpp +++ b/UI/auth-twitch.cpp @@ -78,8 +78,8 @@ try { auto func = [&]() { success = GetRemoteFile("https://api.twitch.tv/kraken/channel", output, error, &error_code, - "application/json", nullptr, headers, - nullptr, 5); + "application/json", "", nullptr, + headers, nullptr, 5); }; ExecThreadedWithoutBlocking( diff --git a/UI/remote-text.cpp b/UI/remote-text.cpp index 6d9fe2e91..9581ad460 100644 --- a/UI/remote-text.cpp +++ b/UI/remote-text.cpp @@ -121,7 +121,8 @@ static size_t header_write(char *ptr, size_t size, size_t nmemb, bool GetRemoteFile(const char *url, std::string &str, std::string &error, long *responseCode, const char *contentType, - const char *postData, std::vector extraHeaders, + std::string request_type, const char *postData, + std::vector extraHeaders, std::string *signature, int timeoutSec) { vector header_in_list; @@ -178,7 +179,21 @@ bool GetRemoteFile(const char *url, std::string &str, std::string &error, // A lot of servers don't yet support ALPN curl_easy_setopt(curl.get(), CURLOPT_SSL_ENABLE_ALPN, 0); #endif + if (!request_type.empty()) { + if (request_type != "GET") + curl_easy_setopt(curl.get(), + CURLOPT_CUSTOMREQUEST, + request_type.c_str()); + // Special case of "POST" + if (request_type == "POST") { + curl_easy_setopt(curl.get(), CURLOPT_POST, 1); + if (!postData) + curl_easy_setopt(curl.get(), + CURLOPT_POSTFIELDS, + "{}"); + } + } if (postData) { curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, postData); diff --git a/UI/remote-text.hpp b/UI/remote-text.hpp index 91c95618b..bc864c976 100644 --- a/UI/remote-text.hpp +++ b/UI/remote-text.hpp @@ -66,6 +66,6 @@ public: bool GetRemoteFile( const char *url, std::string &str, std::string &error, long *responseCode = nullptr, const char *contentType = nullptr, - const char *postData = nullptr, + std::string request_type = "", const char *postData = nullptr, std::vector extraHeaders = std::vector(), std::string *signature = nullptr, int timeoutSec = 0); diff --git a/UI/win-update/win-update.cpp b/UI/win-update/win-update.cpp index 2722ee75c..56a7134e8 100644 --- a/UI/win-update/win-update.cpp +++ b/UI/win-update/win-update.cpp @@ -370,7 +370,7 @@ try { string data; bool success = GetRemoteFile(url, data, error, &responseCode, nullptr, - nullptr, extraHeaders, &signature); + "", nullptr, extraHeaders, &signature); if (!success || (responseCode != 200 && responseCode != 304)) { if (responseCode == 404) @@ -570,7 +570,7 @@ try { * get manifest from server */ success = GetRemoteFile(WIN_MANIFEST_URL, text, error, &responseCode, - nullptr, nullptr, extraHeaders, &signature); + nullptr, "", nullptr, extraHeaders, &signature); if (!success || (responseCode != 200 && responseCode != 304)) { if (responseCode == 404) @@ -766,7 +766,7 @@ try { * get json from server */ success = GetRemoteFile(WIN_WHATSNEW_URL, text, error, &responseCode, - nullptr, nullptr, extraHeaders, &signature); + nullptr, "", nullptr, extraHeaders, &signature); if (!success || (responseCode != 200 && responseCode != 304)) { if (responseCode == 404)