You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. #if DROWAUDIO_USE_CURL
  25. } //namespace drow
  26. #if JUCE_WINDOWS
  27. #include "curl/include/curl/curl.h"
  28. #else
  29. #include <curl/curl.h>
  30. #endif
  31. namespace drow {
  32. //==============================================================================
  33. CURLEasySession::CURLEasySession()
  34. : handle (CURLManager::getInstance()->createEasyCurlHandle()),
  35. remotePath (String()),
  36. progress (1.0f)
  37. {
  38. enableFullDebugging (true);
  39. curl_easy_setopt (handle, CURLOPT_NOPROGRESS, false);
  40. }
  41. CURLEasySession::CURLEasySession (String localPath,
  42. String remotePath,
  43. bool upload,
  44. String username,
  45. String password)
  46. : handle (CURLManager::getInstance()->createEasyCurlHandle())
  47. {
  48. handle = CURLManager::getInstance()->createEasyCurlHandle();
  49. enableFullDebugging (true);
  50. curl_easy_setopt (handle, CURLOPT_NOPROGRESS, false);
  51. setLocalFile (localPath);
  52. setRemotePath (remotePath);
  53. setUserNameAndPassword (username, password);
  54. beginTransfer (upload);
  55. }
  56. CURLEasySession::~CURLEasySession()
  57. {
  58. CURLManager::getInstance()->removeTimeSliceClient (this);
  59. if (CURLManager::getInstance()->getNumClients() == 0)
  60. CURLManager::getInstance()->stopThread (1000);
  61. CURLManager::getInstance()->cleanUpEasyCurlHandle (handle);
  62. }
  63. //==============================================================================
  64. void CURLEasySession::setInputStream (InputStream* newInputStream)
  65. {
  66. localFile = File();
  67. inputStream = newInputStream;
  68. }
  69. void CURLEasySession::setLocalFile (File newLocalFile)
  70. {
  71. localFile = newLocalFile;
  72. inputStream = localFile.createInputStream();
  73. }
  74. void CURLEasySession::setRemotePath (String newRemotePath)
  75. {
  76. remotePath = newRemotePath;
  77. if (remotePath.getLastCharacters (1) == "/")
  78. remotePath = remotePath << localFile.getFileName();
  79. curl_easy_setopt (handle, CURLOPT_URL, remotePath.toUTF8().getAddress());
  80. }
  81. void CURLEasySession::setUserNameAndPassword (String username, String password)
  82. {
  83. userNameAndPassword = username << ":" << password;
  84. curl_easy_setopt (handle, CURLOPT_USERPWD, userNameAndPassword.toUTF8().getAddress());
  85. }
  86. //==============================================================================
  87. String CURLEasySession::getCurrentWorkingDirectory()
  88. {
  89. char url[1000];
  90. CURLcode res = curl_easy_getinfo (handle, CURLINFO_EFFECTIVE_URL, url);
  91. if (res == CURLE_OK && CharPointer_ASCII::isValidString (url, 1000))
  92. return String (url);
  93. else
  94. return String();
  95. }
  96. StringArray CURLEasySession::getDirectoryListing()
  97. {
  98. String remoteUrl (remotePath.upToLastOccurrenceOf ("/", true, false));
  99. curl_easy_setopt (handle, CURLOPT_URL, remoteUrl.toUTF8().getAddress());
  100. directoryContentsList.setSize (0);
  101. curl_easy_setopt (handle, CURLOPT_PROGRESSFUNCTION, 0L);
  102. curl_easy_setopt (handle, CURLOPT_UPLOAD, 0L);
  103. curl_easy_setopt (handle, CURLOPT_DIRLISTONLY, 1L);
  104. curl_easy_setopt (handle, CURLOPT_WRITEDATA, this);
  105. curl_easy_setopt (handle, CURLOPT_WRITEFUNCTION, directoryListingCallback);
  106. // perform the tranfer
  107. progress = 0.0f;
  108. CURLcode result = curl_easy_perform (handle);
  109. reset();
  110. if (result == CURLE_OK)
  111. {
  112. StringArray list;
  113. list.addLines (directoryContentsList.toString().trim());
  114. return list;
  115. }
  116. else
  117. {
  118. return StringArray (curl_easy_strerror (result));
  119. }
  120. }
  121. // not yet ready
  122. //String CURLEasySession::getContentType()
  123. //{
  124. // char *ct;
  125. //
  126. // CURLcode result = curl_easy_getinfo (handle, CURLINFO_CONTENT_TYPE, &ct);
  127. //
  128. // if (CURLE_OK == result)// && ct != nullptr)
  129. // {
  130. // DBG("CURLE_OK: " + remotePath);
  131. // return ct;
  132. // }
  133. // else
  134. // {
  135. // DBG("CURLE_NOT_OK");
  136. // return String();
  137. // }
  138. //}
  139. //==============================================================================
  140. void CURLEasySession::enableFullDebugging (bool shouldEnableFullDebugging)
  141. {
  142. curl_easy_setopt (handle, CURLOPT_VERBOSE, shouldEnableFullDebugging ? 1L : 0L);
  143. }
  144. void CURLEasySession::beginTransfer (bool transferIsUpload, bool performOnBackgroundThread)
  145. {
  146. isUpload = transferIsUpload;
  147. shouldStopTransfer = false;
  148. if (performOnBackgroundThread)
  149. {
  150. CURLManager::getInstance()->addTimeSliceClient (this);
  151. CURLManager::getInstance()->startThread();
  152. }
  153. else
  154. {
  155. performTransfer (isUpload);
  156. }
  157. }
  158. void CURLEasySession::stopTransfer()
  159. {
  160. shouldStopTransfer = true;
  161. }
  162. void CURLEasySession::reset()
  163. {
  164. curl_easy_reset (handle);
  165. curl_easy_setopt (handle, CURLOPT_URL, remotePath.toUTF8().getAddress());
  166. curl_easy_setopt (handle, CURLOPT_USERPWD, userNameAndPassword.toUTF8().getAddress());
  167. curl_easy_setopt (handle, CURLOPT_NOPROGRESS, false);
  168. }
  169. //==============================================================================
  170. void CURLEasySession::addListener (CURLEasySession::Listener* const listener)
  171. {
  172. listeners.add (listener);
  173. }
  174. void CURLEasySession::removeListener (CURLEasySession::Listener* const listener)
  175. {
  176. listeners.remove (listener);
  177. }
  178. //==============================================================================
  179. int CURLEasySession::useTimeSlice()
  180. {
  181. performTransfer (isUpload);
  182. return -1;
  183. }
  184. //==============================================================================
  185. size_t CURLEasySession::writeCallback (void* sourcePointer, size_t blockSize, size_t numBlocks, CURLEasySession* session)
  186. {
  187. if (session != nullptr)
  188. {
  189. if (session->outputStream->failedToOpen())
  190. {
  191. /* failure, can't open file to write */
  192. return ! (blockSize * numBlocks); // return a value not equal to (blockSize * numBlocks)
  193. }
  194. session->outputStream->write (sourcePointer, blockSize * numBlocks);
  195. return blockSize * numBlocks;
  196. }
  197. return ! (blockSize * numBlocks); // return a value not equal to (blockSize * numBlocks)
  198. }
  199. size_t CURLEasySession::readCallback (void* destinationPointer, size_t blockSize, size_t numBlocks, CURLEasySession* session)
  200. {
  201. if (session != nullptr)
  202. {
  203. if (session->inputStream.get() == nullptr)
  204. {
  205. return CURL_READFUNC_ABORT; /* failure, can't open file to read */
  206. }
  207. return session->inputStream->read (destinationPointer, blockSize * numBlocks);
  208. }
  209. return CURL_READFUNC_ABORT;
  210. }
  211. size_t CURLEasySession::directoryListingCallback (void* sourcePointer, size_t blockSize, size_t numBlocks, CURLEasySession* session)
  212. {
  213. if (session != nullptr)
  214. {
  215. session->directoryContentsList.append (sourcePointer, (int) (blockSize * numBlocks));
  216. return blockSize * numBlocks;
  217. }
  218. return ! (blockSize * numBlocks); // return a positive value not equal to (blockSize * numBlocks)
  219. }
  220. int CURLEasySession::internalProgressCallback (CURLEasySession* session, double dltotal, double dlnow, double /*ultotal*/, double ulnow)
  221. {
  222. session->progress = (float) (session->isUpload ? (ulnow / session->inputStream->getTotalLength()) : (dlnow / dltotal));
  223. session->listeners.call (&CURLEasySession::Listener::transferProgressUpdate, session);
  224. return (int) session->shouldStopTransfer;
  225. }
  226. //==============================================================================
  227. int CURLEasySession::performTransfer (bool transferIsUpload)
  228. {
  229. curl_easy_setopt (handle, CURLOPT_URL, remotePath.toUTF8().getAddress());
  230. curl_easy_setopt (handle, CURLOPT_UPLOAD, (long) transferIsUpload);
  231. curl_easy_setopt (handle, CURLOPT_PROGRESSDATA, this);
  232. curl_easy_setopt (handle, CURLOPT_PROGRESSFUNCTION, internalProgressCallback);
  233. if (transferIsUpload == true)
  234. {
  235. // sets the pointer to be passed to the read callback
  236. curl_easy_setopt (handle, CURLOPT_READDATA, this);
  237. curl_easy_setopt (handle, CURLOPT_READFUNCTION, readCallback);
  238. inputStream->setPosition (0);
  239. }
  240. else
  241. {
  242. // sets the pointer to be passed to the write callback
  243. curl_easy_setopt (handle, CURLOPT_WRITEDATA, this);
  244. curl_easy_setopt (handle, CURLOPT_WRITEFUNCTION, writeCallback);
  245. // create local file to recieve transfer
  246. if (localFile.existsAsFile())
  247. localFile = localFile.getNonexistentSibling();
  248. outputStream = localFile.createOutputStream();
  249. }
  250. //perform the transfer
  251. progress = 0.0f;
  252. listeners.call (&CURLEasySession::Listener::transferAboutToStart, this);
  253. CURLcode result = curl_easy_perform (handle);
  254. // delete the streams to flush the buffers
  255. outputStream = nullptr;
  256. listeners.call (&CURLEasySession::Listener::transferEnded, this);
  257. return result;
  258. }
  259. #endif