예제 #1
0
        /// <summary>
        /// Maps the site.
        /// </summary>
        /// <param name="siteMapConfigs">The site map configs.</param>
        /// <param name="branch">The branch that is invoked when a request matches a site map config.</param>
        /// <returns>A middleware func.</returns>
        /// <exception cref="System.ArgumentNullException">siteMapConfigs</exception>
        /// <exception cref="System.ArgumentNullException">branch</exception>
        public static MidFunc MapSite(IEnumerable <MapSiteConfig> siteMapConfigs, AppFunc branch)
        {
            siteMapConfigs.MustNotBeNull("siteMapConfigs");
            branch.MustNotBeNull("branch");

            var siteMapsHashSet = new HashSet <MapSiteConfig>(siteMapConfigs);

            return
                (next =>
                 env =>
            {
                var headers = env.Get <IDictionary <string, string[]> >(OwinRequestHeadersKey);
                var host = headers[HostHeaderKey][0];
                var scheme = env.Get <string>(OwinRequestSchemeKey);
                //If the headers have a X-Forwarded-Proto header then the request has been mapped through
                //a load balancer or reverse proxy and the initial scheme is contained in the header value.
                string[] headerValues;
                if (headers.TryGetValue(XForwardedProtoHeaderKey, out headerValues))
                {
                    string headerValue = headerValues[0];
                    if (string.Equals(headerValue, "https", StringComparison.InvariantCultureIgnoreCase))
                    {
                        scheme = "HttpsXForwardedProto";
                    }
                }
                var requestScheme = (RequestScheme)Enum.Parse(typeof(RequestScheme), scheme, true);
                var siteMap = new MapSiteConfig(host, requestScheme);
                return siteMapsHashSet.Contains(siteMap)
                        ? branch(env)
                        : next(env);
            });
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="mapSiteConfig"></param>
        /// <param name="branch"></param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">builder</exception>
        /// <exception cref="System.ArgumentNullException">MapSiteConfig</exception>
        /// <exception cref="System.ArgumentNullException">branch</exception>
        public static BuildFunc MapSite(this BuildFunc builder, MapSiteConfig mapSiteConfig, AppFunc branch)
        {
            builder.MustNotBeNull("builder");
            mapSiteConfig.MustNotBeNull("MapSiteConfig");
            branch.MustNotBeNull("branch");

            return(MapSite(builder, new[] { mapSiteConfig }, branch));
        }
 public MapSiteConfigTests()
 {
     _config1            = new MapSiteConfig("host1");
     _sameAsConfig1      = new MapSiteConfig("host1");
     _differentToConfig1 = new MapSiteConfig("host2");
 }