12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using System.Collections.Generic;
- using System.Net;
- using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Adapters;
- using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Exceptions;
- using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Stores;
- namespace Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.MethodHandlers
- {
- /// <summary>
- /// This class implements the <c>GET</c> HTTP method for WebDAV#.
- /// </summary>
- internal sealed class WebDavGetMethodHandler : WebDavMethodHandlerBase, IWebDavMethodHandler
- {
- /// <summary>
- /// Gets the collection of the names of the verbs handled by this instance.
- /// </summary>
- /// <value>
- /// The names.
- /// </value>
- public IEnumerable<string> Names => new[]
- {
- "GET"
- };
- /// <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>
- /// <exception cref="WebDavNotFoundException"></exception>
- /// <exception cref="WebDavNotFoundException">
- /// <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="WebDavConflictException">
- /// <paramref name="context" /> specifies a request for a store item using a
- /// collection path that does not exist.
- /// </exception>
- public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
- {
- var collection = GetParentCollection(server, store, context.Request.Url);
- var item = GetItemFromCollection(collection, context.Request.Url);
- var doc = item as IWebDavStoreDocument;
- if (doc == null)
- throw new WebDavNotFoundException();
- var docSize = doc.Size;
- if (docSize == 0)
- {
- context.Response.StatusCode = (int) HttpStatusCode.OK;
- context.Response.ContentLength64 = 0;
- }
- using (var stream = doc.OpenReadStream())
- {
- context.Response.StatusCode = (int) HttpStatusCode.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();
- }
- }
- }
|