private byte[] GetFileContent(HttpContext context, string virtualPath) { byte[] content = null; try { JContext jc = JContext.Current; IArea site = jc.Area; string path = virtualPath; if (path.IndexOf("_res.aspx") == -1) { // 不是样式也不是脚本文件,返回null if (!path.EndsWith(".css", StringComparison.InvariantCultureIgnoreCase) && !path.EndsWith(".js", StringComparison.InvariantCultureIgnoreCase)) { return(null); } int index = path.IndexOf(site.VirtualPath); if (index != -1) { path = path.Substring(index + site.VirtualPath.Length); } if (path.StartsWith("themes", StringComparison.InvariantCultureIgnoreCase)) { path = path.Substring(6); path = string.Concat(VirtualPathUtility.ToAbsolute(jc.url(site.ThemeRoot)), path); } else if (index != -1) { path = StringUtil.CombinUrl(site.VirtualPath, path); } virtualPath = StringUtil.CombinUrl("/", path); string physicalPath = context.Server.MapPath(virtualPath); content = File.ReadAllBytes(physicalPath); } else { NameValueCollection qs = new NameValueCollection(); foreach (var item in new Url(virtualPath).GetQueries()) { qs[item.Key] = item.Value; } content = ResourceController.GetResourceContent(qs); } } catch (Exception ex) { logger.Error("file: {0} is not found. {1}", virtualPath, ExceptionUtil.WriteException(ex)); } if (content == null) { logger.Error("file: {0} is null.", virtualPath); return(null); } // remove bom byte if (content.Length <= 3 || !(content[0] == 0xEF && content[1] == 0xBB && content[2] == 0xBF)) { return(content); } using (var ms = new MemoryStream()) { ms.Write(content, 3, content.Length - 3); return(ms.ToArray()); } }
protected void RerouteRequest(HttpApplication app, string newPath, NameValueCollection qs, NameValueCollection incomingQS) { JContext jc = JContext.Current; // signal to the future page handler that we rerouted from a different URL HttpContext.Current.Items.Add(kCONTEXTITEMS_RAWURLKEY, HttpContext.Current.Request.RawUrl); HttpContext.Current.Items.Add(kCONTEXTITEMS_ADDEDQSKEY, qs); NameValueCollection urlQueryString = null; if (newPath.Contains("?")) { Url url = new Url(newPath); if (StringUtil.HasText(url.Query)) { urlQueryString = StringUtil.CommaDelimitedEquation2NVCollection(url.Query); urlQueryString.Remove("kissMasterFile"); } } // apply the querystring to the path newPath = ApplyQueryString(newPath, qs); jc.QueryString.Clear(); jc.QueryString.Add(qs); if (urlQueryString != null && urlQueryString.HasKeys()) { jc.QueryString.Add(urlQueryString); } // if configured, apply the incoming query string variables too if (_qsBehavior == IncomingQueryStringBehavior.PassThrough) { NameValueCollection _qs = new NameValueCollection(incomingQS); _qs.Remove("kissMasterFile"); _qs.Remove("__VIEWSTATE"); newPath = ApplyQueryString(newPath, _qs); foreach (string item in _qs.Keys) { jc.QueryString[item] = _qs[item]; } } // perform the redirection if (newPath.StartsWith("~/")) { HttpContext.Current.RewritePath(newPath, false); } else if (newPath.StartsWith("/")) { app.Response.Redirect(jc.url(newPath)); } else if (newPath.StartsWith("http:") || newPath.StartsWith("https:")) { app.Response.Status = "301 Moved Permanently"; app.Response.AddHeader("Location", newPath); app.Response.End(); } else { // otherwise, treat it as a local file and force the virtual path HttpContext.Current.RewritePath("~/" + newPath, false); } }