remote-text.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /******************************************************************************
  2. Copyright (C) 2015 by Hugh 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) { curl_easy_cleanup(curl); };
  20. using Curl = unique_ptr<CURL, decltype(curl_deleter)>;
  21. static size_t string_write(char *ptr, size_t size, size_t nmemb, string &str)
  22. {
  23. size_t total = size * nmemb;
  24. if (total)
  25. str.append(ptr, total);
  26. return total;
  27. }
  28. void RemoteTextThread::run()
  29. {
  30. char error[CURL_ERROR_SIZE];
  31. CURLcode code;
  32. string versionString("User-Agent: obs-basic ");
  33. versionString += App()->GetVersionString();
  34. string contentTypeString;
  35. if (!contentType.empty()) {
  36. contentTypeString += "Content-Type: ";
  37. contentTypeString += contentType;
  38. }
  39. Curl curl{curl_easy_init(), curl_deleter};
  40. if (curl) {
  41. struct curl_slist *header = nullptr;
  42. string str;
  43. header = curl_slist_append(header, versionString.c_str());
  44. if (!contentTypeString.empty()) {
  45. header = curl_slist_append(header,
  46. contentTypeString.c_str());
  47. }
  48. for (std::string &h : extraHeaders)
  49. header = curl_slist_append(header, h.c_str());
  50. curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
  51. curl_easy_setopt(curl.get(), CURLOPT_ACCEPT_ENCODING, "");
  52. curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, header);
  53. curl_easy_setopt(curl.get(), CURLOPT_ERRORBUFFER, error);
  54. curl_easy_setopt(curl.get(), CURLOPT_FAILONERROR, 1L);
  55. curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION,
  56. 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,
  61. timeoutSec);
  62. #if LIBCURL_VERSION_NUM >= 0x072400
  63. // A lot of servers don't yet support ALPN
  64. curl_easy_setopt(curl.get(), CURLOPT_SSL_ENABLE_ALPN, 0);
  65. #endif
  66. if (!postData.empty()) {
  67. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS,
  68. postData.c_str());
  69. }
  70. code = curl_easy_perform(curl.get());
  71. if (code != CURLE_OK) {
  72. blog(LOG_WARNING,
  73. "RemoteTextThread: HTTP request failed. %s",
  74. error);
  75. emit Result(QString(), QT_UTF8(error));
  76. } else {
  77. emit Result(QT_UTF8(str.c_str()), QString());
  78. }
  79. curl_slist_free_all(header);
  80. }
  81. }
  82. static size_t header_write(char *ptr, size_t size, size_t nmemb,
  83. vector<string> &list)
  84. {
  85. string str;
  86. size_t total = size * nmemb;
  87. if (total)
  88. str.append(ptr, total);
  89. if (str.back() == '\n')
  90. str.resize(str.size() - 1);
  91. if (str.back() == '\r')
  92. str.resize(str.size() - 1);
  93. list.push_back(std::move(str));
  94. return total;
  95. }
  96. bool GetRemoteFile(const char *url, std::string &str, std::string &error,
  97. long *responseCode, const char *contentType,
  98. std::string request_type, const char *postData,
  99. std::vector<std::string> extraHeaders,
  100. std::string *signature, int timeoutSec)
  101. {
  102. vector<string> header_in_list;
  103. char error_in[CURL_ERROR_SIZE];
  104. CURLcode code = CURLE_FAILED_INIT;
  105. error_in[0] = 0;
  106. string versionString("User-Agent: obs-basic ");
  107. versionString += App()->GetVersionString();
  108. string contentTypeString;
  109. if (contentType) {
  110. contentTypeString += "Content-Type: ";
  111. contentTypeString += contentType;
  112. }
  113. Curl curl{curl_easy_init(), curl_deleter};
  114. if (curl) {
  115. struct curl_slist *header = nullptr;
  116. header = curl_slist_append(header, versionString.c_str());
  117. if (!contentTypeString.empty()) {
  118. header = curl_slist_append(header,
  119. contentTypeString.c_str());
  120. }
  121. for (std::string &h : extraHeaders)
  122. header = curl_slist_append(header, h.c_str());
  123. curl_easy_setopt(curl.get(), CURLOPT_URL, url);
  124. curl_easy_setopt(curl.get(), CURLOPT_ACCEPT_ENCODING, "");
  125. curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, header);
  126. curl_easy_setopt(curl.get(), CURLOPT_ERRORBUFFER, error_in);
  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 LIBCURL_VERSION_NUM >= 0x072400
  142. // A lot of servers don't yet support ALPN
  143. curl_easy_setopt(curl.get(), CURLOPT_SSL_ENABLE_ALPN, 0);
  144. #endif
  145. if (!request_type.empty()) {
  146. if (request_type != "GET")
  147. curl_easy_setopt(curl.get(),
  148. CURLOPT_CUSTOMREQUEST,
  149. request_type.c_str());
  150. // Special case of "POST"
  151. if (request_type == "POST") {
  152. curl_easy_setopt(curl.get(), CURLOPT_POST, 1);
  153. if (!postData)
  154. curl_easy_setopt(curl.get(),
  155. CURLOPT_POSTFIELDS,
  156. "{}");
  157. }
  158. }
  159. if (postData) {
  160. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS,
  161. postData);
  162. }
  163. code = curl_easy_perform(curl.get());
  164. if (responseCode)
  165. curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE,
  166. responseCode);
  167. if (code != CURLE_OK) {
  168. error = error_in;
  169. } else if (signature) {
  170. for (string &h : header_in_list) {
  171. string name = h.substr(0, 13);
  172. if (name == "X-Signature: ") {
  173. *signature = h.substr(13);
  174. break;
  175. }
  176. }
  177. }
  178. curl_slist_free_all(header);
  179. }
  180. return code == CURLE_OK;
  181. }