| | | 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.Collections.ObjectModel; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Security |
| | | 11 | | { |
| | | 12 | | public class MessagePartSpecification |
| | | 13 | | { |
| | | 14 | | private List<XmlQualifiedName> _headerTypes; |
| | | 15 | | private bool _isBodyIncluded; |
| | | 16 | | private static MessagePartSpecification s_noParts; |
| | | 17 | | |
| | | 18 | | public ICollection<XmlQualifiedName> HeaderTypes |
| | | 19 | | { |
| | | 20 | | get |
| | | 21 | | { |
| | 110 | 22 | | if (_headerTypes == null) |
| | | 23 | | { |
| | 93 | 24 | | _headerTypes = new List<XmlQualifiedName>(); |
| | | 25 | | } |
| | | 26 | | |
| | 110 | 27 | | if (IsReadOnly) |
| | | 28 | | { |
| | 18 | 29 | | return new ReadOnlyCollection<XmlQualifiedName>(_headerTypes); |
| | | 30 | | } |
| | | 31 | | else |
| | | 32 | | { |
| | 92 | 33 | | return _headerTypes; |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | internal bool HasHeaders |
| | | 39 | | { |
| | 0 | 40 | | get { return _headerTypes != null && _headerTypes.Count > 0; } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public bool IsBodyIncluded |
| | | 44 | | { |
| | | 45 | | get |
| | | 46 | | { |
| | 6261 | 47 | | return _isBodyIncluded; |
| | | 48 | | } |
| | | 49 | | set |
| | | 50 | | { |
| | 16 | 51 | | if (IsReadOnly) |
| | | 52 | | { |
| | 0 | 53 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 54 | | } |
| | | 55 | | |
| | 16 | 56 | | _isBodyIncluded = value; |
| | 16 | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | 6356 | 60 | | public bool IsReadOnly { get; private set; } |
| | | 61 | | |
| | | 62 | | public static MessagePartSpecification NoParts |
| | | 63 | | { |
| | | 64 | | get |
| | | 65 | | { |
| | 0 | 66 | | if (s_noParts == null) |
| | | 67 | | { |
| | 0 | 68 | | MessagePartSpecification parts = new MessagePartSpecification(); |
| | 0 | 69 | | parts.MakeReadOnly(); |
| | 0 | 70 | | s_noParts = parts; |
| | | 71 | | } |
| | 0 | 72 | | return s_noParts; |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public void Clear() |
| | | 77 | | { |
| | 0 | 78 | | if (IsReadOnly) |
| | | 79 | | { |
| | 0 | 80 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | if (_headerTypes != null) |
| | | 84 | | { |
| | 0 | 85 | | _headerTypes.Clear(); |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | _isBodyIncluded = false; |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | public void Union(MessagePartSpecification specification) |
| | | 92 | | { |
| | 6142 | 93 | | if (IsReadOnly) |
| | | 94 | | { |
| | 0 | 95 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 96 | | } |
| | | 97 | | |
| | 6142 | 98 | | if (specification == null) |
| | | 99 | | { |
| | 0 | 100 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(specification)); |
| | | 101 | | } |
| | | 102 | | |
| | 6142 | 103 | | _isBodyIncluded |= specification.IsBodyIncluded; |
| | | 104 | | |
| | 6142 | 105 | | List<XmlQualifiedName> headerTypes = specification._headerTypes; |
| | 6142 | 106 | | if (headerTypes != null && headerTypes.Count > 0) |
| | | 107 | | { |
| | 72 | 108 | | if (_headerTypes == null) |
| | | 109 | | { |
| | 63 | 110 | | _headerTypes = new List<XmlQualifiedName>(headerTypes.Count); |
| | | 111 | | } |
| | | 112 | | |
| | 1152 | 113 | | for (int i = 0; i < headerTypes.Count; i++) |
| | | 114 | | { |
| | 504 | 115 | | XmlQualifiedName qname = headerTypes[i]; |
| | 504 | 116 | | _headerTypes.Add(qname); |
| | | 117 | | } |
| | | 118 | | } |
| | 6142 | 119 | | } |
| | | 120 | | |
| | | 121 | | public void MakeReadOnly() |
| | | 122 | | { |
| | 44 | 123 | | if (IsReadOnly) |
| | | 124 | | { |
| | 0 | 125 | | return; |
| | | 126 | | } |
| | | 127 | | |
| | 44 | 128 | | if (_headerTypes != null) |
| | | 129 | | { |
| | 35 | 130 | | List<XmlQualifiedName> noDuplicates = new List<XmlQualifiedName>(_headerTypes.Count); |
| | 686 | 131 | | for (int i = 0; i < _headerTypes.Count; i++) |
| | | 132 | | { |
| | 308 | 133 | | XmlQualifiedName qname = _headerTypes[i]; |
| | 308 | 134 | | if (qname != null) |
| | | 135 | | { |
| | 308 | 136 | | bool include = true; |
| | 2464 | 137 | | for (int j = 0; j < noDuplicates.Count; j++) |
| | | 138 | | { |
| | 987 | 139 | | XmlQualifiedName qname1 = noDuplicates[j]; |
| | | 140 | | |
| | 987 | 141 | | if (qname.Name == qname1.Name && qname.Namespace == qname1.Namespace) |
| | | 142 | | { |
| | 63 | 143 | | include = false; |
| | 63 | 144 | | break; |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | 308 | 148 | | if (include) |
| | | 149 | | { |
| | 245 | 150 | | noDuplicates.Add(qname); |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | 35 | 155 | | _headerTypes = noDuplicates; |
| | | 156 | | } |
| | | 157 | | |
| | 44 | 158 | | IsReadOnly = true; |
| | 44 | 159 | | } |
| | | 160 | | |
| | 7083 | 161 | | public MessagePartSpecification() |
| | | 162 | | { |
| | | 163 | | // empty |
| | 7083 | 164 | | } |
| | | 165 | | |
| | 540 | 166 | | public MessagePartSpecification(bool isBodyIncluded) |
| | | 167 | | { |
| | 540 | 168 | | _isBodyIncluded = isBodyIncluded; |
| | 540 | 169 | | } |
| | | 170 | | |
| | | 171 | | public MessagePartSpecification(params XmlQualifiedName[] headerTypes) |
| | 34 | 172 | | : this(false, headerTypes) |
| | | 173 | | { |
| | | 174 | | // empty |
| | 34 | 175 | | } |
| | | 176 | | |
| | 34 | 177 | | public MessagePartSpecification(bool isBodyIncluded, params XmlQualifiedName[] headerTypes) |
| | | 178 | | { |
| | 34 | 179 | | _isBodyIncluded = isBodyIncluded; |
| | 34 | 180 | | if (headerTypes != null && headerTypes.Length > 0) |
| | | 181 | | { |
| | 34 | 182 | | _headerTypes = new List<XmlQualifiedName>(headerTypes.Length); |
| | 448 | 183 | | for (int i = 0; i < headerTypes.Length; i++) |
| | | 184 | | { |
| | 190 | 185 | | _headerTypes.Add(headerTypes[i]); |
| | | 186 | | } |
| | | 187 | | } |
| | 34 | 188 | | } |
| | | 189 | | |
| | | 190 | | internal bool IsHeaderIncluded(MessageHeader header) |
| | | 191 | | { |
| | 0 | 192 | | if (header == null) |
| | | 193 | | { |
| | 0 | 194 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(header)); |
| | | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | return IsHeaderIncluded(header.Name, header.Namespace); |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | internal bool IsHeaderIncluded(string name, string ns) |
| | | 201 | | { |
| | 0 | 202 | | if (name == null) |
| | | 203 | | { |
| | 0 | 204 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name)); |
| | | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | if (ns == null) |
| | | 208 | | { |
| | 0 | 209 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ns)); |
| | | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | if (_headerTypes != null) |
| | | 213 | | { |
| | 0 | 214 | | for (int i = 0; i < _headerTypes.Count; i++) |
| | | 215 | | { |
| | 0 | 216 | | XmlQualifiedName qname = _headerTypes[i]; |
| | | 217 | | // Name is an optional attribute. If not present, compare with only the namespace. |
| | 0 | 218 | | if (string.IsNullOrEmpty(qname.Name)) |
| | | 219 | | { |
| | 0 | 220 | | if (qname.Namespace == ns) |
| | | 221 | | { |
| | 0 | 222 | | return true; |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | else |
| | | 226 | | { |
| | 0 | 227 | | if (qname.Name == name && qname.Namespace == ns) |
| | | 228 | | { |
| | 0 | 229 | | return true; |
| | | 230 | | } |
| | | 231 | | } |
| | | 232 | | } |
| | | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | return false; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | internal bool IsEmpty() |
| | | 239 | | { |
| | 26 | 240 | | if (_headerTypes != null && _headerTypes.Count > 0) |
| | | 241 | | { |
| | 17 | 242 | | return false; |
| | | 243 | | } |
| | | 244 | | |
| | 9 | 245 | | return !IsBodyIncluded; |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | } |