forked from WebDAVSharp/WebDAVSharp.Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebDAVServer.cs
311 lines (284 loc) · 12 KB
/
WebDAVServer.cs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Common.Logging;
using WebDAVSharp.Server.Adapters;
using WebDAVSharp.Server.Exceptions;
using WebDAVSharp.Server.MethodHandlers;
using WebDAVSharp.Server.Stores;
namespace WebDAVSharp.Server
{
/// <summary>
/// This class implements the core WebDAV server.
/// </summary>
public class WebDavServer : WebDavDisposableBase
{
/// <summary>
/// The HTTP user
/// </summary>
public const string HttpUser = "HTTP.User";
private readonly IHttpListener _listener;
private readonly bool _ownsListener;
private readonly IWebDavStore _store;
private readonly Dictionary<string, IWebDavMethodHandler> _methodHandlers;
private readonly ILog _log;
private readonly object _threadLock = new object();
private ManualResetEvent _stopEvent;
private Thread _thread;
/// <summary>
/// Initializes a new instance of the <see cref="WebDavServer" /> class.
/// </summary>
/// <param name="store">The
/// <see cref="IWebDavStore" /> store object that will provide
/// collections and documents for this
/// <see cref="WebDavServer" />.</param>
/// <param name="listener">The
/// <see cref="IHttpListener" /> object that will handle the web server portion of
/// the WebDAV server; or
/// <c>null</c> to use a fresh one.</param>
/// <param name="methodHandlers">A collection of HTTP method handlers to use by this
/// <see cref="WebDavServer" />;
/// or
/// <c>null</c> to use the built-in method handlers.</param>
/// <exception cref="System.ArgumentNullException"><para>
/// <paramref name="listener" /> is <c>null</c>.</para>
/// <para>- or -</para>
/// <para>
/// <paramref name="store" /> is <c>null</c>.</para></exception>
/// <exception cref="System.ArgumentException"><para>
/// <paramref name="methodHandlers" /> is empty.</para>
/// <para>- or -</para>
/// <para>
/// <paramref name="methodHandlers" /> contains a <c>null</c>-reference.</para></exception>
public WebDavServer(IWebDavStore store, IHttpListener listener = null, IEnumerable<IWebDavMethodHandler> methodHandlers = null)
{
if (store == null)
throw new ArgumentNullException("store");
if (listener == null)
{
listener = new HttpListenerAdapter();
_ownsListener = true;
}
methodHandlers = methodHandlers ?? WebDavMethodHandlers.BuiltIn;
IWebDavMethodHandler[] webDavMethodHandlers = methodHandlers as IWebDavMethodHandler[] ?? methodHandlers.ToArray();
if (!webDavMethodHandlers.Any())
throw new ArgumentException("The methodHandlers collection is empty", "methodHandlers");
if (webDavMethodHandlers.Any(methodHandler => methodHandler == null))
throw new ArgumentException("The methodHandlers collection contains a null-reference", "methodHandlers");
_listener = listener;
_store = store;
var handlersWithNames =
from methodHandler in webDavMethodHandlers
from name in methodHandler.Names
select new
{
name,
methodHandler
};
_methodHandlers = handlersWithNames.ToDictionary(v => v.name, v => v.methodHandler);
_log = LogManager.GetCurrentClassLogger();
}
/// <summary>
/// Gets the
/// <see cref="IHttpListener" /> that this
/// <see cref="WebDavServer" /> uses for
/// the web server portion.
/// </summary>
/// <value>
/// The listener.
/// </value>
internal IHttpListener Listener
{
get
{
return _listener;
}
}
/// <summary>
/// Gets the <see cref="IWebDavStore" /> this <see cref="WebDavServer" /> is hosting.
/// </summary>
/// <value>
/// The store.
/// </value>
public IWebDavStore Store
{
get
{
return _store;
}
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
lock (_threadLock)
{
if (_thread != null)
Stop();
}
if (_ownsListener)
_listener.Dispose();
}
/// <summary>
/// Starts this
/// <see cref="WebDavServer" /> and returns once it has
/// been started successfully.
/// </summary>
/// <exception cref="System.InvalidOperationException">This WebDAVServer instance is already running, call to Start is invalid at this point</exception>
/// <exception cref="ObjectDisposedException">This <see cref="WebDavServer" /> instance has been disposed of.</exception>
/// <exception cref="InvalidOperationException">The server is already running.</exception>
public void Start(String Url)
{
Listener.Prefixes.Add(Url);
EnsureNotDisposed();
lock (_threadLock)
{
if (_thread != null)
{
throw new InvalidOperationException(
"This WebDAVServer instance is already running, call to Start is invalid at this point");
}
_stopEvent = new ManualResetEvent(false);
_thread = new Thread(BackgroundThreadMethod)
{
Name = "WebDAVServer.Thread",
Priority = ThreadPriority.Highest
};
_thread.Start();
}
}
/// <summary>
/// Starts this
/// <see cref="WebDavServer" /> and returns once it has
/// been stopped successfully.
/// </summary>
/// <exception cref="System.InvalidOperationException">This WebDAVServer instance is not running, call to Stop is invalid at this point</exception>
/// <exception cref="ObjectDisposedException">This <see cref="WebDavServer" /> instance has been disposed of.</exception>
/// <exception cref="InvalidOperationException">The server is not running.</exception>
public void Stop()
{
EnsureNotDisposed();
lock (_threadLock)
{
if (_thread == null)
{
throw new InvalidOperationException(
"This WebDAVServer instance is not running, call to Stop is invalid at this point");
}
_stopEvent.Set();
_thread.Join();
_stopEvent.Close();
_stopEvent = null;
_thread = null;
}
}
/// <summary>
/// The background thread method.
/// </summary>
private void BackgroundThreadMethod()
{
_log.Info("WebDAVServer background thread has started");
try
{
_listener.Start();
while (true)
{
if (_stopEvent.WaitOne(0))
return;
IHttpListenerContext context = Listener.GetContext(_stopEvent);
if (context == null)
{
_log.Debug("Exiting thread");
return;
}
ThreadPool.QueueUserWorkItem(ProcessRequest, context);
}
}
finally
{
_listener.Stop();
_log.Info("WebDAVServer background thread has terminated");
}
}
/// <summary>
/// Processes the request.
/// </summary>
/// <param name="state">The state.</param>
/// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException">If the method to process is not allowed</exception>
/// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If the user is unauthorized or has no access</exception>
/// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If the item was not found</exception>
/// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotImplementedException">If a method is not yet implemented</exception>
/// <exception cref="WebDAVSharp.Server.Exceptions.WebDavInternalServerException">If the server had an internal problem</exception>
private void ProcessRequest(object state)
{
IHttpListenerContext context = (IHttpListenerContext)state;
// For authentication
Thread.SetData(Thread.GetNamedDataSlot(HttpUser), context.AdaptedInstance.User.Identity);
_log.Info(context.Request.HttpMethod + " " + context.Request.RemoteEndPoint + ": " + context.Request.Url);
try
{
try
{
string method = context.Request.HttpMethod;
IWebDavMethodHandler methodHandler;
if (!_methodHandlers.TryGetValue(method, out methodHandler))
throw new WebDavMethodNotAllowedException(string.Format(CultureInfo.InvariantCulture, "%s ({0})", context.Request.HttpMethod));
context.Response.AppendHeader("DAV", "1,2,1#extend");
methodHandler.ProcessRequest(this, context, Store);
}
catch (WebDavException)
{
throw;
}
catch (UnauthorizedAccessException)
{
throw new WebDavUnauthorizedException();
}
catch (FileNotFoundException ex)
{
_log.Warn(ex.Message);
throw new WebDavNotFoundException(innerException: ex);
}
catch (DirectoryNotFoundException ex)
{
_log.Warn(ex.Message);
throw new WebDavNotFoundException(innerException: ex);
}
catch (NotImplementedException ex)
{
_log.Warn(ex.Message);
throw new WebDavNotImplementedException(innerException: ex);
}
catch (Exception ex)
{
_log.Warn(ex.Message);
throw new WebDavInternalServerException(innerException: ex);
}
}
catch (WebDavException ex)
{
_log.Warn(ex.StatusCode + " " + ex.Message);
context.Response.StatusCode = ex.StatusCode;
context.Response.StatusDescription = ex.StatusDescription;
if (ex.Message != context.Response.StatusDescription)
{
byte[] buffer = Encoding.UTF8.GetBytes(ex.Message);
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
context.Response.Close();
}
finally
{
_log.Info(context.Response.StatusCode + " " + context.Response.StatusDescription + ": " + context.Request.HttpMethod + " " + context.Request.RemoteEndPoint + ": " + context.Request.Url);
}
}
}
}