1
0

BackgroundDownloader.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "Main.h"
  2. #include "BackgroundDownloader.h"
  3. #include "..\..\..\Components\wac_network\wac_network_http_receiver_api.h"
  4. #include "api__in_flv.h"
  5. #include "api/service/waservicefactory.h"
  6. #include "../nu/AutoChar.h"
  7. #include <strsafe.h>
  8. #define HTTP_BUFFER_SIZE 65536
  9. // {C0A565DC-0CFE-405a-A27C-468B0C8A3A5C}
  10. static const GUID internetConfigGroupGUID =
  11. {
  12. 0xc0a565dc, 0xcfe, 0x405a, { 0xa2, 0x7c, 0x46, 0x8b, 0xc, 0x8a, 0x3a, 0x5c }
  13. };
  14. static void SetUserAgent( api_httpreceiver *http )
  15. {
  16. char agent[ 256 ] = { 0 };
  17. StringCchPrintfA( agent, 256, "User-Agent: %S/%S", WASABI_API_APP->main_getAppName(), WASABI_API_APP->main_getVersionNumString() );
  18. http->addheader( agent );
  19. }
  20. static int FeedHTTP( api_httpreceiver *http, Downloader::DownloadCallback *callback, int *downloaded )
  21. {
  22. char downloadedData[ HTTP_BUFFER_SIZE ] = { 0 };
  23. int result = 0;
  24. int downloadSize = http->get_bytes( downloadedData, HTTP_BUFFER_SIZE );
  25. *downloaded = downloadSize;
  26. if ( downloadSize )
  27. {
  28. result = callback->OnData( downloadedData, downloadSize );
  29. }
  30. return result;
  31. }
  32. static void RunDownload( api_httpreceiver *http, Downloader::DownloadCallback *callback )
  33. {
  34. int ret;
  35. int downloaded = 0;
  36. do
  37. {
  38. ret = http->run();
  39. Sleep( 55 );
  40. do
  41. {
  42. if ( FeedHTTP( http, callback, &downloaded ) != 0 )
  43. return;
  44. } while ( downloaded == HTTP_BUFFER_SIZE );
  45. } while ( ret == HTTPRECEIVER_RUN_OK );
  46. // finish off the data
  47. do
  48. {
  49. if ( FeedHTTP( http, callback, &downloaded ) != 0 )
  50. return;
  51. } while ( downloaded );
  52. }
  53. bool Downloader::Download(const char *url, Downloader::DownloadCallback *callback, uint64_t startPosition)
  54. {
  55. api_httpreceiver *http = 0;
  56. waServiceFactory *sf = plugin.service->service_getServiceByGuid(httpreceiverGUID);
  57. if (sf) http = (api_httpreceiver *)sf->getInterface();
  58. if (!http)
  59. return false;
  60. int use_proxy = 1;
  61. bool proxy80 = AGAVE_API_CONFIG->GetBool(internetConfigGroupGUID, L"proxy80", false);
  62. if (proxy80 && strstr(url, ":") && (!strstr(url, ":80/") && strstr(url, ":80") != (url + strlen(url) - 3)))
  63. use_proxy = 0;
  64. const wchar_t *proxy = use_proxy?AGAVE_API_CONFIG->GetString(internetConfigGroupGUID, L"proxy", 0):0;
  65. http->AllowCompression();
  66. http->open(API_DNS_AUTODNS, HTTP_BUFFER_SIZE, (proxy && proxy[0]) ? (const char *)AutoChar(proxy) : NULL);
  67. if (startPosition > 0)
  68. {
  69. char temp[128] = {0};
  70. StringCchPrintfA(temp, 128, "Range: bytes=%d-", startPosition);
  71. http->addheader(temp);
  72. }
  73. SetUserAgent(http);
  74. http->connect(url);
  75. int ret;
  76. do
  77. {
  78. Sleep(55);
  79. ret = http->run();
  80. if (ret == -1) // connection failed
  81. break;
  82. // ---- check our reply code ----
  83. int replycode = http->getreplycode();
  84. switch (replycode)
  85. {
  86. case 0:
  87. case 100:
  88. break;
  89. case 200:
  90. {
  91. if (callback->OnConnect(http) != 0)
  92. {
  93. sf->releaseInterface(http);
  94. return false;
  95. }
  96. RunDownload(http, callback);
  97. sf->releaseInterface(http);
  98. return true;
  99. }
  100. break;
  101. default:
  102. sf->releaseInterface(http);
  103. return false;
  104. }
  105. }
  106. while (ret == HTTPRECEIVER_RUN_OK);
  107. //const char *er = http->geterrorstr();
  108. sf->releaseInterface(http);
  109. return false;
  110. }