diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequest.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequest.cs index a42a915279..df87741878 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequest.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequest.cs @@ -240,6 +240,49 @@ public byte[] BinaryRead(int count) return buffer; } + public void SaveAs(string filename, bool includeHeaders) + { + using var f = new FileStream(filename, FileMode.Create); + + if (includeHeaders) + { + using var w = new StreamWriter(f, leaveOpen: true); + + w.Write(HttpMethod); + w.Write(" "); + w.Write(Path); + + // Includes the leading '?' if non-empty + w.Write(_request.QueryString); + + w.Write(" "); + w.WriteLine(_request.Protocol); + + foreach (var header in _request.Headers) + { + w.Write(header.Key); + w.Write(": "); + w.WriteLine(header.Value); + } + + w.WriteLine(); + } + + WriteTo(InputStream, f); + } + + /// + /// Copies the entire stream, but ensures the position is reset to what it was when starting + /// + private static void WriteTo(Stream source, Stream destination) + { + var currentPosition = source.Position; + + source.Position = 0; + source.CopyTo(destination); + source.Position = currentPosition; + } + public void Abort() => _request.HttpContext.Abort(); [return: NotNullIfNotNull("request")] diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/Ref.Standard.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/Ref.Standard.cs index 3dd9aadcbf..2447123507 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/Ref.Standard.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/Ref.Standard.cs @@ -175,6 +175,7 @@ internal HttpRequest() { } public string[] UserLanguages { get { throw new System.PlatformNotSupportedException("Only support when running on ASP.NET Core or System.Web");} } public void Abort() { throw new System.PlatformNotSupportedException("Only support when running on ASP.NET Core or System.Web");} public byte[] BinaryRead(int count) { throw new System.PlatformNotSupportedException("Only support when running on ASP.NET Core or System.Web");} + public void SaveAs(string filename, bool includeHeaders) { throw new System.PlatformNotSupportedException("Only support when running on ASP.NET Core or System.Web");} } public abstract partial class HttpRequestBase {