Beispiel #1
0
        public string GetContent(Uri url, bool isJsOrCss)
        {
            string path   = String.Format("{0}{1}", url.AbsolutePath, url.Query);
            string result = "";

            MemoryStream MemoryStream     = new MemoryStream();
            string       InstanceFilepath = "";
            string       SampleFilepath   = "";
            bool         isFromSample     = false;

            if (CacheMode)
            {
                InstanceFilepath = GetPocketFilePath(path, !isJsOrCss, true);
                MemoryStream     = GetStreamFromPocket(InstanceFilepath);
                if (MemoryStream.Length == 0)
                {
                    SampleFilepath = GetPocketFilePath(path, !isJsOrCss, false);
                    MemoryStream   = GetStreamFromPocket(SampleFilepath);
                    if (MemoryStream.Length != 0)
                    {
                        isFromSample = true;
                    }
                }
            }

            bool IsFromResponse = false;

            if (MemoryStream.Length == 0)
            {
                if (!locked)
                {
                    MemoryStream = GetStreamFromResponse(path);
                }
                else
                {
                    if (!String.IsNullOrEmpty(url.Query))
                    {
                        Uri NonQueryUrl = new Uri(String.Format("{0}{1}{2}{3}", url.Scheme, Uri.SchemeDelimiter, url.Authority, url.AbsolutePath));
                        return(GetContent(NonQueryUrl, isJsOrCss));
                    }
                }
                IsFromResponse = true;
                if (CacheMode)
                {
                    try
                    {
                        File.AppendAllText(LogFilePath, DateTime.Now + "\t" + path + (locked ? "\t[locked]" : "") + "\r\n");
                    }
                    catch { }
                }
            }

            StreamReader MemoryStreamReader = new StreamReader(MemoryStream);

            result = MemoryStreamReader.ReadToEnd();

            if (IsFromResponse || isFromSample)
            {
                if (!isFromSample)
                {
                    if (!isJsOrCss)
                    {
                        result = EnhanceModel.Enhance(result);
                    }
                    if (CacheMode)
                    {
                        byte[] byteArray = Encoding.UTF8.GetBytes(result);
                        SetStreamToPocket(new MemoryStream(byteArray), SampleFilepath);
                    }
                }

                if (!String.IsNullOrWhiteSpace(ReplacementModel.Hash))
                {
                    result = ReplacementModel.Replace(result);
                    if (CacheMode)
                    {
                        byte[] byteArray = Encoding.UTF8.GetBytes(result);
                        SetStreamToPocket(new MemoryStream(byteArray), InstanceFilepath);
                    }
                }
            }

            return(result);
        }
        public string GetContent(string path, bool isJsOrCss)
        {
            string result = "";

            MemoryStream MemoryStream     = new MemoryStream();
            string       InstanceFilepath = "";
            string       SampleFilepath   = "";
            bool         isFromSample     = false;

            Encoding encoding = Encoding.UTF8;

            if (CacheMode)
            {
                InstanceFilepath = GetPocketFilePath(path, !isJsOrCss, true);
                MemoryStream     = GetStreamFromPocket(InstanceFilepath);
                if (MemoryStream.Length == 0)
                {
                    SampleFilepath = GetPocketFilePath(path, !isJsOrCss, false);
                    MemoryStream   = GetStreamFromPocket(SampleFilepath);
                    if (MemoryStream.Length != 0)
                    {
                        isFromSample = true;
                    }
                }
            }

            bool IsFromResponse = false;

            if (MemoryStream.Length == 0)
            {
                if (!locked)
                {
                    MemoryStream = GetStreamFromResponse(path);
                    //encoding = Encoding.GetEncoding("windows-1251");
                }
                IsFromResponse = true;
                if (CacheMode)
                {
                    if (File.Exists(LogFilePath))
                    {
                        List <string> LogLines = File.ReadAllLines(LogFilePath).ToList();
                        LogLines.Insert(0, DateTime.Now + "\t" + path + (locked ? "\t[locked]" : ""));
                        File.WriteAllLines(LogFilePath, LogLines.Take(300).ToArray());
                    }
                }
            }

            StreamReader MemoryStreamReader = new StreamReader(MemoryStream, encoding);

            result = MemoryStreamReader.ReadToEnd();

            if (IsFromResponse || isFromSample)
            {
                if (!isFromSample)
                {
                    if (CacheMode && !locked)
                    {
                        byte[] byteArray = Encoding.UTF8.GetBytes(result);
                        SetStreamToPocket(new MemoryStream(byteArray), SampleFilepath);
                    }
                }

                if (!String.IsNullOrWhiteSpace(ReplacementModel.Hash))
                {
                    if (!isJsOrCss)
                    {
                        result = EnhanceModel.Enhance(result);
                    }
                    result = ReplacementModel.Replace(result);
                    if (CacheMode && !locked)
                    {
                        byte[] byteArray = Encoding.UTF8.GetBytes(result);
                        SetStreamToPocket(new MemoryStream(byteArray), InstanceFilepath);
                    }
                }
            }

            return(result);
        }