Ejemplo n.º 1
0
        public void CheckArchorInRedirectionFile(string sURL, ref HttpWebResponse resp)
        {
            string[] Param = sURL.Split('#');

            string fileName   = resp.ResponseUri.OriginalString;
            string errProcess = "";
            string sRepalce   = CommonFun.GetConfigurationValue("RepositoryENUSArticleDir", ref errProcess);

            fileName = fileName.Replace("https://docs.azure.cn/zh-cn", sRepalce).Replace("/", "\\").Trim();
            if (fileName.Substring(fileName.Length - 3).ToLower() != ".md")
            {
                fileName = string.Format("{0}.md", fileName);
            }

            string sArchor = string.Format("#{0}", Param[1]);

            bool isRedirection  = true;
            bool isNeedRedirect = false;

            CheckArchorInFile(isRedirection, fileName, sArchor, ref isNeedRedirect);
        }
Ejemplo n.º 2
0
        public void SetFullPathName(string[] para)
        {
            string diskpath     = "";
            string message      = "";
            string relativefile = "";

            if (para[0].ToLower() == "articles")
            {
                diskpath             = CommonFun.GetConfigurationValue("RepositoryENUSArticleDir", ref message);
                relativefile         = GetRightFileName(para);
                this.FullPath        = string.Format(@"{0}\{1}\{2}", diskpath, relativefile, this.File);
                this.ArticleCategory = FileCategory.Article;
            }

            if (para[0].ToLower() == "includes")
            {
                diskpath             = CommonFun.GetConfigurationValue("RepositoryENUSIncludeDir", ref message);
                relativefile         = GetRightFileName(para);
                this.FullPath        = string.Format(@"{0}\{2}", diskpath, relativefile, this.File);
                this.ArticleCategory = FileCategory.Includes;
            }
        }
Ejemplo n.º 3
0
        public void CheckAllLinks(List <string> urlList)
        {
            string error  = string.Empty;
            string sValue = CommonFun.GetConfigurationValue("MaxHttpConnectionCount", ref error);

            if (error.Length > 0)
            {
                Console.WriteLine(error);
                return;
            }
            int  iConCount  = 0;
            bool converFlag = int.TryParse(sValue, out iConCount);

            if (converFlag == false)
            {
                Console.WriteLine(string.Format("MaxHttpConnectionCount {0} is not a valid interge.", sValue));
                return;
            }

            System.Net.ServicePointManager.DefaultConnectionLimit = iConCount;
            //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;


            HttpWebRequest  req         = null;
            HttpWebResponse resp        = null;
            string          sURL        = string.Empty;
            string          sLowCaseURL = string.Empty;

            for (int i = 0; i < urlList.Count; i++)
            {
                try
                {
                    sURL = urlList[i].ToString().Trim();

                    //Check the localhost URL in source code.
                    sLowCaseURL = sURL.ToLower();
                    if (sLowCaseURL.StartsWith("http://127.0.0.1") || sLowCaseURL.StartsWith("https://127.0.0.1") ||
                        sLowCaseURL.StartsWith("http://localhost") || sLowCaseURL.StartsWith("https://localhost") ||
                        sLowCaseURL.StartsWith("http://mysftestcluster.chinaeast.cloudapp.chinacloudapi.cn:19080/explorer") ||
                        sLowCaseURL.StartsWith("https://mysftestcluster.chinaeast.cloudapp.chinacloudapi.cn:19080/explorer") ||
                        sLowCaseURL.StartsWith("https://github.com/Azure/azure-quickstart-templates/") ||
                        sLowCaseURL.StartsWith("https://vortex.data.microsoft.com/collect/v1"))
                    {
                        if (this.ShowHistory == ConvertProcess.ShowHistory)
                        {
                            this.BrokenLink += string.Format("{0} : {1}.\n", sURL, "OK, Discard to check link of source code.");
                        }
                        continue;
                    }

                    //Check the hide URL in source code. Sample : XXXX --> , We will discard the URL which no need to verify.
                    if (sLowCaseURL.EndsWith("-->"))
                    {
                        if (this.ShowHistory == ConvertProcess.ShowHistory)
                        {
                            this.BrokenLink += string.Format("{0} : {1}.\n", sURL.Substring(0, sURL.IndexOf("-->")), "OK, Discard to check link of HIDE in document");
                        }
                        continue;
                    }


                    req        = (HttpWebRequest)WebRequest.Create(sURL);
                    req.Method = "GET";
                    //req.KeepAlive = true;
                    req.ContentType       = "application/x-www-form-urlencoded; charset=UTF-8";
                    req.UserAgent         = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
                    req.Timeout           = 20000;
                    req.AllowAutoRedirect = true;


                    resp = (HttpWebResponse)req.GetResponse();
                    //Console.WriteLine("checking " + urlList[i].ToString());
                    switch (resp.StatusCode)
                    {
                    case HttpStatusCode.OK:                          //200
                    case HttpStatusCode.Accepted:                    //202
                    case HttpStatusCode.Forbidden:                   //403
                    case HttpStatusCode.RequestTimeout:              //408
                    case HttpStatusCode.NonAuthoritativeInformation: //203

                        if (sURL.Contains("#") == true && NeedToCheckArchorInRespostory(sURL) == true)
                        {
                            CheckArchorInRedirectionFile(sURL, ref resp);
                        }
                        else
                        {
                            if (this.ShowHistory == ConvertProcess.ShowHistory)
                            {
                                this.BrokenLink += string.Format("{0} : {1}\n", urlList[i].ToString(), resp.StatusCode.ToString());
                            }
                        }



                        break;

                    default:
                        this.BrokenLink += string.Format("{0} : {1}\n", urlList[i].ToString(), resp.StatusCode.ToString());
                        break;
                    }
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(string.Format("{0}:{1}\n", urlList[i].ToString(), ex.Message.ToString()));
                    string exMsg = ex.Message.ToString();
                    if (exMsg.Contains("(403) Forbidden") == false &&
                        exMsg.Contains("The request was aborted: Could not create SSL/ TLS secure channel") == false &&
                        exMsg.Contains("The request was aborted") == false &&
                        exMsg.Contains("The operation has timed out") == false &&
                        exMsg.Contains("Too many automatic redirections were attempted") == false &&
                        exMsg.Contains("The underlying connection was closed") == false &&
                        exMsg.Contains("NonAuthoritativeInformation") == false &&
                        exMsg.Contains("Internal Server Error") == false &&
                        exMsg.Contains("The Authority/Host could not be parsed") == false)
                    {
                        this.BrokenLink += string.Format("Error : {0} -> {1}\n", urlList[i].ToString(), ex.Message.ToString());
                    }
                }
                finally
                {
                    if (req != null)
                    {
                        req.Abort();
                    }
                    if (resp != null)
                    {
                        resp.Close();
                    }
                }
            }

            if (req != null)
            {
                req = null;
            }
            if (resp != null)
            {
                resp.Close();
                resp = null;
            }
        }
