Skip to content

Commit 129dfe0

Browse files
committed
Added workaround for Additional fragment marker being added under .NET Framework when redirect url contains fragment
1 parent 490950c commit 129dfe0

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

StandardSocketsHttpHandler.FunctionalTests/HttpClientHandlerTest.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,12 +1082,6 @@ public async Task GetAsync_AllowAutoRedirectTrue_RetainsOriginalFragmentIfApprop
10821082
return;
10831083
}
10841084

1085-
if (IsNetfxHandler)
1086-
{
1087-
// Similarly, netfx doesn't send fragments at all.
1088-
return;
1089-
}
1090-
10911085
if (IsWinHttpHandler)
10921086
{
10931087
// According to https://tools.ietf.org/html/rfc7231#section-7.1.2,
@@ -1108,6 +1102,24 @@ public async Task GetAsync_AllowAutoRedirectTrue_RetainsOriginalFragmentIfApprop
11081102
{
11091103
await LoopbackServer.CreateServerAsync(async (origServer, origUrl) =>
11101104
{
1105+
#if NET472
1106+
// .NET Framework 4.7.2 / 4.8 UriBuilder will always append the fragment marker ('#') to fragment starting with '#',
1107+
// while .NET Core will only append the fragment marker if not already present.
1108+
if (origFragment.StartsWith("#"))
1109+
{
1110+
origFragment = origFragment.Substring(1);
1111+
}
1112+
1113+
if (redirFragment.StartsWith("#"))
1114+
{
1115+
redirFragment = redirFragment.Substring(1);
1116+
}
1117+
1118+
if (expectedFragment.StartsWith("#"))
1119+
{
1120+
expectedFragment = expectedFragment.Substring(1);
1121+
}
1122+
#endif
11111123
origUrl = new UriBuilder(origUrl) { Fragment = origFragment }.Uri;
11121124
Uri redirectUrl = new UriBuilder(origUrl) { Fragment = redirFragment }.Uri;
11131125
if (useRelativeRedirect)

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/RedirectHandler.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ private Uri GetUriForRedirect(Uri requestUri, HttpResponseMessage response)
108108
string redirectFragment = location.Fragment;
109109
if (string.IsNullOrEmpty(redirectFragment))
110110
{
111+
#if NETSTANDARD20
112+
// .NET Framework 4.7.2 / 4.8 UriBuilder will always append the fragment marker ('#') to fragment starting with '#',
113+
// while .NET Core will only append the fragment marker if not already present.
114+
if (requestFragment.StartsWith("#"))
115+
{
116+
requestFragment = requestFragment.Substring(1);
117+
}
118+
#endif
111119
location = new UriBuilder(location) { Fragment = requestFragment }.Uri;
112120
}
113121
}

0 commit comments

Comments
 (0)