Skip to content

Commit a41ac2c

Browse files
authored
Mark the port as implicit for empty values in Cookie (#76143)
* [Bug] Fix issue #70227 * Fix cookie tests cases to reflect correct behavior. * Remove trailing semicolon in expected result of Cookie string tests.
1 parent 6556178 commit a41ac2c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,14 +585,17 @@ public string Port
585585
}
586586
set
587587
{
588-
m_port_implicit = false;
589588
if (string.IsNullOrEmpty(value))
590589
{
591590
// "Port" is present but has no value.
591+
// Therefore; the effective port value is implicit.
592+
m_port_implicit = true;
592593
m_port = string.Empty;
593594
}
594595
else
595596
{
597+
// "Port" value is present, so we use the provided value rather than an implicit one.
598+
m_port_implicit = false;
596599
// Parse port list
597600
if (!value.StartsWith('\"') || !value.EndsWith('\"'))
598601
{

src/libraries/System.Net.Primitives/tests/FunctionalTests/CookieTest.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,14 @@ public static void ToString_Compare_Success()
349349
c.Version = 0;
350350
Assert.Equal("name=value; $Path=path; $Domain=domain; $Port=\"80\"", c.ToString());
351351

352+
// If a cookie string specifies either an empty string or no value for the port, then the port should be considered implicit.
353+
// Otherwise such cookies will have no valid ports and also be incapable of assuming a usable port which in turn means they cannot be matched to ANY Uris and will effectively become nonfunctional.
352354
c.Port = "";
353-
Assert.Equal("name=value; $Path=path; $Domain=domain; $Port", c.ToString());
355+
Assert.Equal("name=value; $Path=path; $Domain=domain", c.ToString());
356+
357+
// Test null also, for sanity.
358+
c.Port = null;
359+
Assert.Equal("name=value; $Path=path; $Domain=domain", c.ToString());
354360
}
355361
}
356362
}

0 commit comments

Comments
 (0)