Ejemplo n.º 4
0
        public void CheckMatches(MatchCollection matches, ref List <string> lstURL, ref string articleContent)
        {
            string originalFileName = string.Empty;

            string filename       = string.Empty;
            int    idxSeq         = 0;
            string lablename      = string.Empty;
            string checkdirectory = string.Empty;
            string checkfile      = string.Empty;
            string error          = string.Empty;

            //Match existMath = null;

            string localpath = CommonFun.GetConfigurationValue("RepositoryENUSArticleDir", ref error);

            for (int i = 0; i < matches.Count; i++)
            {
                //First remove the current directory ./ when exists.
                lablename = matches[i].Groups["labelname"].ToString().ToLower().Trim();
                switch (lablename)
                {
                case "401":
                case "binary":
                case "nvarchar":
                    continue;
                }

                originalFileName = matches[i].Groups["mdfilename"].ToString().Trim();
                filename         = originalFileName.ToLower();

                // the lenght of --> is 3 and exclude at first.
                if (filename.Length >= 3 && filename.Substring(filename.Length - 3) == "-->")
                {
                    continue;
                }

                //Exclude the following sample
                //[Azure 门户](http://portal.azure.cn "Azure 门户")

                if (filename.IndexOf(' ') > 0)
                {
                    //idxSeq = filename.IndexOf(' ') - 1;   Correct the Sequence of Sample.
                    idxSeq           = filename.IndexOf(' ');
                    filename         = filename.Substring(0, idxSeq);
                    originalFileName = originalFileName.Substring(0, idxSeq);
                }

                // When we find that the filename is string.empty, we will discard it and go the check next round.
                switch (filename.ToLower())
                {
                case "":
                case "401":
                case "#":
                case "http://mysftestcluster.chinaeast.cloudapp.chinacloudapi.cn:19080/explorer/":
                case "http://mycluster.region.cloudapp.chinacloudapi.cn:19080/explorer":
                case "ssdt":
                case "response.data":
                case "https://configuration-server-name/ip:44315":
                case "eventid":
                case "eventid=evt":
                case "print":
                case "4222":
                case "4222)`":
                case "4222,":
                case "4222`":
                case "mailto:[email protected]":
                case "index.yml":
                case "deviceFile.write(\",":
                case "PS":
                    continue;
                }



                // When the filename is normal item, we will continue to check the following decode.
                if (filename.Substring(0, 2) == "./")
                {
                    filename         = filename.Substring(2);
                    originalFileName = originalFileName.Substring(2);
                }


                //Get the current parent directory.
                if (this.FullPath.LastIndexOf("\\") > -1)
                {
                    checkdirectory = this.FullPath.Substring(0, this.FullPath.LastIndexOf("\\"));
                }


                //Step 1:  Select Http(s):// link
                if (filename.StartsWith("http://") || filename.StartsWith("https://"))
                {
                    //lstURL.Add(filename);
                    lstURL.Add(originalFileName);
                    continue;
                }

                //Step 2: Select inner Archor tag
                if (filename.StartsWith("#") == true)
                {
                    this.CheckArchorInFile(ref articleContent, originalFileName);
                    continue;
                }

                //Step 3: Select the outside Archor tag, omit due to there are exist some archor.
                if (filename.Contains("#") == true)
                {
                }

                string sPostfix = string.Empty;

                //Step 3: Sample of ../virtual-machines/windows/sizes.md?toc=%2fvirtual-machines%2fwindows%2ftoc.json
                if (filename.Contains(".md") == true)
                {
                    sPostfix = originalFileName.Substring(filename.IndexOf(".md") + 3);
                    if (sPostfix.Trim().Length > 0 && sPostfix.Contains("#") == true)
                    {
                        string[] menuAndAnchor = sPostfix.Split('#');
                        sPostfix = "#" + menuAndAnchor[menuAndAnchor.Length - 1];   // Remove the ?toc=XXXX#AnchorName
                    }
                    else
                    {
                        sPostfix = "";
                    }

                    idxSeq           = filename.IndexOf(".md") + 3;
                    filename         = filename.Substring(0, idxSeq) + sPostfix;
                    originalFileName = originalFileName.Substring(0, idxSeq) + sPostfix;
                }


                //Step 4: Check the md and image file.
                //The filename is larger than 4 charactors
                if (filename.Length >= 4 &&
                    (filename.Substring(filename.Length - 4).ToLower().ToString() == ".jpg" ||
                     filename.Substring(filename.Length - 4).ToLower().ToString() == ".svg" ||
                     filename.Substring(filename.Length - 4).ToLower().ToString() == ".png" ||
                     filename.Substring(filename.Length - 4).ToLower().ToString() == ".gif"))

                {
                    //reward to parent directory when exists the ../
                    while (filename.IndexOf("../") > -1)
                    {
                        idxSeq           = filename.IndexOf("../") + 3;
                        filename         = filename.Substring(idxSeq);
                        originalFileName = originalFileName.Substring(idxSeq);
                        checkdirectory   = checkdirectory.Substring(0, checkdirectory.LastIndexOf("\\"));
                    }

                    // images file should add \\ in the following code
                    if (filename.StartsWith("/") == true)
                    {
                        checkfile = string.Format("{0}\\{1}", localpath, originalFileName.Replace("/", "\\"));
                    }
                    else
                    {
                        checkfile = string.Format("{0}\\{1}", checkdirectory, originalFileName.Replace("/", "\\"));
                    }



                    // Replace some case which the file path contains \\\\.
                    checkfile = checkfile.Replace("\\\\", "\\");
                    if (System.IO.File.Exists(checkfile) == false)
                    {
                        this.BrokenLink += string.Format("{0} : missing.\n", checkfile);
                    }
                    else
                    {
                        if (this.ShowHistory == ConvertProcess.ShowHistory)
                        {
                            this.BrokenLink += string.Format("{0} : correct.\n", checkfile);
                        }
                    }

                    continue;
                }

                //The filename should be 3 characters at least.
                if (filename.Length >= 3 && filename.Substring(filename.Length - 3).ToLower().ToString() == ".md")
                {
                    //reward to parent directory when exists the ../
                    while (filename.IndexOf("../") > -1)
                    {
                        idxSeq           = filename.IndexOf("../") + 3;
                        filename         = filename.Substring(idxSeq);
                        originalFileName = originalFileName.Substring(idxSeq);
                        checkdirectory   = checkdirectory.Substring(0, checkdirectory.LastIndexOf("\\"));
                    }

                    if (filename.StartsWith("/") == true)
                    {
                        // forexample /azure-resource-manager/XXXX.md
                        checkfile = string.Format("{0}{1}", localpath, originalFileName.Replace("/", "\\"));
                    }
                    else
                    {
                        // for example include/XXX.md, we should add the \\ to connect correct path.
                        checkfile = string.Format("{0}\\{1}", checkdirectory, originalFileName.Replace("/", "\\"));
                    }

                    if (System.IO.File.Exists(checkfile) == false)
                    {
                        //We will check whether contains redirection URL
                        checkdirectory = checkdirectory.Replace(localpath, "").Replace("\\", "/").TrimStart('/');

                        if (filename.StartsWith("articles/") == true)
                        {
                            // filename = "articles/XXXX.md"
                            // this choise match the sample of /article/XXXX.md
                            filename         = filename.Substring("articles/".Length);
                            originalFileName = originalFileName.Substring("articles/".Length);
                            idxSeq           = filename.IndexOf(".md");
                            filename         = string.Format("https://docs.azure.cn/zh-cn/{0}", filename.Substring(0, idxSeq));
                            originalFileName = string.Format("https://docs.azure.cn/zh-cn/{0}", originalFileName.Substring(0, idxSeq));
                        }
                        else
                        {
                            if (filename.StartsWith("/") == true)
                            {
                                // filename = "/XXXX.md"
                                // this choise match the sample of /article/event-hubs/XXXX.md
                                filename         = filename.Substring(1);
                                originalFileName = originalFileName.Substring(1);
                                idxSeq           = filename.IndexOf(".md");
                                filename         = string.Format("https://docs.azure.cn/zh-cn/{0}/{1}", checkdirectory, filename.Substring(0, idxSeq));
                                originalFileName = string.Format("https://docs.azure.cn/zh-cn/{0}/{1}", checkdirectory, originalFileName.Substring(0, idxSeq));
                            }
                            else
                            {
                                // filename = "XXXX.md"
                                // this choise match the sample of /article/event-hubs/XXXX.md
                                idxSeq           = filename.IndexOf(".md");
                                filename         = string.Format("https://docs.azure.cn/zh-cn/{0}/{1}", checkdirectory, filename.Substring(0, idxSeq));
                                originalFileName = string.Format("https://docs.azure.cn/zh-cn/{0}/{1}", checkdirectory, originalFileName.Substring(0, idxSeq));
                            }
                        }



                        lstURL.Add(originalFileName);
                    }
                    else
                    {
                        if (this.ShowHistory == ConvertProcess.ShowHistory)
                        {
                            this.BrokenLink += string.Format("{0} : correct.\n", checkfile);
                        }
                    }

                    continue;
                }


                // Check the local path.
                while (filename.IndexOf("../") > -1)
                {
                    idxSeq           = filename.IndexOf("../") + 3;
                    filename         = filename.Substring(idxSeq);
                    originalFileName = originalFileName.Substring(idxSeq);
                    checkdirectory   = checkdirectory.Substring(0, checkdirectory.LastIndexOf("\\"));
                }


                if (filename.StartsWith("/") == true)
                {
                    // forexample /azure-resource-manager/XXXX.md
                    checkfile = string.Format("{0}{1}", localpath, filename.Replace("/", "\\"));
                }
                else
                {
                    checkfile = string.Format("{0}\\{1}", checkdirectory, filename.Replace("/", "\\"));
                }

                bool isRedirect = false;
                if (checkfile.Contains("#"))    //we have check the .md file in preceding logical .
                {
                    checkfile = checkfile.ToLower().Replace(".md", "");
                    idxSeq    = checkfile.IndexOf("#");
                    string localfile   = checkfile.Substring(0, idxSeq) + ".md";
                    string localarchor = checkfile.Substring(idxSeq);

                    // isRedirect return flag to confirem the file is redirection or not.
                    bool isNeedRedirect = false;
                    this.CheckArchorInFile(isRedirect, localfile, localarchor, ref isNeedRedirect);
                    if (isNeedRedirect == false)
                    {
                        continue;
                    }
                }
                else
                {
                }

                // The Link style like [](../service-name/XXXX.md)
                if (filename.ToLower().Contains(".md") == true)
                {
                    filename         = filename.Replace(".md", "").Replace(".MD", "");
                    originalFileName = originalFileName.Replace(".md", "").Replace(".MD", "");
                }

                if (filename.Substring(0, 1) == "/")
                {
                    filename         = filename.Substring(1);
                    originalFileName = originalFileName.Substring(1);
                    filename         = string.Format("https://docs.azure.cn/zh-cn/{0}", filename);
                    originalFileName = string.Format("https://docs.azure.cn/zh-cn/{0}", originalFileName);
                }
                else
                {
                    checkdirectory   = checkdirectory.Replace(localpath, "").Replace("\\", "/").TrimStart('/');
                    filename         = string.Format("https://docs.azure.cn/zh-cn/{0}/{1}", checkdirectory, filename);
                    originalFileName = string.Format("https://docs.azure.cn/zh-cn/{0}/{1}", checkdirectory, originalFileName);
                }

                lstURL.Add(originalFileName);
            }
        }