internal System.IO.Stream GetSaveStream(HTTPResponse response) { if (!HTTPCacheService.IsSupported) { return(null); } LastAccess = DateTime.UtcNow; string path = GetPath(); if (File.Exists(path)) { Delete(); } // Path name too long, we don't want to get exceptions if (path.Length > HTTPManager.MaxPathLength) { return(null); } // First write out the headers using (FileStream writer = new FileStream(path, FileMode.Create)) { writer.WriteLine("HTTP/1.1 {0} {1}", response.StatusCode, response.Message); foreach (var kvp in response.Headers) { for (int i = 0; i < kvp.Value.Count; ++i) { writer.WriteLine("{0}: {1}", kvp.Key, kvp.Value[i]); } } writer.WriteLine(); } // If caching is enabled and the response is from cache, and no content-length header set, then we set one to the response. if (response.IsFromCache && !response.Headers.ContainsKey("content-length")) { response.Headers.Add("content-length", new List <string> { BodyLength.ToString() }); } SetUpCachingValues(response); // then create the stream with Append FileMode return(new FileStream(GetPath(), FileMode.Append)); }
public static void WriteLine(this FileStream fs, string format, params object[] values) { var buff = string.Format(format, values).GetASCIIBytes(); fs.Write(buff, 0, buff.Length); fs.WriteLine(); }
public static void WriteLine(this FileStream fs, string line) { var buff = line.GetASCIIBytes(); fs.Write(buff, 0, buff.Length); fs.WriteLine(); }
internal void Store(HTTPResponse response) { if (!HTTPCacheService.IsSupported) { return; } string path = GetPath(); // Path name too long, we don't want to get exceptions if (path.Length > HTTPManager.MaxPathLength) { return; } if (File.Exists(path)) { Delete(); } using (FileStream writer = new FileStream(path, FileMode.Create)) { writer.WriteLine("HTTP/1.1 {0} {1}", response.StatusCode, response.Message); foreach (var kvp in response.Headers) { for (int i = 0; i < kvp.Value.Count; ++i) { writer.WriteLine("{0}: {1}", kvp.Key, kvp.Value[i]); } } writer.WriteLine(); writer.Write(response.Data, 0, response.Data.Length); } BodyLength = response.Data.Length; LastAccess = DateTime.UtcNow; SetUpCachingValues(response); }