/// <summary> /// Sets the necessary headers to disable caching of a response on the client side. /// </summary> /// <param name="this">The <see cref="IHttpResponse"/> interface on which this method is called.</param> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> public static void DisableCaching(this IHttpResponse @this) { var headers = @this.Headers; headers.Set(HttpHeaderNames.Expires, "Sat, 26 Jul 1997 05:00:00 GMT"); headers.Set(HttpHeaderNames.LastModified, HttpDate.Format(DateTime.UtcNow)); headers.Set(HttpHeaderNames.CacheControl, "no-store, no-cache, must-revalidate"); headers.Add(HttpHeaderNames.Pragma, "no-cache"); }
public async Task ListDirectoryAsync( MappedResourceInfo info, string absolutePath, IEnumerable <MappedResourceInfo> entries, Stream stream, CancellationToken cancellationToken) { const int MaxEntryLength = 50; const int SizeIndent = -20; // Negative for right alignment if (!info.IsDirectory) { throw SelfCheck.Failure($"{nameof(HtmlDirectoryLister)}.{nameof(ListDirectoryAsync)} invoked with a file, not a directory."); } var encodedPath = WebUtility.HtmlEncode(absolutePath); using var text = new StreamWriter(stream, Encoding.UTF8); text.Write("<html><head><title>Index of "); text.Write(encodedPath); text.Write("</title></head><body><h1>Index of "); text.Write(encodedPath); text.Write("</h1><hr/><pre>"); if (encodedPath.Length > 1) { text.Write("<a href='../'>../</a>\n"); } entries = entries.ToArray(); foreach (var directory in entries.Where(m => m.IsDirectory).OrderBy(e => e.Name)) { text.Write($"<a href=\"{Uri.EscapeDataString(directory.Name)}{Path.DirectorySeparatorChar}\">{WebUtility.HtmlEncode(directory.Name)}</a>"); text.Write(new string(' ', Math.Max(1, MaxEntryLength - directory.Name.Length + 1))); text.Write(HttpDate.Format(directory.LastModifiedUtc)); text.Write('\n'); await Task.Yield(); } foreach (var file in entries.Where(m => m.IsFile).OrderBy(e => e.Name)) { text.Write($"<a href=\"{Uri.EscapeDataString(file.Name)}{Path.DirectorySeparatorChar}\">{WebUtility.HtmlEncode(file.Name)}</a>"); text.Write(new string(' ', Math.Max(1, MaxEntryLength - file.Name.Length + 1))); text.Write(HttpDate.Format(file.LastModifiedUtc)); text.Write($" {file.Length.ToString("#,###", CultureInfo.InvariantCulture),SizeIndent}\n"); await Task.Yield(); } text.Write("</pre><hr/></body></html>"); }
public async Task ListDirectoryAsync( MappedResourceInfo info, string absoluteUrlPath, IEnumerable <MappedResourceInfo> entries, Stream stream, CancellationToken cancellationToken) { if (info?.IsDirectory != true) { throw new ArgumentException("HtmlDirectoryLister.ListDirectoryAsync invoked with a file, not a directory."); } string str = WebUtility.HtmlEncode(absoluteUrlPath); await using StreamWriter text = new StreamWriter(stream, Encoding.UTF8); text.Write("<html><head><title>Index of "); text.Write(str); text.Write("</title></head><body><h1>Index of "); text.Write(str); text.Write("</h1><hr/><pre>"); if (str?.Length > 1) { text.Write("<a href='../'>../</a>\n"); } entries = entries.ToArray(); foreach (MappedResourceInfo mappedResourceInfo in entries.Where(m => m.IsDirectory).OrderBy(e => e.Name)) { text.Write( $"<a href=\"{(!string.IsNullOrEmpty(str)? str + (str.EndsWith(Path.DirectorySeparatorChar) ? "" : Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)) : "")}{Uri.EscapeDataString(mappedResourceInfo.Name)}{Path.DirectorySeparatorChar}\">{WebUtility.HtmlEncode(mappedResourceInfo.Name)}</a>"); text.Write(new string(' ', Math.Max(1, 50 - mappedResourceInfo.Name.Length + 1))); text.Write(HttpDate.Format(mappedResourceInfo.LastModifiedUtc)); text.Write('\n'); await Task.Yield(); } foreach (MappedResourceInfo mappedResourceInfo in entries.Where(m => m.IsFile).OrderBy(e => e.Name)) { text.Write( $"<a href=\"{(!string.IsNullOrEmpty(str) ? str + (str.EndsWith(Path.DirectorySeparatorChar) ? "" : Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)) : "")}{Uri.EscapeDataString(mappedResourceInfo.Name)}\">{WebUtility.HtmlEncode(mappedResourceInfo.Name)}</a>"); text.Write(new string(' ', Math.Max(1, 50 - mappedResourceInfo.Name.Length + 1))); text.Write(HttpDate.Format(mappedResourceInfo.LastModifiedUtc)); text.Write(" {0,-20}\n", mappedResourceInfo.Length.ToString("#,###", CultureInfo.InvariantCulture)); await Task.Yield(); } text.Write("</pre><hr/></body></html>"); }