Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty response body when debug log is enabled #74

Open
wants to merge 1 commit into
base: release/2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions src/FubarDev.WebDavServer.AspNetCore/LoggingWebDavResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,39 @@

using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;

using FubarDev.WebDavServer.AspNetCore.Logging;
using FubarDev.WebDavServer.Utils;

using Microsoft.AspNetCore.Http;

namespace FubarDev.WebDavServer.AspNetCore
{
/// <summary>
/// A <see cref="IWebDavResponse"/> implementation that buffers the output of a <see cref="IWebDavResult"/>.
/// A <see cref="IWebDavResponse"/> implementation that buffers the output of a <see cref="WebDavResponse"/>.
/// </summary>
public class LoggingWebDavResponse : IWebDavResponse
public class LoggingWebDavResponse : WebDavResponse
{
private readonly HttpResponse _response;

/// <summary>
/// Initializes a new instance of the <see cref="LoggingWebDavResponse"/> class.
/// </summary>
/// <param name="context">The current WebDAV context.</param>
public LoggingWebDavResponse(IWebDavContext context)
/// <param name="response">The ASP.NET Core HTTP response.</param>
public LoggingWebDavResponse(IWebDavContext context, HttpResponse response)
: base(context, response)
{
Context = context;
ContentType = "text/xml";
_response = response;
}

/// <inheritdoc />
public IWebDavContext Context { get; }

/// <inheritdoc />
public IDictionary<string, string[]> Headers { get; } = new Dictionary<string, string[]>();

/// <inheritdoc />
public string ContentType { get; set; }

/// <inheritdoc />
public Stream Body { get; } = new MemoryStream();
/// <summary>
/// Gets the buffered output stream
/// </summary>
public override Stream Body { get; } = new MemoryStream();

/// <summary>
/// Loads the <see cref="Body"/> into a <see cref="XDocument"/>.
Expand Down Expand Up @@ -63,5 +64,16 @@ public LoggingWebDavResponse(IWebDavContext context)
return null;
}
}

/// <summary>
/// Writes the buffered output to the http response body
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the http request</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task WriteBufferedOutPutToResponse(CancellationToken cancellationToken)
{
Body.Position = 0;
await Body.CopyToAsync(_response.Body, SystemInfo.CopyBufferSize, cancellationToken);
}
}
}
5 changes: 4 additions & 1 deletion src/FubarDev.WebDavServer.AspNetCore/WebDavIndirectResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override async Task ExecuteResultAsync(ActionContext context)

if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
{
var loggingResponse = new LoggingWebDavResponse(_context);
var loggingResponse = new LoggingWebDavResponse(_context, response);
await _result.ExecuteResultAsync(loggingResponse, context.HttpContext.RequestAborted).ConfigureAwait(false);
if (!string.IsNullOrEmpty(loggingResponse.ContentType))
{
Expand All @@ -81,6 +81,9 @@ public override async Task ExecuteResultAsync(ActionContext context)
}
}
}

await loggingResponse.WriteBufferedOutPutToResponse(context.HttpContext.RequestAborted).ConfigureAwait(false);
return;
}

// Writes the XML response
Expand Down
2 changes: 1 addition & 1 deletion src/FubarDev.WebDavServer.AspNetCore/WebDavResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public string ContentType
}

/// <inheritdoc />
public Stream Body => _response.Body;
public virtual Stream Body => _response.Body;

private class HeadersDictionary : IDictionary<string, string[]>
{
Expand Down