コード例 #1
0
        public void Handle(string map, string filePath, bool enableScript, bool isIncludePage)
        {
#if DEBUG
            Xy.Tools.Debug.Log.WriteEventLog("start page:" + map);
#endif
            string _staticCacheDir = string.Empty, _staticCacheFile = string.Empty, _staticCachePath = string.Empty;
            if (_threadEntity.URLItem.EnableCache && !isIncludePage)
            {
                _staticCacheDir  = WebSetting.CacheDir + "PageCache\\" + _threadEntity.URL.Dir.Replace('/', '\\');
                _staticCacheFile = filePath + (_threadEntity.URL.HasParam ? _threadEntity.URL.Param.Replace('?', '#') : string.Empty);
                _staticCachePath = _staticCacheDir + _staticCacheFile + ".xycache";
                if (!UpdateCache(_staticCachePath, DateTime.Now))
                {
                    if (System.IO.File.Exists(_staticCachePath))
                    {
                        DateTime _modifiedTime = System.IO.File.GetLastWriteTime(_staticCachePath);
                        if (Xy.Tools.IO.File.IsClientCached(_request.Headers["If-Modified-Since"], _modifiedTime) && !_webSetting.DebugMode)
                        {
                            _response.StatusCode      = 304;
                            _response.SuppressContent = true;
                        }
                        else
                        {
                            _response.Cache.SetLastModified(DateTime.Now);
                            if (_threadEntity.URLItem.Age.TotalMinutes > 0)
                            {
                                _response.Cache.SetMaxAge(_threadEntity.URLItem.Age);
                                _response.Cache.SetExpires(DateTime.Now.Add(_threadEntity.URLItem.Age));
                                _response.Expires         = Convert.ToInt32(_threadEntity.URLItem.Age.TotalMinutes);
                                _response.ExpiresAbsolute = DateTime.Now.Add(_threadEntity.URLItem.Age);
                                _response.AddHeader("Cache-Control", "max-age=" + _threadEntity.URLItem.Age.TotalMinutes);
                            }
                            _htmlContainer.Write(System.IO.File.ReadAllBytes(_staticCachePath));
                        }
                        return;
                    }
                }
            }

            onGetRequest();
#if DEBUG
            Xy.Tools.Debug.Log.WriteEventLog(map + " page process:onGetRequest finished");
#endif
            Validate();
#if DEBUG
            Xy.Tools.Debug.Log.WriteEventLog(map + " page process:Validate finished");
#endif
            string _sourcefile = string.Empty;
            if (!string.IsNullOrEmpty(filePath))
            {
                _sourcefile = LoadSourceFile((isIncludePage ? _webSetting.IncludeDir : _webSetting.PageDir) + filePath);
#if DEBUG
                Xy.Tools.Debug.Log.WriteEventLog(map + " page process:LoadSourceFile finished");
#endif
            }
            Control.ControlAnalyze _controls = Cache.PageAnalyze.GetInstance(_threadEntity, this, map);
            if (!_controls.IsHandled || WebSetting.DebugMode)
            {
                if (!string.IsNullOrEmpty(_sourcefile))
                {
                    _content = System.IO.File.ReadAllBytes(_sourcefile);
                    _controls.SetContent(_content);
                }
            }
            if (_contentChanged)
            {
                _controls.SetContent(_content);
            }
            if (enableScript)
            {
                if (!_controls.IsHandled)
                {
                    _controls.Analyze();
                }
                HandleControl(_controls.ControlCollection);
            }

#if DEBUG
            Xy.Tools.Debug.Log.WriteEventLog(map + " page process:HandleControl finished");
#endif
            _controls.Handle(this, _htmlContainer);
#if DEBUG
            Xy.Tools.Debug.Log.WriteEventLog(map + " controls handled");
#endif
            OutputHtml(_htmlContainer);
#if DEBUG
            Xy.Tools.Debug.Log.WriteEventLog(map + " page process:OutputHtml finished");
#endif

            if (_threadEntity.URLItem.EnableCache && !isIncludePage)
            {
                Xy.Tools.IO.File.ifNotExistsThenCreate(_staticCachePath);
                using (System.IO.FileStream fs = new System.IO.FileStream(_staticCachePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Read)) {
                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs)) {
                        try {
                            sw.Write(_htmlContainer.ToString());
                            sw.Flush();
                        } finally {
                            sw.Close();
                            fs.Close();
                        }
                    }
                }
#if DEBUG
                Xy.Tools.Debug.Log.WriteEventLog(map + " page cache writed");
#endif
            }
#if DEBUG
            Xy.Tools.Debug.Log.WriteEventLog(map + " end");
#endif
        }