| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Security |
| | | 10 | | { |
| | | 11 | | public class ScopedMessagePartSpecification |
| | | 12 | | { |
| | | 13 | | private readonly Dictionary<string, MessagePartSpecification> _actionParts; |
| | | 14 | | private Dictionary<string, MessagePartSpecification> _readOnlyNormalizedActionParts; |
| | | 15 | | |
| | 3140 | 16 | | public ScopedMessagePartSpecification() |
| | | 17 | | { |
| | 3140 | 18 | | ChannelParts = new MessagePartSpecification(); |
| | 3140 | 19 | | _actionParts = new Dictionary<string, MessagePartSpecification>(); |
| | 3140 | 20 | | } |
| | | 21 | | |
| | | 22 | | public ICollection<string> Actions |
| | | 23 | | { |
| | | 24 | | get |
| | | 25 | | { |
| | 296 | 26 | | return _actionParts.Keys; |
| | | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | 5570 | 30 | | public MessagePartSpecification ChannelParts { get; } |
| | | 31 | | |
| | 3878 | 32 | | public bool IsReadOnly { get; private set; } |
| | | 33 | | |
| | | 34 | | public ScopedMessagePartSpecification(ScopedMessagePartSpecification other) |
| | 2156 | 35 | | : this() |
| | | 36 | | { |
| | 2156 | 37 | | if (other == null) |
| | | 38 | | { |
| | 0 | 39 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(other))); |
| | | 40 | | } |
| | | 41 | | |
| | 2156 | 42 | | ChannelParts.Union(other.ChannelParts); |
| | 2156 | 43 | | if (other._actionParts != null) |
| | | 44 | | { |
| | 4312 | 45 | | foreach (string action in other._actionParts.Keys) |
| | | 46 | | { |
| | 0 | 47 | | MessagePartSpecification p = new MessagePartSpecification(); |
| | 0 | 48 | | p.Union(other._actionParts[action]); |
| | 0 | 49 | | _actionParts[action] = p; |
| | | 50 | | } |
| | | 51 | | } |
| | 2156 | 52 | | } |
| | | 53 | | |
| | | 54 | | internal ScopedMessagePartSpecification(ScopedMessagePartSpecification other, bool newIncludeBody) |
| | 0 | 55 | | : this(other) |
| | | 56 | | { |
| | 0 | 57 | | ChannelParts.IsBodyIncluded = newIncludeBody; |
| | 0 | 58 | | foreach (string action in _actionParts.Keys) |
| | | 59 | | { |
| | 0 | 60 | | _actionParts[action].IsBodyIncluded = newIncludeBody; |
| | | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public void AddParts(MessagePartSpecification parts) |
| | | 65 | | { |
| | 830 | 66 | | if (parts == null) |
| | | 67 | | { |
| | 0 | 68 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(parts))); |
| | | 69 | | } |
| | | 70 | | |
| | 830 | 71 | | ThrowIfReadOnly(); |
| | | 72 | | |
| | 830 | 73 | | ChannelParts.Union(parts); |
| | 830 | 74 | | } |
| | | 75 | | |
| | | 76 | | public void AddParts(MessagePartSpecification parts, string action) |
| | | 77 | | { |
| | 2032 | 78 | | if (action == null) |
| | | 79 | | { |
| | 0 | 80 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(action))); |
| | | 81 | | } |
| | | 82 | | |
| | 2032 | 83 | | if (parts == null) |
| | | 84 | | { |
| | 0 | 85 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(parts))); |
| | | 86 | | } |
| | | 87 | | |
| | 2032 | 88 | | ThrowIfReadOnly(); |
| | | 89 | | |
| | 2032 | 90 | | if (!_actionParts.ContainsKey(action)) |
| | | 91 | | { |
| | 1892 | 92 | | _actionParts[action] = new MessagePartSpecification(); |
| | | 93 | | } |
| | | 94 | | |
| | 2032 | 95 | | _actionParts[action].Union(parts); |
| | 2032 | 96 | | } |
| | | 97 | | |
| | | 98 | | internal void AddParts(MessagePartSpecification parts, XmlDictionaryString action) |
| | | 99 | | { |
| | 0 | 100 | | if (action == null) |
| | | 101 | | { |
| | 0 | 102 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(action))); |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | AddParts(parts, action.Value); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | internal bool IsEmpty() |
| | | 109 | | { |
| | | 110 | | bool result; |
| | 0 | 111 | | if (!ChannelParts.IsEmpty()) |
| | | 112 | | { |
| | 0 | 113 | | result = false; |
| | | 114 | | } |
| | | 115 | | else |
| | | 116 | | { |
| | 0 | 117 | | result = true; |
| | 0 | 118 | | foreach (string action in Actions) |
| | | 119 | | { |
| | 0 | 120 | | if (TryGetParts(action, true, out MessagePartSpecification parts)) |
| | | 121 | | { |
| | 0 | 122 | | if (!parts.IsEmpty()) |
| | | 123 | | { |
| | 0 | 124 | | result = false; |
| | 0 | 125 | | break; |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | return result; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public bool TryGetParts(string action, bool excludeChannelScope, out MessagePartSpecification parts) |
| | | 135 | | { |
| | 1016 | 136 | | if (action == null) |
| | | 137 | | { |
| | 0 | 138 | | action = MessageHeaders.WildcardAction; |
| | | 139 | | } |
| | | 140 | | |
| | 1016 | 141 | | parts = null; |
| | | 142 | | |
| | 1016 | 143 | | if (IsReadOnly) |
| | | 144 | | { |
| | 0 | 145 | | if (_readOnlyNormalizedActionParts.ContainsKey(action)) |
| | | 146 | | { |
| | 0 | 147 | | if (excludeChannelScope) |
| | | 148 | | { |
| | 0 | 149 | | parts = _actionParts[action]; |
| | | 150 | | } |
| | | 151 | | else |
| | | 152 | | { |
| | 0 | 153 | | parts = _readOnlyNormalizedActionParts[action]; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | } |
| | 1016 | 157 | | else if (_actionParts.ContainsKey(action)) |
| | | 158 | | { |
| | 1016 | 159 | | MessagePartSpecification p = new MessagePartSpecification(); |
| | 1016 | 160 | | p.Union(_actionParts[action]); |
| | 1016 | 161 | | if (!excludeChannelScope) |
| | | 162 | | { |
| | 72 | 163 | | p.Union(ChannelParts); |
| | | 164 | | } |
| | | 165 | | |
| | 1016 | 166 | | parts = p; |
| | | 167 | | } |
| | | 168 | | |
| | 1016 | 169 | | return parts != null; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | internal void CopyTo(ScopedMessagePartSpecification target) |
| | | 173 | | { |
| | 12 | 174 | | if (target == null) |
| | | 175 | | { |
| | 0 | 176 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(target)); |
| | | 177 | | } |
| | 12 | 178 | | target.ChannelParts.IsBodyIncluded = ChannelParts.IsBodyIncluded; |
| | 24 | 179 | | foreach (XmlQualifiedName headerType in ChannelParts.HeaderTypes) |
| | | 180 | | { |
| | 0 | 181 | | if (!target.ChannelParts.IsHeaderIncluded(headerType.Name, headerType.Namespace)) |
| | | 182 | | { |
| | 0 | 183 | | target.ChannelParts.HeaderTypes.Add(headerType); |
| | | 184 | | } |
| | | 185 | | } |
| | 168 | 186 | | foreach (string action in _actionParts.Keys) |
| | | 187 | | { |
| | 72 | 188 | | target.AddParts(_actionParts[action], action); |
| | | 189 | | } |
| | 12 | 190 | | } |
| | | 191 | | |
| | | 192 | | public bool TryGetParts(string action, out MessagePartSpecification parts) |
| | | 193 | | { |
| | 72 | 194 | | return TryGetParts(action, false, out parts); |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | public void MakeReadOnly() |
| | | 198 | | { |
| | 0 | 199 | | if (!IsReadOnly) |
| | | 200 | | { |
| | 0 | 201 | | _readOnlyNormalizedActionParts = new Dictionary<string, MessagePartSpecification>(); |
| | 0 | 202 | | foreach (string action in _actionParts.Keys) |
| | | 203 | | { |
| | 0 | 204 | | MessagePartSpecification p = new MessagePartSpecification(); |
| | 0 | 205 | | p.Union(_actionParts[action]); |
| | 0 | 206 | | p.Union(ChannelParts); |
| | 0 | 207 | | p.MakeReadOnly(); |
| | 0 | 208 | | _readOnlyNormalizedActionParts[action] = p; |
| | | 209 | | } |
| | 0 | 210 | | IsReadOnly = true; |
| | | 211 | | } |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | private void ThrowIfReadOnly() |
| | | 215 | | { |
| | 2862 | 216 | | if (IsReadOnly) |
| | | 217 | | { |
| | 0 | 218 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 219 | | } |
| | 2862 | 220 | | } |
| | | 221 | | } |
| | | 222 | | } |