Example #1
0
        public static void Main()
        {
            var logger = new MultiLogger(new ILogger[]
            {
                new ConsoleLogger(entry => entry.Level < LogLevel.Error, null, Console.Out),
                new ConsoleLogger(entry => entry.Level >= LogLevel.Error, null, Console.Error),
                new DebugLogger(null, null),
            });

            var methodHandlers = new List <IWebDAVMethodHandler>();

            foreach (IWebDAVMethodHandler methodHandler in WebDAVMethodHandlers.BuiltIn)
            {
                methodHandlers.Add(new LitmusWebDAVMethodHandler(methodHandler));
            }

            using (var server = new WebDAVServer(new WebDAVMemoryStore(), logger, new HttpListenerAdapter(), methodHandlers))
            {
                server.Listener.Prefixes.Add("http://localhost:8888/");

                var ipv4Addresses =
                    from address in Dns.GetHostEntry(Environment.MachineName).AddressList
                    where !address.IsIPv6LinkLocal && !address.IsIPv6Multicast && !address.IsIPv6SiteLocal
                    select address.ToString();

                foreach (var address in ipv4Addresses)
                {
                    server.Listener.Prefixes.Add(string.Format(CultureInfo.InvariantCulture, "http://{0}:8888/", address));
                }
                server.Start();

                Console.In.ReadLine();
                server.Stop();
            }
        }
Example #2
0
        private void MoveCollection(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, IWebDAVStoreCollection source)
        {
            var destinationUri = new Uri(context.Request.Headers["Destination"]);
            var destinationParentCollection = destinationUri.GetParentUri().GetItem(server, store) as IWebDAVStoreCollection;

            if (destinationParentCollection == null)
            {
                throw new HttpConflictException();
            }

            string destinationName = destinationUri.Segments.Last().TrimEnd('/', '\\');
            var    destination     = destinationParentCollection.GetItemByName(destinationName);

            if (destination != null)
            {
                if (context.Request.Headers["Overwrite"] == "F")
                {
                    throw new HttpException(HttpStatusCodes.ClientErrors.PreconditionFailed);
                }

                destinationParentCollection.Delete(destination);
            }

            destinationParentCollection.MoveItemHere(source, destinationName);

            context.SendSimpleResponse(HttpStatusCodes.Successful.Created);
        }
        /// <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);
        }
Example #4
0
        public void Stop_WhenDisposed_ThrowsObjectDisposedException()
        {
            var server = new WebDAVServer(_Store, _Logger, _Listener, _MethodHandlers);

            server.Dispose();

            Assert.Throws <ObjectDisposedException>(() => server.Stop());
        }
        /// <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();
            }
        }
Example #6
0
        /// <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)
        {
            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);
        }
Example #8
0
        public void Start_WhenNotRunning_StartsServer()
        {
            using (var evt = new ManualResetEvent(false))
            {
                var server = new WebDAVServer(_Store, _Logger, _Listener, _MethodHandlers);
                Isolate.WhenCalled(() => _Listener.GetContext(null)).DoInstead(e =>
                {
                    evt.Set();
                    return(null);
                });

                server.Start();

                evt.WaitOne(10000);
                server.Stop();

                Isolate.Verify.WasCalledWithAnyArguments(() => _Listener.GetContext(null));
            }
        }
        /// <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();
        }
Example #10
0
        /// <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();
        }
Example #11
0
        /// <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();
        }
Example #12
0
 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"] + " -------");
 }
            public TransferInfo(WebDAVServer server, long size)
            {
                Server = server;

                lock (Server.TransferLock)
                {
                    TransferSize = size;
                    Server.TransferTotalBytes += size;
                }

                Update();
            }