Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 9abc09d

Browse files
hughbestephentoub
authored andcommitted
Move internal cookie fields to common
1 parent db952e0 commit 9abc09d

File tree

6 files changed

+66
-51
lines changed

6 files changed

+66
-51
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace System.Net
6+
{
7+
internal static class CookieFields
8+
{
9+
internal const string CommentAttributeName = "Comment";
10+
internal const string CommentUrlAttributeName = "CommentURL";
11+
internal const string DiscardAttributeName = "Discard";
12+
internal const string DomainAttributeName = "Domain";
13+
internal const string ExpiresAttributeName = "Expires";
14+
internal const string MaxAgeAttributeName = "Max-Age";
15+
internal const string PathAttributeName = "Path";
16+
internal const string PortAttributeName = "Port";
17+
internal const string SecureAttributeName = "Secure";
18+
internal const string VersionAttributeName = "Version";
19+
internal const string HttpOnlyAttributeName = "HttpOnly";
20+
}
21+
}

src/Common/src/System/Net/CookieParser.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -461,25 +461,25 @@ internal bool IsEqualTo(string value)
461461

462462
// Recognized attributes in order of expected frequency.
463463
private static readonly RecognizedAttribute[] s_recognizedAttributes = {
464-
new RecognizedAttribute(Cookie.PathAttributeName, CookieToken.Path),
465-
new RecognizedAttribute(Cookie.MaxAgeAttributeName, CookieToken.MaxAge),
466-
new RecognizedAttribute(Cookie.ExpiresAttributeName, CookieToken.Expires),
467-
new RecognizedAttribute(Cookie.VersionAttributeName, CookieToken.Version),
468-
new RecognizedAttribute(Cookie.DomainAttributeName, CookieToken.Domain),
469-
new RecognizedAttribute(Cookie.SecureAttributeName, CookieToken.Secure),
470-
new RecognizedAttribute(Cookie.DiscardAttributeName, CookieToken.Discard),
471-
new RecognizedAttribute(Cookie.PortAttributeName, CookieToken.Port),
472-
new RecognizedAttribute(Cookie.CommentAttributeName, CookieToken.Comment),
473-
new RecognizedAttribute(Cookie.CommentUrlAttributeName, CookieToken.CommentUrl),
474-
new RecognizedAttribute(Cookie.HttpOnlyAttributeName, CookieToken.HttpOnly),
464+
new RecognizedAttribute(CookieFields.PathAttributeName, CookieToken.Path),
465+
new RecognizedAttribute(CookieFields.MaxAgeAttributeName, CookieToken.MaxAge),
466+
new RecognizedAttribute(CookieFields.ExpiresAttributeName, CookieToken.Expires),
467+
new RecognizedAttribute(CookieFields.VersionAttributeName, CookieToken.Version),
468+
new RecognizedAttribute(CookieFields.DomainAttributeName, CookieToken.Domain),
469+
new RecognizedAttribute(CookieFields.SecureAttributeName, CookieToken.Secure),
470+
new RecognizedAttribute(CookieFields.DiscardAttributeName, CookieToken.Discard),
471+
new RecognizedAttribute(CookieFields.PortAttributeName, CookieToken.Port),
472+
new RecognizedAttribute(CookieFields.CommentAttributeName, CookieToken.Comment),
473+
new RecognizedAttribute(CookieFields.CommentUrlAttributeName, CookieToken.CommentUrl),
474+
new RecognizedAttribute(CookieFields.HttpOnlyAttributeName, CookieToken.HttpOnly),
475475
};
476476

