Ejemplo n.º 1
0
        /// <summary>
        /// Generates XML representing the given CORS properties.
        /// </summary>
        /// <param name="cors">The CORS properties.</param>
        /// <returns>An XML logging element.</returns>
        private static XElement GenerateCorsXml(CorsProperties cors)
        {
            CommonUtility.AssertNotNull("cors", cors);

            IList <CorsRule> corsRules = cors.CorsRules;

            XElement ret = new XElement(CorsName);

            foreach (CorsRule rule in corsRules)
            {
                if (rule.AllowedOrigins.Count < 1 || rule.AllowedMethods == CorsHttpMethods.None || rule.MaxAgeInSeconds < 0)
                {
                    throw new InvalidOperationException(SR.InvalidCorsRule);
                }

                XElement ruleElement = new XElement(
                    CorsRuleName,
                    new XElement(AllowedOriginsName, string.Join(",", rule.AllowedOrigins.ToArray())),
                    new XElement(AllowedMethodsName, rule.AllowedMethods.ToString().Replace(" ", string.Empty).ToUpperInvariant()),
                    new XElement(ExposedHeadersName, string.Join(",", rule.ExposedHeaders.ToArray())),
                    new XElement(AllowedHeadersName, string.Join(",", rule.AllowedHeaders.ToArray())),
                    new XElement(MaxAgeInSecondsName, rule.MaxAgeInSeconds));

                ret.Add(ruleElement);
            }

            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructs a <c>CorsProperties</c> object from an XML element.
        /// </summary>
        /// <param name="element">The XML element.</param>
        /// <returns>A <c>CorsProperties</c> object containing the properties in the element.</returns>
        internal static CorsProperties ReadCorsPropertiesFromXml(XElement element)
        {
            if (element == null)
            {
                return(null);
            }

            CorsProperties ret = new CorsProperties();

            IEnumerable <XElement> corsRules = element.Descendants(CorsRuleName);

            ret.CorsRules =
                corsRules.Select(
                    rule =>
                    new CorsRule
            {
                AllowedOrigins = rule.Element(AllowedOriginsName).Value.Split(',').ToList(),
                AllowedMethods =
                    (CorsHttpMethods)
                    Enum.Parse(
                        typeof(CorsHttpMethods), rule.Element(AllowedMethodsName).Value, true),
                AllowedHeaders =
                    rule.Element(AllowedHeadersName)
                    .Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .ToList(),
                ExposedHeaders =
                    rule.Element(ExposedHeadersName)
                    .Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .ToList(),
                MaxAgeInSeconds =
                    int.Parse(
                        rule.Element(MaxAgeInSecondsName).Value, CultureInfo.InvariantCulture)
            }).ToList();

            return(ret);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the ServiceProperties class.
 /// </summary>
 public ServiceProperties(LoggingProperties logging = null, MetricsProperties hourMetrics = null, MetricsProperties minuteMetrics = null, CorsProperties cors = null)
 {
     this.Logging       = logging;
     this.HourMetrics   = hourMetrics;
     this.MinuteMetrics = minuteMetrics;
     this.Cors          = cors;
 }