Released windows auth, connect to ES.Services.

This commit is contained in:
ITRANSITION\e.revyakin 2014-12-03 11:43:26 +03:00
parent 2569e55609
commit d29c347ff4
294 changed files with 329583 additions and 2315 deletions

View file

@ -0,0 +1,6 @@
namespace WebsitePanel.WebDav.Core.Exceptions
{
public class UnauthorizedException : WebDavHttpException
{
}
}

View file

@ -0,0 +1,8 @@
using System;
namespace WebsitePanel.WebDav.Core.Exceptions
{
public class WebDavException : Exception
{
}
}

View file

@ -0,0 +1,6 @@
namespace WebsitePanel.WebDav.Core.Exceptions
{
public class WebDavHttpException : WebDavException
{
}
}

View file

@ -0,0 +1,32 @@
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public interface IConnectionSettings
{
bool AllowWriteStreamBuffering { get; set; }
bool SendChunked { get; set; }
int TimeOut { get; set; }
}
public class WebDavConnectionSettings
{
private int _timeOut = 30000;
public WebDavConnectionSettings()
{
SendChunked = false;
AllowWriteStreamBuffering = false;
}
public bool AllowWriteStreamBuffering { get; set; }
public bool SendChunked { get; set; }
public int TimeOut
{
get { return _timeOut; }
set { _timeOut = value; }
}
}
}
}

View file