477477
private static readonly RecognizedAttribute[] s_recognizedServerAttributes = {
478-
new RecognizedAttribute('$' + Cookie.PathAttributeName, CookieToken.Path),
479-
new RecognizedAttribute('$' + Cookie.VersionAttributeName, CookieToken.Version),
480-
new RecognizedAttribute('$' + Cookie.DomainAttributeName, CookieToken.Domain),
481-
new RecognizedAttribute('$' + Cookie.PortAttributeName, CookieToken.Port),
482-
new RecognizedAttribute('$' + Cookie.HttpOnlyAttributeName, CookieToken.HttpOnly),
478+
new RecognizedAttribute('$' + CookieFields.PathAttributeName, CookieToken.Path),
479+
new RecognizedAttribute('$' + CookieFields.VersionAttributeName, CookieToken.Version),
480+
new RecognizedAttribute('$' + CookieFields.DomainAttributeName, CookieToken.Domain),
481+
new RecognizedAttribute('$' + CookieFields.PortAttributeName, CookieToken.Port),
482+
new RecognizedAttribute('$' + CookieFields.HttpOnlyAttributeName, CookieToken.HttpOnly),
483483
};
484484

485485
internal CookieToken TokenFromName(bool parseResponseCookies)

src/System.Net.Primitives/src/System.Net.Primitives.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
<Compile Include="$(CommonPath)\System\Net\ByteOrder.cs">
6363
<Link>Common\System\Net\ByteOrder.cs</Link>
6464
</Compile>
65+
<Compile Include="$(CommonPath)\System\Net\CookieFields.cs">
66+
<Link>Common\System\Net\CookieFields.cs</Link>
67+
</Compile>
6568
<Compile Include="$(CommonPath)\System\Net\CookieParser.cs">
6669
<Link>Common\System\Net\CookieParser.cs</Link>
6770
</Compile>

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

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ public sealed class Cookie
3333
internal const int MaxSupportedVersion = 1;
3434
internal const string MaxSupportedVersionString = "1";
3535

36-
internal const string CommentAttributeName = "Comment";
37-
internal const string CommentUrlAttributeName = "CommentURL";
38-
internal const string DiscardAttributeName = "Discard";
39-
internal const string DomainAttributeName = "Domain";
40-
internal const string ExpiresAttributeName = "Expires";
41-
internal const string MaxAgeAttributeName = "Max-Age";
42-
internal const string PathAttributeName = "Path";
43-
internal const string PortAttributeName = "Port";
44-
internal const string SecureAttributeName = "Secure";
45-
internal const string VersionAttributeName = "Version";
46-
internal const string HttpOnlyAttributeName = "HttpOnly";
47-
4836
internal const string SeparatorLiteral = "; ";
4937
internal const string EqualsLiteral = "=";
5038
internal const string QuotesLiteral = "\"";
@@ -355,7 +343,7 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma
355343
{
356344
if (shouldThrow)
357345
{
358-
throw new CookieException(SR.Format(SR.net_cookie_attribute, CommentAttributeName, Comment));
346+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.CommentAttributeName, Comment));
359347
}
360348
return false;
361349
}
@@ -366,7 +354,7 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma
366354
{
367355
if (shouldThrow)
368356
{
369-
throw new CookieException(SR.Format(SR.net_cookie_attribute, PathAttributeName, Path));
357+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PathAttributeName, Path));
370358
}
371359
return false;
372360
}
@@ -395,7 +383,7 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma
395383
{
396384
if (shouldThrow)
397385
{
398-
throw new CookieException(SR.Format(SR.net_cookie_attribute, DomainAttributeName, domain == null ? "<null>" : domain));
386+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.DomainAttributeName, domain == null ? "<null>" : domain));
399387
}
400388
return false;
401389
}
@@ -407,7 +395,7 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma
407395
{
408396
if (shouldThrow)
409397
{
410-
throw new CookieException(SR.Format(SR.net_cookie_attribute, DomainAttributeName, _domain));
398+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.DomainAttributeName, _domain));
411399
}
412400
return false;
413401
}
@@ -474,7 +462,7 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma
474462
{
475463
if (shouldThrow)
476464
{
477-
throw new CookieException(SR.Format(SR.net_cookie_attribute, DomainAttributeName, _domain));
465+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.DomainAttributeName, _domain));
478466
}
479467
return false;
480468
}
@@ -507,7 +495,7 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma
507495
{
508496
if (shouldThrow)
509497
{
510-
throw new CookieException(SR.Format(SR.net_cookie_attribute, PathAttributeName, _path));
498+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PathAttributeName, _path));
511499
}
512500
return false;
513501
}
@@ -535,7 +523,7 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma
535523
{
536524
if (shouldThrow)
537525
{
538-
throw new CookieException(SR.Format(SR.net_cookie_attribute, PortAttributeName, _port));
526+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PortAttributeName, _port));
539527
}
540528
return false;
541529
}
@@ -585,7 +573,7 @@ public string Port
585573
// Parse port list
586574
if (value[0] != '\"' || value[value.Length - 1] != '\"')
587575
{
588-
throw new CookieException(SR.Format(SR.net_cookie_attribute, PortAttributeName, value));
576+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PortAttributeName, value));
589577
}
590578
string[] ports = value.Split(PortSplitDelimiters);
591579

