using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using WebDAVSharp.Server.Adapters;
using WebDAVSharp.Server.Exceptions;
using WebDAVSharp.Server.Stores;
namespace WebDAVSharp.Server.MethodHandlers
{
///
/// This class implements the PUT HTTP method for WebDAV#.
///
internal class WebDavPutMethodHandler : WebDavMethodHandlerBase
{
#region Properties
///
/// Gets the collection of the names of the HTTP methods handled by this instance.
///
///
/// The names.
///
public override IEnumerable Names => new[]
{
"PUT"
};
#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.
///
/// If the ContentLength header was not found
public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
{
// Get the parent collection
IWebDavStoreCollection parentCollection = GetParentCollection(server, store, context.Request.Url);
// Gets the item name from the url
string itemName = Uri.UnescapeDataString(context.Request.Url.Segments.Last().TrimEnd('/', '\\'));
IWebDavStoreItem item = parentCollection.GetItemByName(itemName);
IWebDavStoreDocument doc;
if (item != null)
{
doc = item as IWebDavStoreDocument;
if (doc == null)
throw new WebDavMethodNotAllowedException();
}
else
{
doc = parentCollection.CreateDocument(itemName);
}
if (context.Request.ContentLength64 < 0)
throw new WebDavLengthRequiredException();
using (Stream stream = doc.OpenWriteStream(false))
{
long left = context.Request.ContentLength64;
stream.SetLength(left);
byte[] buffer = new byte[65536];
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((int) HttpStatusCode.Created);
}
#endregion
}
}