示例#1
0
        public static bool IsNeedProcess(string absolutePath)
        {
            if (StringUtil.EndsWithIgnoreCase(absolutePath, ".aspx") || StringUtil.EndsWith(absolutePath, '/'))
            {
                return(true);
            }

            UrlFormat urlFormat = AllSettings.Current.FriendlyUrlSettings.UrlFormat;

            switch (urlFormat)
            {
            case UrlFormat.Html:
                return(StringUtil.EndsWithIgnoreCase(absolutePath, ".html"));

            case UrlFormat.Folder:
                string ext = Path.GetExtension(absolutePath);
                return(string.Compare(ext, string.Empty, true) == 0);

            default:
                return(false);
            }
        }
示例#2
0
        public static bool IsUrlInMainDomain(string url)
        {
            //如果有提交ReturnUrl且是本程序内的安全地址,则登陆成功后自动跳转到returnurl
            if (string.IsNullOrEmpty(url))
            {
                return(false);
            }

            //绝对地址,那么肯定是安全地址
            if (url[0] == '/')
            {
                return(true);
            }

            Uri uri;

            try
            {
                uri = new Uri(url);
            }
            catch
            {
                return(false);
            }

            //得到传入url的主域(例如 www.abc.com 将得到 abc.com)
            string mainDomain = GetMainDomain(uri.DnsSafeHost);

            string domain = HttpContext.Current.Request.Url.DnsSafeHost;

            //如果当前请求的域名确实属于传入url的主域,那么return true
            if (string.Compare(domain, mainDomain, true) == 0 || StringUtil.EndsWithIgnoreCase(domain, "." + mainDomain))
            {
                return(true);
            }

            return(false);
        }
示例#3
0
        public static bool CompressStaticContent(HttpContext context)
        {
            if (Globals.UseStaticCompress == false || StringUtil.EndsWithIgnoreCase(context.Request.PhysicalPath, ".fast.aspx") == false)
            {
                return(false);
            }

            SetInstalledKey(context);

            string physicalPath = context.Request.PhysicalPath.Remove(context.Request.PhysicalPath.Length - 10);
            string contentType  = null;

            if (StringUtil.EndsWithIgnoreCase(physicalPath, ".css"))
            {
                contentType = "text/css";
            }

            else if (StringUtil.EndsWithIgnoreCase(physicalPath, ".js"))
            {
                contentType = "application/x-javascript";
            }

            else
            {
                return(false);
            }

            CompressingType compressingType = RequestUtil.GetCompressingType(context);

            context.Response.ClearHeaders();

            if (compressingType == CompressingType.None)
            {
                if (File.Exists(physicalPath))
                {
                    DateTime lastModified = File.GetLastWriteTime(physicalPath);

                    if (context.Request.Headers["If-Modified-Since"] != null)
                    {
                        DateTime ifModifiedSince;

                        if (DateTime.TryParse(context.Request.Headers["If-Modified-Since"].Split(';')[0], out ifModifiedSince))
                        {
                            if (ifModifiedSince > lastModified.AddSeconds(-1))
                            {
                                context.Response.StatusCode        = 304;
                                context.Response.StatusDescription = "Not Modified";
                                context.ApplicationInstance.CompleteRequest();
                                return(true);
                            }
                        }
                    }

                    context.Response.ContentType = contentType;

                    if (lastModified < DateTime.Now)
                    {
                        context.Response.Cache.SetLastModified(lastModified);
                    }

                    context.Response.Cache.SetETag(lastModified.Ticks.ToString());
                    context.Response.BinaryWrite(File.ReadAllBytes(physicalPath));

                    context.ApplicationInstance.CompleteRequest();
                }
            }
            else
            {
                string cacheKey = string.Concat("fast.aspx/", compressingType.ToString(), "/", physicalPath);

                FastAspxCacheData cacheData = null;

                if (CacheUtil.TryGetValue <FastAspxCacheData>(cacheKey, out cacheData) == false)
                {
                    if (File.Exists(physicalPath))
                    {
                        cacheData = new FastAspxCacheData();

                        using (MemoryStream stream = new MemoryStream())
                        {
                            Stream compressStream = null;

                            if (compressingType == CompressingType.GZip)
                            {
                                compressStream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Compress);
                            }
                            else if (compressingType == CompressingType.Deflate)
                            {
                                compressStream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Compress);
                            }

                            byte[] buffer = File.ReadAllBytes(physicalPath);

                            compressStream.Write(buffer, 0, buffer.Length);
                            compressStream.Close();
                            compressStream.Dispose();
                            compressStream = null;

                            cacheData.Data = stream.ToArray();
                        }

                        cacheData.LastModified = File.GetLastWriteTime(physicalPath);

                        CacheUtil.Set <FastAspxCacheData>(cacheKey, cacheData, CacheTime.Long, CacheExpiresType.Sliding, new CacheDependency(physicalPath));
                    }
                }

                if (cacheData != null && cacheData.Data.Length > 0)
                {
                    if (context.Request.Headers["If-Modified-Since"] != null)
                    {
                        DateTime ifModifiedSince;

                        if (DateTime.TryParse(context.Request.Headers["If-Modified-Since"].Split(';')[0], out ifModifiedSince))
                        {
                            if (ifModifiedSince > cacheData.LastModified.AddSeconds(-1))
                            {
                                context.Response.StatusCode        = 304;
                                context.Response.StatusDescription = "Not Modified";
                                context.ApplicationInstance.CompleteRequest();
                                return(true);
                            }
                        }
                    }

                    context.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;

                    context.Response.ContentType = contentType;

                    if (compressingType == CompressingType.GZip)
                    {
                        context.Response.AppendHeader("Content-Encoding", "gzip");
                    }
                    else
                    {
                        context.Response.AppendHeader("Content-Encoding", "deflate");
                    }

                    if (cacheData.LastModified < DateTime.Now)
                    {
                        context.Response.Cache.SetLastModified(cacheData.LastModified);
                    }

                    context.Response.Cache.SetETag(cacheData.LastModified.Ticks.ToString());
                    context.Response.BinaryWrite(cacheData.Data);

                    context.ApplicationInstance.CompleteRequest();
                }
            }

            return(true);
        }