@@ -599,13 +587,13 @@ public string Port
599587
{
600588
if (!Int32.TryParse(ports[i], out port))
601589
{
602-
throw new CookieException(SR.Format(SR.net_cookie_attribute, PortAttributeName, value));
590+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PortAttributeName, value));
603591
}
604592

605593
// valid values for port 0 - 0xFFFF
606594
if ((port < 0) || (port > 0xFFFF))
607595
{
608-
throw new CookieException(SR.Format(SR.net_cookie_attribute, PortAttributeName, value));
596+
throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PortAttributeName, value));
609597
}
610598

611599
portList.Add(port);
@@ -742,7 +730,7 @@ internal void ToString(StringBuilder sb)
742730
// Add the Cookie version if necessary.
743731
if (Version != 0)
744732
{
745-
sb.Append(SpecialAttributeLiteral + VersionAttributeName + EqualsLiteral); // const strings
733+
sb.Append(SpecialAttributeLiteral + CookieFields.VersionAttributeName + EqualsLiteral); // const strings
746734
if (IsQuotedVersion) sb.Append('"');
747735
sb.Append(_version.ToString(NumberFormatInfo.InvariantInfo));
748736
if (IsQuotedVersion) sb.Append('"');
@@ -757,14 +745,14 @@ internal void ToString(StringBuilder sb)
757745
// Add the Path if necessary.
758746
if (!_pathImplicit && _path.Length > 0)
759747
{
760-
sb.Append(SeparatorLiteral + SpecialAttributeLiteral + PathAttributeName + EqualsLiteral); // const strings
748+
sb.Append(SeparatorLiteral + SpecialAttributeLiteral + CookieFields.PathAttributeName + EqualsLiteral); // const strings
761749
sb.Append(_path);
762750
}
763751

764752
// Add the Domain if necessary.
765753
if (!_domainImplicit && _domain.Length > 0)
766754
{
767-
sb.Append(SeparatorLiteral + SpecialAttributeLiteral + DomainAttributeName + EqualsLiteral); // const strings
755+
sb.Append(SeparatorLiteral + SpecialAttributeLiteral + CookieFields.DomainAttributeName + EqualsLiteral); // const strings
768756
if (IsQuotedDomain) sb.Append('"');
769757
sb.Append(_domain);
770758
if (IsQuotedDomain) sb.Append('"');
@@ -774,7 +762,7 @@ internal void ToString(StringBuilder sb)
774762
// Add the Port if necessary.
775763
if (!_portImplicit)
776764
{
777-
sb.Append(SeparatorLiteral + SpecialAttributeLiteral + PortAttributeName); // const strings
765+
sb.Append(SeparatorLiteral + SpecialAttributeLiteral + CookieFields.PortAttributeName); // const strings
778766
if (_port.Length > 0)
779767
{
780768
sb.Append(EqualsLiteral);
@@ -796,19 +784,19 @@ internal string ToServerString()
796784
string result = Name + EqualsLiteral + Value;
797785
if (_comment != null && _comment.Length > 0)
798786
{
799-
result += SeparatorLiteral + CommentAttributeName + EqualsLiteral + _comment;
787+
result += SeparatorLiteral + CookieFields.CommentAttributeName + EqualsLiteral + _comment;
800788
}
801789
if (_commentUri != null)
802790
{
803-
result += SeparatorLiteral + CommentUrlAttributeName + EqualsLiteral + QuotesLiteral + _commentUri.ToString() + QuotesLiteral;
791+
result += SeparatorLiteral + CookieFields.CommentUrlAttributeName + EqualsLiteral + QuotesLiteral + _commentUri.ToString() + QuotesLiteral;
804792
}
805793
if (_discard)
806794
{
807-
result += SeparatorLiteral + DiscardAttributeName;
795+
result += SeparatorLiteral + CookieFields.DiscardAttributeName;
808796
}
809797
if (!_domainImplicit && _domain != null && _domain.Length > 0)
810798
{
811-
result += SeparatorLiteral + DomainAttributeName + EqualsLiteral + _domain;
799+
result += SeparatorLiteral + CookieFields.DomainAttributeName + EqualsLiteral + _domain;
812800
}
813801
if (Expires != DateTime.MinValue)
814802
{
@@ -819,20 +807,20 @@ internal string ToServerString()
819807
// so that the client will discard the cookie immediately.
820808
seconds = 0;
821809
}
822-
result += SeparatorLiteral + MaxAgeAttributeName + EqualsLiteral + seconds.ToString(NumberFormatInfo.InvariantInfo);
810+
result += SeparatorLiteral + CookieFields.MaxAgeAttributeName + EqualsLiteral + seconds.ToString(NumberFormatInfo.InvariantInfo);
823811
}
824812
if (!_pathImplicit && _path != null && _path.Length > 0)
825813
{
826-
result += SeparatorLiteral + PathAttributeName + EqualsLiteral + _path;
814+
result += SeparatorLiteral + CookieFields.PathAttributeName + EqualsLiteral + _path;
827815
}
828816
if (!Plain && !_portImplicit && _port != null && _port.Length > 0)
829817
{
830818
// QuotesLiteral are included in _port.
831-
result += SeparatorLiteral + PortAttributeName + EqualsLiteral + _port;
819+
result += SeparatorLiteral + CookieFields.PortAttributeName + EqualsLiteral + _port;
832820
}
833821
if (_version > 0)
834822
{
835-
result += SeparatorLiteral + VersionAttributeName + EqualsLiteral + _version.ToString(NumberFormatInfo.InvariantInfo);
823+
result += SeparatorLiteral + CookieFields.VersionAttributeName + EqualsLiteral + _version.ToString(NumberFormatInfo.InvariantInfo);
836824
}
837825
return result == EqualsLiteral ? null : result;
838826
}

src/System.Net.Primitives/src/System/Net/CookieContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ internal string GetCookieHeader(Uri uri, out string optCookie2)
960960

961961
optCookie2 = cookies.IsOtherVersionSeen ?
962962
(Cookie.SpecialAttributeLiteral +
963-
Cookie.VersionAttributeName +
963+
CookieFields.VersionAttributeName +
964964
Cookie.EqualsLiteral +
965965
Cookie.MaxSupportedVersionString) : string.Empty;
966966

src/System.Net.Primitives/tests/UnitTests/System.Net.Primitives.UnitTests.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
<Compile Include="..\..\src\System\Net\Sockets\AddressFamily.cs">
5151
<Link>ProductionCode\System\Net\Sockets\AddressFamily.cs</Link>
5252
</Compile>
53+
<Compile Include="$(CommonPath)\System\Net\CookieFields.cs">
54+
<Link>Common\System\Net\CookieFields.cs</Link>
55+
</Compile>
5356
<Compile Include="$(CommonPath)\System\Net\CookieParser.cs">
5457
<Link>ProductionCode\System\Net\CookieParser.cs</Link>
5558
</Compile>

0 commit comments

Comments
 (0)