public void SetUp() { _Logger = Isolate.Fake.Instance <ILogger>(); _Store = Isolate.Fake.Instance <IWebDAVStore>(); _Listener = Isolate.Fake.Instance <IHttpListener>(); _MethodHandlers = WebDAVMethodHandlers.BuiltIn.ToList(); }
/// <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="logger"> /// The <see cref="ILogger"/> object that log messages will be sent to; /// or <c>null</c> to not use a logger. /// </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="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="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, ILogger logger = null, 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; if (!methodHandlers.Any()) { throw new ArgumentException("The methodHandlers collection is empty", "methodHandlers"); } if (methodHandlers.Any(methodHandler => methodHandler == null)) { throw new ArgumentException("The methodHandlers collection contains a null-reference", "methodHandlers"); } _Listener = listener; _Store = store; var handlersWithNames = from methodHandler in methodHandlers from name in methodHandler.Names select new { name, methodHandler }; _MethodHandlers = handlersWithNames.ToDictionary(v => v.name, v => v.methodHandler); _Logger = logger ?? new VoidLogger(); }
/// <summary> /// Retrieves a store item through the specified <see cref="Uri"/> from the /// specified <see cref="WebDAVServer"/> and <see cref="IWebDAVStore"/>. /// </summary> /// <param name="uri"> /// The <see cref="Uri"/> to retrieve the store item for. /// </param> /// <param name="server"> /// The <see cref="WebDAVServer"/> that hosts the <paramref name="store"/>. /// </param> /// <param name="store"> /// The <see cref="IWebDAVStore"/> from which to retrieve the store item. /// </param> /// <returns> /// The retrieved store item. /// </returns> /// <exception cref="ArgumentNullException"> /// <para><paramref name="uri"/> is <c>null</c>.</para> /// <para><paramref name="server"/> is <c>null</c>.</para> /// <para><paramref name="store"/> is <c>null</c>.</para> /// </exception> /// <exception cref="HttpConflictException"> /// <para><paramref name="uri"/> refers to a document in a collection, where the collection does not exist.</para> /// </exception> /// <exception cref="HttpNotFoundException"> /// <para><paramref name="uri"/> refers to a document that does not exist.</para> /// </exception> public static IWebDAVStoreItem GetItem(this Uri uri, WebDAVServer server, IWebDAVStore store) { if (uri == null) { throw new ArgumentNullException("uri"); } if (server == null) { throw new ArgumentNullException("server"); } if (store == null) { throw new ArgumentNullException("store"); } Uri prefixUri = uri.GetPrefixUri(server); IWebDAVStoreCollection collection = store.Root; IWebDAVStoreItem item = null; if (prefixUri.Segments.Length == uri.Segments.Length) { return(collection); } for (int index = prefixUri.Segments.Length; index < uri.Segments.Length; index++) { string segmentName = uri.Segments[index]; IWebDAVStoreItem nextItem = collection.GetItemByName(segmentName.TrimEnd('/', '\\')); if (nextItem == null) { throw new HttpConflictException(); } if (index == uri.Segments.Length - 1) { item = nextItem; } else { collection = nextItem as IWebDAVStoreCollection; if (collection == null) { throw new HttpNotFoundException(); } } } if (item == null) { throw new HttpNotFoundException(); } return(item); }
private void CopyItem(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, IWebDAVStoreItem source) { var destinationUri = new Uri(context.Request.Headers["Destination"]); var destinationParentCollection = destinationUri.GetParentUri().GetItem(server, store) as IWebDAVStoreCollection; if (destinationParentCollection == null) { throw new HttpConflictException(); } bool isNew = true; string destinationName = destinationUri.Segments.Last().TrimEnd('/', '\\'); IWebDAVStoreItem destination = destinationParentCollection.GetItemByName(destinationName); if (source == destination) { throw new HttpConflictException(); } if (destination != null) { if (context.Request.Headers["Overwrite"] == "F") { throw new HttpException(HttpStatusCodes.ClientErrors.PreconditionFailed); } if (destination is IWebDAVStoreCollection) { destinationParentCollection.Delete(destination); } isNew = false; } destinationParentCollection.CopyItemHere(source, destinationName); if (isNew) { context.SendSimpleResponse(HttpStatusCodes.Successful.Created); } else { context.SendSimpleResponse(HttpStatusCodes.Successful.NoContent); } }
/// <summary> /// Processes the request. /// </summary> /// <param name="server"> /// The <see cref="WebDAVServer"/> through which the request came in from the client. /// </param> /// <param name="context"> /// The <see cref="IHttpListenerContext"/> object containing both the request and response /// objects to use. /// </param> /// <param name="store"> /// The <see cref="IWebDAVStore"/> that the <see cref="WebDAVServer"/> is hosting. /// </param> /// <param name="logger"> /// The <see cref="ILogger"/> to log to. /// </param> public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger) { var parentCollection = context.Request.Url.GetParentUri().GetItem(server, store) as IWebDAVStoreCollection; if (parentCollection == null) { throw new HttpConflictException(); } string itemName = context.Request.Url.Segments.Last().TrimEnd('/', '\\'); var item = parentCollection.GetItemByName(itemName); IWebDAVStoreDocument doc; if (item != null) { doc = item as IWebDAVStoreDocument; if (doc == null) { throw new HttpMethodNotAllowedException(); } } else { doc = parentCollection.CreateDocument(itemName); } if (context.Request.ContentLength64 < 0) { throw new HttpException(HttpStatusCodes.ClientErrors.LengthRequired); } using (var stream = doc.OpenWriteStream(false)) { long left = context.Request.ContentLength64; byte[] buffer = new byte[4096]; while (left > 0) { int toRead = Convert.ToInt32(Math.Min(left, buffer.Length)); int inBuffer = context.Request.InputStream.Read(buffer, 0, toRead); stream.Write(buffer, 0, inBuffer); left -= inBuffer; } } context.SendSimpleResponse(HttpStatusCodes.Successful.Created); }
public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger) { logger.Log(LogLevel.Debug, "------- " + context.Request.Headers["X-litmus"] + " -------"); _Handler.ProcessRequest(server, context, store, logger); logger.Log(LogLevel.Debug, "------- " + context.Request.Headers["X-litmus"] + " -------"); }
/// <summary> /// Processes the request. /// </summary> /// <param name="server"> /// The <see cref="WebDAVServer"/> through which the request came in from the client. /// </param> /// <param name="context"> /// The <see cref="IHttpListenerContext"/> object containing both the request and response /// objects to use. /// </param> /// <param name="store"> /// The <see cref="IWebDAVStore"/> that the <see cref="WebDAVServer"/> is hosting. /// </param> /// <param name="logger"> /// The <see cref="ILogger"/> to log to. /// </param> public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger) { // var item = context.Request.Url.GetItem(server, store); var verbsAllowed = new List <string> { "OPTIONS" }; foreach (var verb in verbsAllowed) { context.Response.AppendHeader("Allow", verb); } context.SendSimpleResponse(HttpStatusCodes.Successful.OK); }
/// <summary> /// Processes the request. /// </summary> /// <param name="server"> /// The <see cref="WebDAVServer"/> through which the request came in from the client. /// </param> /// <param name="context"> /// The <see cref="IHttpListenerContext"/> object containing both the request and response /// objects to use. /// </param> /// <param name="store"> /// The <see cref="IWebDAVStore"/> that the <see cref="WebDAVServer"/> is hosting. /// </param> /// <param name="logger"> /// The <see cref="ILogger"/> to log to. /// </param> public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger) { Uri parentCollectionUri = context.Request.Url.GetParentUri(); var collection = parentCollectionUri.GetItem(server, store) as IWebDAVStoreCollection; if (collection == null) { throw new HttpConflictException(); } IWebDAVStoreItem item = collection.GetItemByName(context.Request.Url.Segments.Last().TrimEnd('/', '\\')); if (item == null) { throw new HttpNotFoundException(); } collection.Delete(item); context.SendSimpleResponse(); }
/// <summary> /// Processes the request. /// </summary> /// <param name="server"> /// The <see cref="WebDAVServer"/> through which the request came in from the client. /// </param> /// <param name="context"> /// The <see cref="IHttpListenerContext"/> object containing both the request and response /// objects to use. /// </param> /// <param name="store"> /// The <see cref="IWebDAVStore"/> that the <see cref="WebDAVServer"/> is hosting. /// </param> /// <param name="logger"> /// The <see cref="ILogger"/> to log to. /// </param> /// <exception cref="HttpNotFoundException"> /// <para><paramref name="context"/> specifies a request for a store item that does not exist.</para> /// <para>- or -</para> /// <para><paramref name="context"/> specifies a request for a store item that is not a document.</para> /// </exception> /// <exception cref="HttpConflictException"> /// <para><paramref name="context"/> specifies a request for a store item using a collection path that does not exist.</para> /// </exception> public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger) { var doc = context.Request.Url.GetItem(server, store) as IWebDAVStoreDocument; if (doc == null) { throw new HttpNotFoundException(); } long docSize = doc.Size; if (docSize == 0) { context.Response.StatusCode = HttpStatusCodes.Successful.OK; context.Response.ContentLength64 = 0; context.Response.Close(); } using (Stream stream = doc.OpenReadStream()) { context.Response.StatusCode = HttpStatusCodes.Successful.OK; if (docSize > 0) { context.Response.ContentLength64 = docSize; } var buffer = new byte[4096]; int inBuffer; while ((inBuffer = stream.Read(buffer, 0, buffer.Length)) > 0) { context.Response.OutputStream.Write(buffer, 0, inBuffer); } } context.Response.Close(); }
/// <summary> /// Processes the request. /// </summary> /// <param name="server"> /// The <see cref="WebDAVServer"/> through which the request came in from the client. /// </param> /// <param name="context"> /// The <see cref="IHttpListenerContext"/> object containing both the request and response /// objects to use. /// </param> /// <param name="store"> /// The <see cref="IWebDAVStore"/> that the <see cref="WebDAVServer"/> is hosting. /// </param> /// <param name="logger"> /// The <see cref="ILogger"/> to log to. /// </param> public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger) { var source = context.Request.Url.GetItem(server, store); var document = source as IWebDAVStoreDocument; if (document != null) { MoveDocument(server, context, store, document); return; } var collection = source as IWebDAVStoreCollection; if (collection != null) { MoveCollection(server, context, store, collection); return; } throw new HttpMethodNotAllowedException(); }
/// <summary> /// Processes the request. /// </summary> /// <param name="server"> /// The <see cref="WebDAVServer"/> through which the request came in from the client. /// </param> /// <param name="context"> /// The <see cref="IHttpListenerContext"/> object containing both the request and response /// objects to use. /// </param> /// <param name="store"> /// The <see cref="IWebDAVStore"/> that the <see cref="WebDAVServer"/> is hosting. /// </param> /// <param name="logger"> /// The <see cref="ILogger"/> to log to. /// </param> public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger) { if (context.Request.ContentLength64 > 0) { throw new HttpException(HttpStatusCodes.ClientErrors.UnsupportedMediaType); } Uri uri = context.Request.Url.GetParentUri(); var collection = uri.GetItem(server, store) as IWebDAVStoreCollection; if (collection == null) { throw new HttpNotFoundException(); } string collectionName = context.Request.Url.Segments.Last().TrimEnd('/', '\\'); if (collection.GetItemByName(collectionName) != null) { throw new HttpException(HttpStatusCodes.ClientErrors.MethodNotAllowed); } collection.CreateCollection(collectionName); context.SendSimpleResponse(HttpStatusCodes.Successful.OK); }
/// <summary> /// Processes the request. /// </summary> /// <param name="server"> /// The <see cref="WebDAVServer"/> through which the request came in from the client. /// </param> /// <param name="context"> /// The <see cref="IHttpListenerContext"/> object containing both the request and response /// objects to use. /// </param> /// <param name="store"> /// The <see cref="IWebDAVStore"/> that the <see cref="WebDAVServer"/> is hosting. /// </param> /// <param name="logger"> /// The <see cref="ILogger"/> to log to. /// </param> public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger) { IWebDAVStoreItem source = context.Request.Url.GetItem(server, store); if (source is IWebDAVStoreDocument || source is IWebDAVStoreCollection) { CopyItem(server, context, store, source); } else { throw new HttpMethodNotAllowedException(); } }