remote-text.cpp 6.0 KB

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