react-native-windows 0.70.0 → 0.70.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Microsoft.ReactNative/Pch/pch.h +0 -1
- package/Microsoft.ReactNative/Views/ViewManagerBase.cpp +4 -2
- package/PropertySheets/Generated/PackageVersion.g.props +2 -2
- package/Shared/Modules/HttpModule.cpp +4 -2
- package/Shared/Networking/IHttpResource.h +1 -1
- package/Shared/Networking/IRedirectEventSource.h +18 -0
- package/Shared/Networking/IWinRTHttpRequestFactory.h +22 -0
- package/Shared/Networking/OriginPolicyHttpFilter.cpp +47 -16
- package/Shared/Networking/OriginPolicyHttpFilter.h +16 -3
- package/Shared/Networking/RedirectHttpFilter.cpp +283 -0
- package/Shared/Networking/RedirectHttpFilter.h +97 -0
- package/Shared/Networking/WinRTHttpResource.cpp +207 -154
- package/Shared/Networking/WinRTHttpResource.h +17 -4
- package/Shared/Shared.vcxitems +4 -0
- package/Shared/Shared.vcxitems.filters +12 -0
- package/package.json +1 -1
|
@@ -43,7 +43,6 @@
|
|
|
43
43
|
#include <winrt/Windows.Storage.Streams.h>
|
|
44
44
|
#include <winrt/Windows.UI.ViewManagement.Core.h>
|
|
45
45
|
#include <winrt/Windows.UI.ViewManagement.h>
|
|
46
|
-
#include <winrt/Windows.Web.Http.Filters.h>
|
|
47
46
|
#include <winrt/Windows.Web.Http.Headers.h>
|
|
48
47
|
|
|
49
48
|
#include "Base/CxxReactIncludes.h"
|
|
@@ -271,9 +271,11 @@ bool ViewManagerBase::UpdateProperty(
|
|
|
271
271
|
const auto iter = pointerEventsMap.find(propertyValue.AsString());
|
|
272
272
|
if (iter != pointerEventsMap.end()) {
|
|
273
273
|
nodeToUpdate->m_pointerEvents = iter->second;
|
|
274
|
-
if (nodeToUpdate->
|
|
275
|
-
if (
|
|
274
|
+
if (const auto uiElement = nodeToUpdate->GetView().try_as<xaml::UIElement>()) {
|
|
275
|
+
if (nodeToUpdate->m_pointerEvents == PointerEventsKind::None) {
|
|
276
276
|
uiElement.IsHitTestVisible(false);
|
|
277
|
+
} else {
|
|
278
|
+
uiElement.ClearValue(xaml::UIElement::IsHitTestVisibleProperty());
|
|
277
279
|
}
|
|
278
280
|
}
|
|
279
281
|
} else {
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
-->
|
|
11
11
|
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
|
12
12
|
<PropertyGroup>
|
|
13
|
-
<ReactNativeWindowsVersion>0.70.
|
|
13
|
+
<ReactNativeWindowsVersion>0.70.1</ReactNativeWindowsVersion>
|
|
14
14
|
<ReactNativeWindowsMajor>0</ReactNativeWindowsMajor>
|
|
15
15
|
<ReactNativeWindowsMinor>70</ReactNativeWindowsMinor>
|
|
16
|
-
<ReactNativeWindowsPatch>
|
|
16
|
+
<ReactNativeWindowsPatch>1</ReactNativeWindowsPatch>
|
|
17
17
|
<ReactNativeWindowsCanary>false</ReactNativeWindowsCanary>
|
|
18
18
|
</PropertyGroup>
|
|
19
19
|
</Project>
|
|
@@ -72,9 +72,11 @@ static void SetUpHttpResource(
|
|
|
72
72
|
};
|
|
73
73
|
resource->SetOnData(std::move(onDataDynamic));
|
|
74
74
|
|
|
75
|
-
resource->SetOnError([weakReactInstance](int64_t requestId, string &&message) {
|
|
75
|
+
resource->SetOnError([weakReactInstance](int64_t requestId, string &&message, bool isTimeout) {
|
|
76
76
|
dynamic args = dynamic::array(requestId, std::move(message));
|
|
77
|
-
|
|
77
|
+
if (isTimeout) {
|
|
78
|
+
args.push_back(true);
|
|
79
|
+
}
|
|
78
80
|
|
|
79
81
|
SendEvent(weakReactInstance, completedResponse, std::move(args));
|
|
80
82
|
});
|
|
@@ -96,7 +96,7 @@ struct IHttpResource {
|
|
|
96
96
|
virtual void SetOnData(std::function<void(int64_t requestId, std::string &&responseData)> &&handler) noexcept = 0;
|
|
97
97
|
virtual void SetOnData(std::function<void(int64_t requestId, folly::dynamic &&responseData)> &&handler) noexcept = 0;
|
|
98
98
|
virtual void SetOnError(
|
|
99
|
-
std::function<void(int64_t requestId, std::string &&errorMessage
|
|
99
|
+
std::function<void(int64_t requestId, std::string &&errorMessage, bool isTimeout)> &&handler) noexcept = 0;
|
|
100
100
|
};
|
|
101
101
|
|
|
102
102
|
} // namespace Microsoft::React::Networking
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include <winrt/Windows.Foundation.h>
|
|
7
|
+
#include <winrt/Windows.Web.Http.h>
|
|
8
|
+
#include <winrt/base.h>
|
|
9
|
+
|
|
10
|
+
namespace Microsoft::React::Networking {
|
|
11
|
+
|
|
12
|
+
struct IRedirectEventSource : winrt::implements<IRedirectEventSource, winrt::Windows::Foundation::IInspectable> {
|
|
13
|
+
virtual bool OnRedirecting(
|
|
14
|
+
winrt::Windows::Web::Http::HttpRequestMessage const &request,
|
|
15
|
+
winrt::Windows::Web::Http::HttpResponseMessage const &response) noexcept = 0;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
} // namespace Microsoft::React::Networking
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include <winrt/Windows.Foundation.Collections.h>
|
|
7
|
+
#include <winrt/Windows.Foundation.h>
|
|
8
|
+
#include <winrt/Windows.Web.Http.h>
|
|
9
|
+
|
|
10
|
+
namespace Microsoft::React::Networking {
|
|
11
|
+
|
|
12
|
+
struct IWinRTHttpRequestFactory {
|
|
13
|
+
virtual ~IWinRTHttpRequestFactory() noexcept {}
|
|
14
|
+
|
|
15
|
+
virtual winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Web::Http::HttpRequestMessage> CreateRequest(
|
|
16
|
+
winrt::Windows::Web::Http::HttpMethod &&method,
|
|
17
|
+
winrt::Windows::Foundation::Uri &&uri,
|
|
18
|
+
winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::Windows::Foundation::IInspectable>
|
|
19
|
+
props) noexcept = 0;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
} // namespace Microsoft::React::Networking
|
|
@@ -11,10 +11,6 @@
|
|
|
11
11
|
#include <boost/algorithm/string.hpp>
|
|
12
12
|
#include <boost/lexical_cast/try_lexical_convert.hpp>
|
|
13
13
|
|
|
14
|
-
// Windows API
|
|
15
|
-
#include <winrt/Windows.Foundation.Collections.h>
|
|
16
|
-
#include <winrt/Windows.Web.Http.Headers.h>
|
|
17
|
-
|
|
18
14
|
// Standard Library
|
|
19
15
|
#include <queue>
|
|
20
16
|
#include <regex>
|
|
@@ -28,6 +24,7 @@ using winrt::to_hstring;
|
|
|
28
24
|
using winrt::Windows::Foundation::IInspectable;
|
|
29
25
|
using winrt::Windows::Foundation::IPropertyValue;
|
|
30
26
|
using winrt::Windows::Foundation::Uri;
|
|
27
|
+
using winrt::Windows::Foundation::Collections::IMap;
|
|
31
28
|
using winrt::Windows::Web::Http::HttpMethod;
|
|
32
29
|
using winrt::Windows::Web::Http::HttpRequestMessage;
|
|
33
30
|
using winrt::Windows::Web::Http::HttpResponseMessage;
|
|
@@ -119,7 +116,7 @@ bool OriginPolicyHttpFilter::ConstWcharComparer::operator()(const wchar_t *a, co
|
|
|
119
116
|
}
|
|
120
117
|
|
|
121
118
|
/*static*/ bool OriginPolicyHttpFilter::IsSameOrigin(Uri const &u1, Uri const &u2) noexcept {
|
|
122
|
-
return u1.SchemeName() == u2.SchemeName() && u1.Host() == u2.Host() && u1.Port() == u2.Port();
|
|
119
|
+
return (u1 && u2) && u1.SchemeName() == u2.SchemeName() && u1.Host() == u2.Host() && u1.Port() == u2.Port();
|
|
123
120
|
}
|
|
124
121
|
|
|
125
122
|
/*static*/ bool OriginPolicyHttpFilter::IsSimpleCorsRequest(HttpRequestMessage const &request) noexcept {
|
|
@@ -374,7 +371,7 @@ bool OriginPolicyHttpFilter::ConstWcharComparer::operator()(const wchar_t *a, co
|
|
|
374
371
|
}
|
|
375
372
|
}
|
|
376
373
|
|
|
377
|
-
OriginPolicyHttpFilter::OriginPolicyHttpFilter(IHttpFilter
|
|
374
|
+
OriginPolicyHttpFilter::OriginPolicyHttpFilter(IHttpFilter const &innerFilter) : m_innerFilter{innerFilter} {}
|
|
378
375
|
|
|
379
376
|
OriginPolicyHttpFilter::OriginPolicyHttpFilter()
|
|
380
377
|
: OriginPolicyHttpFilter(winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter{}) {}
|
|
@@ -443,21 +440,24 @@ OriginPolicy OriginPolicyHttpFilter::ValidateRequest(HttpRequestMessage const &r
|
|
|
443
440
|
void OriginPolicyHttpFilter::ValidateAllowOrigin(
|
|
444
441
|
hstring const &allowedOrigin,
|
|
445
442
|
hstring const &allowCredentials,
|
|
446
|
-
IInspectable
|
|
443
|
+
IMap<hstring, IInspectable> props) const {
|
|
447
444
|
// 4.10.1-2 - null allow origin
|
|
448
445
|
if (L"null" == allowedOrigin)
|
|
449
446
|
throw hresult_error{
|
|
450
447
|
E_INVALIDARG,
|
|
451
448
|
L"Response header Access-Control-Allow-Origin has a value of [null] which differs from the supplied origin"};
|
|
452
449
|
|
|
453
|
-
bool withCredentials =
|
|
450
|
+
bool withCredentials = props.Lookup(L"RequestArgs").as<RequestArgs>()->WithCredentials;
|
|
454
451
|
// 4.10.3 - valid wild card allow origin
|
|
455
452
|
if (!withCredentials && L"*" == allowedOrigin)
|
|
456
453
|
return;
|
|
457
454
|
|
|
458
455
|
// We assume the source (request) origin is not "*", "null", or empty string. Valid URI is expected
|
|
459
456
|
// 4.10.4 - Mismatched allow origin
|
|
460
|
-
|
|
457
|
+
auto taintedOriginProp = props.TryLookup(L"TaintedOrigin");
|
|
458
|
+
auto taintedOrigin = taintedOriginProp && winrt::unbox_value<bool>(taintedOriginProp);
|
|
459
|
+
auto origin = taintedOrigin ? nullptr : s_origin;
|
|
460
|
+
if (allowedOrigin.empty() || !IsSameOrigin(origin, Uri{allowedOrigin})) {
|
|
461
461
|
hstring errorMessage;
|
|
462
462
|
if (allowedOrigin.empty())
|
|
463
463
|
errorMessage = L"No valid origin in response";
|
|
@@ -511,14 +511,14 @@ void OriginPolicyHttpFilter::ValidatePreflightResponse(
|
|
|
511
511
|
|
|
512
512
|
auto controlValues = ExtractAccessControlValues(response.Headers());
|
|
513
513
|
|
|
514
|
-
auto
|
|
514
|
+
auto props = request.Properties();
|
|
515
515
|
// Check if the origin is allowed in conjuction with the withCredentials flag
|
|
516
516
|
// CORS preflight should always exclude credentials although the subsequent CORS request may include credentials.
|
|
517
|
-
ValidateAllowOrigin(controlValues.AllowedOrigin, controlValues.AllowedCredentials,
|
|
517
|
+
ValidateAllowOrigin(controlValues.AllowedOrigin, controlValues.AllowedCredentials, props);
|
|
518
518
|
|
|
519
519
|
// See https://fetch.spec.whatwg.org/#cors-preflight-fetch, section 4.8.7.5
|
|
520
520
|
// Check if the request method is allowed
|
|
521
|
-
bool withCredentials =
|
|
521
|
+
bool withCredentials = props.Lookup(L"RequestArgs").as<RequestArgs>()->WithCredentials;
|
|
522
522
|
bool requestMethodAllowed = false;
|
|
523
523
|
for (const auto &method : controlValues.AllowedMethods) {
|
|
524
524
|
if (L"*" == method) {
|
|
@@ -579,8 +579,8 @@ void OriginPolicyHttpFilter::ValidateResponse(HttpResponseMessage const &respons
|
|
|
579
579
|
if (originPolicy == OriginPolicy::SimpleCrossOriginResourceSharing ||
|
|
580
580
|
originPolicy == OriginPolicy::CrossOriginResourceSharing) {
|
|
581
581
|
auto controlValues = ExtractAccessControlValues(response.Headers());
|
|
582
|
-
auto
|
|
583
|
-
|
|
582
|
+
auto props = response.RequestMessage().Properties();
|
|
583
|
+
auto withCredentials = props.Lookup(L"RequestArgs").try_as<RequestArgs>()->WithCredentials;
|
|
584
584
|
|
|
585
585
|
if (GetRuntimeOptionBool("Http.StrictOriginCheckSimpleCors") &&
|
|
586
586
|
originPolicy == OriginPolicy::SimpleCrossOriginResourceSharing) {
|
|
@@ -595,8 +595,7 @@ void OriginPolicyHttpFilter::ValidateResponse(HttpResponseMessage const &respons
|
|
|
595
595
|
throw hresult_error{E_INVALIDARG, L"The server does not support CORS or the origin is not allowed"};
|
|
596
596
|
}
|
|
597
597
|
} else {
|
|
598
|
-
|
|
599
|
-
ValidateAllowOrigin(controlValues.AllowedOrigin, controlValues.AllowedCredentials, iRequestArgs);
|
|
598
|
+
ValidateAllowOrigin(controlValues.AllowedOrigin, controlValues.AllowedCredentials, props);
|
|
600
599
|
}
|
|
601
600
|
|
|
602
601
|
if (originPolicy == OriginPolicy::SimpleCrossOriginResourceSharing) {
|
|
@@ -684,6 +683,38 @@ ResponseOperation OriginPolicyHttpFilter::SendPreflightAsync(HttpRequestMessage
|
|
|
684
683
|
co_return {co_await m_innerFilter.SendRequestAsync(preflightRequest)};
|
|
685
684
|
}
|
|
686
685
|
|
|
686
|
+
#pragma region IRedirectEventSource
|
|
687
|
+
|
|
688
|
+
bool OriginPolicyHttpFilter::OnRedirecting(
|
|
689
|
+
HttpRequestMessage const &request,
|
|
690
|
+
HttpResponseMessage const &response) noexcept {
|
|
691
|
+
// Consider the following scenario.
|
|
692
|
+
// User signs in to http://a.com and visits a page that makes CORS request to http://b.com with origin=http://a.com.
|
|
693
|
+
// Http://b.com reponds with a redirect to http://a.com. The browser follows the redirect to http://a.com with
|
|
694
|
+
// origin=http://a.com. Since the origin matches the URL, the request is authorized at http://a.com, but it actually
|
|
695
|
+
// allows http://b.com to bypass the CORS check at http://a.com since the redirected URL is from http://b.com.
|
|
696
|
+
if (!IsSameOrigin(response.Headers().Location(), request.RequestUri()) &&
|
|
697
|
+
!IsSameOrigin(s_origin, request.RequestUri())) {
|
|
698
|
+
// By masking the origin field in the request header, we make it impossible for the server to set a single value for
|
|
699
|
+
// the access-control-allow-origin header. It means, the only way to support redirect is that server allows access
|
|
700
|
+
// from all sites through wildcard.
|
|
701
|
+
request.Headers().Insert(L"Origin", L"null");
|
|
702
|
+
|
|
703
|
+
auto props = request.Properties();
|
|
704
|
+
// Look for 'RequestArgs' key to ensure we are redirecting the main request.
|
|
705
|
+
if (auto iReqArgs = props.TryLookup(L"RequestArgs")) {
|
|
706
|
+
props.Insert(L"TaintedOrigin", winrt::box_value(true));
|
|
707
|
+
} else {
|
|
708
|
+
// Abort redirection if the request is either preflight or extraneous.
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
return true;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
#pragma endregion IRedirectEventSource
|
|
717
|
+
|
|
687
718
|
#pragma region IHttpFilter
|
|
688
719
|
|
|
689
720
|
ResponseOperation OriginPolicyHttpFilter::SendRequestAsync(HttpRequestMessage const &request) {
|
|
@@ -3,11 +3,14 @@
|
|
|
3
3
|
|
|
4
4
|
#pragma once
|
|
5
5
|
|
|
6
|
+
#include "IRedirectEventSource.h"
|
|
6
7
|
#include "OriginPolicy.h"
|
|
7
8
|
|
|
8
9
|
// Windows API
|
|
10
|
+
#include <winrt/Windows.Foundation.Collections.h>
|
|
9
11
|
#include <winrt/Windows.Foundation.h>
|
|
10
12
|
#include <winrt/Windows.Web.Http.Filters.h>
|
|
13
|
+
#include <winrt/Windows.Web.Http.Headers.h>
|
|
11
14
|
#include <winrt/Windows.Web.Http.h>
|
|
12
15
|
|
|
13
16
|
// Standard Library
|
|
@@ -16,7 +19,8 @@
|
|
|
16
19
|
namespace Microsoft::React::Networking {
|
|
17
20
|
|
|
18
21
|
class OriginPolicyHttpFilter
|
|
19
|
-
: public winrt::
|
|
22
|
+
: public winrt::
|
|
23
|
+
implements<OriginPolicyHttpFilter, winrt::Windows::Web::Http::Filters::IHttpFilter, IRedirectEventSource> {
|
|
20
24
|
public:
|
|
21
25
|
struct ConstWcharComparer {
|
|
22
26
|
bool operator()(const wchar_t *, const wchar_t *) const;
|
|
@@ -75,7 +79,7 @@ class OriginPolicyHttpFilter
|
|
|
75
79
|
winrt::Windows::Web::Http::HttpResponseMessage const &response,
|
|
76
80
|
bool removeAll);
|
|
77
81
|
|
|
78
|
-
OriginPolicyHttpFilter(winrt::Windows::Web::Http::Filters::IHttpFilter
|
|
82
|
+
OriginPolicyHttpFilter(winrt::Windows::Web::Http::Filters::IHttpFilter const &innerFilter);
|
|
79
83
|
|
|
80
84
|
OriginPolicyHttpFilter();
|
|
81
85
|
|
|
@@ -92,13 +96,22 @@ class OriginPolicyHttpFilter
|
|
|
92
96
|
void ValidateAllowOrigin(
|
|
93
97
|
winrt::hstring const &allowedOrigin,
|
|
94
98
|
winrt::hstring const &allowCredentials,
|
|
95
|
-
winrt::Windows::Foundation::IInspectable
|
|
99
|
+
winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::Windows::Foundation::IInspectable> props)
|
|
100
|
+
const;
|
|
96
101
|
|
|
97
102
|
winrt::Windows::Foundation::IAsyncOperationWithProgress<
|
|
98
103
|
winrt::Windows::Web::Http::HttpResponseMessage,
|
|
99
104
|
winrt::Windows::Web::Http::HttpProgress>
|
|
100
105
|
SendPreflightAsync(winrt::Windows::Web::Http::HttpRequestMessage const &request) const;
|
|
101
106
|
|
|
107
|
+
#pragma region IRedirectEventSource
|
|
108
|
+
|
|
109
|
+
bool OnRedirecting(
|
|
110
|
+
winrt::Windows::Web::Http::HttpRequestMessage const &request,
|
|
111
|
+
winrt::Windows::Web::Http::HttpResponseMessage const &response) noexcept override;
|
|
112
|
+
|
|
113
|
+
#pragma endregion IRedirectEventSource
|
|
114
|
+
|
|
102
115
|
#pragma region IHttpFilter
|
|
103
116
|
|
|
104
117
|
winrt::Windows::Foundation::IAsyncOperationWithProgress<
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
|
|
4
|
+
#undef WINRT_LEAN_AND_MEAN
|
|
5
|
+
|
|
6
|
+
#include "RedirectHttpFilter.h"
|
|
7
|
+
|
|
8
|
+
#include "WinRTTypes.h"
|
|
9
|
+
|
|
10
|
+
// Windows API
|
|
11
|
+
#include <winapifamily.h>
|
|
12
|
+
#include <winrt/Windows.Web.Http.Headers.h>
|
|
13
|
+
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
|
14
|
+
#include <WinInet.h>
|
|
15
|
+
#else
|
|
16
|
+
#define INTERNET_ERROR_BASE 12000
|
|
17
|
+
#define ERROR_HTTP_REDIRECT_FAILED (INTERNET_ERROR_BASE + 156)
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
namespace {
|
|
21
|
+
constexpr size_t DefaultMaxRedirects = 20;
|
|
22
|
+
} // namespace
|
|
23
|
+
|
|
24
|
+
using winrt::Windows::Foundation::Uri;
|
|
25
|
+
using winrt::Windows::Foundation::Collections::IVector;
|
|
26
|
+
using winrt::Windows::Security::Credentials::PasswordCredential;
|
|
27
|
+
using winrt::Windows::Security::Cryptography::Certificates::Certificate;
|
|
28
|
+
using winrt::Windows::Security::Cryptography::Certificates::ChainValidationResult;
|
|
29
|
+
using winrt::Windows::Web::Http::HttpMethod;
|
|
30
|
+
using winrt::Windows::Web::Http::HttpRequestMessage;
|
|
31
|
+
using winrt::Windows::Web::Http::HttpResponseMessage;
|
|
32
|
+
using winrt::Windows::Web::Http::HttpStatusCode;
|
|
33
|
+
using winrt::Windows::Web::Http::Filters::IHttpBaseProtocolFilter;
|
|
34
|
+
using winrt::Windows::Web::Http::Filters::IHttpFilter;
|
|
35
|
+
|
|
36
|
+
namespace Microsoft::React::Networking {
|
|
37
|
+
|
|
38
|
+
#pragma region RedirectHttpFilter
|
|
39
|
+
|
|
40
|
+
RedirectHttpFilter::RedirectHttpFilter(
|
|
41
|
+
size_t maxRedirects,
|
|
42
|
+
IHttpFilter &&innerFilter,
|
|
43
|
+
IHttpFilter &&innerFilterWithNoCredentials) noexcept
|
|
44
|
+
: m_maximumRedirects{maxRedirects},
|
|
45
|
+
m_innerFilter{std::move(innerFilter)},
|
|
46
|
+
m_innerFilterWithNoCredentials{std::move(innerFilterWithNoCredentials)} {
|
|
47
|
+
// Prevent automatic redirections.
|
|
48
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
49
|
+
baseFilter.AllowAutoRedirect(false);
|
|
50
|
+
baseFilter.AllowUI(false);
|
|
51
|
+
}
|
|
52
|
+
if (auto baseFilter = m_innerFilterWithNoCredentials.try_as<IHttpBaseProtocolFilter>()) {
|
|
53
|
+
baseFilter.AllowAutoRedirect(false);
|
|
54
|
+
baseFilter.AllowUI(false);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
RedirectHttpFilter::RedirectHttpFilter(IHttpFilter &&innerFilter, IHttpFilter &&innerFilterWithNoCredentials) noexcept
|
|
59
|
+
: RedirectHttpFilter(DefaultMaxRedirects, std::move(innerFilter), std::move(innerFilterWithNoCredentials)) {}
|
|
60
|
+
|
|
61
|
+
RedirectHttpFilter::RedirectHttpFilter() noexcept
|
|
62
|
+
: RedirectHttpFilter(
|
|
63
|
+
winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter{},
|
|
64
|
+
winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter{}) {}
|
|
65
|
+
|
|
66
|
+
void RedirectHttpFilter::SetRequestFactory(std::weak_ptr<IWinRTHttpRequestFactory> factory) noexcept {
|
|
67
|
+
m_requestFactory = factory;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
void RedirectHttpFilter::SetRedirectSource(
|
|
71
|
+
winrt::com_ptr<Microsoft::React::Networking::IRedirectEventSource> const &eventSrc) noexcept {
|
|
72
|
+
m_redirEventSrc = eventSrc;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#pragma region IHttpBaseProtocolFilter
|
|
76
|
+
|
|
77
|
+
bool RedirectHttpFilter::AllowAutoRedirect() const {
|
|
78
|
+
return m_allowAutoRedirect;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
void RedirectHttpFilter::AllowAutoRedirect(bool value) {
|
|
82
|
+
m_allowAutoRedirect = value;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
bool RedirectHttpFilter::AllowUI() const {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
void RedirectHttpFilter::AllowUI(bool /*value*/) const {
|
|
89
|
+
throw winrt::hresult_error{HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED)};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
bool RedirectHttpFilter::AutomaticDecompression() const {
|
|
93
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
94
|
+
return baseFilter.AutomaticDecompression();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
void RedirectHttpFilter::AutomaticDecompression(bool value) const {
|
|
100
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
101
|
+
baseFilter.AutomaticDecompression(value);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
winrt::Windows::Web::Http::Filters::HttpCacheControl RedirectHttpFilter::CacheControl() const {
|
|
106
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
107
|
+
return baseFilter.CacheControl();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return nullptr;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
winrt::Windows::Web::Http::HttpCookieManager RedirectHttpFilter::CookieManager() const {
|
|
114
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
115
|
+
return baseFilter.CookieManager();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return nullptr;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
Certificate RedirectHttpFilter::ClientCertificate() const {
|
|
122
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
123
|
+
return baseFilter.ClientCertificate();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return nullptr;
|
|
127
|
+
}
|
|
128
|
+
void RedirectHttpFilter::ClientCertificate(Certificate const &value) const {
|
|
129
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
130
|
+
baseFilter.ClientCertificate(value);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
IVector<ChainValidationResult> RedirectHttpFilter::IgnorableServerCertificateErrors() const {
|
|
135
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
136
|
+
return baseFilter.IgnorableServerCertificateErrors();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return nullptr;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
uint32_t RedirectHttpFilter::MaxConnectionsPerServer() const {
|
|
143
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
144
|
+
return baseFilter.MaxConnectionsPerServer();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
void RedirectHttpFilter::MaxConnectionsPerServer(uint32_t value) const {
|
|
150
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
151
|
+
baseFilter.MaxConnectionsPerServer(value);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
PasswordCredential RedirectHttpFilter::ProxyCredential() const {
|
|
156
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
157
|
+
return baseFilter.ProxyCredential();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return nullptr;
|
|
161
|
+
}
|
|
162
|
+
void RedirectHttpFilter::ProxyCredential(PasswordCredential const &value) const {
|
|
163
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
164
|
+
baseFilter.ProxyCredential(value);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
PasswordCredential RedirectHttpFilter::ServerCredential() const {
|
|
169
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
170
|
+
return baseFilter.ServerCredential();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return nullptr;
|
|
174
|
+
}
|
|
175
|
+
void RedirectHttpFilter::ServerCredential(PasswordCredential const &value) const {
|
|
176
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
177
|
+
baseFilter.ServerCredential(value);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
bool RedirectHttpFilter::UseProxy() const {
|
|
182
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
183
|
+
return baseFilter.UseProxy();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
void RedirectHttpFilter::UseProxy(bool value) const {
|
|
189
|
+
if (auto baseFilter = m_innerFilter.try_as<IHttpBaseProtocolFilter>()) {
|
|
190
|
+
baseFilter.UseProxy(value);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#pragma endregion IHttpBaseProtocolFilter
|
|
195
|
+
|
|
196
|
+
#pragma region IHttpFilter
|
|
197
|
+
|
|
198
|
+
///
|
|
199
|
+
/// See https://github.com/dotnet/corefx/pull/22702
|
|
200
|
+
ResponseOperation RedirectHttpFilter::SendRequestAsync(HttpRequestMessage const &request) {
|
|
201
|
+
size_t redirectCount = 0;
|
|
202
|
+
HttpMethod method{nullptr};
|
|
203
|
+
HttpResponseMessage response{nullptr};
|
|
204
|
+
|
|
205
|
+
auto coRequest = request;
|
|
206
|
+
auto coAllowAutoRedirect = m_allowAutoRedirect;
|
|
207
|
+
auto coMaxRedirects = m_maximumRedirects;
|
|
208
|
+
auto coRequestFactory = m_requestFactory;
|
|
209
|
+
auto coEventSrc = m_redirEventSrc;
|
|
210
|
+
|
|
211
|
+
method = coRequest.Method();
|
|
212
|
+
|
|
213
|
+
do {
|
|
214
|
+
// Send subsequent requests through the filter that doesn't have the credentials included in the first request
|
|
215
|
+
response =
|
|
216
|
+
co_await (redirectCount > 0 ? m_innerFilterWithNoCredentials : m_innerFilter).SendRequestAsync(coRequest);
|
|
217
|
+
|
|
218
|
+
// Stop redirecting when a non-redirect status is responded.
|
|
219
|
+
if (response.StatusCode() != HttpStatusCode::MultipleChoices &&
|
|
220
|
+
response.StatusCode() != HttpStatusCode::MovedPermanently &&
|
|
221
|
+
response.StatusCode() != HttpStatusCode::Found && // Redirect
|
|
222
|
+
response.StatusCode() != HttpStatusCode::SeeOther && // RedirectMethod
|
|
223
|
+
response.StatusCode() != HttpStatusCode::TemporaryRedirect && // RedirectKeepVerb
|
|
224
|
+
response.StatusCode() != HttpStatusCode::PermanentRedirect) {
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
redirectCount++;
|
|
229
|
+
if (redirectCount > coMaxRedirects) {
|
|
230
|
+
throw winrt::hresult_error{HRESULT_FROM_WIN32(ERROR_HTTP_REDIRECT_FAILED), L"Too many redirects"};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Call event source's OnRedirecting before modifying request parameters.
|
|
234
|
+
if (coEventSrc && !coEventSrc->OnRedirecting(coRequest, response)) {
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (auto requestFactory = coRequestFactory.lock()) {
|
|
239
|
+
coRequest =
|
|
240
|
+
co_await requestFactory->CreateRequest(HttpMethod{method}, coRequest.RequestUri(), coRequest.Properties());
|
|
241
|
+
|
|
242
|
+
if (!coRequest) {
|
|
243
|
+
throw winrt::hresult_error{E_INVALIDARG, L"Invalid request handle"};
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
auto redirectUri = Uri{response.Headers().Location().AbsoluteUri()};
|
|
248
|
+
if (!redirectUri) {
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (redirectUri.SchemeName() != L"http" && redirectUri.SchemeName() != L"https") {
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Do not "downgrade" from HTTPS to HTTP
|
|
257
|
+
if (coRequest.RequestUri().SchemeName() == L"https" && redirectUri.SchemeName() == L"http") {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/// See https://github.com/dotnet/corefx/blob/v3.1.28/src/System.Net.Http/src/uap/System/Net/HttpClientHandler.cs
|
|
262
|
+
// Follow HTTP RFC 7231 rules. In general, 3xx responses
|
|
263
|
+
// except for 307 and 308 will keep verb except POST becomes GET.
|
|
264
|
+
// 307 and 308 responses have all verbs stay the same.
|
|
265
|
+
// https://tools.ietf.org/html/rfc7231#section-6.4
|
|
266
|
+
if (response.StatusCode() != HttpStatusCode::TemporaryRedirect &&
|
|
267
|
+
response.StatusCode() != HttpStatusCode::PermanentRedirect && method != HttpMethod::Post()) {
|
|
268
|
+
method = HttpMethod::Get();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
coRequest.RequestUri(redirectUri);
|
|
272
|
+
} while (coAllowAutoRedirect);
|
|
273
|
+
|
|
274
|
+
response.RequestMessage(coRequest);
|
|
275
|
+
|
|
276
|
+
co_return response;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
#pragma endregion IHttpFilter
|
|
280
|
+
|
|
281
|
+
#pragma endregion RedirectHttpFilter
|
|
282
|
+
|
|
283
|
+
} // namespace Microsoft::React::Networking
|