1
0

remote-text.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <curl/curl.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,
  44. versionString.c_str());
  45. if (!contentTypeString.empty()) {
  46. header = curl_slist_append(header,
  47. 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,
  54. header);
  55. curl_easy_setopt(curl.get(), CURLOPT_ERRORBUFFER,
  56. error);
  57. curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION,
  58. string_write);
  59. curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA,
  60. &str);
  61. if (timeoutSec)
  62. curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT,
  63. timeoutSec);
  64. #if LIBCURL_VERSION_NUM >= 0x072400
  65. // A lot of servers don't yet support ALPN
  66. curl_easy_setopt(curl.get(), CURLOPT_SSL_ENABLE_ALPN, 0);
  67. #endif
  68. if (!postData.empty()) {
  69. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS,
  70. postData.c_str());
  71. }
  72. code = curl_easy_perform(curl.get());
  73. if (code != CURLE_OK) {
  74. emit Result(QString(), QT_UTF8(error));
  75. } else {
  76. emit Result(QT_UTF8(str.c_str()), QString());
  77. }
  78. curl_slist_free_all(header);
  79. }
  80. }
  81. static size_t header_write(char *ptr, size_t size, size_t nmemb,
  82. vector<string> &list)
  83. {
  84. string str;
  85. size_t total = size * nmemb;
  86. if (total)
  87. str.append(ptr, total);
  88. if (str.back() == '\n')
  89. str.resize(str.size() - 1);
  90. if (str.back() == '\r')
  91. str.resize(str.size() - 1);
  92. list.push_back(std::move(str));
  93. return total;
  94. }
  95. bool GetRemoteFile(
  96. const char *url,
  97. std::string &str,
  98. std::string &error,
  99. long *responseCode,
  100. const char *contentType,
  101. const char *postData,
  102. std::vector<std::string> extraHeaders,
  103. std::string *signature,
  104. int timeoutSec)
  105. {
  106. vector<string> header_in_list;
  107. char error_in[CURL_ERROR_SIZE];
  108. CURLcode code = CURLE_FAILED_INIT;
  109. error_in[0] = 0;
  110. string versionString("User-Agent: obs-basic ");
  111. versionString += App()->GetVersionString();
  112. string contentTypeString;
  113. if (contentType) {
  114. contentTypeString += "Content-Type: ";
  115. contentTypeString += contentType;
  116. }
  117. Curl curl{curl_easy_init(), curl_deleter};
  118. if (curl) {
  119. struct curl_slist *header = nullptr;
  120. header = curl_slist_append(header,
  121. versionString.c_str());
  122. if (!contentTypeString.empty()) {
  123. header = curl_slist_append(header,
  124. contentTypeString.c_str());
  125. }
  126. for (std::string &h : extraHeaders)
  127. header = curl_slist_append(header, h.c_str());
  128. curl_easy_setopt(curl.get(), CURLOPT_URL, url);
  129. curl_easy_setopt(curl.get(), CURLOPT_ACCEPT_ENCODING, "");
  130. curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER,
  131. header);
  132. curl_easy_setopt(curl.get(), CURLOPT_ERRORBUFFER,
  133. error_in);
  134. curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION,
  135. string_write);
  136. curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA,
  137. &str);
  138. if (signature) {
  139. curl_easy_setopt(curl.get(), CURLOPT_HEADERFUNCTION,
  140. header_write);
  141. curl_easy_setopt(curl.get(), CURLOPT_HEADERDATA,
  142. &header_in_list);
  143. }
  144. if (timeoutSec)
  145. curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT,
  146. timeoutSec);
  147. #if LIBCURL_VERSION_NUM >= 0x072400
  148. // A lot of servers don't yet support ALPN
  149. curl_easy_setopt(curl.get(), CURLOPT_SSL_ENABLE_ALPN, 0);
  150. #endif
  151. if (postData) {
  152. curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS,
  153. postData);
  154. }
  155. code = curl_easy_perform(curl.get());
  156. if (responseCode)
  157. curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE,
  158. responseCode);
  159. if (code != CURLE_OK) {
  160. error = error_in;
  161. } else if (signature) {
  162. for (string &h : header_in_list) {
  163. string name = h.substr(0, 13);
  164. if (name == "X-Signature: ") {
  165. *signature = h.substr(13);
  166. break;
  167. }
  168. }
  169. }
  170. curl_slist_free_all(header);
  171. }
  172. return code == CURLE_OK;
  173. }