remote-text.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <util/curl/curl-helper.h>
  15. #include "obs-app.hpp"
  16. #include "qt-wrappers.hpp"
  17. #include "remote-text.hpp"
  18. using namespace std;
  19. static auto curl_deleter = [](CURL *curl) {
  20. curl_easy_cleanup(curl);
  21. };
  22. using Curl = unique_ptr<CURL, decltype(curl_deleter)>;
  23. static size_t string_write(char *ptr, size_t size, size_t nmemb, string &str)
  24. {
  25. size_t total = size * nmemb;
  26. if (total)
  27. str.append(ptr, total);
  28. return total;
  29. }
  30. void RemoteTextThread::run()
  31. {
  32. char error[CURL_ERROR_SIZE];
  33. CURLcode code;
  34. string versionString("User-Agent: obs-basic ");
  35. versionString += App()->GetVersionString();
  36. string contentTypeString;
  37. if (!contentType.empty()) {
  38. contentTypeString += "Content-Type: ";
  39. contentTypeString += contentType;
  40. }
  41. Curl curl{curl_easy_init(), curl_deleter};
  42. if (curl) {
  43. struct curl_slist *header = nullptr;
  44. string str;
  45. header = curl_slist_append(header, versionString.c_str());
  46. if (!contentTypeString.empty()) {
  47. header = curl_slist_append(header,
  48. contentTypeString.c_str());
  49. }
  50. for (std::string &h : extraHeaders)
  51. header = curl_slist_append(header, h.c_str());
  52. curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
  53. curl_easy_setopt(curl.get(), CURLOPT_ACCEPT_ENCODING, "");
  54. curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, header);
  55. curl_easy_setopt(curl.get(), CURLOPT_ERRORBUFFER, error);
  56. curl_easy_setopt(curl.get(), CURLOPT_FAILONERROR, 1L);
  57. curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION,
  58. string_write);
  59. curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &str);
  60. curl_obs_set_revoke_setting(curl.get());
  61. if (timeoutSec)
  62. curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT,
  63. timeoutSec);
  64. if (!postData.empty()) {
  65. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS,
  66. postData.c_str());
  67. }
  68. code = curl_easy_perform(curl.get());
  69. if (code != CURLE_OK) {
  70. blog(LOG_WARNING,
  71. "RemoteTextThread: HTTP request failed. %s",
  72. strlen(error) ? error : curl_easy_strerror(code));
  73. emit Result(QString(), QT_UTF8(error));
  74. } else {
  75. emit Result(QT_UTF8(str.c_str()), QString());
  76. }
  77. curl_slist_free_all(header);
  78. }
  79. }
  80. static size_t header_write(char *ptr, size_t size, size_t nmemb,
  81. vector<string> &list)
  82. {
  83. string str;
  84. size_t total = size * nmemb;
  85. if (total)
  86. str.append(ptr, total);
  87. if (str.back() == '\n')
  88. str.resize(str.size() - 1);
  89. if (str.back() == '\r')
  90. str.resize(str.size() - 1);
  91. list.push_back(std::move(str));
  92. return total;
  93. }
  94. bool GetRemoteFile(const char *url, std::string &str, std::string &error,
  95. long *responseCode, const char *contentType,
  96. std::string request_type, const char *postData,
  97. std::vector<std::string> extraHeaders,
  98. std::string *signature, int timeoutSec, bool fail_on_error,
  99. int postDataSize)
  100. {
  101. vector<string> header_in_list;
  102. char error_in[CURL_ERROR_SIZE];
  103. CURLcode code = CURLE_FAILED_INIT;
  104. error_in[0] = 0;
  105. string versionString("User-Agent: obs-basic ");
  106. versionString += App()->GetVersionString();
  107. string contentTypeString;
  108. if (contentType) {
  109. contentTypeString += "Content-Type: ";
  110. contentTypeString += contentType;
  111. }
  112. Curl curl{curl_easy_init(), curl_deleter};
  113. if (curl) {
  114. struct curl_slist *header = nullptr;
  115. header = curl_slist_append(header, versionString.c_str());
  116. if (!contentTypeString.empty()) {
  117. header = curl_slist_append(header,
  118. contentTypeString.c_str());
  119. }
  120. for (std::string &h : extraHeaders)
  121. header = curl_slist_append(header, h.c_str());
  122. curl_easy_setopt(curl.get(), CURLOPT_URL, url);
  123. curl_easy_setopt(curl.get(), CURLOPT_ACCEPT_ENCODING, "");
  124. curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, header);
  125. curl_easy_setopt(curl.get(), CURLOPT_ERRORBUFFER, error_in);
  126. if (fail_on_error)
  127. curl_easy_setopt(curl.get(), CURLOPT_FAILONERROR, 1L);
  128. curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION,
  129. string_write);
  130. curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &str);
  131. curl_obs_set_revoke_setting(curl.get());
  132. if (signature) {
  133. curl_easy_setopt(curl.get(), CURLOPT_HEADERFUNCTION,
  134. header_write);
  135. curl_easy_setopt(curl.get(), CURLOPT_HEADERDATA,
  136. &header_in_list);
  137. }
  138. if (timeoutSec)
  139. curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT,
  140. timeoutSec);
  141. if (!request_type.empty()) {
  142. if (request_type != "GET")
  143. curl_easy_setopt(curl.get(),
  144. CURLOPT_CUSTOMREQUEST,
  145. request_type.c_str());
  146. // Special case of "POST"
  147. if (request_type == "POST") {
  148. curl_easy_setopt(curl.get(), CURLOPT_POST, 1);
  149. if (!postData)
  150. curl_easy_setopt(curl.get(),
  151. CURLOPT_POSTFIELDS,
  152. "{}");
  153. }
  154. }
  155. if (postData) {
  156. if (postDataSize > 0) {
  157. curl_easy_setopt(curl.get(),
  158. CURLOPT_POSTFIELDSIZE,
  159. (long)postDataSize);
  160. }
  161. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS,
  162. postData);
  163. }
  164. code = curl_easy_perform(curl.get());
  165. if (responseCode)
  166. curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE,
  167. responseCode);
  168. if (code != CURLE_OK) {
  169. error = strlen(error_in) ? error_in
  170. : curl_easy_strerror(code);
  171. } else if (signature) {
  172. for (string &h : header_in_list) {
  173. string name = h.substr(0, 13);
  174. // HTTP headers are technically case-insensitive
  175. if (name == "X-Signature: " ||
  176. name == "x-signature: ") {
  177. *signature = h.substr(13);
  178. break;
  179. }
  180. }
  181. }
  182. curl_slist_free_all(header);
  183. }
  184. return code == CURLE_OK;
  185. }