@@ -1051,9 +1051,126 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
1051
1051
std::string body,
1052
1052
std::function<void (std::string, std::string, std::string)> callback) noexcept
1053
1053
{
1054
- winrt::Microsoft::ReactNative::JSValueArray emptyArray;
1055
- createBlobForm (options, taskId, method, url, headers, body, emptyArray, callback);
1056
- co_return ;
1054
+ winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter filter;
1055
+
1056
+ RNFetchBlobConfig config;
1057
+ if (options[" appendExt" ].IsNull () == true )
1058
+ {
1059
+ config.appendExt = " " ;
1060
+ }
1061
+ else
1062
+ {
1063
+ std::filesystem::path path (options[" appendExt" ].AsString ());
1064
+ path.make_preferred ();
1065
+ config.appendExt = winrt::to_string (path.c_str ());
1066
+ }
1067
+ config.taskId = taskId;
1068
+ config.appendExt = options[" appendExt" ].IsNull () ? " " : options[" appendExt" ].AsString ();
1069
+ config.fileCache = options[" fileCache" ].AsBoolean ();
1070
+ config.followRedirect = options[" followRedirect" ].AsBoolean ();
1071
+ config.overwrite = options[" overwrite" ].AsBoolean ();
1072
+ if (options[" path" ].IsNull () == true )
1073
+ {
1074
+ config.path = " " ;
1075
+ }
1076
+ else
1077
+ {
1078
+ std::filesystem::path path{ options[" path" ].AsString () };
1079
+ path.make_preferred ();
1080
+ config.path = winrt::to_string (path.c_str ());
1081
+ }
1082
+ config.timeout = options[" timeout" ].AsInt32 ();
1083
+
1084
+ config.trusty = options[" trusty" ].AsBoolean ();
1085
+ if (config.followRedirect == true )
1086
+ {
1087
+ filter.AllowAutoRedirect (true );
1088
+ }
1089
+ else
1090
+ {
1091
+ filter.AllowAutoRedirect (false );
1092
+ }
1093
+
1094
+ if (config.timeout > 0 )
1095
+ {
1096
+ // TODO: find winrt config property
1097
+ }
1098
+
1099
+ if (config.trusty )
1100
+ {
1101
+ filter.IgnorableServerCertificateErrors ().Append (Cryptography::Certificates::ChainValidationResult::Untrusted);
1102
+ }
1103
+
1104
+ winrt::Windows::Web::Http::HttpClient httpClient{ filter };
1105
+
1106
+ winrt::Windows::Web::Http::HttpMethod httpMethod{ winrt::Windows::Web::Http::HttpMethod::Post () };
1107
+ std::string methodUpperCase{ method };
1108
+ for (auto & c : methodUpperCase)
1109
+ {
1110
+ toupper (c);
1111
+ }
1112
+
1113
+ // Delete, Patch, Post, Put, Get, Options, Head
1114
+ if (methodUpperCase.compare (" DELETE" ) == 0 )
1115
+ {
1116
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Delete ();
1117
+ }
1118
+ else if (methodUpperCase.compare (" PATCH" ) == 0 )
1119
+ {
1120
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Patch ();
1121
+ }
1122
+ else if (methodUpperCase.compare (" PUT" ) == 0 )
1123
+ {
1124
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Put ();
1125
+ }
1126
+ else if (methodUpperCase.compare (" GET" ) == 0 )
1127
+ {
1128
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Get ();
1129
+ }
1130
+ else if (methodUpperCase.compare (" OPTIONS" ) == 0 )
1131
+ {
1132
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Options ();
1133
+ }
1134
+ else if (methodUpperCase.compare (" HEAD" ) == 0 )
1135
+ {
1136
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Head ();
1137
+ }
1138
+ else if (methodUpperCase.compare (" POST" ) != 0 )
1139
+ {
1140
+ // Method not supported by winrt
1141
+ co_return ;
1142
+ }
1143
+
1144
+ winrt::Windows::Web::Http::HttpRequestMessage requestMessage{ httpMethod, Uri{url} };
1145
+
1146
+ std::string prefix{ " RNFetchBlob-file://" };
1147
+ bool pathToFile{ body.rfind (prefix, 0 ) == 0 };
1148
+ winrt::hstring fileContent;
1149
+ if (pathToFile)
1150
+ {
1151
+ std::string contentPath{ body.substr (prefix.length ()) };
1152
+ std::filesystem::path path{ contentPath };
1153
+ path.make_preferred ();
1154
+
1155
+ StorageFile storageFile{ co_await StorageFile::GetFileFromPathAsync (winrt::to_hstring (path.c_str ())) };
1156
+ fileContent = co_await FileIO::ReadTextAsync (storageFile);
1157
+
1158
+ }
1159
+
1160
+ winrt::Windows::Web::Http::HttpStringContent requestContent{ pathToFile ? fileContent : winrt::to_hstring (body) };
1161
+
1162
+ for (auto const & entry : headers)
1163
+ {
1164
+ if (!requestMessage.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ())))
1165
+ {
1166
+ requestContent.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ()));
1167
+ }
1168
+ }
1169
+
1170
+ requestMessage.Content (requestContent);
1171
+
1172
+ co_await m_tasks.Add (taskId, ProcessRequestAsync (filter, requestMessage, config, callback));
1173
+
1057
1174
}
1058
1175
1059
1176
winrt::fire_and_forget RNFetchBlob::fetchBlobForm (
@@ -1065,21 +1182,8 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
1065
1182
winrt::Microsoft::ReactNative::JSValueArray body,
1066
1183
std::function<void (std::string, std::string, std::string)> callback) noexcept
1067
1184
{
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
-
1185
+ // createBlobForm(options, taskId, method, url, headers, "", body, callback);
1186
+ // co_return;
1083
1187
winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter filter;
1084
1188
1085
1189
RNFetchBlobConfig config;
@@ -1125,7 +1229,7 @@ winrt::fire_and_forget RNFetchBlob::createBlobForm(
1125
1229
// TODO: find winrt config property
1126
1230
}
1127
1231
1128
- if (! config.trusty )
1232
+ if (config.trusty )
1129
1233
{
1130
1234
filter.IgnorableServerCertificateErrors ().Append (Cryptography::Certificates::ChainValidationResult::Untrusted);
1131
1235
}
@@ -1183,18 +1287,32 @@ winrt::fire_and_forget RNFetchBlob::createBlobForm(
1183
1287
}
1184
1288
}
1185
1289
1186
- if (bodyString.length () > 0 ) {
1187
- winrt::Windows::Web::Http::HttpBufferContent content{ CryptographicBuffer::ConvertStringToBinary (winrt::to_hstring (bodyString), BinaryStringEncoding::Utf8) };
1188
- requestMessage.Content (content);
1189
- }
1190
- else if (!bodyArray.empty ())
1191
- {
1192
- // TODO: Add in multipart aspects
1193
- }
1290
+ // if (bodyString.length() > 0) {
1291
+ // winrt::Windows::Web::Http::HttpBufferContent content{ CryptographicBuffer::ConvertStringToBinary(winrt::to_hstring(bodyString), BinaryStringEncoding::Utf8) };
1292
+ // requestMessage.Content(content);
1293
+ // }
1294
+ // else if (!bodyArray.empty())
1295
+ // {
1296
+ // // TODO: Add in multipart aspects
1297
+ // }
1194
1298
1195
1299
co_await m_tasks.Add (taskId, ProcessRequestAsync (filter, requestMessage, config, callback));
1196
1300
}
1197
1301
1302
+ winrt::fire_and_forget RNFetchBlob::createBlobForm (
1303
+ const winrt::Microsoft::ReactNative::JSValueObject& options,
1304
+ const std::string& taskId,
1305
+ const std::string& method,
1306
+ const std::wstring& url,
1307
+ const winrt::Microsoft::ReactNative::JSValueObject& headers,
1308
+ const std::string& bodyString,
1309
+ const winrt::Microsoft::ReactNative::JSValueArray& bodyArray,
1310
+ std::function<void (std::string, std::string, std::string)> callback) noexcept
1311
+ {
1312
+
1313
+ co_return ;
1314
+ }
1315
+
1198
1316
void RNFetchBlob::enableProgressReport (
1199
1317
std::string taskId,
1200
1318
int interval,
@@ -1314,7 +1432,6 @@ try
1314
1432
for (;;)
1315
1433
{
1316
1434
buffer.Length (0 );
1317
- // TODO: fix 0x80072EFF
1318
1435
auto readBuffer = contentStream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None).get ();
1319
1436
if (readBuffer.Length () == 0 )
1320
1437
{
0 commit comments