1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml;
using WsdlNS = System.Web.Services.Description;
using System.Globalization;
//
// PolicyReader is a complex nested class in the MetadataImporter
//
public abstract partial class MetadataImporter
{
internal MetadataImporterQuotas Quotas;
PolicyReader policyNormalizer = null;
internal delegate void PolicyWarningHandler(XmlElement contextAssertion, string warningMessage);
// Consider, [....], make this public?
internal event PolicyWarningHandler PolicyWarningOccured;
internal IEnumerable<IEnumerable<XmlElement>> NormalizePolicy(IEnumerable<XmlElement> policyAssertions)
{
if (this.policyNormalizer == null)
{
this.policyNormalizer = new PolicyReader(this);
}
return this.policyNormalizer.NormalizePolicy(policyAssertions);
}
//DevNote: The error handling goal for this class is to NEVER throw an exception.
// * Any Ignored Policy should generate a warning
// * All policy parsing errors should be logged as warnings in the WSDLImporter.Errors collection.
sealed class PolicyReader
{
int nodesRead = 0;
readonly MetadataImporter metadataImporter;
internal PolicyReader(MetadataImporter metadataImporter)
{
this.metadataImporter = metadataImporter;
}
static IEnumerable<XmlElement> Empty = new PolicyHelper.EmptyEnumerable<XmlElement>();
static IEnumerable<IEnumerable<XmlElement>> EmptyEmpty = new PolicyHelper.SingleEnumerable<IEnumerable<XmlElement>>(new PolicyHelper.EmptyEnumerable<XmlElement>());
//
// the core policy reading logic
// each step returns a list of lists -- an "and of ors":
// each inner list is a policy alternative: it contains the set of assertions that comprise the alternative
// the outer list represents the choice between alternatives
//
IEnumerable<IEnumerable<XmlElement>> ReadNode(XmlNode node, XmlElement contextAssertion, YieldLimiter yieldLimiter)
{
if (nodesRead >= this.metadataImporter.Quotas.MaxPolicyNodes)
{
if (nodesRead == this.metadataImporter.Quotas.MaxPolicyNodes)
{
// add wirning once
string warningMsg = SR.GetString(SR.ExceededMaxPolicyComplexity, node.Name, PolicyHelper.GetFragmentIdentifier((XmlElement)node));
metadataImporter.PolicyWarningOccured.Invoke(contextAssertion, warningMsg);
nodesRead++;
}
return EmptyEmpty;
}
nodesRead++;
IEnumerable<IEnumerable<XmlElement>> nodes = EmptyEmpty;
switch (PolicyHelper.GetNodeType(node))
{
case PolicyHelper.NodeType.Policy:
case PolicyHelper.NodeType.All:
nodes = ReadNode_PolicyOrAll((XmlElement)node, contextAssertion, yieldLimiter);
break;
case PolicyHelper.NodeType.ExactlyOne:
nodes = ReadNode_ExactlyOne((XmlElement)node, contextAssertion, yieldLimiter);
break;
case PolicyHelper.NodeType.Assertion:
nodes = ReadNode_Assertion((XmlElement)node, yieldLimiter);
break;
case PolicyHelper.NodeType.PolicyReference:
nodes = ReadNode_PolicyReference((XmlElement)node, contextAssertion, yieldLimiter);
break;
case PolicyHelper.NodeType.UnrecognizedWSPolicy:
string warningMsg = SR.GetString(SR.UnrecognizedPolicyElementInNamespace, node.Name, node.NamespaceURI);
metadataImporter.PolicyWarningOccured.Invoke(contextAssertion, warningMsg);
break;
//consider [....], add more error handling here. default?
}
return nodes;
}
IEnumerable<IEnumerable<XmlElement>> ReadNode_PolicyReference(XmlElement element, XmlElement contextAssertion, YieldLimiter yieldLimiter)
{
string idRef = element.GetAttribute(MetadataStrings.WSPolicy.Attributes.URI);
if (idRef == null)
{
string warningMsg = SR.GetString(SR.PolicyReferenceMissingURI, MetadataStrings.WSPolicy.Attributes.URI);
metadataImporter.PolicyWarningOccured.Invoke(contextAssertion, warningMsg);
return EmptyEmpty;
}
else if (idRef == string.Empty)
{
string warningMsg = SR.GetString(SR.PolicyReferenceInvalidId);
metadataImporter.PolicyWarningOccured.Invoke(contextAssertion, warningMsg);
return EmptyEmpty;
}
XmlElement policy = metadataImporter.ResolvePolicyReference(idRef, contextAssertion);
if (policy == null)
{
string warningMsg = SR.GetString(SR.UnableToFindPolicyWithId, idRef);
metadataImporter.PolicyWarningOccured.Invoke(contextAssertion, warningMsg);
return EmptyEmpty;
}
//
// Since we looked up a reference, the context assertion changes.
//
return ReadNode_PolicyOrAll(policy, policy, yieldLimiter);
}
IEnumerable<IEnumerable<XmlElement>> ReadNode_Assertion(XmlElement element, YieldLimiter yieldLimiter)
{
if (yieldLimiter.IncrementAndLogIfExceededLimit())
yield return Empty;
else
yield return new PolicyHelper.SingleEnumerable<XmlElement>(element);
}
IEnumerable<IEnumerable<XmlElement>> ReadNode_ExactlyOne(XmlElement element, XmlElement contextAssertion, YieldLimiter yieldLimiter)
{
foreach (XmlNode child in element.ChildNodes)
{
if (child.NodeType == XmlNodeType.Element)
{
foreach (IEnumerable<XmlElement> alternative in ReadNode(child, contextAssertion, yieldLimiter))
{
if (yieldLimiter.IncrementAndLogIfExceededLimit())
{
yield break;
}
else
{
yield return alternative;
}
}
}
}
}
IEnumerable<IEnumerable<XmlElement>> ReadNode_PolicyOrAll(XmlElement element, XmlElement contextAssertion, YieldLimiter yieldLimiter)
{
IEnumerable<IEnumerable<XmlElement>> target = EmptyEmpty;
foreach (XmlNode child in element.ChildNodes)
{
if (child.NodeType == XmlNodeType.Element)
{
IEnumerable<IEnumerable<XmlElement>> childPolicy = ReadNode(child, contextAssertion, yieldLimiter);
target = PolicyHelper.CrossProduct<XmlElement>(target, childPolicy, yieldLimiter);
}
}
return target;
}
internal IEnumerable<IEnumerable<XmlElement>> NormalizePolicy(IEnumerable<XmlElement> policyAssertions)
{
IEnumerable<IEnumerable<XmlElement>> target = EmptyEmpty;
YieldLimiter yieldLimiter = new YieldLimiter(this.metadataImporter.Quotas.MaxYields, this.metadataImporter);
foreach (XmlElement child in policyAssertions)
{
IEnumerable<IEnumerable<XmlElement>> childPolicy = ReadNode(child, child, yieldLimiter);
target = PolicyHelper.CrossProduct<XmlElement>(target, childPolicy, yieldLimiter);
}
return target;
}
}
internal class YieldLimiter
{
int maxYields;
int yieldsHit;
readonly MetadataImporter metadataImporter;
internal YieldLimiter(int maxYields, MetadataImporter metadataImporter)
{
this.metadataImporter = metadataImporter;
this.yieldsHit = 0;
this.maxYields = maxYields;
}
internal bool IncrementAndLogIfExceededLimit()
{
if (++yieldsHit > maxYields)
{
string warningMsg = SR.GetString(SR.ExceededMaxPolicySize);
metadataImporter.PolicyWarningOccured.Invoke(null, warningMsg);
return true;
}
else
{
return false;
}
}
}
internal static class PolicyHelper
{
internal static string GetFragmentIdentifier(XmlElement element)
{
string id = element.GetAttribute(MetadataStrings.Wsu.Attributes.Id, MetadataStrings.Wsu.NamespaceUri);
if (id == null)
{
id = element.GetAttribute(MetadataStrings.Xml.Attributes.Id, MetadataStrings.Xml.NamespaceUri);
}
if (string.IsNullOrEmpty(id))
return string.Empty;
else
return string.Format(CultureInfo.InvariantCulture, "#{0}", id);
}
internal static bool IsPolicyURIs(XmlAttribute attribute)
{
return ((attribute.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri
|| attribute.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri15)
&& attribute.LocalName == MetadataStrings.WSPolicy.Attributes.PolicyURIs);
}
internal static NodeType GetNodeType(XmlNode node)
{
XmlElement currentElement = node as XmlElement;
if (currentElement == null)
return PolicyHelper.NodeType.NonElement;
if (currentElement.NamespaceURI != MetadataStrings.WSPolicy.NamespaceUri
&& currentElement.NamespaceURI != MetadataStrings.WSPolicy.NamespaceUri15)
return NodeType.Assertion;
else if (currentElement.LocalName == MetadataStrings.WSPolicy.Elements.Policy)
return NodeType.Policy;
else if (currentElement.LocalName == MetadataStrings.WSPolicy.Elements.All)
return NodeType.All;
else if (currentElement.LocalName == MetadataStrings.WSPolicy.Elements.ExactlyOne)
return NodeType.ExactlyOne;
else if (currentElement.LocalName == MetadataStrings.WSPolicy.Elements.PolicyReference)
return NodeType.PolicyReference;
else
return PolicyHelper.NodeType.UnrecognizedWSPolicy;
}
//
// some helpers for dealing with ands of ors
//
internal static IEnumerable<IEnumerable<T>> CrossProduct<T>(IEnumerable<IEnumerable<T>> xs, IEnumerable<IEnumerable<T>> ys, YieldLimiter yieldLimiter)
{
foreach (IEnumerable<T> x in AtLeastOne<T>(xs, yieldLimiter))
{
foreach (IEnumerable<T> y in AtLeastOne<T>(ys, yieldLimiter))
{
if (yieldLimiter.IncrementAndLogIfExceededLimit())
{
yield break;
}
else
{
yield return Merge<T>(x, y, yieldLimiter);
}
}
}
}
static IEnumerable<IEnumerable<T>> AtLeastOne<T>(IEnumerable<IEnumerable<T>> xs, YieldLimiter yieldLimiter)
{
bool gotOne = false;
foreach (IEnumerable<T> x in xs)
{
gotOne = true;
if (yieldLimiter.IncrementAndLogIfExceededLimit())
{
yield break;
}
else
{
yield return x;
}
}
if (!gotOne)
{
if (yieldLimiter.IncrementAndLogIfExceededLimit())
{
yield break;
}
else
{
yield return new EmptyEnumerable<T>();
}
}
}
static IEnumerable<T> Merge<T>(IEnumerable<T> e1, IEnumerable<T> e2, YieldLimiter yieldLimiter)
{
foreach (T t1 in e1)
{
if (yieldLimiter.IncrementAndLogIfExceededLimit())
{
yield break;
}
else
{
yield return t1;
}
}
foreach (T t2 in e2)
{
if (yieldLimiter.IncrementAndLogIfExceededLimit())
{
yield break;
}
else
{
yield return t2;
}
}
}
//
// some helper enumerators
//
internal class EmptyEnumerable<T> : IEnumerable<T>, IEnumerator<T>
{
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return this;
}
object IEnumerator.Current
{
get { return this.Current; }
}
public T Current
{
get
{
#pragma warning suppress 56503 // [....], IEnumerator guidelines, Current throws exception before calling MoveNext
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.NoValue0)));
}
}
public bool MoveNext()
{
return false;
}
public void Dispose()
{
}
void IEnumerator.Reset()
{
}
}
internal class SingleEnumerable<T> : IEnumerable<T>
{
T value;
internal SingleEnumerable(T value)
{
this.value = value;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
yield return this.value;
}
}
//
// the NodeType enum
//
internal enum NodeType
{
NonElement,
Policy,
All,
ExactlyOne,
Assertion,
PolicyReference,
UnrecognizedWSPolicy,
}
}
}
}
|