|
| 1 | +namespace AngleSharp.Io.Cookie |
| 2 | +{ |
| 3 | + using AngleSharp.Text; |
| 4 | + using System; |
| 5 | + using System.Collections.Generic; |
| 6 | + using System.Linq; |
| 7 | + using static Helpers; |
| 8 | + using static NetscapeCookieSerializer; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Represents an advanced cookie provider that allows |
| 12 | + /// persistence, snapshots, and more. |
| 13 | + /// Stores the cookies in the Netscape compatible file |
| 14 | + /// format. |
| 15 | + /// </summary> |
| 16 | + public class AdvancedCookieProvider : ICookieProvider |
| 17 | + { |
| 18 | + private readonly IFileHandler _handler; |
| 19 | + private readonly Boolean _forceParse; |
| 20 | + private readonly Boolean _httpOnlyExtension; |
| 21 | + private readonly List<WebCookie> _cookies; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Creates a new cookie provider with the given handler and options. |
| 25 | + /// </summary> |
| 26 | + /// <param name="handler">The handler responsible for file system interaction.</param> |
| 27 | + /// <param name="options">The options to use for the cookie provider.</param> |
| 28 | + public AdvancedCookieProvider(IFileHandler handler, AdvancedCookieProviderOptions options = default) |
| 29 | + { |
| 30 | + _handler = handler; |
| 31 | + _forceParse = options.IsForceParse; |
| 32 | + _httpOnlyExtension = options.IsHttpOnlyExtension; |
| 33 | + _cookies = ReadCookies(); |
| 34 | + } |
| 35 | + |
| 36 | + String ICookieProvider.GetCookie(Url url) |
| 37 | + { |
| 38 | + throw new NotImplementedException(); |
| 39 | + } |
| 40 | + |
| 41 | + void ICookieProvider.SetCookie(Url url, String value) |
| 42 | + { |
| 43 | + throw new NotImplementedException(); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets the available cookies. |
| 48 | + /// </summary> |
| 49 | + public IEnumerable<WebCookie> Cookies => _cookies.AsEnumerable(); |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the cookie with the given key. |
| 53 | + /// </summary> |
| 54 | + /// <param name="domain">The domain of the cookie.</param> |
| 55 | + /// <param name="path">The path of the cookie.</param> |
| 56 | + /// <param name="key">The key of the cookie.</param> |
| 57 | + /// <returns>If matching cookie, if any.</returns> |
| 58 | + public WebCookie FindCookie(String domain, String path, String key) |
| 59 | + { |
| 60 | + domain = CanonicalDomain(domain); |
| 61 | + return _cookies.FirstOrDefault(cookie => cookie.Domain.Is(domain) && cookie.Path.Is(path) && cookie.Key.Is(key)); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Finds all cookies that match the given domain and optional path. |
| 66 | + /// </summary> |
| 67 | + /// <param name="domain">The domain to look for.</param> |
| 68 | + /// <param name="path">The optional path of the cookie.</param> |
| 69 | + /// <returns>The matching cookies.</returns> |
| 70 | + public IEnumerable<WebCookie> FindCookies(String domain, String path = null) |
| 71 | + { |
| 72 | + domain = CanonicalDomain(domain); |
| 73 | + var enumerable = _cookies.Where(cookie => cookie.Domain.Is(domain)); |
| 74 | + |
| 75 | + if (!String.IsNullOrEmpty(path)) |
| 76 | + { |
| 77 | + enumerable = enumerable |
| 78 | + .Where(cookie => CheckPaths(path, cookie.Path)); |
| 79 | + } |
| 80 | + |
| 81 | + return enumerable; |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Adds a new cookie to the collection. |
| 86 | + /// </summary> |
| 87 | + /// <param name="cookie">The cookie to add.</param> |
| 88 | + public void AddCookie(WebCookie cookie) |
| 89 | + { |
| 90 | + _cookies.Remove(FindCookie(cookie.Domain, cookie.Path, cookie.Key)); |
| 91 | + _cookies.Add(cookie); |
| 92 | + WriteCookies(); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Updates an existing cookie with a new cookie. |
| 97 | + /// </summary> |
| 98 | + /// <param name="oldCookie">The cookie to update.</param> |
| 99 | + /// <param name="newCookie">The updated cookie content.</param> |
| 100 | + public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) |
| 101 | + { |
| 102 | + _cookies.Remove(FindCookie(oldCookie.Domain, oldCookie.Path, oldCookie.Key)); |
| 103 | + AddCookie(newCookie); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Removes a specific cookie matched by its domain, path, and key. |
| 108 | + /// </summary> |
| 109 | + /// <param name="domain">The domain to look for.</param> |
| 110 | + /// <param name="path">The path to look for.</param> |
| 111 | + /// <param name="key">The key of the cookie.</param> |
| 112 | + /// <returns>The removed cookie if any.</returns> |
| 113 | + public WebCookie RemoveCookie(String domain, String path, String key) |
| 114 | + { |
| 115 | + var cookie = FindCookie(domain, path, key); |
| 116 | + |
| 117 | + if (cookie != null) |
| 118 | + { |
| 119 | + _cookies.Remove(cookie); |
| 120 | + WriteCookies(); |
| 121 | + } |
| 122 | + |
| 123 | + return cookie; |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Removes the cookies found for the provided domain and path. |
| 128 | + /// </summary> |
| 129 | + /// <param name="domain">The domain to look for.</param> |
| 130 | + /// <param name="path">The optional path to match.</param> |
| 131 | + /// <returns>The removed cookies.</returns> |
| 132 | + public IEnumerable<WebCookie> RemoveCookies(String domain, String path = null) |
| 133 | + { |
| 134 | + var cookies = FindCookies(domain, path); |
| 135 | + |
| 136 | + if (cookies.Any()) |
| 137 | + { |
| 138 | + _cookies.RemoveAll(cookie => cookies.Contains(cookie)); |
| 139 | + WriteCookies(); |
| 140 | + } |
| 141 | + |
| 142 | + return cookies; |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Removes all currently available cookies. |
| 147 | + /// </summary> |
| 148 | + /// <returns>The removed cookies.</returns> |
| 149 | + public IEnumerable<WebCookie> RemoveAllCookies() |
| 150 | + { |
| 151 | + var cookies = _cookies.ToArray(); |
| 152 | + _cookies.RemoveAll(cookie => true); |
| 153 | + return cookies; |
| 154 | + } |
| 155 | + |
| 156 | + private List<WebCookie> ReadCookies() => Deserialize(_handler.ReadFile(), _forceParse, _httpOnlyExtension); |
| 157 | + |
| 158 | + private void WriteCookies() |
| 159 | + { |
| 160 | + var selection = _cookies.Where(m => m.IsPersistent); |
| 161 | + var content = Serialize(selection, _httpOnlyExtension); |
| 162 | + _handler.WriteFile(content); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments