public void Issue1_dont_screw_up_the_page_when_using_tables() { var markdown = @" ###Liste des paquets Nuget pour IvFragment Secondaire N3. Nom | Version | Descriptions --- | ------- | ------------ N3_IVFragment_Secondaire_MVC5 | 1.2.1 | Utilitaires pour les fragments secondaire ###N3 IvFragment Secondaire MVC5 >Ce paquet Nuget intégrera les fichiers utilitaires requis pour aider à convertir une section d'application .NET MVC en fragment secondaire. Quelques modifications seront exécutées dans le fichier de config pour installer le filtre N3 pour le fragment secondaire. ![image](https://cloud.githubusercontent.com/assets/10774173/5977134/0b3bfc66-a863-11e4-805b-a55f218f3b41.png) The result code is like below; it is missing a th closing tag and merge the rest of the document in the table. "; var parser = new PageParser(AppDomain.CurrentDomain.BaseDirectory, ""); var actual = parser.ParseString("", markdown); actual.Body.Should().Contain("<h3 id=\"N3IvFragmentSecondaireMVC5\">N3 IvFragment Secondaire MVC5</h3>"); var posTable = actual.Body.IndexOf("</table>"); var h3 = actual.Body.IndexOf("<h3 id=\"N3IvFragmentSecondaireMVC5"); posTable.Should().BeLessThan(h3); }
public void Reset() { _parser = null; _isWorking = false; _service.ConnectionLeaseTimeout = 0; GC.Collect(); }
public void finds_http_link_correctly() { var sut = new PageParser(Environment.CurrentDirectory + "\\TestDocs\\", "/intranet/"); var actual = sut.ParseString("/", @" http://this.link "); actual.Body.Should().Contain(@"href=""http://this.link"""); }
public void finds_unc_path_correctly() { var sut = new PageParser(Environment.CurrentDirectory + "\\TestDocs\\", "/intranet/"); sut.VirtualPathHandler = OnResolvePath; var actual = sut.ParseString("/", @" \\unc_path\to\folder "); actual.Body.Should().Contain(@"href=""file://\\unc_path\to\folder"""); }
public void headings_should_be_anchored() { var sut = new PageParser(Environment.CurrentDirectory + "\\TestDocs\\", "/intranet/"); sut.VirtualPathHandler = OnResolvePath; var actual = sut.ParseString("/", @" <h1>Parse specific</h1> "); actual.Body.Should().Contain(@"id=""Parsespecific"""); }
public void finds_www_link_correctly() { var sut = new PageParser(Environment.CurrentDirectory + "\\TestDocs\\", "/intranet/"); sut.VirtualPathHandler = OnResolvePath; var actual = sut.ParseString("/", @" hello world this www.onetrueerror.com is a link "); actual.Body.Should().Contain(@"href=""http://www.onetrueerror.com"""); }
private static MarkdownPage Parse() { string directory = HostingEnvironment.MapPath("~/pages"); PageParser parser = new PageParser(directory); MarkdownPage index = parser.Parse(); if (!parser.IsValid) { var args = parser.ValidationMessages.Select(m => string.Join("in", m.Split('|'))); throw new Exception(string.Join(Environment.NewLine, args)); } return index; }
public void do_not_destroy_regular_anchor_links() { var sut = new PageParser(Environment.CurrentDirectory + "\\TestDocs\\", "/intranet/"); sut.VirtualPathHandler = OnResolvePath; var actual = sut.ParseString("/", @" hello world this <a href=""http://www.onetrueerror.com"">regular</a> is a link "); actual.Body.Should().Contain(@"href=""http://www.onetrueerror.com"""); }
public void Parser(PageParser parser) { if (parser == null) throw new ArgumentNullException("parser"); _parser = parser; _address = new Uri(string.Format("http://{0}wowhead.com/", _locales[parser.Locale])); ServicePointManager.DefaultConnectionLimit = SemaphoreCount * 10; _service = ServicePointManager.FindServicePoint(_address); { _service.SetTcpKeepAlive(true, 100000, 100000); } _address = new Uri(_address, parser.Address); }
public string Execute(PageParser parser, string currentUrlPath, string baseUrlPath, string text) { foreach (var filter in _filters) { var context = new PreFilterContext(parser) { TextToParse = text, CurrentUrlPath = currentUrlPath, RootUrlPath = baseUrlPath, }; text = filter.Parse(context); } return text; }
public Worker(ParsingType type, PageParser parser, EventHandler onPageDownloadingComplete = null) { _type = type; _parser = parser; _address = LocaleMgr.GetAddress(parser.Locale); ServicePointManager.DefaultConnectionLimit = SemaphoreCount * 10; { _service = ServicePointManager.FindServicePoint(_address); _service.SetTcpKeepAlive(true, KeepAliveTime, KeepAliveTime); } _address = new Uri(_address, parser.Address); _semaphore = new SemaphoreSlim(SemaphoreCount, SemaphoreCount); _badIds = new ConcurrentQueue<uint>(); PageDownloadingComplete += onPageDownloadingComplete; }
public Worker(ParsingType type, PageParser parser, EventHandler onPageDownloadingComplete = null) { m_type = type; m_parser = parser; m_address = new Uri(string.Format("http://{0}.wowhead.com/", parser.Locale.GetLocalePrefix())); ServicePointManager.DefaultConnectionLimit = SemaphoreCount * 10; { m_service = ServicePointManager.FindServicePoint(m_address); m_service.SetTcpKeepAlive(true, KeepAliveTime, KeepAliveTime); } m_semaphore = new SemaphoreSlim(SemaphoreCount, SemaphoreCount); m_storeUnprocessedIds = m_type == ParsingType.TypeByList || m_type == ParsingType.TypeByWoWHeadFilter; if (m_storeUnprocessedIds) m_badIds = new Queue<uint>(); PageDownloadingComplete += onPageDownloadingComplete; }
/*<tr class="odd2"> <td class="num">7</td> <td class="ava"><img src="/cards/fire/fire_servant_1_100.jpg" height="25" width="25" alt=""></td> <td class="user"> <a class="tdn" href="/user/324875/"> <img class="mr5" src="/img/lg/lg-red-1-pvp.png" width="16" height="16" alt=""> <span class="c_66">Аккэлия</span> <span class="c_99"> 4 269 183 371</span><br/> <span class="c_66 ml21">Маршал</span> <span class="fr c_99">163 д</span> </a> </td> </tr> */ public CharacterNode(HtmlNode _node) : base(_node) { HtmlNodeCollection collection; string strValue; string attrValue; //HtmlNode characterNode; //HtmlNodeCollection characterPropertiesCollection; //int num; PageParser parser; collection = _node.SelectNodes(@"td"); foreach (HtmlNode locNode in collection) { strValue = locNode.InnerText; if (locNode.Attributes.Count > 0 ) { attrValue = locNode.Attributes[0].Value; switch (attrValue) { case "num": internalId = Convert.ToInt32(strValue); break; case "ava": break; case "user": parser = new PageParser(locNode, @"d:\temp\char.txt"); parser.parseNode(this, locNode); break; default: break; } } } }
public void parses_page_link_correctly() { var sut = new PageParser(Environment.CurrentDirectory + "\\TestDocs\\", "/intranet/"); sut.VirtualPathHandler = OnResolvePath; var actual = sut.ParseUrl("/withlink"); actual.Body.Should().Contain("intranet/FolderTest"); }
public SoapDocumentationHandler(Type type, HttpContext context) : base(type) { _url = context.Request.Url.ToString(); int i = _url.LastIndexOf('?'); if (i != -1) { _url = _url.Substring(0, i); } _typeStubInfo = (SoapTypeStubInfo)TypeStubManager.GetTypeStub(ServiceType, "Soap"); HttpRequest req = context.Request; string key = null; if (req.QueryString.Count == 1) { key = req.QueryString.GetKey(0); if (key == null) { key = req.QueryString [0]; } if (key != null) { key = key.ToLower(CultureInfo.InvariantCulture); } } if (key == "wsdl" || key == "schema" || key == "code" || key == "disco") { return; } #if NET_2_0 string help = WebServicesSection.Current.WsdlHelpGenerator.Href; string path = Path.GetDirectoryName(ConfigurationManager.OpenMachineConfiguration().FilePath); #else string help = WSConfig.Instance.WsdlHelpPage; string path = Path.GetDirectoryName(WSConfig.Instance.ConfigFilePath); #endif string appPath = AppDomain.CurrentDomain.GetData(".appPath").ToString(); string vpath; if (path.StartsWith(appPath)) { vpath = path.Substring(appPath.Length); vpath = vpath.Replace("\\", "/"); } else { vpath = "/"; } if (vpath.EndsWith("/")) { vpath += help; } else { vpath += "/" + help; } string physPath = Path.Combine(path, help); #if !TARGET_JVM if (!File.Exists(physPath)) { throw new InvalidOperationException("Documentation page '" + physPath + "' not found"); } #endif _pageHandler = PageParser.GetCompiledPageInstance(vpath, physPath, context); }
public PageCompiler(PageParser pageParser) : base(pageParser) { this.pageParser = pageParser; }
static void Main(string[] args) { var db = new Model1(); foreach (var product in db.Productions.Where(o => !o.Done).OrderBy(o => o.FromPage)) { var db1 = new Model1(); var frompage = int.Parse(System.Configuration.ConfigurationManager.AppSettings["fromPage"]); Pager page = new Pager { Currentpage = frompage }; int correctPage = frompage; do { var url = GetUrl(page.Currentpage + 1, product.ProductionName); try { var html = HttpHelper.Get(url).Result; HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); //Thread.Sleep(1000); //共 18702条 第 1页/共1247页 var pageNodeText = doc.DocumentNode.SelectSingleNode(@"//tr[@height='70']/td[@width='30%' and @style='padding-left:30px']").InnerText; page = PageParser.MedicalListParse(pageNodeText); if (page.TotalCount == 0) { Console.WriteLine(product.ProductionName + "没有发现记录"); break; } Console.WriteLine("当前页:" + page.Currentpage + ",共" + page.TotalPage + "页"); foreach (var item in DrugListParser.Parse(doc)) { item.ProductId = product.ID; item.FromPage = page.Currentpage; item.Class = "国产药"; db1.DrugItems.Add(item); } try { db1.SaveChanges(); } catch (Exception) { db1.SaveChanges(); } Console.WriteLine("保存完成"); } catch (Exception) { continue; } } while (page.Currentpage < page.TotalPage); var p = db1.Productions.FirstOrDefault(o => o.ID == product.ID); p.Done = true; db1.SaveChanges(); } Console.ReadKey(); }
// 摘要: // 通过实现 System.Web.IHttpHandler 接口的自定义 HttpHandler 启用 HTTP Web 请求的处理。 // // 参数: // context: // System.Web.HttpContext 对象,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session // 和 Server)的引用。 public void ProcessRequest(HttpContext context) { string sendToUrl = context.Request.Url.PathAndQuery; string applicationPath = context.Request.PhysicalApplicationPath; //检测是否为Ajax调用 bool AjaxProcess = WebHelper.GetRequestParam <bool>(context.Request, "X-Ajax-Process", false); if (!AjaxProcess && context.Request.RequestType == "GET") { var config = StaticPageConfiguration.GetConfig(); //判断config配置信息 if (config != null && config.Enabled) { bool isMatch = false; // iterate through the rules foreach (StaticPageRule rule in config.Rules) { // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) string lookFor = "^" + RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rule.LookFor) + "$"; // Create a regex (note that IgnoreCase is set...) Regex reg = new Regex(lookFor, RegexOptions.IgnoreCase); if (reg.IsMatch(sendToUrl)) { isMatch = true; // match found - do any replacement needed string staticUrl = RewriterUtils.ResolveUrl(context.Request.ApplicationPath, reg.Replace(sendToUrl, rule.WriteTo)); string staticFile = context.Server.MapPath(staticUrl); //将域名进行替换 staticFile = staticFile.Replace("{domain}", context.Request.Url.Authority); //需要生成静态页面 if (!File.Exists(staticFile)) //静态页面不存在 { var filter = new ResponseFilter(context.Response.Filter, staticFile, rule.ValidateString, config.Replace, config.Extension); context.Response.Filter = filter; break; } else { //静态页面存在 FileInfo file = new FileInfo(staticFile); //按秒检测页面重新生成 int span = (int)DateTime.Now.Subtract(file.LastWriteTime).TotalSeconds; if (rule.Timeout > 0 && span >= rule.Timeout) //静态页面过期 { var filter = new ResponseFilter(context.Response.Filter, staticFile, rule.ValidateString, config.Replace, config.Extension); context.Response.Filter = filter; break; } //检测是否需要更新 else if (!string.IsNullOrEmpty(rule.UpdateFor) && CheckUpdate(context, rule.UpdateFor)) { var filter = new ResponseFilter(context.Response.Filter, staticFile, rule.ValidateString, config.Replace, config.Extension); context.Response.Filter = filter; break; } else { //设置格式 SetContentType(context, file.Extension); //将文件写入流中 context.Response.Clear(); context.Response.WriteFile(staticFile); //结束请求 context.Response.Close(); context.ApplicationInstance.CompleteRequest(); } } } } //如果没有匹配上 if (!isMatch) { //处理updates if (config.Replace && !string.IsNullOrEmpty(config.Extension)) { var filter = new AspNetFilter(context.Response.Filter, config.Replace, config.Extension); context.Response.Filter = filter; } } } } //进行错误处理,如果出错,则转到原有的静态页面 try { //if (sendToUrl.IndexOf('?') > 0) sendToUrl = sendToUrl.Substring(0, sendToUrl.IndexOf('?')); //IHttpHandler handler = BuildManager.CreateInstanceFromVirtualPath(sendToUrl, typeof(Page)) as IHttpHandler; string sendToUrlLessQString; RewriterUtils.RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out applicationPath); IHttpHandler handler = PageParser.GetCompiledPageInstance(sendToUrlLessQString, applicationPath, context); handler.ProcessRequest(context); } catch (ThreadAbortException) { } catch (Exception) { throw; } }
public override void ProcessRequest(HttpContext context) { PageParser.GetCompiledPageInstance(path, context.Server.MapPath(path), context).ProcessRequest(context); }
public void ProcessRequest(HttpContext context) { queryString = PageLink.SEMPageQuerystring; string hotelIDString = ""; if (!context.Request.Path.Contains("/") && !context.Request.Path.Contains(".")) { Utility.Utilities.RedirectToErrorPage(); } string[] urlPathArray = context.Request.Path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); //UrlPathArray[0] == flights-from //UrlPathArray[1] == abu-dhabi-flights.auh.aspx string[] urlPageArray = urlPathArray[1].Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); //UrlPageArray[0] = abu-dhabi-flights //UrlPageArray[1] = auh //UrlPageArray[2] = aspx string mode = urlPathArray[0].ToLower().Replace("-", ""); queryString = queryString.Replace("[args]", urlPageArray[urlPageArray.Length - 2]); if (mode == SEMMode.Products.ToString().ToLower()) { isForHotelDetails = true; queryString = context.Request.Url.Query.Trim('?'); hotelIDString = FormatHelper.CleanUpNonNumericCharacters( Regex.Match(context.Request.Path, RegEx.ProductPropertyID, RegexOptions.IgnoreCase).Value); } else if (mode == SEMMode.Product.ToString().ToLower()) { isForHotelDetails = true; queryString = context.Request.Url.Query.Trim('?'); hotelIDString = FormatHelper.CleanUpNonNumericCharacters( Regex.Match(context.Request.Path, RegEx.ProductPropertyID, RegexOptions.IgnoreCase).Value); } else { Utility.Utilities.RedirectToErrorPage(); } Page aspxHandler = new Page(); #region Hotels if (isForHotelDetails) { int hotelId; if (int.TryParse(hotelIDString, out hotelId)) { context.RewritePath(context.Request.Path, string.Empty, queryString); string hotelPropertyFriendlyURL = URLHelper.GetResolvedProductDetailsLink(hotelId); if (string.IsNullOrEmpty(hotelPropertyFriendlyURL)) { Utility.Utilities.RedirectToErrorPage(); } if (context.Request.Url.AbsolutePath.ToLower() != hotelPropertyFriendlyURL.ToLower()) { context.Response.StatusCode = (int)HttpStatusCode.MovedPermanently; if (!string.IsNullOrEmpty(queryString)) { hotelPropertyFriendlyURL += ("?" + queryString); } context.Response.AddHeader("Location", hotelPropertyFriendlyURL); } else { aspxHandler = (Page)PageParser.GetCompiledPageInstance( PageLink.ProductDetailsPage, context.Server.MapPath(PageLink.ProductDetailsPage), context); aspxHandler.PreRenderComplete += new EventHandler(AspxPage_PreRenderComplete); aspxHandler.ProcessRequest(context); } } else { Utility.Utilities.RedirectToErrorPage(); } } #endregion else { context.RewritePath(context.Request.Path, string.Empty, queryString); string resolvedUrlPath = string.Empty; if (mode == SEMMode.Products.ToString().ToLower()) { if (urlPageArray[0].ToLower() == defaultPage) { resolvedUrlPath = context.Request.Path; } } else { Utility.Utilities.RedirectToErrorPage(); } if (string.IsNullOrEmpty(resolvedUrlPath)) { Utility.Utilities.RedirectToErrorPage(); } if (context.Request.Url.AbsolutePath.ToLower() != resolvedUrlPath.ToLower()) { context.Response.StatusCode = (int)HttpStatusCode.MovedPermanently; context.Response.AddHeader("Location", resolvedUrlPath); } else { aspxHandler = (Page)PageParser.GetCompiledPageInstance(PageLink.SEMPage, context.Server.MapPath(PageLink.SEMPage), context); aspxHandler.PreRenderComplete += new EventHandler(AspxPage_PreRenderComplete); aspxHandler.ProcessRequest(context); } } }
private IHttpHandler GetCompiledPageInstance(string virtualPath, string inputFile, HttpContext context) { return(PageParser.GetCompiledPageInstance(virtualPath, inputFile, context)); }
/// <summary> /// GetHandler is executed by the ASP.NET pipeline after the associated HttpModules have run. The job of /// GetHandler is to return an instance of an HttpHandler that can process the page. /// </summary> /// <param name="context">The HttpContext for this request.</param> /// <param name="requestType">The HTTP data transfer method (<b>GET</b> or <b>POST</b>)</param> /// <param name="url">The RawUrl of the requested resource.</param> /// <param name="pathTranslated">The physical path to the requested resource.</param> /// <returns>An instance that implements IHttpHandler; specifically, an HttpHandler instance returned /// by the <b>PageParser</b> class, which is the same class that the default ASP.NET PageHandlerFactory delegates /// to.</returns> public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { /* * url= /default.aspx,未包含www等 * */ // log info to the Trace object... //context.Trace.Write("RewriterFactoryHandler", "Entering RewriterFactoryHandler"); url = url.ToLower(); string sendToUrl = url;//User/coloneShop.html string filePath = pathTranslated; string baseLookFor = Config.RewriterConfiguration.GetBaseConfig().LookFor.ToLower(); //如果后台目录就是manage,则不判断,直接返回,这断是为了加快速度,先注释掉 //不行,前台也会需要重写 if (!ReferCheck.ReferChecker(context)) { context.Response.Redirect("~/Prompt/FileNotFound.html"); return(null); } // get the configuration rules //RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules; bool flag = false; //throw (new Exception(url)); // iterate through the rules for (int i = 0; i < Rules.Count; i++) { // Get the pattern to look for (and resolve its URL) string lookFor = "^" + RewriterUtils.ResolveUrl(context.Request.ApplicationPath, Rules[i].LookFor) + "$"; // Create a regular expression object that ignores case... Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); // Check to see if we've found a match if (re.IsMatch(url)) { flag = true; // do any replacement needed,解析后为/manage/default.aspx sendToUrl = RewriterUtils.ResolveUrl(context.Request.ApplicationPath, re.Replace(url, Rules[i].SendTo)); // log info to the Trace object... //context.Trace.Write("RewriterFactoryHandler", "Found match, rewriting to " + sendToUrl); // Rewrite the path, getting the querystring-less url and the physical file path string sendToUrlLessQString; //赋值后: //sendToUrlLessQString=manage/default.aspx //filePath=E:\Code\Zoomla6x\ZoomLa.WebSite\manage\default.aspx RewriterUtils.RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath); // return a compiled version of the page //context.Trace.Write("RewriterFactoryHandler", "Exiting RewriterFactoryHandler"); // log info to the Trace object... return(PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context)); } } //个性化目录就是manage,不判断 //包含/user/manage/此类目录不跳转 //包含/manage/的才会跳转 //-------------这里做原目录的检测判断与限制 if (!flag)//&&!baseLookFor.ToLower().Equals("manage")应该不需要这个判断 { //不管Post的 if (context.Request.HttpMethod.ToLower() == "post") { flag = true; } if (!flag && url.ToLower().IndexOf("/user/") > -1 && url.ToLower().IndexOf("/manage/") > -1 && url.ToLower().IndexOf("/user/") < url.ToLower().IndexOf("/manage/")) { flag = true; } if (!flag && url.ToLower().Contains("/manage/") && !baseLookFor.Equals("manage")) { context.Response.Redirect("~/Prompt/FileNotFound.html"); return(null); } } // if we reached this point, we didn't find a rewrite match //context.Trace.Write("RewriterFactoryHandler", "Exiting RewriterFactoryHandler"); return(PageParser.GetCompiledPageInstance(url, filePath, context)); }
private void ParsePage() { var parser = PageParser.GetParser(); AspxInfo aspx; string dtdHeader; string pageContent = string.Empty; using (var stream = File.OpenText(m_filePath)) { pageContent = stream.ReadToEnd(); } // strip the document type header and any ASP tags pageContent = parser.PreParse(pageContent, out dtdHeader, out aspx); pageContent = pageContent.Trim(); #region Load the code-behind assembly and invoke the page load event // ---------------------------------------------------------------------- var pageType = GetCodeBehindClass(aspx.CodeBehindAssemblyName, aspx.CodeBehindTypeName); if (pageType == null) { throw new TypeLoadException(String.Format("Could not load Page class '{0}!{1}'", aspx.CodeBehindAssemblyName, aspx.CodeBehindTypeName)); } var instance = CreatePageInstance(pageType); instance.DTDHeader = dtdHeader; instance.AspxInfo = aspx; // parse for ASP controls string content; try { content = parser.Parse(instance, pageContent); } catch (System.Xml.XmlException ex) { // TODO: format a custom error screen for this (page parsing issue) throw new HttpException(HttpErrorCode.InternalServerError, ex.Message); } // for page life cycle see http://msdn.microsoft.com/en-us/library/ms178472.aspx instance.CallOnPreInit(EventArgs.Empty); instance.Content = content; instance.IsPostBack = instance.Request.Form["__EVENTTARGET"] != null; IPostBackEventHandler postbackSource = null; string postbackArg = null; if (instance.IsPostBack) { postbackSource = DeterminePostbackSource(instance, out postbackArg); } // TODO: controls raise their OnInit instance.OnInit(EventArgs.Empty); // TODO: load view state (only on postback) instance.CallOnInitComplete(EventArgs.Empty); // TODO: load postback data (only on postback) instance.CallOnPreLoad(EventArgs.Empty); if (aspx.AutoEventWireup) { WireupPage_Load(instance); } instance.OnLoad(EventArgs.Empty); // TODO: controls raise their Load events // TODO: controls raise any other events (click, etc) // TODO: RaisePostBackEvent (only on postback) if (instance.IsPostBack) { instance.RaisePostBackEvent(postbackSource, postbackArg); } instance.CallOnLoadComplete(EventArgs.Empty); instance.OnPreRender(EventArgs.Empty); // TODO: call PreRender() on page controls // TODO: call DataBind() on controls instance.CallOnPreRenderComplete(EventArgs.Empty); // TODO: SaveViewState // Render if (!instance.Response.IsRequestBeingRedirected) { // do not render on a redirect RenderPage(instance); } instance.Response.Flush(); #endregion -------------------------------------------------------------- }
public bool Run() { //Step: find Console.WriteLine("Start getting html data"); string htmlData = DownloadPage(projectName, scriptName, paraStr); // //string path = @"C:\Users\wwu\Desktop\temp.txt"; //string path2 = @"C:\Users\wwu\Desktop\temp2.txt"; //string htmlData = File.ReadAllText(path); Console.WriteLine("Html data get."); //Step: transform Console.WriteLine("Transform html to xml."); //File.WriteAllText(path, htmlData); // xmlData is empty... .Werid, fixed by itself string xmlData = PageParser.ToXml(htmlData); xmlData = xmlData.Replace("xmlns=\"http://www.w3.org/1999/xhtml\"", string.Empty); //File.WriteAllText(path2, xmlData); Console.WriteLine("Transformation from html to xml completed."); //string xmlData = File.ReadAllText(path2); //Step: validate Console.WriteLine("Start to validate xmlData."); _pageContent.LoadXml(xmlData); ////testing xslt //XmlNodeList testnode = _pageContent.SelectNodes(".//descendant::table[@id='viewns_Z7_01A41A42IGEOE0A0S3EDN81020_:mainContent:datatable1']/descendant::tr[position()>3]"); //var nnnnn = (from xTable in xPageContent.Descendants("table") // where xTable.Attribute("id")!=null&&xTable.Attribute("id").Value == "viewns_Z7_01A41A42IGEOE0A0S3EDN81020_:mainContent:datatable1" // select xTable).ToArray(); //XElement[] xtestNodes = (from xTable in xPageContent.Descendants("table") // from xTr in xTable.Descendants("tr") // where xTable.Attribute("id") != null && xTable.Attribute("id").Value == "viewns_Z7_01A41A42IGEOE0A0S3EDN81020_:mainContent:datatable1" // select xTr).Skip(3).ToArray(); //foreach (var xtn in xtestNodes) //{ // string v = xtn.Value; //} if (!ValidateDataForm()) { Console.WriteLine("Invalid Data Form"); return(false); } Console.WriteLine("Valid Data Form."); Console.WriteLine("Start to extract xmlData."); //Step: Extract data and save as output //string csvText = ExtractData(xmlData); _outputPath = _taskSettings.SelectSingleNode("./output").InnerText; string result = ExtractData(xmlData); Console.WriteLine("Extraction Done."); _outputPath = BuildOutput(result); if (string.IsNullOrEmpty(_outputPath)) { return(false); } return(true); }
public void markdown_parsing_does_not_destroy_more_complex_tables() { var sut = new PageParser(Environment.CurrentDirectory + "\\TestDocs\\", "/intranet/"); sut.VirtualPathHandler = OnResolvePath; var actual = sut.ParseString("/", @" System | Alias | Nod 1 | Nod 2 ---------------|-------------------|-----------------|------- **Winservice** | \\blgn165282 | | **Mellanlager** | [AnnoTest](http://AnnoTest) | \\blgn201086 | \\blgn201087 **AnnoCache** | | \\blgn201158 | \\blgn201159 **Databas** | blgn165277 | | System | Alias | Nod 1 | Nod 2 ---------------|-------------------|-----------------|------- **Winservice** | \\blgn165282 | | **Mellanlager** | [AnnoTest](http://AnnoTest) | \\blgn201086 | \\blgn201087 **AnnoCache** | | \\blgn201158 | \\blgn201159 **Databas** | blgn165277 | | "); actual.Body.Should().Contain(@"</table>"); }
public void markdown_parsing_does_not_destroy_tables() { var sut = new PageParser(Environment.CurrentDirectory + "\\TestDocs\\", "/intranet/"); sut.VirtualPathHandler = OnResolvePath; var actual = sut.ParseString("/", @" this | is a | table header ----- | ----- | ---------- *italic* | description | comment **bold** | description | comment ***italic bold*** | description | comment http://autolink.com | yeay | wow "); actual.Body.Should().Contain(@"</table>"); }
public void Dispose() { _parser = null; _semaphore.Dispose(); _service.ConnectionLeaseTimeout = 0; }
public PageParserTests() { pageDownloader = new PageDownloader(); pageParser = new PageParser(pageDownloader); }
internal PageCompiler(PageParser pageParser) : base(pageParser) { _pageParser = pageParser; }
/// <summary> /// 得到处理程序 /// </summary> /// <param name="context">上下文</param> /// <param name="requestType">请求类型</param> /// <param name="url">请求地址</param> /// <param name="pathTranslated">物理地址</param> public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { //UrlRewriteCollection urlrewrite = Common.Configuration.UrlRewrite; string opath; string path = opath = context.Request.Path; string source; //当物理文件存在时,不进行Mvc处理 //if (File.Exists(pathTranslated)) return PageParser.GetCompiledPageInstance(url, pathTranslated, context); foreach (UrlRewirteRule rule in Common.Configuration.UrlRewrite) { source = rule.Source; if (source[0] == '~') { string ap = context.Request.ApplicationPath; if (!ap.EndsWith("/")) { ap += "/"; } source = ap + source.Substring(2); } Regex re = new Regex(source, RegexOptions.IgnoreCase); if (re.IsMatch(path)) { path = re.Replace(path, rule.Object); string query = context.Request.QueryString.ToString(); if (!string.IsNullOrEmpty(query)) { if (path.IndexOf('?') != -1) { path = path + "&" + query; } else { path += "?" + query; } } //if Redirect if (rule.Redirect) { context.Response.StatusCode = 301; context.Response.Status = "301 Moved Permanently"; context.Response.AddHeader("Location", path); context.ApplicationInstance.CompleteRequest(); //直接跳出 return(null); } else { context.Items.Add("_urlrewrite_rewrite_path", path); //重写路径 context.Items.Add("_urlrewrite_source_path", opath); //源路径 string[] ps = path.Split('?'); string fp = context.Request.MapPath(ps[0]); if (ps.Length > 1) { context.RewritePath(ps[0], "", ps[1]); } return(PageParser.GetCompiledPageInstance(ps[0], fp, context)); } } } return(PageParser.GetCompiledPageInstance(url, pathTranslated, context)); }
/// <summary> /// Verifies the input parameters and starts the download. /// </summary> private void startDownloadButton_Click(object sender, EventArgs e) { if (VerifyInputs()) { pageParser = new PageParser(pageURLTextBox.Text); pageParser.Finished += new EventHandler(pageParser_Finished); pageParser.BeginDownloadAndParse(); abortible = pageParser; UpdateUIEnabledState(); } }
public void ProcessRequest(HttpContext httpContext) { //截获请求,判断程序权限 string theUrl = httpContext.Request.ServerVariables["URL"]; int ii = theUrl.IndexOf("/", 1); if (ii < 0) ii = 0; string theUrlTemp = theUrl.Substring(0, ii); string thePath = httpContext.Request.Path; //string theProgramValue = thePath.Substring(theUrlTemp.Length, thePath.Length - theUrlTemp.Length); string theProgramValue = theUrl; string theClientIp = httpContext.Request.UserHostAddress; string requestedUrl = "" ; string targetUrl=""; int urlLength=0; // save requested, target url requestedUrl = httpContext.Request.RawUrl; targetUrl = requestedUrl; // save target url length urlLength = targetUrl.IndexOf("?"); if (urlLength == -1) urlLength = targetUrl.Length; string theUserId = ""; //得到session里面的当前用户,结合上面得到的程序,判断是否有权限访问 userManager theUserManagerTemp = (userManager)httpContext.Session["theUserManager"]; if (theUserManagerTemp == null) { if (targetUrl.Length>14 && targetUrl.Substring(1, 14) != "RmesLogin.aspx") //modified by liuzhy 2013/12/24,这里把参数改了一下(原来是从后往前找,如果带参会不正确),修正了url不对的情况会提示超时。。 { //targetUrl = theUrlTemp + "/Rmes/Login/RmesReLogin.aspx"; //改自动重新登录到默认出错页面 targetUrl = "~/Rmes/Exception/DefaultException.aspx"; urlLength = targetUrl.IndexOf("?"); if (urlLength == -1) urlLength = targetUrl.Length; } } else { theUserId = theUserManagerTemp.getUserId().ToString(); theCompanyCode = theUserManagerTemp.getCompanyCode().ToString(); } //根据这两个值进行判断是否有登录权限,判断逻辑由存储过程完成 string theRetStr = ""; string theRetProgramCode = ""; string theRetProgramName = ""; MW_CHECK_USERRIGHT sp = new MW_CHECK_USERRIGHT() { THECOMPANYCODE1 = theCompanyCode, THEUSERID1 = theUserId, THECLIENTIP1 = theClientIp, THEPROGRAMVALUE1 = theProgramValue, THERETSTR1="", THERETPROGRAMCODE1="", THERETPROGRAMNAME1="" }; Procedure.run(sp); theRetStr = sp.THERETSTR1; theRetProgramCode = sp.THERETPROGRAMCODE1; theRetProgramName = sp.THERETPROGRAMNAME1; //dataConn theDataConn = new dataConn(); //theDataConn.theComd.CommandType = CommandType.StoredProcedure; //theDataConn.theComd.CommandText = "MW_CHECK_USERRIGHT"; //theDataConn.theComd.Parameters.Clear(); //theDataConn.theComd.Parameters.Add("THECOMPANYCODE1", OracleDbType.Varchar2).Value = theCompanyCode; ////theDataConn.theComd.Parameters.Add("@THECOMPANYCODE1", SqlDbType.VarChar).Direction = ParameterDirection.Input; //theDataConn.theComd.Parameters.Add("THEUSERID1", OracleDbType.Varchar2).Value = theUserId; ////theDataConn.theComd.Parameters.Add("@THEUSERCODE1", SqlDbType.VarChar).Direction = ParameterDirection.Input; //theDataConn.theComd.Parameters.Add("THECLIENTIP1", OracleDbType.Varchar2).Value = theClientIp; ////theDataConn.theComd.Parameters.Add("@THEUSERCODE1", SqlDbType.VarChar).Direction = ParameterDirection.Input; //theDataConn.theComd.Parameters.Add("THEPROGRAMVALUE1", OracleDbType.Varchar2).Value = theProgramValue; ////theDataConn.theComd.Parameters.Add("@THEPROGRAMVALUE1", SqlDbType.VarChar).Direction = ParameterDirection.Input; //theDataConn.theComd.Parameters.Add("THERETSTR1", OracleDbType.Varchar2, 50).Direction = ParameterDirection.Output; //theDataConn.theComd.Parameters.Add("THERETPROGRAMCODE1", OracleDbType.Varchar2, 50).Direction = ParameterDirection.Output; //theDataConn.theComd.Parameters.Add("THERETPROGRAMNAME1", OracleDbType.Varchar2, 50).Direction = ParameterDirection.Output; //theDataConn.OpenConn(); //theDataConn.theComd.ExecuteNonQuery(); //theRetStr = theDataConn.theComd.Parameters["THERETSTR1"].Value.ToString(); //theRetProgramCode = theDataConn.theComd.Parameters["THERETPROGRAMCODE1"].Value.ToString(); //theRetProgramName = theDataConn.theComd.Parameters["THERETPROGRAMNAME1"].Value.ToString(); //theDataConn.CloseConn(); //根据返回数据判断,进行不同的处理 switch (theRetStr) { case "0": //无需授权访问,只是继续请求,不做任何处理,包括未定义的程序,定义为无需授权的程序,比如登录和一些公用查询程序 //保证登录程序的顺利执行,在session里面传递公司号过去 try { httpContext.Session["theCompanyCode"] = theCompanyCode; httpContext.RewritePath(targetUrl); IHttpHandler handler = PageParser.GetCompiledPageInstance( targetUrl.Substring(0, urlLength), null, httpContext); handler.ProcessRequest(httpContext); } catch (Exception ex) { } break; case "1": //没有权限,终止请求 //httpContext.Response.StatusCode = 400; //httpContext.Response.StatusDescription = "你没有访问权限,请联系系统管理员!"; //映射到错误处理界面 targetUrl = "~/Rmes/Exception/DefaultException.aspx"; urlLength = targetUrl.IndexOf("?"); if (urlLength == -1) urlLength = targetUrl.Length; httpContext.Session["theCompanyCode"] = theCompanyCode; httpContext.RewritePath(targetUrl); IHttpHandler handler2 = PageParser.GetCompiledPageInstance( targetUrl.Substring(0, urlLength), null, httpContext); handler2.ProcessRequest(httpContext); break; case "2": //有权限访问,更新当前会话的程序号和程序名称信息 theUserManagerTemp.setProgCode(theRetProgramCode); theUserManagerTemp.setProgName(theRetProgramName); httpContext.Session["theUserManager"] = theUserManagerTemp; //try //{ httpContext.RewritePath(targetUrl); IHttpHandler handler1 = PageParser.GetCompiledPageInstance( targetUrl.Substring(0, urlLength), null, httpContext); //IHttpHandler handler1 = PageParser.GetCompiledPageInstance(thePath, null, httpContext); handler1.ProcessRequest(httpContext); //} //catch //{ // httpContext.RewritePath(targetUrl.Substring(0, urlLength)); // IHttpHandler handler1 = PageParser.GetCompiledPageInstance( // targetUrl.Substring(0, urlLength), null, httpContext); // handler1.ProcessRequest(httpContext); //} break; default: //没有权限,终止请求 httpContext.Response.StatusCode = 400; httpContext.Response.StatusDescription = "你没有访问权限,请联系系统管理员!"; break; } }
/// <summary> /// Returns an instance of a class that implements the <see cref="T:System.Web.IHttpHandler"></see> interface. /// </summary> /// <param name="context">An instance of the <see cref="T:System.Web.HttpContext"></see> class that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> /// <param name="requestType">The HTTP data transfer method (GET or POST) that the client uses.</param> /// <param name="url">The <see cref="P:System.Web.HttpRequest.RawUrl"></see> of the requested resource.</param> /// <param name="pathTranslated">The <see cref="P:System.Web.HttpRequest.PhysicalApplicationPath"></see> to the requested resource.</param> /// <returns> /// A new <see cref="T:System.Web.IHttpHandler"></see> object that processes the request. /// </returns> public IHttpHandler GetHandler( HttpContext context, string requestType, string url, string pathTranslated) { if (url.EndsWith("logout.aspx")) { FormsAuthentication.SignOut(); context.Response.Redirect(CMSContext.Current.ResolveUrl("~")); context.Response.End(); } string outline = url; if (CMSContext.Current.AppPath.Length != 1) { outline = outline.Substring(CMSContext.Current.AppPath.Length); } // Outline must start with "/", so add it if we are missing if (!outline.StartsWith("/", StringComparison.Ordinal)) { outline = "/" + outline; } // Set the outline CMSContext.Current.Outline = outline; // Load current outline bool isFolder = false; int folderId = 0; int pageId = -1; string masterFile = String.Empty; string cacheKey = CmsCache.CreateCacheKey("filetree", CMSContext.Current.SiteId.ToString(), outline); DataTable fileTreeItemTable = null; // check cache first object cachedObject = CmsCache.Get(cacheKey); if (cachedObject != null) { fileTreeItemTable = (DataTable)cachedObject; } if (fileTreeItemTable == null) { lock (CmsCache.GetLock(cacheKey)) { cachedObject = CmsCache.Get(cacheKey); if (cachedObject != null) { fileTreeItemTable = (DataTable)cachedObject; } else { fileTreeItemTable = FileTreeItem.GetItemByOutlineAllDT(outline, CMSContext.Current.SiteId); if (fileTreeItemTable.Rows.Count > 0) { CmsCache.Insert(cacheKey, fileTreeItemTable, CmsConfiguration.Instance.Cache.PageDocumentTimeout); } } } } if (fileTreeItemTable != null && fileTreeItemTable.Rows.Count > 0) { DataRow row = fileTreeItemTable.Rows[0]; folderId = (int)row[0]; CMSContext.Current.PageId = folderId; isFolder = (bool)row[4]; masterFile = (string)row[8]; if (String.Compare(outline, (string)row[2], true, CultureInfo.InvariantCulture) == 0) { pageId = (int)row[0]; } } /* * using (IDataReader reader = FileTreeItem.GetItemByOutlineAll(outline, CMSContext.Current.SiteId)) * { * if (reader.Read()) * { * folderId = reader.GetInt32(0); * CMSContext.Current.PageId = folderId; * isFolder = reader.GetBoolean(4); * masterFile = reader.GetString(8); * if (String.Compare(outline, reader.GetString(2), true, CultureInfo.InvariantCulture) == 0) * { * pageId = reader.GetInt32(0); * } * } * * reader.Close(); * } * */ //if (FileTreeItem.IsVirtual(outline)) if (pageId != -1) // is this folder/page virtual? { // If URL is not rewritten, then save the one originally requested if (!CMSContext.Current.IsUrlReWritten) { CMSContext.Current.IsUrlReWritten = true; if (HttpContext.Current.Request.QueryString.Count == 0) { CMSContext.Current.CurrentUrl = url; } else { CMSContext.Current.CurrentUrl = String.Format(url + "?" + HttpContext.Current.Request.QueryString); } } /* * bool isFolder = false; * int folderId = 0; * string masterFile = String.Empty; * * using (IDataReader reader = FileTreeItem.GetItemByOutlineAll(outline, CMSContext.Current.SiteId)) * { * if (reader.Read()) * { * folderId = reader.GetInt32(0); * isFolder = reader.GetBoolean(4); * masterFile = reader.GetString(8); * } * } * */ if (!isFolder) { Uri rawUrl = BuildUri(String.Format("~/template.aspx"), HttpContext.Current.Request.IsSecureConnection); string filePath; string sendToUrlLessQString; string sendToUrl = rawUrl.PathAndQuery; RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath); return(PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context)); } else { string newUrl = String.Empty; //try to find default page for folder using (IDataReader reader = FileTreeItem.GetFolderDefaultPage(folderId)) { if (reader.Read()) { newUrl = Mediachase.Commerce.Shared.CommerceHelper.GetAbsolutePath(reader.GetString(2)); } else { reader.Close(); throw new HttpException(204, "Default page for folder not found"); } reader.Close(); } Uri rawUrl = BuildUri(newUrl, HttpContext.Current.Request.IsSecureConnection); string filePath; string sendToUrlLessQString; string sendToUrl = rawUrl.PathAndQuery; RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath); return(PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context)); } } else if (!string.IsNullOrEmpty(pathTranslated)) { return(PageParser.GetCompiledPageInstance(url, pathTranslated, context)); } else { return (PageParser.GetCompiledPageInstance( url, context.Server.MapPath("~"), context)); } }
public virtual void ExportGrid2Excel(HttpContext context) { YZRequest request = new YZRequest(context); bool dynamicParams = request.GetBool("dynamicParams", false); //获得数据 - jsonResponse string jsonResponse; JObject jRequest = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(context.Request.Params["request"]))); string url = (string)jRequest["url"]; url = "~/" + url; JToken jToken; using (StringWriter sw = new StringWriter()) { HtmlTextWriter writer = new HtmlTextWriter(sw); HttpResponse response = new HttpResponse(writer); List <string> queryParams = new List <string>(); queryParams.Add("DateFormat=text"); foreach (KeyValuePair <string, JToken> property in (jRequest["params"] as JObject)) { queryParams.Add(property.Key + "=" + HttpUtility.UrlEncode((string)property.Value, Encoding.Default)); } HttpRequest callrequest = new HttpRequest(null, context.Request.Url.ToString(), String.Join("&", queryParams.ToArray())); HttpContext callcontext = new HttpContext(callrequest, response); IHttpHandler handler = PageParser.GetCompiledPageInstance(url, context.Server.MapPath(url), context); handler.ProcessRequest(callcontext); jsonResponse = sw.ToString(); jToken = JToken.Parse(jsonResponse); if (jToken is JObject) { JObject jObject = jToken as JObject; if (jObject["success"] != null && jObject["success"].Type == JTokenType.Boolean && (bool)jObject["success"] == false) { throw new Exception((string)jObject["errorMessage"]); } } } //将数据转化为Table DataTable table; string rootProperty = request.GetString("rootProperty", null); JArray jTable; if (String.IsNullOrEmpty(rootProperty)) { jTable = jToken as JArray; } else { jTable = (jToken as JObject)[rootProperty] as JArray; } foreach (JObject jRow in jTable) { foreach (KeyValuePair <string, JToken> jProperty in jRow) { if (jProperty.Value is JArray) { jRow[jProperty.Key] = Convert.ToString(jProperty.Value); } if (jProperty.Value is JObject) { jRow[jProperty.Key] = Convert.ToString(jProperty.Value); } } } table = jTable.ToObject <DataTable>(); table.TableName = "GridStore"; //SQL Server数据库中monery4位小数点处理 foreach (DataRow row in table.Rows) { foreach (DataColumn column in table.Columns) { object value = row[column]; if (value is decimal) { value = (decimal)Decimal.ToDouble((decimal)value); } row[column] = value; } } //生成Excel string templateExcel = request.GetString("templateExcel", String.Empty); HSSFWorkbook book = null; if (String.IsNullOrEmpty(templateExcel)) { book = this.NoTemplateExport(context, table); } else { if (!templateExcel.StartsWith("~/")) { templateExcel = "~/" + templateExcel; } Dictionary <string, string> reportParams = new Dictionary <string, string>(); //获得查询参数 foreach (KeyValuePair <string, JToken> property in (jRequest["params"] as JObject)) { reportParams.Add(property.Key, (string)property.Value); } if (!reportParams.ContainsKey("Kwd")) { reportParams.Add("Kwd", ""); } if (dynamicParams) { string strRuntimeParams = reportParams["params"]; if (!String.IsNullOrEmpty(strRuntimeParams)) { YZClientParamCollection runtimeParams = JArray.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(strRuntimeParams))).ToObject <YZClientParamCollection>(); foreach (YZClientParam clientParams in runtimeParams) { reportParams[clientParams.name] = Convert.ToString(clientParams.value); } } } //打开文件 using (FileStream file = new FileStream(context.Server.MapPath(templateExcel), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { book = new HSSFWorkbook(file); } DataSet dataset = new DataSet(); dataset.Tables.Add(table); YZExcelGenerate.Fill(book, reportParams, dataset); YZExcelGenerate.PrepareForOutput(book); } //Excel文件保存到流 byte[] bytes; using (MemoryStream ms = new MemoryStream()) { book.Write(ms); bytes = ms.ToArray(); } //导出文件名 string fileName = context.Request.Params["fileName"]; if (String.IsNullOrEmpty(fileName)) { fileName = "Export"; } fileName += YZStringHelper.DateToString(DateTime.Now) + ".xls"; this.ProcessResponseHeader(context, fileName, true); context.Response.BinaryWrite(bytes); //this.OnExported(context, table); //设置Response头 //context.Response.Clear(); //context.Response.ContentType = "application/vnd.ms-excel"; //context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //context.Response.AppendHeader("Content-Length", bytes.Length.ToString()); //context.Response.BinaryWrite(bytes); //context.Response.End(); }
private void parseAnswer(Stream _stream, string _pageName, string _fileName) { HtmlAgilityPack.HtmlDocument document; HtmlNode node; System.Text.Encoding locEncoding = Encoding.Default; StreamReader _Answer = new StreamReader(_stream, System.Text.Encoding.UTF8);//locEncoding); document = new HtmlAgilityPack.HtmlDocument(); document.Load(_stream); node = document.DocumentNode; parser = new PageParser(node); parser.FileName = _fileName; //parser.saveToFile(saveFileName); parser.traverse("top", node); closeResponse(); }
public IHttpHandler GetHandler(HttpContext context, string requestType, string url1, string pathTranslated) { if (HttpContext.Current.Request.Url.Query != String.Empty) { if (context.Request.Url.Query.Length > 0) { context.Items["VirtualUrl"] = context.Request.Path + context.Request.Url.Query; } } if (context.Items["VirtualUrl"] == null) { context.Items["VirtualUrl"] = context.Request.Path; } string url = context.Request.Url.AbsolutePath; while (url.EndsWith("/") && !string.IsNullOrEmpty(url)) { url = url.Substring(0, url.Length - 1); } if (url == "/blank.aspx") { url = context.Request.Url.Query.ToLower(); url = url.Replace("/:80", ""); string host = "?404;" + ConfigurationManager.AppSettings["SITE_URL"].ToLower(); url = url.Substring(url.IndexOf(host) + host.Length); if (!url.StartsWith("/")) { url = "/" + url; } } string rewrite = ""; RewriteRules rewriteRules = RewriteRules.GetCurrentRewriteRules(); rewrite = rewriteRules.GetMatchingRewrite(url); if (!string.IsNullOrEmpty(rewrite)) { if (context.Request.Url.AbsolutePath == "/blank.aspx") { rewrite = rewrite.Replace("/:80", ""); } context.RewritePath("~" + rewrite + (context.Request.Url.Query.Length > 0 ? "?" + HttpContext.Current.Request.Url.Query.Replace("?", "") : "")); } else { if (context.Request.Url.AbsolutePath == "/blank.aspx") { rewrite = "/error.aspx?status=404"; } else { rewrite = context.Request.Path + context.Request.Url.Query; } } string newFilePath = rewrite.IndexOf("?") > 0 ? rewrite.Substring(0, rewrite.IndexOf("?")) : rewrite; return(PageParser.GetCompiledPageInstance(newFilePath, context.Server.MapPath(newFilePath), context)); }
public PageParserTests() { parser = new PageParser(); }
public static Type CompilePageType(PageParser pageParser) { PageCompiler compiler = new PageCompiler(pageParser); return(compiler.GetCompiledType()); }
internal PageCodeDomTreeGenerator(PageParser pageParser) : base(pageParser) { _pageParser = pageParser; }
private void PostResolveRequestCache(HttpContext context) { System.Diagnostics.Debug.Write("\r\n PostResolveRequestCache -- " + context.Request.Path); var cacheItem = context.Items[_requestCacheItemKey] as LoadRouteResult; RouteInfoNavigate nav = cacheItem != null ? cacheItem.Navigate : null; if (nav != null) { string relative = context.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + context.Request.PathInfo; var destinationPage = nav.Info.DestinationPage; string[] partsRelative = relative.Split('/'); string[] partsNav = nav.FullName.Split('/'); string[] extensions = nav.Info.Path.Split(','); string pageName = null; string pageExtension = null; string part = partsRelative[partsRelative.Length - 1]; if (string.IsNullOrEmpty(part)) { if (Array.FindIndex(extensions, delegate(string s) { return(s == "*"); }) < 0) { return; } pageName = ""; } else { System.IO.FileInfo fi = new System.IO.FileInfo(partsRelative[partsRelative.Length - 1]); pageExtension = fi.Extension; if (partsRelative[partsRelative.Length - 1].IndexOf('.') >= 0) { int i = 0; if (extensions.Length >= 1 && extensions[0] != "*") { for (i = 0; i < extensions.Length; i++) { if (string.Compare(fi.Extension.Substring(1), extensions[i].Trim(), true) == 0) { break; } } if (i == extensions.Length) { return; } } if (fi.Extension.Length > 0) { pageName = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length); } else { pageName = fi.Name; } } else { int i = 0; for (i = 0; i < extensions.Length; i++) { if (extensions[i].Trim() == "*") { break; } } if (i == extensions.Length) { return; } pageName = fi.Name; } } var routeParameters = new System.Collections.Specialized.NameValueCollection(); int count = (pageName == null ? partsRelative.Length : (partsRelative.Length - 1)); for (int i = partsNav.Length; i < count; i++) { routeParameters.Add("param" + (i - partsNav.Length), partsRelative[i]); } if (pageName != null) { routeParameters.Add("pageName", pageName); } if (pageExtension != null) { routeParameters.Add("pageExtension", pageExtension); } if (nav.ComplexPartsCount > 0) { try { var match = System.Text.RegularExpressions.Regex.Match(relative, nav.ComplexRegex, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.ExplicitCapture); if (match.Success) { for (int i = 0; i < nav.ComplexPartsCount; i++) { routeParameters.Add("complex" + i, match.Groups["complex" + i].Value); } } } catch (Exception ex) { throw new Exception(string.Format("Fail on process complex route for {0}.", relative), ex); } } var originalQueryStringParameters = new System.Collections.Specialized.NameValueCollection(); foreach (var i in context.Request.QueryString.AllKeys) { originalQueryStringParameters.Add(i, context.Request.QueryString[i]); } var qsPos = destinationPage.IndexOf('?'); if (qsPos >= 0) { var queryStringItems = destinationPage.Substring(qsPos + 1).Split('&'); foreach (var i in queryStringItems) { var pos1 = i.IndexOf('='); if (pos1 > 0) { routeParameters.Add(i.Substring(0, pos1), i.Substring(pos1 + 1)); } } destinationPage = destinationPage.Substring(0, qsPos); } var virtualPath = ""; RouteActionResponse actionResponse = null; var isCompiled = nav.Info.Compiled; var startPos = 0; if (nav.Info.IsDirectory) { var relativeDir = relative.Length > cacheItem.FormattedLocation.Length ? relative.Substring(cacheItem.FormattedLocation.Length) : relative; startPos = 0; if (!string.IsNullOrEmpty(relativeDir)) { while (startPos < relativeDir.Length && relativeDir[startPos] == '/' || relativeDir[startPos] == '\\') { startPos++; } } destinationPage += (startPos > 0 ? relativeDir.Substring(startPos) : relativeDir); } startPos = 0; if (!string.IsNullOrEmpty(destinationPage)) { while (startPos < destinationPage.Length && destinationPage[startPos] == '/' || destinationPage[startPos] == '\\') { startPos++; } } destinationPage = (startPos > 0 ? destinationPage.Substring(startPos) : destinationPage); if (!isCompiled) { virtualPath = "~/" + destinationPage; actionResponse = nav.Info.PreLoad(new RouteActionRequest { DestinationVirtualPath = virtualPath, OriginalQueryString = originalQueryStringParameters, RouteParameters = routeParameters, Context = context }); if (actionResponse != null && !string.IsNullOrEmpty(actionResponse.RedirectVirtualPath)) { isCompiled = actionResponse.Compiled; virtualPath = actionResponse.RedirectVirtualPath; } } else { virtualPath = "~/" + destinationPage; actionResponse = nav.Info.PreLoad(new RouteActionRequest { DestinationVirtualPath = virtualPath, OriginalQueryString = originalQueryStringParameters, RouteParameters = routeParameters, Context = context }); if (actionResponse != null && !string.IsNullOrEmpty(actionResponse.RedirectVirtualPath)) { isCompiled = actionResponse.Compiled; virtualPath = actionResponse.RedirectVirtualPath; } } IHttpHandler ret = null; var inputFile = context.Server.MapPath(virtualPath); if (actionResponse != null && actionResponse.RouteHandler != null) { ret = actionResponse.RouteHandler; } else if (!isCompiled) { ret = new NotCompiledRedirect { Filename = inputFile } } ; else if (string.Compare(System.IO.Path.GetExtension(inputFile), ".asmx", true) == 0) { ret = (new System.Web.Services.Protocols.WebServiceHandlerFactory()).GetHandler(context, "*", virtualPath, inputFile); } else { ret = PageParser.GetCompiledPageInstance(virtualPath, inputFile, context); } RequestData data2 = new RequestData(); data2.OriginalPath = context.Request.Path; data2.OriginalQueryStringParameters = originalQueryStringParameters; data2.HttpHandler = ret; data2.RouteParameters = routeParameters; data2.Route = nav.Info; context.Items[_requestDataKey] = data2; context.RewritePath("~/Route.axd"); } }
public void parse_code_blocks() { var sut = new PageParser(Environment.CurrentDirectory + "\\TestDocs\\", "/intranet/"); sut.VirtualPathHandler = OnResolvePath; var actual = sut.ParseString("/", @" ```csharp for (int i = 0; i < 10; ++i) { } ``` "); actual.Body.Should().Contain(@"code data-lang=""csharp"" class=""language-csharp"""); }
public IHttpActionResult UpdateDb() { String Answer = "Nothing"; try { List <String> Urls = new List <string>(); List <String> Animals = new List <string>() { "dogs", "cats", "birds", "rodents", "reptile", "fish", "insects", "horses", "Домашний-скот", "exotic" }; using (var Parser = new PageParser()) { List <String> Types = new List <string>() { "sale", "buy", "love", "free" }; List <DataLayer.Post> ParsedPosts = new List <DataLayer.Post>(); foreach (String AnimalPage in Animals) { foreach (String Type in Types) { using (var ZooParser = new Content.ZooParser()) { Parser.OnParsed = (String ParsedUrl) => { if (ParsedUrl != "") { DataLayer.DealType Dt = DataLayer.DealType.All; Enum.TryParse <DataLayer.DealType>(Type, out Dt); DataLayer.Animal An = DataLayer.Animal.All; Enum.TryParse <DataLayer.Animal>(AnimalPage, out An); ZooParser.OnParsed = (DataLayer.Post ParsedPost) => { ParsedPosts.Add(ParsedPost); }; if (AnimalPage != "Домашний-скот") { ZooParser.ParsePost(ParsedUrl, An, Dt); } else { ZooParser.ParsePost(ParsedUrl, DataLayer.Animal.Home, Dt); } } }; Parser.ParsePage("http://www.zoo-zoo.ru/" + AnimalPage + "/" + Type + "/"); } } } DataLayer.DataManager.AddList = ParsedPosts; Answer = "True"; } } catch (Exception ex) { Console.WriteLine(ex.Message); Answer = "False"; } return(Ok(Answer)); }
/// <summary> /// 通过实现 IHttpHandler 接口的自定义 HttpHandler 启用 HTTP Web 请求的处理。 /// </summary> /// <param name="context">HttpContext 对象,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session 和 Server)的引用。</param> public override void ProcessRequest(System.Web.HttpContext context) { base.ProcessRequest(context); string path = context.Server.MapPath(this.RealPath); if (System.IO.File.Exists(path)) { switch (this.Config.Action) { case "none": PageParser.GetCompiledPageInstance(this.RealPath, path, context).ProcessRequest(context); break; case "rewrite": context.RewritePath(context.Request.Path, this.PathInfo, this.QueryString); PageParser.GetCompiledPageInstance(this.RealPath, path, context).ProcessRequest(context); break; case "cache_static": //一般静态文件缓存. case "cache_static_news": //新闻查看静态文件缓存 case "cache_static_pub": //蒲点查看静态文件缓存 string filePath = context.Server.MapPath(this.FilePath); FileInfo fileInfo = new FileInfo(filePath); FileInfo fileInfoSource = new FileInfo(path); bool toRebuild = false; if (!fileInfo.Exists || (this.Config.TimeSpan.Ticks > 0 && fileInfo.LastWriteTime.Add(this.Config.TimeSpan) < DateTime.Now) || fileInfoSource.LastWriteTime > fileInfo.LastWriteTime) { toRebuild = true; } if (toRebuild) { context.RewritePath(context.Request.Path, this.PathInfo, this.QueryString); switch (this.Config.Action) { case "cache_static_news": #region 新闻查看静态文件缓存 /*long newsId = 0; * if (context.Request.QueryString["newsId"] != null && * context.Request.QueryString["newsId"].Length != 0) * { * newsId = Convert.ToInt64(context.Request.QueryString["newsId"] , 10); * * } * DataModel.News.News newsItem = null; * * if (context.Items[News.PageBaseNewsView.CONTEXT_KEY_NEWS] == null && * newsId > 0) * { * context.Items[News.PageBaseNewsView.CONTEXT_KEY_NEWS] = DAL.News.News.Get(newsId); * } * * newsItem = context.Items[News.PageBaseNewsView.CONTEXT_KEY_NEWS] as DataModel.News.News; * * if (newsItem == null) * { * context.Response.Redirect(this.Error404Url , true); * } * * if (newsItem != null && * newsItem.CheckState == DataModel.News.CheckState.Passed && * newsItem.IsView) * { * PageParser.GetCompiledPageInstance(this.RealPath , path , context).ProcessRequest(context); * * this.SetStaticFilter(context , filePath); * return; * } * else * { * context.Response.Redirect(this.Error404Url , true); * return; * }*/ #endregion case "cache_static": default: #region 一般静态文件缓存 PageParser.GetCompiledPageInstance(this.RealPath, path, context).ProcessRequest(context); this.SetStaticFilter(context, filePath); return; #endregion } } else { //直接调用静态文件 context.Response.WriteFile(filePath); context.Response.End(); } break; } } else { context.Response.Redirect(this.Error404Url, true); } }