void makeResponseFromCache(swFile sf, HttpApplication app) { string eTag = app.Request.Headers["If-None-Match"]; app.Response.AppendHeader("Etag", sf.eTag); app.Response.ContentType = DesignNew.Consts.contentTypes[sf.ext]; if (sf.eTag == eTag) { //souhlasi eTag => not modified app.Response.ClearContent(); app.Response.StatusCode = (int)HttpStatusCode.NotModified; app.Response.AddHeader("Content-Length", "0"); app.Response.SuppressContent = true; } else { //nova stranka (a ev. jeji gzip verze) string acceptEncoding = app.Request.Headers["Accept-Encoding"]; app.Response.StatusCode = (int)HttpStatusCode.OK; if (!string.IsNullOrEmpty(acceptEncoding) && acceptEncoding.Contains("gzip") && sf.gzipData != null) { app.Response.AppendHeader("Content-Encoding", "gzip"); app.Response.OutputStream.Write(sf.gzipData, 0, sf.gzipData.Length); } else { app.Response.OutputStream.Write(sf.data, 0, sf.data.Length); } } app.Response.End(); }
public static void extractSwFilesToCache() { //soubory z d:\LMCom\rew\WebCode\App_Data\swfiles.zip do cache pri startu aplikace var zipFn = @"d:\LMCom\rew\WebCommon\swfiles.zip"; // HostingEnvironment.MapPath("~/app_data/swfiles.zip"); var mStr = new MemoryStream(); using (MD5 md5 = MD5.Create()) using (var zipStr = File.OpenRead(zipFn)) using (ZipArchive zip = new ZipArchive(zipStr, ZipArchiveMode.Read)) { foreach (var f in zip.Entries) using (var fStr = f.Open()) { mStr.SetLength(0); fStr.CopyTo(mStr); string name = f.FullName.Replace('\\', '/'); bool isGZip = false; if (name.EndsWith(".gzip")) { name = name.Substring(0, name.Length - 5); isGZip = true; } swFile actFile; if (!swFiles.TryGetValue(name, out actFile)) swFiles.Add(name, actFile = new swFile(name)); if (isGZip) actFile.gzipData = mStr.ToArray(); else actFile.setData(mStr.ToArray(), md5); } } }
public static swFile addToCache(string name, string ext, byte[] data) { //index-*.html do cache lock (swFiles) { swFile actFile; if (swFiles.TryGetValue(name, out actFile)) return actFile; swFiles.Add(name, actFile = new swFile(name) { ext = ext }); using (MD5 md5 = MD5.Create()) actFile.setData(data, md5); using (var ms = new MemoryStream()) { using (var gzip = new GZipStream(ms, CompressionMode.Compress)) gzip.Write(data, 0, data.Length); actFile.gzipData = ms.ToArray(); } return actFile; } }