BackgroundDownloader.cpp 3.0 KB

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