コード例 #1
0
        public static string ResolveWithHash(string contentUrl)
        {
            if (contentUrl.IsNullOrEmpty())
            {
                throw new ArgumentNullException("contentUrl");
            }

            if (contentUrl[0] != '/' &&
                (contentUrl[0] != '~' || contentUrl.Length < 2 || contentUrl[1] != '/'))
            {
                return(contentUrl);
            }

            string cdnMatch = contentUrl;

            if (contentUrl.IndexOf(".axd/", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                if (!cdnEnabled)
                {
                    return(contentUrl);
                }

                cdnMatch = contentUrl.Split('?')[0];
            }
            else
            {
                var path = contentUrl;
                path = HostingEnvironment.MapPath(path);

                object hash;
                hash = hashByContentPath[path];
                if (hash == null)
                {
                    if (File.Exists(path))
                    {
                        hash = GetFileSHA1(path);
                    }
                    else
                    {
                        hash = DateTime.Now.ToString("yyyymmddhh");
                    }

                    Hashtable.Synchronized(hashByContentPath)[path] = hash;
                }

                contentUrl = VirtualPathUtility.ToAbsolute(contentUrl) + "?v=" + (string)hash;
                if (!cdnEnabled)
                {
                    return(contentUrl);
                }
            }

            if (cdnMatch[0] == '/')
            {
                var root = VirtualPathUtility.ToAbsolute("~/");
                if (cdnMatch.StartsWith(root, StringComparison.OrdinalIgnoreCase))
                {
                    cdnMatch = cdnMatch.Substring(root.Length);
                }
            }
            else
            {
                cdnMatch = cdnMatch.Substring(2);
            }

            if (!cdnFilter.IsMatch(cdnMatch.Replace('/', Path.DirectorySeparatorChar)))
            {
                return(contentUrl);
            }

#if ASPNETCORE
            var  contextAccessor    = Dependency.TryResolve <Microsoft.AspNetCore.Http.IHttpContextAccessor>();
            bool isSecureConnection = contextAccessor != null && contextAccessor.HttpContext != null &&
                                      contextAccessor.HttpContext.Request.IsHttps;
#else
            bool isSecureConnection = HttpContext.Current != null &&
                                      HttpContext.Current.Request.IsSecureConnection;
#endif
            string cdnRoot = isSecureConnection ? cdnHttps : cdnHttp;
            contentUrl = VirtualPathUtility.ToAbsolute(contentUrl);
            return(UriHelper.Combine(cdnRoot, contentUrl));
        }
コード例 #2
0
        public void Glob_Filter_Works_Properly(string path, bool result)
        {
            var filter = new GlobFilter(filters);

            Assert.Equal(result, filter.IsMatch(path));
        }