Skip to content

Commit a6dcaaf

Browse files
authored
Merge pull request RonRadtke#20 from avmoroz/dev
Windows implementation: Update fetchBlob(), fetchBlobForm(), and ProcessRequestAsync()
2 parents aef7b38 + 7926c9d commit a6dcaaf

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

windows/RNFetchBlob/RNFetchBlob.cpp

+71-17
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,10 @@ catch (...)
10441044
}
10451045

10461046

1047+
IAsyncAction setTimeout(std::chrono::seconds time) {
1048+
co_await time;
1049+
}
1050+
10471051
winrt::fire_and_forget RNFetchBlob::fetchBlob(
10481052
winrt::Microsoft::ReactNative::JSValueObject options,
10491053
std::string taskId,
@@ -1186,9 +1190,38 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
11861190
}
11871191
}
11881192
}
1193+
1194+
std::string error;
1195+
auto cancellationTimer{ setTimeout(config.timeout) };
1196+
cancellationTimer.Completed([weak_this = weak_from_this(), taskId, error](IAsyncAction const& action, AsyncStatus status) {
1197+
if (status == AsyncStatus::Completed) {
1198+
auto strong_this{ weak_this.lock() };
1199+
if (strong_this) {
1200+
strong_this->m_tasks.Cancel(taskId);
1201+
{
1202+
std::scoped_lock lock{ strong_this->m_mutex };
1203+
strong_this->uploadProgressMap.extract(taskId);
1204+
strong_this->downloadProgressMap.extract(taskId);
1205+
}
1206+
}
1207+
}
1208+
});
1209+
try {
1210+
co_await m_tasks.Add(taskId, ProcessRequestAsync(taskId, filter, requestMessage, config, callback, error));
1211+
}
1212+
catch (...) {
1213+
1214+
}
1215+
if (!error.empty()) {
1216+
if (cancellationTimer.Status() != AsyncStatus::Completed) {
1217+
callback(error, "error", "");
1218+
}
1219+
else {
1220+
callback("RNFetchBlob request timed out", "error", "");
1221+
}
1222+
}
11891223

1190-
co_await m_tasks.Add(taskId, ProcessRequestAsync(taskId, filter, requestMessage, config, callback, eventState));
1191-
1224+
cancellationTimer.Cancel();
11921225
m_tasks.Cancel(taskId);
11931226
{
11941227
std::scoped_lock lock{ m_mutex };
@@ -1211,14 +1244,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
12111244

12121245
RNFetchBlobConfig config{ options };
12131246

1214-
if (config.followRedirect == true)
1215-
{
1216-
filter.AllowAutoRedirect(true);
1217-
}
1218-
else
1219-
{
1220-
filter.AllowAutoRedirect(false);
1221-
}
1247+
filter.AllowAutoRedirect(false);
12221248

12231249
if (config.trusty)
12241250
{
@@ -1375,11 +1401,37 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
13751401
}
13761402
}
13771403

1378-
RNFetchBlobState eventState;
1379-
1380-
co_await m_tasks.Add(taskId, ProcessRequestAsync(taskId, filter, requestMessage, config, callback, eventState));
1404+
std::string error;
1405+
auto cancellationTimer{ setTimeout(config.timeout) };
1406+
cancellationTimer.Completed([weak_this = weak_from_this(), taskId, error](IAsyncAction const& action, AsyncStatus status) {
1407+
if (status == AsyncStatus::Completed) {
1408+
auto strong_this{ weak_this.lock() };
1409+
if (strong_this) {
1410+
strong_this->m_tasks.Cancel(taskId);
1411+
{
1412+
std::scoped_lock lock{ strong_this->m_mutex };
1413+
strong_this->uploadProgressMap.extract(taskId);
1414+
strong_this->downloadProgressMap.extract(taskId);
1415+
}
1416+
}
1417+
}
1418+
});
1419+
try {
1420+
co_await m_tasks.Add(taskId, ProcessRequestAsync(taskId, filter, requestMessage, config, callback, error));
1421+
}
1422+
catch (...) {
13811423

1424+
}
1425+
if (!error.empty()) {
1426+
if (cancellationTimer.Status() != AsyncStatus::Completed) {
1427+
callback(error, "error", "");
1428+
}
1429+
else {
1430+
callback("RNFetchBlob request timed out", "error", "");
1431+
}
1432+
}
13821433

1434+
cancellationTimer.Cancel();
13831435
m_tasks.Cancel(taskId);
13841436
{
13851437
std::scoped_lock lock{ m_mutex };
@@ -1480,13 +1532,15 @@ winrt::Windows::Foundation::IAsyncAction RNFetchBlob::ProcessRequestAsync(
14801532
winrt::Windows::Web::Http::HttpRequestMessage& httpRequestMessage,
14811533
RNFetchBlobConfig& config,
14821534
std::function<void(std::string, std::string, std::string)> callback,
1483-
RNFetchBlobState& eventState) noexcept
1535+
std::string& error) noexcept
14841536
try
14851537
{
14861538
winrt::Windows::Web::Http::HttpClient httpClient{filter};
1487-
1539+
14881540
winrt::Windows::Web::Http::HttpResponseMessage response{ co_await httpClient.SendRequestAsync(httpRequestMessage, winrt::Windows::Web::Http::HttpCompletionOption::ResponseHeadersRead) };
14891541

1542+
RNFetchBlobState eventState;
1543+
14901544
auto status{ static_cast<int>(response.StatusCode()) };
14911545
if (config.followRedirect) {
14921546
while (status >= 300 && status < 400) {
@@ -1620,11 +1674,11 @@ try
16201674
else {
16211675
callback("", "result", resultOutput.str());
16221676
}
1623-
// callback("RNFetchBlob request timed out", "error", "");
16241677
}
16251678
catch (const hresult_error& ex)
16261679
{
1627-
callback(winrt::to_string(ex.message().c_str()), "error", "");
1680+
error = winrt::to_string(ex.message().c_str());
1681+
//callback(winrt::to_string(ex.message().c_str()), "error", "");
16281682
}
16291683
catch (...) {
16301684
co_return;

windows/RNFetchBlob/RNFetchBlob.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct RNFetchBlobProgressConfig {
112112

113113

114114
REACT_MODULE(RNFetchBlob, L"RNFetchBlob");
115-
struct RNFetchBlob
115+
struct RNFetchBlob : std::enable_shared_from_this<RNFetchBlob>
116116
{
117117
public:
118118
using StreamId = std::string;
@@ -337,7 +337,7 @@ struct RNFetchBlob
337337
winrt::Windows::Web::Http::HttpRequestMessage& httpRequestMessage,
338338
RNFetchBlobConfig& config,
339339
std::function<void(std::string, std::string, std::string)> callback,
340-
RNFetchBlobState& eventState) noexcept;
340+
std::string& error) noexcept;
341341

342342
const std::map<std::string, std::function<CryptographyCore::HashAlgorithmProvider()>> availableHashes{
343343
{"md5", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Md5()); } },

0 commit comments

Comments
 (0)