@ -0,0 +1,354 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using WebsitePanel.WebDav.Core.Exceptions;
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public interface IFolder : IHierarchyItem
{
IResource CreateResource(string name);
IFolder CreateFolder(string name);
IHierarchyItem[] GetChildren();
IResource GetResource(string name);
}
public class WebDavFolder : WebDavHierarchyItem, IFolder
{
private IHierarchyItem[] _children = new IHierarchyItem[0];
private Uri _path;
/// <summary>
/// The constructor
/// </summary>
public WebDavFolder()
{
}
/// <summary>
/// The constructor
/// </summary>
/// <param name="path">Path to the folder.</param>
public WebDavFolder(string path)
{
_path = new Uri(path);
}
/// <summary>
/// The constructor
/// </summary>
/// <param name="path">Path to the folder.</param>
public WebDavFolder(Uri path)
{
_path = path;
}
/// <summary>
/// Creates a resource with a specified name.
/// </summary>
/// <param name="name">Name of the new resource.</param>
/// <returns>Newly created resource.</returns>
public IResource CreateResource(string name)
{
var resource = new WebDavResource();
try
{
resource.SetHref(new Uri(Href.AbsoluteUri + name));
var credentials = (NetworkCredential) _credentials;
string auth = "Basic " +
Convert.ToBase64String(
Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
var request = (HttpWebRequest) WebRequest.Create(resource.Href);
request.Method = "PUT";
request.Credentials = credentials;
request.ContentType = "text/xml";
request.Accept = "text/xml";
request.Headers["translate"] = "f";
request.Headers.Add("Authorization", auth);
using (var response = (HttpWebResponse) request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.Created ||
response.StatusCode == HttpStatusCode.NoContent)
{
Open(Href);
resource = (WebDavResource) GetResource(name);
resource.SetCredentials(_credentials);
}
}
}
catch (Exception e)
{
}
return resource;
}
/// <summary>
/// Creates new folder with specified name as child of this one.
/// </summary>
/// <param name="name">Name of the new folder.</param>
/// <returns>IFolder</returns>
public IFolder CreateFolder(string name)
{
var folder = new WebDavFolder();
try
{
var credentials = (NetworkCredential) _credentials;
var request = (HttpWebRequest) WebRequest.Create(Href.AbsoluteUri + name);
request.Method = "MKCOL";
request.Credentials = credentials;
string auth = "Basic " +
Convert.ToBase64String(
Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
request.Headers.Add("Authorization", auth);
using (var response = (HttpWebResponse) request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.Created ||
response.StatusCode == HttpStatusCode.NoContent)
{
folder.SetCredentials(_credentials);
folder.Open(Href.AbsoluteUri + name + "/");
}
}
}
catch (Exception e)
{
}
return folder;
}
/// <summary>
/// Returns children of this folder.
/// </summary>
/// <returns>Array that include child folders and resources.</returns>
public IHierarchyItem[] GetChildren()
{
return _children;
}
/// <summary>
/// Gets the specified resource from server.
/// </summary>
/// <param name="name">Name of the resource.</param>
/// <returns>Resource corresponding to requested name.</returns>
public IResource GetResource(string name)
{
IHierarchyItem item =
_children.Single(i => i.ItemType == ItemType.Resource && i.DisplayName.Trim('/') == name.Trim('/'));
var resource = new WebDavResource();
resource.SetCredentials(_credentials);
resource.SetHierarchyItem(item);
return resource;
}
/// <summary>
/// Opens the folder.
/// </summary>
public void Open()
{
var request = (HttpWebRequest) WebRequest.Create(_path);
request.PreAuthenticate = true;
request.Method = "PROPFIND";
request.ContentType = "application/xml";
request.Headers["Depth"] = "1";
var credentials = (NetworkCredential) _credentials;
if (credentials != null && credentials.UserName != null)
{
request.Credentials = credentials;
string auth = "Basic " +
Convert.ToBase64String(
Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
request.Headers.Add("Authorization", auth);
}
try
{
using (var response = (HttpWebResponse) request.GetResponse())
{
using (var responseStream = new StreamReader(response.GetResponseStream()))
{
string responseString = responseStream.ReadToEnd();
ProcessResponse(responseString);
}
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
throw new UnauthorizedException();
}
throw e;
}
}
/// <summary>
/// Opens the folder
/// </summary>
/// <param name="path">Path of the folder to open.</param>
public void Open(string path)
{
_path = new Uri(path);
Open();
}
/// <summary>
/// Opens the folder
/// </summary>
/// <param name="path">Path of the folder to open.</param>
public void Open(Uri path)
{
_path = path;
Open();
}
/// <summary>
/// Processes the response from the server.
/// </summary>
/// <param name="response">The raw response from the server.</param>
private void ProcessResponse(string response)
{
try
{
var XmlDoc = new XmlDocument();
XmlDoc.LoadXml(response);
XmlNodeList XmlResponseList = XmlDoc.GetElementsByTagName("D:response");
if (XmlResponseList.Count == 0)
{
XmlResponseList = XmlDoc.GetElementsByTagName("d:response");
}
var children = new WebDavHierarchyItem[XmlResponseList.Count];
int counter = 0;
foreach (XmlNode XmlCurrentResponse in XmlResponseList)
{
var item = new WebDavHierarchyItem();
item.SetCredentials(_credentials);
foreach (XmlNode XmlCurrentNode in XmlCurrentResponse.ChildNodes)
{
switch (XmlCurrentNode.LocalName)
{
case "href":
string href = XmlCurrentNode.InnerText;
if (!Regex.Match(href, "^https?:\\/\\/").Success)
{
href = _path.Scheme + "://" + _path.Host + href;
}
item.SetHref(href, _path);
break;
case "propstat":
foreach (XmlNode XmlCurrentPropStatNode in XmlCurrentNode)
{
switch (XmlCurrentPropStatNode.LocalName)
{
case "prop":
foreach (XmlNode XmlCurrentPropNode in XmlCurrentPropStatNode)
{
switch (XmlCurrentPropNode.LocalName)
{
case "creationdate":
item.SetCreationDate(XmlCurrentPropNode.InnerText);
break;
case "getcontentlanguage":
item.SetProperty(
new Property(
new PropertyName("getcontentlanguage",
XmlCurrentPropNode.NamespaceURI),
XmlCurrentPropNode.InnerText));
break;
case "getcontentlength":
item.SetProperty(
new Property(
new PropertyName("getcontentlength",
XmlCurrentPropNode.NamespaceURI),
XmlCurrentPropNode.InnerText));
break;
case "getcontenttype":
item.SetProperty(
new Property(
new PropertyName("getcontenttype",
XmlCurrentPropNode.NamespaceURI),
XmlCurrentPropNode.InnerText));
break;
case "getlastmodified":
item.SetLastModified(XmlCurrentPropNode.InnerText);
break;
case "resourcetype":
item.SetProperty(
new Property(
new PropertyName("resourcetype",
XmlCurrentPropNode.NamespaceURI),
XmlCurrentPropNode.InnerXml));
break;
}
}
break;
case "status":
break;
}
}
break;
}
}
if (item.DisplayName != String.Empty)
{
children[counter] = item;
counter++;
}
else
{
SetItemType(ItemType.Folder);
SetHref(item.Href.AbsoluteUri, item.Href);
SetCreationDate(item.CreationDate);
SetComment(item.Comment);
SetCreatorDisplayName(item.CreatorDisplayName);
SetLastModified(item.LastModified);
foreach (Property property in item.Properties)
{
SetProperty(property);
}
}
}
int childrenCount = 0;
foreach (IHierarchyItem item in children)
{
if (item != null)
{
childrenCount++;
}
}
_children = new IHierarchyItem[childrenCount];
counter = 0;
foreach (IHierarchyItem item in children)
{
if (item != null)
{
_children[counter] = item;
counter++;
}
}
}
catch (XmlException e)
{
}
}
}
}
}

