RemoteTextThread.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "RemoteTextThread.hpp"
  15. #include <OBSApp.hpp>
  16. #include <qt-wrappers.hpp>
  17. #include <util/curl/curl-helper.h>
  18. #include "moc_RemoteTextThread.cpp"
  19. using namespace std;
  20. static auto curl_deleter = [](CURL *curl) {
  21. curl_easy_cleanup(curl);
  22. };
  23. using Curl = unique_ptr<CURL, decltype(curl_deleter)>;
  24. static size_t string_write(char *ptr, size_t size, size_t nmemb, string &str)
  25. {
  26. size_t total = size * nmemb;
  27. if (total)
  28. str.append(ptr, total);
  29. return total;
  30. }
  31. void RemoteTextThread::run()
  32. {
  33. char error[CURL_ERROR_SIZE];
  34. CURLcode code;
  35. string versionString("User-Agent: obs-basic ");
  36. versionString += App()->GetVersionString();
  37. string contentTypeString;
  38. if (!contentType.empty()) {
  39. contentTypeString += "Content-Type: ";
  40. contentTypeString += contentType;
  41. }
  42. Curl curl{curl_easy_init(), curl_deleter};
  43. if (curl) {
  44. struct curl_slist *header = nullptr;
  45. string str;
  46. header = curl_slist_append(header, versionString.c_str());
  47. if (!contentTypeString.empty()) {
  48. header = curl_slist_append(header, 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, string_write);
  58. curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &str);
  59. curl_obs_set_revoke_setting(curl.get());
  60. if (timeoutSec)
  61. curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, timeoutSec);
  62. if (!postData.empty()) {
  63. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, postData.c_str());
  64. }
  65. code = curl_easy_perform(curl.get());
  66. if (code != CURLE_OK) {
  67. blog(LOG_WARNING, "RemoteTextThread: HTTP request failed. %s",
  68. strlen(error) ? error : curl_easy_strerror(code));
  69. emit Result(QString(), QT_UTF8(error));
  70. } else {
  71. emit Result(QT_UTF8(str.c_str()), QString());
  72. }
  73. curl_slist_free_all(header);
  74. }
  75. }
  76. static size_t header_write(char *ptr, size_t size, size_t nmemb, vector<string> &list)
  77. {
  78. string str;
  79. size_t total = size * nmemb;
  80. if (total)
  81. str.append(ptr, total);
  82. if (str.back() == '\n')
  83. str.resize(str.size() - 1);
  84. if (str.back() == '\r')
  85. str.resize(str.size() - 1);
  86. list.push_back(std::move(str));
  87. return total;
  88. }
  89. bool GetRemoteFile(const char *url, std::string &str, std::string &error, long *responseCode, const char *contentType,
  90. std::string request_type, const char *postData, std::vector<std::string> extraHeaders,
  91. std::string *signature, int timeoutSec, bool fail_on_error, int postDataSize)
  92. {
  93. vector<string> header_in_list;
  94. char error_in[CURL_ERROR_SIZE];
  95. CURLcode code = CURLE_FAILED_INIT;
  96. error_in[0] = 0;
  97. string versionString("User-Agent: obs-basic ");
  98. versionString += App()->GetVersionString();
  99. string contentTypeString;
  100. if (contentType) {
  101. contentTypeString += "Content-Type: ";
  102. contentTypeString += contentType;
  103. }
  104. Curl curl{curl_easy_init(), curl_deleter};
  105. if (curl) {
  106. struct curl_slist *header = nullptr;
  107. header = curl_slist_append(header, versionString.c_str());
  108. if (!contentTypeString.empty()) {
  109. header = curl_slist_append(header, contentTypeString.c_str());
  110. }
  111. for (std::string &h : extraHeaders)
  112. header = curl_slist_append(header, h.c_str());
  113. curl_easy_setopt(curl.get(), CURLOPT_URL, url);
  114. curl_easy_setopt(curl.get(), CURLOPT_ACCEPT_ENCODING, "");
  115. curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, header);
  116. curl_easy_setopt(curl.get(), CURLOPT_ERRORBUFFER, error_in);
  117. if (fail_on_error)
  118. curl_easy_setopt(curl.get(), CURLOPT_FAILONERROR, 1L);
  119. curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, string_write);
  120. curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &str);
  121. curl_obs_set_revoke_setting(curl.get());
  122. if (signature) {
  123. curl_easy_setopt(curl.get(), CURLOPT_HEADERFUNCTION, header_write);
  124. curl_easy_setopt(curl.get(), CURLOPT_HEADERDATA, &header_in_list);
  125. }
  126. if (timeoutSec)
  127. curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, timeoutSec);
  128. if (!request_type.empty()) {
  129. if (request_type != "GET")
  130. curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, request_type.c_str());
  131. // Special case of "POST"
  132. if (request_type == "POST") {
  133. curl_easy_setopt(curl.get(), CURLOPT_POST, 1);
  134. if (!postData)
  135. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, "{}");
  136. }
  137. }
  138. if (postData) {
  139. if (postDataSize > 0) {
  140. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDSIZE, (long)postDataSize);
  141. }
  142. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, postData);
  143. }
  144. code = curl_easy_perform(curl.get());
  145. if (responseCode)
  146. curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, responseCode);
  147. if (code != CURLE_OK) {
  148. error = strlen(error_in) ? error_in : curl_easy_strerror(code);
  149. } else if (signature) {
  150. for (string &h : header_in_list) {
  151. string name = h.substr(0, 13);
  152. // HTTP headers are technically case-insensitive
  153. if (name == "X-Signature: " || name == "x-signature: ") {
  154. *signature = h.substr(13);
  155. break;
  156. }
  157. }
  158. }
  159. curl_slist_free_all(header);
  160. }
  161. return code == CURLE_OK;
  162. }