9
9
10
10
#include < winrt/Windows.Web.Http.h>
11
11
#include < winrt/Windows.Web.Http.Headers.h>
12
+ #include < winrt/windows.web.http.filters.h>
12
13
13
14
#include < filesystem>
15
+ #include < sstream>
14
16
15
17
using namespace winrt ;
16
18
using namespace winrt ::Windows::ApplicationModel;
@@ -1049,9 +1051,37 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
1049
1051
std::string body,
1050
1052
std::function<void (std::string, std::string, std::string)> callback) noexcept
1051
1053
{
1052
- // winrt::Windows::Web::Http::HttpMethod::Hea
1053
-
1054
- // Method
1054
+ winrt::Microsoft::ReactNative::JSValueArray emptyArray;
1055
+ createBlobForm (options, taskId, method, url, headers, body, emptyArray, callback);
1056
+ co_return ;
1057
+ }
1058
+
1059
+ winrt::fire_and_forget RNFetchBlob::fetchBlobForm (
1060
+ winrt::Microsoft::ReactNative::JSValueObject options,
1061
+ std::string taskId,
1062
+ std::string method,
1063
+ std::wstring url,
1064
+ winrt::Microsoft::ReactNative::JSValueObject headers,
1065
+ winrt::Microsoft::ReactNative::JSValueArray body,
1066
+ std::function<void (std::string, std::string, std::string)> callback) noexcept
1067
+ {
1068
+ createBlobForm (options, taskId, method, url, headers, " " , body, callback);
1069
+ co_return ;
1070
+ }
1071
+
1072
+ winrt::fire_and_forget RNFetchBlob::createBlobForm (
1073
+ const winrt::Microsoft::ReactNative::JSValueObject& options,
1074
+ const std::string& taskId,
1075
+ const std::string& method,
1076
+ const std::wstring& url,
1077
+ const winrt::Microsoft::ReactNative::JSValueObject& headers,
1078
+ const std::string& bodyString,
1079
+ const winrt::Microsoft::ReactNative::JSValueArray& bodyArray,
1080
+ std::function<void (std::string, std::string, std::string)> callback) noexcept
1081
+ {
1082
+
1083
+ winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter filter;
1084
+
1055
1085
RNFetchBlobConfig config;
1056
1086
if (options[" appendExt" ].IsNull () == true )
1057
1087
{
@@ -1063,6 +1093,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
1063
1093
path.make_preferred ();
1064
1094
config.appendExt = winrt::to_string (path.c_str ());
1065
1095
}
1096
+ config.taskId = taskId;
1066
1097
config.appendExt = options[" appendExt" ].IsNull () ? " " : options[" appendExt" ].AsString ();
1067
1098
config.fileCache = options[" fileCache" ].AsBoolean ();
1068
1099
config.followRedirect = options[" followRedirect" ].AsBoolean ();
@@ -1082,25 +1113,26 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
1082
1113
1083
1114
if (config.followRedirect == true )
1084
1115
{
1085
- // TODO: find winrt config property
1116
+ filter. AllowAutoRedirect ( true );
1086
1117
}
1087
- if (config. fileCache == true )
1118
+ else
1088
1119
{
1089
-
1120
+ filter. AllowAutoRedirect ( false );
1090
1121
}
1122
+
1091
1123
if (config.timeout > 0 )
1092
1124
{
1093
1125
// TODO: find winrt config property
1094
1126
}
1095
- if (config.trusty == true )
1127
+
1128
+ if (!config.trusty )
1096
1129
{
1097
- // TODO: find winrt config property
1130
+ filter. IgnorableServerCertificateErrors (). Append (Cryptography::Certificates::ChainValidationResult::Untrusted);
1098
1131
}
1099
1132
1100
-
1133
+ winrt::Windows::Web::Http::HttpClient httpClient{ filter };
1101
1134
1102
1135
winrt::Windows::Web::Http::HttpMethod httpMethod{ winrt::Windows::Web::Http::HttpMethod::Post () };
1103
-
1104
1136
std::string methodUpperCase{ method };
1105
1137
for (auto & c : methodUpperCase)
1106
1138
{
@@ -1151,32 +1183,18 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
1151
1183
}
1152
1184
}
1153
1185
1154
- std::string temp{ body };
1155
- if (body.length () > 0 ) {
1156
- winrt::Windows::Web::Http::HttpBufferContent content{ CryptographicBuffer::ConvertStringToBinary (winrt::to_hstring (body), BinaryStringEncoding::Utf8) };
1186
+ if (bodyString.length () > 0 ) {
1187
+ winrt::Windows::Web::Http::HttpBufferContent content{ CryptographicBuffer::ConvertStringToBinary (winrt::to_hstring (bodyString), BinaryStringEncoding::Utf8) };
1157
1188
requestMessage.Content (content);
1158
1189
}
1159
- // requestMessage.Content(requestContent);
1160
-
1161
- // winrt::Windows::Web::Http::HttpResponseMessage response = co_await m_httpClient.SendRequestAsync(requestMessage, winrt::Windows::Web::Http::HttpCompletionOption::ResponseHeadersRead);
1190
+ else if (!bodyArray.empty ())
1191
+ {
1192
+ // TODO: Add in multipart aspects
1193
+ }
1162
1194
1163
- co_await m_tasks.Add (taskId, ProcessRequestAsync (requestMessage, config, callback));
1195
+ co_await m_tasks.Add (taskId, ProcessRequestAsync (filter, requestMessage, config, callback));
1164
1196
}
1165
1197
1166
- void RNFetchBlob::fetchBlobForm (
1167
- winrt::Microsoft::ReactNative::JSValueObject options,
1168
- std::string taskId,
1169
- std::string method,
1170
- std::wstring url,
1171
- winrt::Microsoft::ReactNative::JSValueObject headers,
1172
- winrt::Microsoft::ReactNative::JSValueArray body,
1173
- std::function<void (std::string)> callback) noexcept
1174
- {
1175
- return ;
1176
- }
1177
-
1178
-
1179
-
1180
1198
void RNFetchBlob::enableProgressReport (
1181
1199
std::string taskId,
1182
1200
int interval,
@@ -1208,11 +1226,23 @@ catch (const hresult_error& ex)
1208
1226
callback (winrt::to_string (ex.message ()), " " );
1209
1227
}
1210
1228
1211
- void RNFetchBlob::removeSession (
1212
- winrt::Microsoft::ReactNative::JSValueObject paths,
1229
+ winrt::fire_and_forget RNFetchBlob::removeSession (
1230
+ winrt::Microsoft::ReactNative::JSValueArray paths,
1213
1231
std::function<void (std::string)> callback) noexcept
1232
+ try
1214
1233
{
1215
- return ;
1234
+ for (auto & path : paths)
1235
+ {
1236
+ std::filesystem::path toDelete{ path.AsString () };
1237
+ toDelete.make_preferred ();
1238
+ StorageFile file{ co_await StorageFile::GetFileFromPathAsync (winrt::to_hstring (toDelete.c_str ())) };
1239
+ co_await file.DeleteAsync ();
1240
+ }
1241
+ callback (" " );
1242
+ }
1243
+ catch (const hresult_error& ex)
1244
+ {
1245
+ callback (winrt::to_string (ex.message ()).c_str ());
1216
1246
}
1217
1247
1218
1248
void RNFetchBlob::closeStream (
@@ -1250,35 +1280,56 @@ void RNFetchBlob::splitPath(const std::wstring& fullPath, winrt::hstring& direct
1250
1280
}
1251
1281
1252
1282
winrt::Windows::Foundation::IAsyncAction RNFetchBlob::ProcessRequestAsync (
1283
+ const winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter& filter,
1253
1284
winrt::Windows::Web::Http::HttpRequestMessage& httpRequestMessage,
1254
- const RNFetchBlobConfig& config,
1255
- std::function<void (std::string, std::string, std::string)>& callback)
1285
+ RNFetchBlobConfig& config,
1286
+ std::function<void (std::string, std::string, std::string)> callback) noexcept
1287
+ try
1256
1288
{
1257
- winrt::Windows::Web::Http::HttpResponseMessage response = co_await m_httpClient.SendRequestAsync (httpRequestMessage, winrt::Windows::Web::Http::HttpCompletionOption::ResponseHeadersRead);
1289
+ // TODO: implement timeouts
1290
+ winrt::Windows::Web::Http::HttpClient httpClient{filter};
1291
+ winrt::Windows::Web::Http::HttpResponseMessage response = co_await httpClient.SendRequestAsync (httpRequestMessage, winrt::Windows::Web::Http::HttpCompletionOption::ResponseHeadersRead);
1292
+
1258
1293
1259
- if (config.path . empty () == false )
1294
+ if (config.fileCache )
1260
1295
{
1296
+ if (config.path .empty ())
1297
+ {
1298
+ config.path = winrt::to_string (ApplicationData::Current ().TemporaryFolder ().Path ()) + " \\ RNFetchBlobTmp_" + config.taskId ;
1299
+ if (config.appendExt .length () > 0 )
1300
+ {
1301
+ config.path += " ." + config.appendExt ;
1302
+ }
1303
+ }
1304
+
1261
1305
std::filesystem::path path{ config.path };
1262
- StorageFolder storageFolder{ co_await StorageFolder::GetFolderFromPathAsync (path. parent_path ().wstring ()) };
1263
- StorageFile storageFile{ co_await storageFolder.CreateFileAsync (path.filename ().wstring (), CreationCollisionOption::ReplaceExisting ) };
1306
+ StorageFolder storageFolder{ co_await StorageFolder::GetFolderFromPathAsync (ApplicationData::Current (). TemporaryFolder ().Path ()) };
1307
+ StorageFile storageFile{ co_await storageFolder.CreateFileAsync (path.filename ().wstring (), CreationCollisionOption::FailIfExists ) };
1264
1308
IRandomAccessStream stream{ co_await storageFile.OpenAsync (FileAccessMode::ReadWrite) };
1265
1309
IOutputStream outputStream{ stream.GetOutputStreamAt (0 ) };
1266
- //
1267
- auto contentStream = co_await response.Content ().ReadAsInputStreamAsync ();
1268
- Buffer buffer{ 8 * 1024 };
1310
+
1311
+ auto contentStream{ co_await response.Content ().ReadAsInputStreamAsync () } ;
1312
+ Buffer buffer{ 10 * 1024 };
1269
1313
1270
1314
for (;;)
1271
1315
{
1272
1316
buffer.Length (0 );
1273
- auto readBuffer = co_await contentStream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None);
1317
+ // TODO: fix 0x80072EFF
1318
+ auto readBuffer = contentStream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None).get ();
1274
1319
if (readBuffer.Length () == 0 )
1275
1320
{
1276
1321
break ;
1277
1322
}
1278
-
1279
1323
co_await outputStream.WriteAsync (readBuffer);
1280
1324
}
1325
+
1326
+ callback (" " , " path" , config.path );
1281
1327
}
1328
+ co_return ;
1329
+ }
1330
+ catch (const hresult_error& ex)
1331
+ {
1332
+ callback (winrt::to_string (ex.message ().c_str ()), " error" , " " );
1282
1333
}
1283
1334
1284
1335
RNFetchBlobStream::RNFetchBlobStream (Streams::IRandomAccessStream& _streamInstance, EncodingOptions _encoding) noexcept
0 commit comments