View file

@ -0,0 +1,255 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public interface IHierarchyItem : IConnectionSettings
{
string Comment { get; }
DateTime CreationDate { get; }
string CreatorDisplayName { get; }
string DisplayName { get; }
Uri Href { get; }
ItemType ItemType { get; }
DateTime LastModified { get; }
Property[] Properties { get; }
Property[] GetAllProperties();
PropertyName[] GetPropertyNames();
Property[] GetPropertyValues(PropertyName[] names);
void Delete();
}
public class WebDavHierarchyItem : WebDavConnectionSettings, IHierarchyItem
{
private Uri _baseUri;
private string _comment = "";
private DateTime _creationDate = new DateTime(0);
private string _creatorDisplayName = "";
protected ICredentials _credentials = new NetworkCredential();
private Uri _href;
private ItemType _itemType;
private DateTime _lastModified = new DateTime(0);
private Property[] _properties = {};
public string Comment
{
get { return _comment; }
}
public DateTime CreationDate
{
get { return _creationDate; }
}
public string CreatorDisplayName
{
get { return _creatorDisplayName; }
}
public string DisplayName
{
get
{
string displayName = _href.AbsoluteUri.Replace(_baseUri.AbsoluteUri, "");
displayName = Regex.Replace(displayName, "\\/$", "");
Match displayNameMatch = Regex.Match(displayName, "([\\/]+)$");
if (displayNameMatch.Success)
{
displayName = displayNameMatch.Groups[1].Value;
}
return HttpUtility.UrlDecode(displayName);
}
}
public Uri Href
{
get { return _href; }
}
public ItemType ItemType
{
get { return _itemType; }
}
public DateTime LastModified
{
get { return _lastModified; }
}
public Property[] Properties
{
get { return _properties; }
}
public Property[] GetAllProperties()
{
return _properties;
}
public PropertyName[] GetPropertyNames()
{
return _properties.Select(p => p.Name).ToArray();
}
public Property[] GetPropertyValues(PropertyName[] names)
{
return (from p in _properties from pn in names where pn.Equals(p.Name) select p).ToArray();
}
public void Delete()
{
var credentials = (NetworkCredential) _credentials;
string auth = "Basic " +
Convert.ToBase64String(
Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
WebRequest webRequest = WebRequest.Create(Href);
webRequest.Method = "DELETE";
webRequest.Credentials = credentials;
webRequest.Headers.Add("Authorization", auth);
using (WebResponse webResponse = webRequest.GetResponse())
{
using (Stream responseStream = webResponse.GetResponseStream())
{
var buffer = new byte[8192];
string result = "";
int bytesRead = 0;
do
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
result += Encoding.UTF8.GetString(buffer, 0, bytesRead);
}
} while (bytesRead > 0);
}
}
}
public void Delete(LockUriTokenPair[] lockTokens)
{
}
public void Delete(string lockToken)
{
}
public void SetComment(string comment)
{
_comment = comment;
}
public void SetCreationDate(string creationDate)
{
_creationDate = DateTime.Parse(creationDate);
}
public void SetCreationDate(DateTime creationDate)
{
_creationDate = creationDate;
}
public void SetCreatorDisplayName(string creatorDisplayName)
{
_creatorDisplayName = creatorDisplayName;
}
public void SetItemType(ItemType itemType)
{
_itemType = itemType;
}
public void SetHref(string href, Uri baseUri)
{
_href = new Uri(href);
_baseUri = baseUri;
}
public void SetLastModified(string lastModified)
{
_lastModified = DateTime.Parse(lastModified);
}
public void SetLastModified(DateTime lastModified)
{
_lastModified = lastModified;
}
public void SetProperty(Property property)
{
if (property.Name.Name == "resourcetype" && property.StringValue != String.Empty)
{
var XmlDoc = new XmlDocument();
try
{
if (property.StringValue == "collection")
{
_itemType = ItemType.Folder;
}
else
{
XmlDoc.LoadXml(property.StringValue);
property.StringValue = XmlDoc.DocumentElement.LocalName;
switch (property.StringValue)
{
case "collection":
_itemType = ItemType.Folder;
break;
default:
break;
}
}
}
catch (XmlException e)
{
}
}
bool propertyFound = false;
foreach (Property prop in _properties)
{
if (prop.Name.Equals(property.Name))
{
prop.StringValue = property.StringValue;
propertyFound = true;
}
}
if (!propertyFound)
{
var newProperties = new Property[_properties.Length + 1];
for (int i = 0; i < _properties.Length; i++)
{
newProperties[i] = _properties[i];
}
newProperties[_properties.Length] = property;
_properties = newProperties;
}
}
public void SetProperty(PropertyName propertyName, string value)
{
SetProperty(new Property(propertyName, value));
}
public void SetProperty(string name, string nameSpace, string value)
{
SetProperty(new Property(name, nameSpace, value));
}
public void SetCredentials(ICredentials credentials)
{
_credentials = credentials;
}
}
}
}

