Esempio n. 1
0
        private static Dictionary <String, Object> callResource(Dictionary <String, Object> resData)
        {
            String resUrl   = resData["name"].ToString();
            String truncUrl = resUrl.ToLower().Split(new string[] { "\\?" }, StringSplitOptions.None)[0];

            Dictionary <String, Object> rs = new Dictionary <string, object>();
            StringBuilder sb = new StringBuilder();
            Boolean       staticResrcStatus = false;
            Boolean       isImage           = false;
            Boolean       flagSet           = false;
            String        resourceType      = "others";

            //LoggingMessage.DisplayMessage("Started Thread to get ResourceTiming : " + resUrl, LoggingMessage.MessageType.DEBUG, log);

            rs = (from x in resData select x).ToDictionary(x => x.Key, x => x.Value);

            try
            {
                if (Convert.ToDouble(resData["duration"]) <= CollectorConstants.getResDurThreshold() || Convert.ToDouble(resData["duration"]) <= 0.0)
                {
                    rs.Add("IsCached", true);
                }
                else
                {
                    rs.Add("IsCached", false);
                }
            }
            catch (Exception e)
            {            }

            foreach (String img in CollectorConstants.getImageList())
            {
                if (truncUrl.Contains(img.ToLower().Trim()))
                {
                    isImage           = true;
                    staticResrcStatus = true;
                    flagSet           = true;
                    resourceType      = img.Trim();
                    break;
                }
            }

            if (!flagSet)
            {
                foreach (String stat in CollectorConstants.getStaticList())
                {
                    if (truncUrl.Contains(stat.ToLower().Trim()))
                    {
                        staticResrcStatus = true;
                        isImage           = false;
                        resourceType      = stat.Trim();
                        break;
                    }
                }
            }

            rs.Add("IsStaticResrc", staticResrcStatus);
            rs.Add("IsImage", isImage);
            rs.Add("ResourceType", resourceType);
            rs.Add("HostName", getHostName(truncUrl));

            //if(resUrl.contains(".js") || resUrl.contains(".css") || resUrl.contains(".png") || resUrl.contains(".jpg") || resUrl.contains(".jpeg") || resUrl.contains(".gif") || resUrl.contains(".svg") || resUrl.contains(".html"))
            if (stringContainsItemFromList(resUrl, CollectorConstants.getStaticExt() + "," + CollectorConstants.getImages()))
            {
                try
                {
                    HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(resUrl);
                    httpReq.Timeout   = 5000;
                    httpReq.UserAgent = CollectorConstants.getUserAgent();

                    if (stringContainsItemFromList(resUrl, CollectorConstants.getStaticExt() + ",.svg"))
                    {
                        httpReq.Headers["Accept-Encoding"] = "gzip,deflate,sdch";
                    }

                    using (HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse())
                    {
                        int statusCode = (int)httpRes.StatusCode;
                        rs.Add("Status", statusCode);
                        if (statusCode == 200)
                        {
                            string[] header = httpRes.Headers.AllKeys;

                            if (httpRes.GetResponseHeader("Last-Modified") != null)
                            {
                                rs.Add("Last-Modified", httpRes.GetResponseHeader("Last-Modified").Replace(",", ""));
                            }
                            if (httpRes.GetResponseHeader("Content-Length") != null)
                            {
                                rs.Add("Content-Length", httpRes.GetResponseHeader("Content-Length"));
                            }
                            if (httpRes.GetResponseHeader("Connection") != null)
                            {
                                rs.Add("Connection", httpRes.GetResponseHeader("Connection"));
                            }
                            if (httpRes.GetResponseHeader("Cache-Control") != null)
                            {
                                rs.Add("Cache-Control", httpRes.GetResponseHeader("Cache-Control").Replace(",", "#").Replace("=", "#"));
                            }
                            if (httpRes.GetResponseHeader("ETag") != null)
                            {
                                rs.Add("ETag", httpRes.GetResponseHeader("ETag").Replace("\"", ""));
                            }
                            if (httpRes.GetResponseHeader("Expires") != null)
                            {
                                rs.Add("Expires", httpRes.GetResponseHeader("Expires").Replace(",", ""));
                            }

                            if (stringContainsItemFromList(resUrl, CollectorConstants.getImages()))
                            {
                                if (stringContainsItemFromList(resUrl, ".svg"))
                                {
                                    if (httpRes.GetResponseHeader("Content-Encoding") != null)
                                    {
                                        rs.Add("Content-Encoding", httpRes.GetResponseHeader("Content-Encoding").Replace(",", ""));
                                    }
                                }
                                Image image = Image.FromStream(httpRes.GetResponseStream());
                                rs.Add("Height", image.Height);
                                rs.Add("Width", image.Width);
                            }
                            else
                            {
                                if (header.Contains("Content-Encoding") && stringContainsItemFromList(header[Array.IndexOf(header, "Content-Encoding")], "gzip"))
                                {
                                    try
                                    {
                                        using (GZipStream ungzippedResponse = new GZipStream(httpRes.GetResponseStream(), CompressionMode.Decompress))
                                        {
                                            using (StreamReader file = new StreamReader(ungzippedResponse, Encoding.UTF8))
                                            {
                                                String line;
                                                while ((line = file.ReadLine()) != null)
                                                {
                                                    sb.Append(line);
                                                }
                                                if (httpRes.GetResponseHeader("Content-Encoding") != null)
                                                {
                                                    rs.Add("Content-Encoding", httpRes.GetResponseHeader("Content-Encoding").Replace(",", ""));
                                                }
                                                rs.Add("OrgSize", sb.ToString().Length);
                                                if (stringContainsItemFromList(resUrl, ".js,.css"))
                                                {
                                                    rs.Add("MinfSize", compress(sb.ToString(), ref resUrl));
                                                }
                                            }
                                        }
                                    }
                                    catch { }
                                }
                                else
                                {
                                    using (StreamReader file = new StreamReader(httpRes.GetResponseStream(), Encoding.UTF8))
                                    {
                                        String line;
                                        while ((line = file.ReadLine()) != null)
                                        {
                                            sb.Append(line);
                                        }
                                    }

                                    rs.Add("OrgSize", sb.ToString().Length);
                                    if (stringContainsItemFromList(resUrl, ".js,.css"))
                                    {
                                        if (httpRes.GetResponseHeader("Content-Encoding") != null)
                                        {
                                            rs.Add("Content-Encoding", httpRes.GetResponseHeader("Content-Encoding").Replace(",", ""));
                                        }
                                        //striplen = sb.ToString().Replace("\\n| {2}|\\t|\\r", "").Length;
                                        rs.Add("MinfSize", compress(sb.ToString(), ref resUrl));
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LoggingMessage.DisplayMessage("PerfInsight - CallResource Error: " + ex.Message, LoggingMessage.MessageType.ERROR, log);
                }
            }

            return(rs);
        }