示例#4
0
        public void DoParse(string rawUrl, UrlFormat urlFormat)
        {
            int appUrlLength = Globals.AppRoot.Length + 1;
            int queryIndex;

            string main, originalMain, query;
            string nameLink = null;

            int nameLinkIndex = rawUrl.IndexOf('#');

            if (nameLinkIndex >= 0)
            {
                nameLink = rawUrl.Substring(nameLinkIndex);
                rawUrl   = rawUrl.Remove(nameLinkIndex);
            }

            switch (urlFormat)
            {
            case UrlFormat.Aspx:
            case UrlFormat.Html:
                queryIndex = rawUrl.IndexOf('?', appUrlLength);
                if (queryIndex == -1)
                {
                    originalMain = rawUrl.Substring(appUrlLength, rawUrl.Length - appUrlLength);
                    query        = string.Empty;
                }
                else
                {
                    originalMain = rawUrl.Substring(appUrlLength, queryIndex - appUrlLength);
                    query        = rawUrl.Substring(queryIndex);
                }
                main = originalMain.Remove(originalMain.Length - 5);
                break;

            default:


                if (urlFormat == UrlFormat.Query)
                {
                    int rawLength = rawUrl.Length;

                    if (rawUrl[appUrlLength] == '?')
                    {
                        originalMain = "?";
                        appUrlLength++;
                    }

                    else if (rawLength > appUrlLength + 12 && rawUrl[appUrlLength + 12] == '?' && string.Compare(rawUrl.Substring(appUrlLength, 7), "default", true) == 0)
                    {
                        originalMain  = "?";
                        appUrlLength += 13;
                    }

                    else if (rawLength > appUrlLength + 10 && rawUrl[appUrlLength + 10] == '?' && string.Compare(rawUrl.Substring(appUrlLength, 5), "index", true) == 0)
                    {
                        originalMain  = "?";
                        appUrlLength += 11;
                    }

                    else if (rawLength == appUrlLength)
                    {
                        this.m_OriginalMain = string.Empty;
                        this.m_Main         = string.Empty;
                        this.m_QueryString  = string.Empty;
                        return;
                    }
                    else
                    {
                        originalMain = string.Empty;
                    }
                }
                else
                {
                    originalMain = string.Empty;
                }

                queryIndex = rawUrl.IndexOf('?', appUrlLength);
                if (queryIndex == -1)
                {
                    main  = rawUrl.Substring(appUrlLength, rawUrl.Length - appUrlLength);
                    query = string.Empty;
                }
                else
                {
                    main  = rawUrl.Substring(appUrlLength, queryIndex - appUrlLength);
                    query = rawUrl.Substring(queryIndex);
                }
                originalMain += main;
                break;
            }

            if (main.Length > 5 && main[main.Length - 5] == '.' &&
                (StringUtil.EndsWithIgnoreCase(main, ".aspx") || StringUtil.EndsWithIgnoreCase(main, ".html"))
                )
            {
                main = main.Remove(main.Length - 5);
            }

            if (main.Length == 0 || (main.Length == 5 && string.Compare(main, "index", true) == 0))
            {
                main = "default";

                if (urlFormat == UrlFormat.Query)
                {
                    originalMain = "?default";
                }
            }
            else if (urlFormat == UrlFormat.Query && string.Compare(main, "default", true) == 0)
            {
                originalMain = "?default";
            }

            //if (originalMain.Length == 0 || )

            this.m_OriginalMain = originalMain;
            this.m_Main         = main;
            this.m_QueryString  = query;

            if (nameLink != null)
            {
                this.m_NameLink = nameLink;
            }
        }