-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathLoggingWebDavResponse.cs
More file actions
79 lines (69 loc) · 2.5 KB
/
LoggingWebDavResponse.cs
File metadata and controls
79 lines (69 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// <copyright file="LoggingWebDavResponse.cs" company="Fubar Development Junker">
// Copyright (c) Fubar Development Junker. All rights reserved.
// </copyright>
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="WebDavResponse"/>.
/// </summary>
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>
/// <param name="response">The ASP.NET Core HTTP response.</param>
public LoggingWebDavResponse(IWebDavContext context, HttpResponse response)
: base(context, response)
{
_response = response;
}
/// <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"/>.
/// </summary>
/// <returns>The <see cref="XDocument"/> from the <see cref="Body"/>.</returns>
public XDocument? Load()
{
Body.Position = 0;
if (Body.Length == 0)
{
return null;
}
if (!RequestLogMiddleware.IsXml(ContentType))
{
return null;
}
try
{
return XDocument.Load(Body);
}
catch
{
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);
}
}
}