|
| 1 | +// |
| 2 | +// DomainList.cs |
| 3 | +// |
| 4 | +// Author: Jeffrey Stedfast <jestedfa@microsoft.com> |
| 5 | +// |
| 6 | +// Copyright (c) 2013-2025 .NET Foundation and Contributors |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in |
| 16 | +// all copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +// THE SOFTWARE. |
| 25 | +// |
| 26 | + |
| 27 | +using System; |
| 28 | +using System.Text; |
| 29 | +using System.Collections; |
| 30 | +using System.Globalization; |
| 31 | +using System.Collections.Generic; |
| 32 | + |
| 33 | +using MimeKit.Utils; |
| 34 | + |
| 35 | +namespace MimeKit { |
| 36 | + /// <summary> |
| 37 | + /// A domain list. |
| 38 | + /// </summary> |
| 39 | + /// <remarks> |
| 40 | + /// Represents a list of domains, such as those that an email was routed through. |
| 41 | + /// </remarks> |
| 42 | + internal class DomainList : IEnumerable<string> |
| 43 | + { |
| 44 | + readonly static byte[] DomainSentinels = new [] { (byte) ',', (byte) ':' }; |
| 45 | + IList<string> domains; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Initialize a new instance of the <see cref="DomainList"/> class. |
| 49 | + /// </summary> |
| 50 | + /// <remarks> |
| 51 | + /// Creates a new <see cref="DomainList"/> based on the domains provided. |
| 52 | + /// </remarks> |
| 53 | + /// <param name="domains">A domain list.</param> |
| 54 | + /// <exception cref="System.ArgumentNullException"> |
| 55 | + /// <paramref name="domains"/> is <see langword="null"/>. |
| 56 | + /// </exception> |
| 57 | + public DomainList (IEnumerable<string> domains) |
| 58 | + { |
| 59 | + if (domains is null) |
| 60 | + throw new ArgumentNullException (nameof (domains)); |
| 61 | + |
| 62 | + this.domains = new List<string> (domains); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Initialize a new instance of the <see cref="DomainList"/> class. |
| 67 | + /// </summary> |
| 68 | + /// <remarks> |
| 69 | + /// Creates a new <see cref="DomainList"/>. |
| 70 | + /// </remarks> |
| 71 | + public DomainList () |
| 72 | + { |
| 73 | + domains = Array.Empty<string> (); |
| 74 | + } |
| 75 | + |
| 76 | + #region IEnumerable implementation |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Get an enumerator for the list of domains. |
| 80 | + /// </summary> |
| 81 | + /// <remarks> |
| 82 | + /// Gets an enumerator for the list of domains. |
| 83 | + /// </remarks> |
| 84 | + /// <returns>The enumerator.</returns> |
| 85 | + public IEnumerator<string> GetEnumerator () |
| 86 | + { |
| 87 | + return domains.GetEnumerator (); |
| 88 | + } |
| 89 | + |
| 90 | + #endregion |
| 91 | + |
| 92 | + #region IEnumerable implementation |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Get an enumerator for the list of domains. |
| 96 | + /// </summary> |
| 97 | + /// <remarks> |
| 98 | + /// Gets an enumerator for the list of domains. |
| 99 | + /// </remarks> |
| 100 | + /// <returns>The enumerator.</returns> |
| 101 | + IEnumerator IEnumerable.GetEnumerator () |
| 102 | + { |
| 103 | + return domains.GetEnumerator (); |
| 104 | + } |
| 105 | + |
| 106 | + #endregion |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Try to parse a list of domains. |
| 110 | + /// </summary> |
| 111 | + /// <remarks> |
| 112 | + /// Attempts to parse a <see cref="DomainList"/> from the text buffer starting at the |
| 113 | + /// specified index. The index will only be updated if a <see cref="DomainList"/> was |
| 114 | + /// successfully parsed. |
| 115 | + /// </remarks> |
| 116 | + /// <returns><see langword="true" /> if a <see cref="DomainList"/> was successfully parsed; |
| 117 | + /// otherwise, <see langword="false" />.</returns> |
| 118 | + /// <param name="buffer">The buffer to parse.</param> |
| 119 | + /// <param name="index">The index to start parsing.</param> |
| 120 | + /// <param name="endIndex">An index of the end of the input.</param> |
| 121 | + /// <param name="throwOnError">A flag indicating whether an |
| 122 | + /// exception should be thrown on error.</param> |
| 123 | + /// <param name="route">The parsed DomainList.</param> |
| 124 | + internal static bool TryParse (byte[] buffer, ref int index, int endIndex, bool throwOnError, out DomainList route) |
| 125 | + { |
| 126 | + var domains = new List<string> (); |
| 127 | + int startIndex = index; |
| 128 | + |
| 129 | + route = null; |
| 130 | + |
| 131 | + do { |
| 132 | + // skip over the '@' |
| 133 | + index++; |
| 134 | + |
| 135 | + if (index >= endIndex) { |
| 136 | + if (throwOnError) |
| 137 | + throw new ParseException (string.Format (CultureInfo.InvariantCulture, "Incomplete domain-list at offset: {0}", startIndex), startIndex, index); |
| 138 | + |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + if (!ParseUtils.TryParseDomain (buffer, ref index, endIndex, DomainSentinels, throwOnError, out var domain)) |
| 143 | + return false; |
| 144 | + |
| 145 | + domains.Add (domain); |
| 146 | + |
| 147 | + // Note: obs-domain-list allows for null domains between commas |
| 148 | + do { |
| 149 | + if (!ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, throwOnError)) |
| 150 | + return false; |
| 151 | + |
| 152 | + if (index >= endIndex || buffer[index] != (byte) ',') |
| 153 | + break; |
| 154 | + |
| 155 | + index++; |
| 156 | + } while (true); |
| 157 | + |
| 158 | + if (!ParseUtils.SkipCommentsAndWhiteSpace (buffer, ref index, endIndex, throwOnError)) |
| 159 | + return false; |
| 160 | + } while (index < buffer.Length && buffer[index] == (byte) '@'); |
| 161 | + |
| 162 | + route = new DomainList (domains); |
| 163 | + |
| 164 | + return true; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments