/// <summary> /// 启动预览 /// </summary> public void Run() { TcpListener listener = ClientService.CreatTcpListener(); Port = ClientService.Port; Jeelu.SimplusD.PathService.Initialize(Application.StartupPath); while (true) { TcpClient client = listener.AcceptTcpClient(); NetworkStream ns = client.GetStream(); StreamReader srs = new StreamReader(ns); string received = ""; //接收的GET请求 received += srs.ReadLine(); //获得GET请求 this.Index = StringService.GetRequest(received); //获取请求字符川 string resquestType = StringService.GetRequestType(this.Index); //获取请求类型 //如果发送php和shtml请求,说明sdsite被改编重新打开sdsite实例,则重新打开sdsite if (resquestType == "php" || resquestType == "shtml" || string.IsNullOrEmpty(resquestType)) { ///SdsiteService.OpenSdsite(PathService.AbsolutePath); SiteResourceService.Initialize (null, GetResourceAbsolutePath.GetResourceAbsPath, GetResourceAbsolutePath.GetResourcePath, GetResourceAbsolutePath.GetResourceUrl, null); } //处理除不同类型请求 HttpType(this.Index, resquestType, ns); } }
/// <summary> /// 处理shtml请求 /// </summary> public static void ShtmlHeadBody(string index, Stream ns) { try { string pageId = PathService.GetPageGuid(index); if (!string.IsNullOrEmpty(pageId)) { string shtmlContent = TmpltAndPageService.GetPageContent(pageId); //获取shtml中include所有内容 string strInclude = RegexService.GetShtmlInclude(shtmlContent); //获取引号中所有内容 string quoMarkContent = StringService.GetQuotationMarkComment(strInclude); HttpHeadInfo ht = new HttpHeadInfo(); string content = ConstService.PUBlISHDTD; ht.Content_Type = "text/html"; string path = StringService.DeleteQuest(quoMarkContent); string tmpltId = PathService.GetAnyUrlGuid(path); content += TmpltAndPageService.GetTmpltContent(tmpltId); content = RegexService.ChangeShtmlTag(content, quoMarkContent); if (quoMarkContent.Contains("keywordList=")) { content = ChangeKeyList(content, tmpltId, pageId); } //判断其页面是否含有正文 if (quoMarkContent.Contains("content=")) { string contentId = RegexService.GetContentId(quoMarkContent); //string contentId = PathService.GetContentId(contentPath); //将正文型内容插到模板中 if (TmpltAndPageService.IsContentElement(contentId)) { string strChange = TmpltAndPageService.GetContentPageContent(contentId); //先运行将正文部分替换 content = RegexService.ChangeContent(content, strChange); } //再将其他页面片替换 content = PhpBody(content, tmpltId); } else { content = PhpBody(content, tmpltId); content += ConstService.HTMLUTF8END; } int leng = Encoding.UTF8.GetByteCount(content); ht.Content_Length = leng.ToString(); string shtmlHeadBody = ConstService.OK + ht.GetHttpHead() + content; byte[] shtmlBytes = Encoding.UTF8.GetBytes(shtmlHeadBody); ns.Write(shtmlBytes, 0, shtmlBytes.Length); ns.Flush(); ns.Close(); } else { WrongHeadBody(ns); } } catch (Exception e) { ExceptionService.CreateException(e); } }