using System.Collections.Generic; using System.IO; using System.Net; using WebDAVSharp.Server.Adapters; using WebDAVSharp.Server.Exceptions; using WebDAVSharp.Server.Stores; namespace WebDAVSharp.Server.MethodHandlers { /// /// This class implements the GET HTTP method for WebDAV#. /// internal sealed class WebDavGetMethodHandler : WebDavMethodHandlerBase { #region Properties /// /// Gets the collection of the names of the verbs handled by this instance. /// /// /// The names. /// public override IEnumerable Names => new[] { "GET" }; #endregion #region Functions /// /// Processes the request. /// /// The through which the request came in from the client. /// /// The /// object containing both the request and response /// objects to use. /// /// The that the is hosting. /// /// /// /// specifies a request for a store item that does not exist. /// /// - or - /// /// specifies a request for a store item that is not a document. /// /// /// /// specifies a request for a store item using a /// collection path that does not exist. /// public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store) { IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url); IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url); IWebDavStoreDocument doc = item as IWebDavStoreDocument; if (doc == null) throw new WebDavNotFoundException(); long docSize = doc.Size; if (docSize == 0) { context.Response.StatusCode = (int) HttpStatusCode.OK; context.Response.ContentLength64 = 0; } using (Stream stream = doc.OpenReadStream()) { if (stream == null) { context.Response.StatusCode = (int) HttpStatusCode.OK; context.Response.ContentLength64 = 0; } else { context.Response.StatusCode = (int) HttpStatusCode.OK; if (docSize > 0) context.Response.ContentLength64 = docSize; byte[] buffer = new byte[65536]; int inBuffer; while ((inBuffer = stream.Read(buffer, 0, buffer.Length)) > 0) context.Response.OutputStream.Write(buffer, 0, inBuffer); context.Response.OutputStream.Flush(); } } context.Response.Close(); } #endregion } }