View file

@ -0,0 +1,20 @@
using System.IO;
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public interface IItemContent
{
long ContentLength { get; }
string ContentType { get; }
void Download(string filename);
byte[] Download();
void Upload(string filename);
Stream GetReadStream();
Stream GetWriteStream(long contentLength);
Stream GetWriteStream(string contentType, long contentLength);
}
}
}

View file

@ -0,0 +1,521 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Xml;
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public interface IResource : IItemContent, IHierarchyItem, IConnectionSettings
{
bool CheckedOut { get; }
bool VersionControlled { get; }
}
public class WebDavResource : IResource
{
// IResource
private Uri _baseUri;
private bool _checkedOut = false;
private string _comment = "";
private long _contentLength;
private string _contentType = "";
private DateTime _creationDate = new DateTime(0);
private string _creatorDisplayName = "";
private ICredentials _credentials = new NetworkCredential();
private Uri _href;
private ItemType _itemType;
private DateTime _lastModified = new DateTime(0);
private Property[] _properties = {};
private int _timeOut = 30000;
private bool _versionControlled = false;
public WebDavResource()
{
SendChunked = false;
AllowWriteStreamBuffering = false;
}
public Uri BaseUri
{
get { return _baseUri; }
}
public bool CheckedOut
{
get { return _checkedOut; }
}
public bool VersionControlled
{
get { return _versionControlled; }
}
// IItemContent
public long ContentLength
{
get { return _contentLength; }
}
public string ContentType
{
get { return _contentType; }
}
/// <summary>
/// Downloads content of the resource to a file specified by filename
/// </summary>
/// <param name="filename">Full path of a file to be downloaded to</param>
public void Download(string filename)
{
var webClient = new WebClient();
webClient.DownloadFile(_href, filename);
}
public byte[] Download()
{
try
{
var webClient = new WebClient();
return webClient.DownloadData(_href);
}
catch (WebException exception)
{
throw;
}
}
/// <summary>
/// Uploads content of a file specified by filename to the server
/// </summary>
/// <param name="filename">Full path of a file to be uploaded from</param>
public void Upload(string filename)
{
var credentials = (NetworkCredential) _credentials;
string auth = "Basic " +
Convert.ToBase64String(
Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
var webClient = new WebClient();
webClient.Credentials = credentials;
webClient.Headers.Add("Authorization", auth);
webClient.UploadFile(Href, "PUT", filename);
}
/// <summary>
/// Loads content of the resource from WebDAV server.
/// </summary>
/// <returns>Stream to read resource content.</returns>
public Stream GetReadStream()
{
var credentials = (NetworkCredential) _credentials;
string auth = "Basic " +
Convert.ToBase64String(
Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
var webClient = new WebClient();
webClient.Credentials = credentials;
webClient.Headers.Add("Authorization", auth);
return webClient.OpenRead(_href);
}
/// <summary>
/// Saves resource's content to WebDAV server.
/// </summary>
/// <param name="contentLength">Length of data to be written.</param>
/// <returns>Stream to write resource content.</returns>
public Stream GetWriteStream(long contentLength)
{
return GetWriteStream("application/octet-stream", contentLength);
}
/// <summary>
/// Saves resource's content to WebDAV server.
/// </summary>
/// <param name="contentType">Media type of the resource.</param>
/// <param name="contentLength">Length of data to be written.</param>
/// <returns>Stream to write resource content.</returns>
public Stream GetWriteStream(string contentType, long contentLength)
{
var tcpClient = new TcpClient(Href.Host, Href.Port);
if (tcpClient.Connected)
{
var credentials = (NetworkCredential) _credentials;
string auth = "Basic " +
Convert.ToBase64String(
Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
try
{
if (TimeOut != Timeout.Infinite)
{
tcpClient.SendTimeout = TimeOut;
tcpClient.ReceiveTimeout = TimeOut;
}
else
{
tcpClient.SendTimeout = 0;
tcpClient.ReceiveTimeout = 0;
}
}
catch (SocketException e)
{
tcpClient.SendTimeout = 0;
tcpClient.ReceiveTimeout = 0;
}
NetworkStream networkStream = tcpClient.GetStream();
if (networkStream.CanTimeout)
{
try
{
networkStream.WriteTimeout = TimeOut;
networkStream.ReadTimeout = TimeOut;
}
catch (Exception e)
{
}
}
byte[] methodBuffer = Encoding.UTF8.GetBytes("PUT " + Href.AbsolutePath + " HTTP/1.1\r\n");
byte[] hostBuffer = Encoding.UTF8.GetBytes("Host: " + Href.Host + "\r\n");
byte[] contentLengthBuffer = Encoding.UTF8.GetBytes("Content-Length: " + contentLength + "\r\n");
byte[] authorizationBuffer = Encoding.UTF8.GetBytes("Authorization: " + auth + "\r\n");
byte[] connectionBuffer = Encoding.UTF8.GetBytes("Connection: Close\r\n\r\n");
networkStream.Write(methodBuffer, 0, methodBuffer.Length);
networkStream.Write(hostBuffer, 0, hostBuffer.Length);
networkStream.Write(contentLengthBuffer, 0, contentLengthBuffer.Length);
networkStream.Write(authorizationBuffer, 0, authorizationBuffer.Length);
networkStream.Write(connectionBuffer, 0, connectionBuffer.Length);
return networkStream;
}
throw new IOException("could not connect to server");
}
// IHierarchyItem
public string Comment
{
get { return _comment; }
}
public DateTime CreationDate
{
get { return _creationDate; }
}
public string CreatorDisplayName
{
get { return _creatorDisplayName; }
}
public string DisplayName
{
get
{
string displayName = _href.AbsoluteUri.Replace(_baseUri.AbsoluteUri, "");
displayName = Regex.Replace(displayName, "\\/$", "");
Match displayNameMatch = Regex.Match(displayName, "([\\/]+)$");
if (displayNameMatch.Success)
{
displayName = displayNameMatch.Groups[1].Value;
}
return HttpUtility.UrlDecode(displayName);
}
}
public Uri Href
{
get { return _href; }
}
public ItemType ItemType
{
get { return _itemType; }
}
public DateTime LastModified
{
get { return _lastModified; }
}
public Property[] Properties
{
get { return _properties; }
}
// IHierarchyItem Methods
/// <summary>
/// Retrieves all custom properties exposed by the item.
/// </summary>
/// <returns>This method returns the array of custom properties exposed by the item.</returns>
public Property[] GetAllProperties()
{
return _properties;
}
/// <summary>
/// Returns names of all custom properties exposed by this item.
/// </summary>
/// <returns></returns>
public PropertyName[] GetPropertyNames()
{
return _properties.Select(p => p.Name).ToArray();
}
/// <summary>
/// Retrieves values of specific properties.
/// </summary>
/// <param name="names"></param>
/// <returns>Array of requested properties with values.</returns>
public Property[] GetPropertyValues(PropertyName[] names)
{
return (from p in _properties from pn in names where pn.Equals(p.Name) select p).ToArray();
}
/// <summary>
/// Deletes this item.
/// </summary>
public void Delete()
{
var credentials = (NetworkCredential) _credentials;
string auth = "Basic " +
Convert.ToBase64String(
Encoding.Default.GetBytes(credentials.UserName + ":" + credentials.Password));
WebRequest webRequest = WebRequest.Create(Href);
webRequest.Method = "DELETE";
webRequest.Credentials = credentials;
webRequest.Headers.Add("Authorization", auth);
using (WebResponse webResponse = webRequest.GetResponse())
{
using (Stream responseStream = webResponse.GetResponseStream())
{
var buffer = new byte[8192];
string result = "";
int bytesRead = 0;
do
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
result += Encoding.UTF8.GetString(buffer, 0, bytesRead);
}
} while (bytesRead > 0);
}
}
}
public bool AllowWriteStreamBuffering { get; set; }
public bool SendChunked { get; set; }
public int TimeOut
{
get { return _timeOut; }
set { _timeOut = value; }
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetComment(string comment)
{
_comment = comment;
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetCreationDate(string creationDate)
{
_creationDate = DateTime.Parse(creationDate);
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetCreationDate(DateTime creationDate)
{
_creationDate = creationDate;
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetCreatorDisplayName(string creatorDisplayName)
{
_creatorDisplayName = creatorDisplayName;
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetHref(string href, Uri baseUri)
{
_href = new Uri(href);
_baseUri = baseUri;
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetHref(Uri href)
{
_href = href;
string baseUri = _href.Scheme + "://" + _href.Host;
for (int i = 0; i < _href.Segments.Length - 1; i++)
{
if (_href.Segments[i] != "/")
{
baseUri += "/" + _href.Segments[i];
}
}
_baseUri = new Uri(baseUri);
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetLastModified(string lastModified)
{
_lastModified = DateTime.Parse(lastModified);
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetLastModified(DateTime lastModified)
{
_lastModified = lastModified;
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetProperty(Property property)
{
if (property.Name.Name == "resourcetype" && property.StringValue != String.Empty)
{
var XmlDoc = new XmlDocument();
try
{
XmlDoc.LoadXml(property.StringValue);
property.StringValue = XmlDoc.DocumentElement.LocalName;
switch (property.StringValue)
{
case "collection":
_itemType = ItemType.Folder;
break;
default:
break;
}
}
catch (Exception e)
{
}
}
bool propertyFound = false;
foreach (Property prop in _properties)
{
if (prop.Name.Equals(property.Name))
{
prop.StringValue = property.StringValue;
propertyFound = true;
}
}
if (!propertyFound)
{
var newProperties = new Property[_properties.Length + 1];
for (int i = 0; i < _properties.Length; i++)
{
newProperties[i] = _properties[i];
}
if (property.Name.Name == "getcontentlength")
{
try
{
_contentLength = Convert.ToInt64(property.StringValue);
}
catch (Exception)
{
}
}
newProperties[_properties.Length] = property;
_properties = newProperties;
}
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetProperty(PropertyName propertyName, string value)
{
SetProperty(new Property(propertyName, value));
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetProperty(string name, string nameSpace, string value)
{
SetProperty(new Property(name, nameSpace, value));
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetProperties(Property[] properties)
{
foreach (Property property in properties)
{
SetProperty(property);
}
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetCredentials(ICredentials credentials)
{
_credentials = credentials;
}
// IConnectionSettings
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="comment"></param>
public void SetHierarchyItem(IHierarchyItem item)
{
SetComment(item.Comment);
SetCreationDate(item.CreationDate);
SetCreatorDisplayName(item.CreatorDisplayName);
SetHref(item.Href);
SetLastModified(item.LastModified);
SetProperties(item.Properties);
}
}
}
}

View file

@ -0,0 +1,53 @@
using System.IO;
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public interface IResumableUpload
{
void CancelUpload();
void CancelUpload(string lockToken);
long GetBytesUploaded();
Stream GetWriteStream(long startIndex, long contentLength, long resourceTotalSize);
Stream GetWriteStream(long startIndex, long contentLength, long resourceTotalSize, string contentType);
Stream GetWriteStream(long startIndex, long contentLength, long resourceTotalSize, string contentType,
string lockToken);
}
public class WebDavResumableUpload
{
private long _bytesUploaded = 0;
public void CancelUpload()
{
}
public void CancelUpload(string lockToken)
{
}
public long GetBytesUploaded()
{
return _bytesUploaded;
}
public Stream GetWriteStream(long startIndex, long contentLength, long resourceTotalSize)
{
return new MemoryStream();
}
public Stream GetWriteStream(long startIndex, long contentLength, long resourceTotalSize, string contentType)
{
return new MemoryStream();
}
public Stream GetWriteStream(long startIndex, long contentLength, long resourceTotalSize, string contentType,
string lockToken)
{
return new MemoryStream();
}
}
}
}

View file

@ -0,0 +1,13 @@
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public enum ItemType
{
Resource,
Folder,
Version,
VersionHistory
}
}
}

View file

@ -0,0 +1,17 @@
using System;
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public class LockUriTokenPair
{
public readonly Uri Href;
public readonly string lockToken;
public LockUriTokenPair(Uri href, string lockToken)
{
}
}
}
}

View file

@ -0,0 +1,39 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WebsitePanel.WebDav.Core")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WebsitePanel.WebDav.Core")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6d5fcace-d8d1-4340-a905-d882b8f3c7fe")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -0,0 +1,34 @@
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public class Property
{
public readonly PropertyName Name;
private string _value = "";
public Property(PropertyName name, string value)
{
Name = name;
StringValue = value;
}
public Property(string name, string nameSpace, string value)
{
Name = new PropertyName(name, nameSpace);
StringValue = value;
}
public string StringValue
{
get { return _value; }
set { _value = value; }
}
public override string ToString()
{
return StringValue;
}
}
}
}

View file

@ -0,0 +1,40 @@
namespace WebsitePanel.WebDav.Core
{
namespace Client
{
public sealed class PropertyName
{
public readonly string Name;
public readonly string NamespaceUri;
public PropertyName(string name, string namespaceUri)
{
Name = name;
NamespaceUri = namespaceUri;
}
public override bool Equals(object obj)
{
if (obj.GetType() == GetType())
{
if (((PropertyName) obj).Name == Name && ((PropertyName) obj).NamespaceUri == NamespaceUri)
{
return true;
}
}
return false;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return Name;
}
}
}
}

View file

@ -0,0 +1,64 @@
using System;
using System.Net;
namespace WebsitePanel.WebDav.Core
{
/// <summary>
/// WebDav.Client Namespace.
/// </summary>
/// <see cref="http://doc.webdavsystem.com/ITHit.WebDAV.Client.html"/>
namespace Client
{
public class WebDavSession
{
public ICredentials Credentials { get; set; }
/// <summary>
/// Returns IFolder corresponding to path.
/// </summary>
/// <param name="path">Path to the folder.</param>
/// <returns>Folder corresponding to requested path.</returns>
public IFolder OpenFolder(string path)
{
var folder = new WebDavFolder();
folder.SetCredentials(Credentials);
folder.Open(path);
return folder;
}
/// <summary>
/// Returns IFolder corresponding to path.
/// </summary>
/// <param name="path">Path to the folder.</param>
/// <returns>Folder corresponding to requested path.</returns>
public IFolder OpenFolder(Uri path)
{
var folder = new WebDavFolder();
folder.SetCredentials(Credentials);
folder.Open(path);
return folder;
}
/// <summary>
/// Returns IResource corresponding to path.
/// </summary>
/// <param name="path">Path to the resource.</param>
/// <returns>Resource corresponding to requested path.</returns>
public IResource OpenResource(string path)
{
return OpenResource(new Uri(path));
}
/// <summary>
/// Returns IResource corresponding to path.
/// </summary>
/// <param name="path">Path to the resource.</param>
/// <returns>Resource corresponding to requested path.</returns>
public IResource OpenResource(Uri path)
{
IFolder folder = OpenFolder(path);
return folder.GetResource(path.Segments[path.Segments.Length - 1]);
}
}
}
}

View file

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{BA147805-9EF1-45F2-BF32-A5825D4E950D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WebsitePanel.WebDav.Core</RootNamespace>
<AssemblyName>WebsitePanel.WebDav.Core</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Web.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\UnauthorizedException.cs" />
<Compile Include="Exceptions\WebDavException.cs" />
<Compile Include="Exceptions\WebDavHttpException.cs" />
<Compile Include="IConnectionSettings.cs" />
<Compile Include="IFolder.cs" />
<Compile Include="IHierarchyItem.cs" />
<Compile Include="IItemContent.cs" />
<Compile Include="IResource.cs" />
<Compile Include="IResumableUpload.cs" />
<Compile Include="ItemType.cs" />
<Compile Include="LockUriTokenPair.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Property.cs" />
<Compile Include="PropertyName.cs" />
<Compile Include="WebDavSession.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>