|
23 | 23 | using System;
|
24 | 24 | using System.Net;
|
25 | 25 |
|
26 |
| -// Disable until we get around to enable + fix any issues. |
27 |
| -#nullable disable |
| 26 | +#nullable enable |
28 | 27 |
|
29 | 28 | namespace Foundation {
|
30 | 29 | public partial class NSHttpCookie {
|
31 | 30 | // same order as System.Net.Cookie
|
32 | 31 | // http://msdn.microsoft.com/en-us/library/a18ka3h2.aspx
|
| 32 | + /// <summary>Create a new cookie with the supplied name and value.</summary> |
33 | 33 | /// <param name="name">Cookie's name. Cannot be null.</param>
|
34 |
| - /// <param name="value">Cookie's value. Cannot be null.</param> |
35 |
| - /// <summary>Create a new cookie with the supplied name and value.</summary> |
36 |
| - /// <remarks>A default Path and Domain will be used to ensure a valid instance is created.</remarks> |
37 |
| - public NSHttpCookie (string name, string value) : this (name, value, null, null) |
| 34 | + /// <param name="value">Cookie's value. Cannot be null.</param> |
| 35 | + /// <remarks>A default Path and Domain will be used to ensure a valid instance is created.</remarks> |
| 36 | + public NSHttpCookie (string name, string value) |
| 37 | + : this (name, value, null, null) |
38 | 38 | {
|
39 |
| - CreateCookie (name, value, null, null, null, null, null, null, null, null, null, null); |
40 | 39 | }
|
41 | 40 |
|
| 41 | + /// <summary>Create a new cookie with the supplied name, value and path.</summary> |
42 | 42 | /// <param name="name">Cookie's name. Cannot be null.</param>
|
43 |
| - /// <param name="value">Cookie's value. Cannot be null.</param> |
44 |
| - /// <param name="path">Path where the cookie will be applied on the domain. Using "/" will send the cookie to every URL on the domain.</param> |
45 |
| - /// <summary>Create a new cookie with the supplied name, value and path.</summary> |
46 |
| - /// <remarks>A default Domain will be used to ensure a valid instance is created</remarks> |
47 |
| - public NSHttpCookie (string name, string value, string path) : this (name, value, path, null) |
| 43 | + /// <param name="value">Cookie's value. Cannot be null.</param> |
| 44 | + /// <param name="path">Path where the cookie will be applied on the domain. Using "/" will send the cookie to every URL on the domain.</param> |
| 45 | + /// <remarks>A default Domain will be used to ensure a valid instance is created</remarks> |
| 46 | + public NSHttpCookie (string name, string value, string? path) |
| 47 | + : this (name, value, path, null) |
48 | 48 | {
|
49 |
| - CreateCookie (name, value, path, null, null, null, null, null, null, null, null, null); |
50 | 49 | }
|
51 | 50 |
|
| 51 | + /// <summary>Create a new cookie with the supplied name, value, path and domain.</summary> |
52 | 52 | /// <param name="name">Cookie's name. Cannot be null.</param>
|
53 |
| - /// <param name="value">Cookie's value. Cannot be null.</param> |
54 |
| - /// <param name="path">Path where the cookie will be applied on the domain. Using "/" will send the cookie to every URL on the domain.</param> |
55 |
| - /// <param name="domain">Domain (e.g. xamarin.com) related to the cookie</param> |
56 |
| - /// <summary>Create a new cookie with the supplied name, value, path and domain.</summary> |
57 |
| - /// <remarks>An ArgumentNullException will be thrown if either `name` or `value` are null.</remarks> |
58 |
| - public NSHttpCookie (string name, string value, string path, string domain) |
| 53 | + /// <param name="value">Cookie's value. Cannot be null.</param> |
| 54 | + /// <param name="path">Path where the cookie will be applied on the domain. Using "/" will send the cookie to every URL on the domain.</param> |
| 55 | + /// <param name="domain">Domain (e.g. microsoft.com) related to the cookie</param> |
| 56 | + /// <remarks>An <see cref="ArgumentNullException" /> will be thrown if either <paramref name="name" /> or <paramref name="value" /> are null.</remarks> |
| 57 | + public NSHttpCookie (string name, string value, string? path, string? domain) |
| 58 | + : base (NSObjectFlag.Empty) |
59 | 59 | {
|
60 | 60 | CreateCookie (name, value, path, domain, null, null, null, null, null, null, null, null);
|
61 | 61 | }
|
62 | 62 |
|
63 | 63 | // FIXME: should we expose more complex/long ctor or point people to use a Cookie ?
|
64 | 64 |
|
65 |
| - /// <param name="cookie">An existing Cookie from the .NET framework</param> |
66 |
| - /// <summary>Create a new cookie from the supplied System.Net.Cookie instance properties</summary> |
67 |
| - /// <remarks>This constructor will throw an ArgumentNullException if `cookie` is null</remarks> |
| 65 | + /// <summary>Create a new cookie from the supplied <see cref="System.Net.Cookie" /> instance's properties.</summary> |
| 66 | + /// <param name="cookie">An existing <see cref="System.Net.Cookie" /> from the .NET framework</param> |
| 67 | + /// <remarks>This constructor will throw an <see cref="ArgumentNullException" /> if <paramref name="cookie" /> is null</remarks> |
68 | 68 | public NSHttpCookie (Cookie cookie)
|
| 69 | + : base (NSObjectFlag.Empty) |
69 | 70 | {
|
70 | 71 | if (cookie is null)
|
71 |
| - throw new ArgumentNullException ("cookie"); |
| 72 | + throw new ArgumentNullException (nameof (cookie)); |
72 | 73 |
|
73 |
| - string commentUrl = cookie.CommentUri is not null ? cookie.CommentUri.ToString () : null; |
| 74 | + var commentUrl = cookie.CommentUri?.ToString (); |
74 | 75 | bool? discard = null;
|
75 | 76 | if (cookie.Discard)
|
76 | 77 | discard = true;
|
77 | 78 | CreateCookie (cookie.Name, cookie.Value, cookie.Path, cookie.Domain, cookie.Comment, commentUrl, discard, cookie.Expires, null, cookie.Port, cookie.Secure, cookie.Version);
|
78 | 79 | }
|
79 | 80 |
|
80 |
| - void CreateCookie (string name, string value, string path, string domain, string comment, string commentUrl, bool? discard, DateTime? expires, int? maximumAge, string ports, bool? secure, int? version) |
| 81 | + void CreateCookie (string name, string value, string? path, string? domain, string? comment, string? commentUrl, bool? discard, DateTime? expires, int? maximumAge, string? ports, bool? secure, int? version) |
81 | 82 | {
|
82 | 83 | // mandatory checks or defaults
|
83 | 84 | if (name is null)
|
84 |
| - throw new ArgumentNullException ("name"); |
| 85 | + throw new ArgumentNullException (nameof (name)); |
85 | 86 | if (value is null)
|
86 |
| - throw new ArgumentNullException ("value"); |
| 87 | + throw new ArgumentNullException (nameof (value)); |
87 | 88 | if (String.IsNullOrEmpty (path))
|
88 | 89 | path = "/"; // default in .net
|
89 | 90 | if (String.IsNullOrEmpty (domain))
|
@@ -115,10 +116,16 @@ void CreateCookie (string name, string value, string path, string domain, string
|
115 | 116 | if (version.HasValue)
|
116 | 117 | properties.Add (NSHttpCookie.KeyVersion, new NSString (version.Value.ToString ()));
|
117 | 118 |
|
| 119 | +#if NET10_0_OR_GREATER |
| 120 | + var throwOnInitFailure = true; |
| 121 | +#else |
| 122 | + var throwOnInitFailure = false; |
| 123 | +#endif |
| 124 | + |
118 | 125 | if (IsDirectBinding) {
|
119 |
| - Handle = Messaging.IntPtr_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle ("initWithProperties:"), properties.Handle); |
| 126 | + InitializeHandle (Messaging.IntPtr_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle ("initWithProperties:"), properties.Handle), "initWithProperties:", throwOnInitFailure); |
120 | 127 | } else {
|
121 |
| - Handle = Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithProperties:"), properties.Handle); |
| 128 | + InitializeHandle (Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithProperties:"), properties.Handle), "initWithProperties:", throwOnInitFailure); |
122 | 129 | }
|
123 | 130 | }
|
124 | 131 | }
|
|
0 commit comments