-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathWebDavResponse.cs
More file actions
156 lines (127 loc) · 4.82 KB
/
WebDavResponse.cs
File metadata and controls
156 lines (127 loc) · 4.82 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// <copyright file="WebDavResponse.cs" company="Fubar Development Junker">
// Copyright (c) Fubar Development Junker. All rights reserved.
// </copyright>
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
namespace FubarDev.WebDavServer.AspNetCore
{
/// <summary>
/// The implementation of the <see cref="IWebDavResponse"/>.
/// </summary>
/// <remarks>
/// This class wraps a <see cref="HttpResponse"/> to be accessible by the WebDAV serves <see cref="IWebDavResult"/>.
/// </remarks>
public class WebDavResponse : IWebDavResponse
{
private readonly HttpResponse _response;
/// <summary>
/// Initializes a new instance of the <see cref="WebDavResponse"/> class.
/// </summary>
/// <param name="context">The current WebDAV context.</param>
/// <param name="response">The ASP.NET Core HTTP response.</param>
public WebDavResponse(IWebDavContext context, HttpResponse response)
{
Context = context;
_response = response;
Headers = new HeadersDictionary(_response.Headers);
}
/// <inheritdoc />
public IWebDavContext Context { get; }
/// <inheritdoc />
public IDictionary<string, string[]> Headers { get; }
/// <inheritdoc />
public string ContentType
{
get { return _response.ContentType; }
set { _response.ContentType = value; }
}
/// <inheritdoc />
public virtual Stream Body => _response.Body;
private class HeadersDictionary : IDictionary<string, string[]>
{
private readonly IHeaderDictionary _headers;
public HeadersDictionary(IHeaderDictionary headers)
{
_headers = headers;
}
public int Count => _headers.Count;
public bool IsReadOnly => _headers.IsReadOnly;
public ICollection<string> Keys => _headers.Keys;
public ICollection<string[]> Values => _headers.Values.Select(x => x.ToArray()).ToList();
public string[] this[string key]
{
get { return _headers[key].ToArray(); }
set { _headers[key] = new StringValues(value); }
}
public IEnumerator<KeyValuePair<string, string[]>> GetEnumerator()
{
return _headers.Select(x => new KeyValuePair<string, string[]>(x.Key, x.Value.ToArray())).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
void ICollection<KeyValuePair<string, string[]>>.Add(KeyValuePair<string, string[]> item)
{
_headers[item.Key] = new StringValues(item.Value);
}
public void Clear()
{
_headers.Clear();
}
bool ICollection<KeyValuePair<string, string[]>>.Contains(KeyValuePair<string, string[]> item)
{
var values = _headers[item.Key].ToArray();
if (item.Value.Length != values.Length)
{
return false;
}
for (var i = 0; i != values.Length; ++i)
{
if (item.Value[i] != values[i])
{
return false;
}
}
return true;
}
void ICollection<KeyValuePair<string, string[]>>.CopyTo(KeyValuePair<string, string[]>[] array, int arrayIndex)
{
foreach (KeyValuePair<string, StringValues> header in _headers)
{
array[arrayIndex++] = new KeyValuePair<string, string[]>(header.Key, header.Value.ToArray());
}
}
bool ICollection<KeyValuePair<string, string[]>>.Remove(KeyValuePair<string, string[]> item)
{
return Remove(item.Key);
}
public void Add(string key, string[] value)
{
_headers.Add(key, new StringValues(value));
}
public bool ContainsKey(string key)
{
return _headers.ContainsKey(key);
}
public bool Remove(string key)
{
return _headers.Remove(key);
}
public bool TryGetValue(string key, out string[] value)
{
if (_headers.TryGetValue(key, out var values))
{
value = values.ToArray();
return true;
}
value = null!;
return false;
}
}
}
}