wasDAVClient

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 2  →  ?path2? @ 3
/wasDAVClient/Client.cs
@@ -14,11 +14,6 @@
{
public class Client : IClient
{
private static readonly HttpMethod PropFind = new HttpMethod("PROPFIND");
private static readonly HttpMethod MoveMethod = new HttpMethod("MOVE");
 
private static readonly HttpMethod MkCol = new HttpMethod(WebRequestMethods.Http.MkCol);
 
private const int HttpStatusCode_MultiStatus = 207;
 
// http://webdav.org/specs/rfc4918.html#METHOD_PROPFIND
@@ -38,17 +33,46 @@
//" </prop> " +
"</propfind>";
 
private static readonly HttpMethod PropFind = new HttpMethod("PROPFIND");
private static readonly HttpMethod MoveMethod = new HttpMethod("MOVE");
 
private static readonly HttpMethod MkCol = new HttpMethod(WebRequestMethods.Http.MkCol);
 
private static readonly string AssemblyVersion = typeof(IClient).Assembly.GetName().Version.ToString();
 
private readonly HttpClient _client;
private readonly HttpClient _uploadClient;
private string _server;
private string _basePath = "/";
 
private string _encodedBasePath;
private string _server;
 
 
public Client(NetworkCredential credential = null)
{
var handler = new HttpClientHandler();
 
if (handler.SupportsProxy)
handler.Proxy = WebRequest.DefaultWebProxy;
 
if (handler.SupportsAutomaticDecompression)
handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
 
if (credential != null)
{
handler.Credentials = credential;
handler.PreAuthenticate = true;
}
 
_client = new HttpClient(handler);
_client.Timeout = TimeSpan.FromSeconds(Timeout);
_client.DefaultRequestHeaders.ExpectContinue = false;
 
_uploadClient = new HttpClient(handler);
_uploadClient.Timeout = TimeSpan.FromSeconds(Timeout);
_uploadClient.DefaultRequestHeaders.ExpectContinue = false;
}
 
#region WebDAV connection parameters
 
/// <summary>
@@ -56,10 +80,7 @@
/// </summary>
public string Server
{
get
{
return _server;
}
get { return _server; }
set
{
value = value.TrimEnd('/');
@@ -72,10 +93,7 @@
/// </summary>
public string BasePath
{
get
{
return _basePath;
}
get { return _basePath; }
set
{
value = value.Trim('/');
@@ -89,55 +107,25 @@
/// <summary>
/// Specify an port (default: null = auto-detect)
/// </summary>
public int? Port
{
get; set;
}
public int? Port { get; set; }
 
/// <summary>
/// Specify the UserAgent (and UserAgent version) string to use in requests
/// </summary>
public string UserAgent
{
get; set;
}
public string UserAgent { get; set; }
 
/// <summary>
/// Specify the UserAgent (and UserAgent version) string to use in requests
/// </summary>
public string UserAgentVersion
{
get; set;
}
public string UserAgentVersion { get; set; }
 
/// <summary>
/// The HTTP request timeout in seconds.
/// </summary>
public int Timeout { get; set; } = 60;
 
#endregion
 
 
public Client(NetworkCredential credential = null, TimeSpan? timeout = null, IWebProxy proxy = null)
{
var handler = new HttpClientHandler();
if (proxy != null && handler.SupportsProxy)
handler.Proxy = proxy;
if (handler.SupportsAutomaticDecompression)
handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
if (credential != null)
{
handler.Credentials = credential;
handler.PreAuthenticate = true;
}
 
_client = new HttpClient(handler);
_client.DefaultRequestHeaders.ExpectContinue = false;
 
if (timeout != null)
{
_uploadClient = new HttpClient(handler);
_uploadClient.DefaultRequestHeaders.ExpectContinue = false;
_uploadClient.Timeout = timeout.Value;
}
 
}
 
#region WebDAV operations
 
/// <summary>
@@ -157,12 +145,14 @@
headers.Add("Depth", depth.ToString());
}
 
 
HttpResponseMessage response = null;
 
try
{
response = await HttpRequest(listUri.Uri, PropFind, headers, Encoding.UTF8.GetBytes(PropFindRequestContent)).ConfigureAwait(false);
response =
await
HttpRequest(listUri.Uri, PropFind, headers, Encoding.UTF8.GetBytes(PropFindRequestContent))
.ConfigureAwait(false);
 
if (response.StatusCode != HttpStatusCode.OK &&
(int)response.StatusCode != HttpStatusCode_MultiStatus)
@@ -201,7 +191,6 @@
}
return result;
}
 
}
finally
{
@@ -237,7 +226,6 @@
/// <returns>A list of files (entries without a trailing slash) and directories (entries with a trailing slash)</returns>
private async Task<Item> Get(Uri listUri, string path)
{
 
// Depth header: http://webdav.org/specs/rfc4918.html#rfc.section.9.1.4
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Depth", "0");
@@ -247,12 +235,16 @@
 
try
{
response = await HttpRequest(listUri, PropFind, headers, Encoding.UTF8.GetBytes(PropFindRequestContent)).ConfigureAwait(false);
response =
await
HttpRequest(listUri, PropFind, headers, Encoding.UTF8.GetBytes(PropFindRequestContent))
.ConfigureAwait(false);
 
if (response.StatusCode != HttpStatusCode.OK &&
(int)response.StatusCode != HttpStatusCode_MultiStatus)
{
throw new wasDAVException((int)response.StatusCode, string.Format("Failed retrieving item/folder (Status Code: {0})", response.StatusCode));
throw new wasDAVException((int) response.StatusCode,
string.Format("Failed retrieving item/folder (Status Code: {0})", response.StatusCode));
}
 
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
@@ -301,7 +293,8 @@
public async Task<bool> Upload(string remoteFilePath, Stream content, string name)
{
// Should not have a trailing slash.
var uploadUri = await GetServerUrl(remoteFilePath.TrimEnd('/') + "/" + name.TrimStart('/'), false).ConfigureAwait(false);
var uploadUri =
await GetServerUrl(remoteFilePath.TrimEnd('/') + "/" + name.TrimStart('/'), false).ConfigureAwait(false);
 
HttpResponseMessage response = null;
 
@@ -323,7 +316,6 @@
if (response != null)
response.Dispose();
}
 
}
 
 
@@ -335,7 +327,8 @@
public async Task<bool> CreateDir(string remotePath, string name)
{
// Should not have a trailing slash.
var dirUri = await GetServerUrl(remotePath.TrimEnd('/') + "/" + name.TrimStart('/'), false).ConfigureAwait(false);
var dirUri =
await GetServerUrl(remotePath.TrimEnd('/') + "/" + name.TrimStart('/'), false).ConfigureAwait(false);
 
HttpResponseMessage response = null;
 
@@ -393,7 +386,6 @@
var dstUri = await GetServerUrl(dstFolderPath, true).ConfigureAwait(false);
 
return await Move(srcUri.Uri, dstUri.Uri).ConfigureAwait(false);
 
}
 
public async Task<bool> MoveFile(string srcFilePath, string dstFilePath)
@@ -413,7 +405,10 @@
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Destination", dstUri.ToString());
 
var response = await HttpRequest(srcUri, MoveMethod, headers, Encoding.UTF8.GetBytes(requestContent)).ConfigureAwait(false);
var response =
await
HttpRequest(srcUri, MoveMethod, headers, Encoding.UTF8.GetBytes(requestContent))
.ConfigureAwait(false);
 
if (response.StatusCode != HttpStatusCode.OK &&
response.StatusCode != HttpStatusCode.Created)
@@ -435,7 +430,8 @@
/// <param name="method"></param>
/// <param name="headers"></param>
/// <param name="content"></param>
private async Task<HttpResponseMessage> HttpRequest(Uri uri, HttpMethod method, IDictionary<string, string> headers = null, byte[] content = null)
private async Task<HttpResponseMessage> HttpRequest(Uri uri, HttpMethod method,
IDictionary<string, string> headers = null, byte[] content = null)
{
using (var request = new HttpRequestMessage(method, uri))
{
@@ -447,7 +443,7 @@
 
if (headers != null)
{
foreach (string key in headers.Keys)
foreach (var key in headers.Keys)
{
request.Headers.Add(key, headers[key]);
}
@@ -471,7 +467,8 @@
/// <param name="headers"></param>
/// <param name="method"></param>
/// <param name="content"></param>
private async Task<HttpResponseMessage> HttpUploadRequest(Uri uri, HttpMethod method, Stream content, IDictionary<string, string> headers = null)
private async Task<HttpResponseMessage> HttpUploadRequest(Uri uri, HttpMethod method, Stream content,
IDictionary<string, string> headers = null)
{
using (var request = new HttpRequestMessage(method, uri))
{
@@ -483,7 +480,7 @@
 
if (headers != null)
{
foreach (string key in headers.Keys)
foreach (var key in headers.Keys)
{
request.Headers.Add(key, headers[key]);
}
/wasDAVClient/Helpers/ResponseParser.cs
@@ -13,6 +13,13 @@
/// </summary>
internal static class ResponseParser
{
internal static XmlReaderSettings XmlReaderSettings = new XmlReaderSettings
{
IgnoreComments = true,
IgnoreProcessingInstructions = true,
IgnoreWhitespace = true
};
 
/// <summary>
/// Parses the disk item.
/// </summary>
@@ -23,13 +30,6 @@
return ParseItems(stream).FirstOrDefault();
}
 
internal static XmlReaderSettings XmlReaderSettings = new XmlReaderSettings
{
IgnoreComments = true,
IgnoreProcessingInstructions = true,
IgnoreWhitespace = true
};
 
/// <summary>
/// Parses the disk items.
/// </summary>
@@ -40,7 +40,6 @@
var items = new List<Item>();
using (var reader = XmlReader.Create(stream, XmlReaderSettings))
{
 
Item itemInfo = null;
while (reader.Read())
{
@@ -125,7 +124,8 @@
{
reader.Read();
var resourceType = reader.LocalName.ToLower();
if (string.Equals(resourceType, "collection", StringComparison.InvariantCultureIgnoreCase))
if (string.Equals(resourceType, "collection",
StringComparison.InvariantCultureIgnoreCase))
itemInfo.IsCollection = true;
}
break;
@@ -139,7 +139,7 @@
break;
default:
{
int a = 0;
var a = 0;
break;
}
}
@@ -164,7 +164,5 @@
 
return items;
}
 
 
}
}
}
/wasDAVClient/Helpers/wasDAVConflictException.cs
@@ -11,30 +11,37 @@
 
public wasDAVConflictException(string message)
: base(message)
{}
{
}
 
public wasDAVConflictException(string message, int hr)
: base(message, hr)
{}
{
}
 
public wasDAVConflictException(string message, Exception innerException)
: base(message, innerException)
{}
{
}
 
public wasDAVConflictException(int httpCode, string message, Exception innerException)
: base(httpCode, message, innerException)
{}
{
}
 
public wasDAVConflictException(int httpCode, string message)
: base(httpCode, message)
{}
{
}
 
public wasDAVConflictException(int httpCode, string message, int hr)
: base(httpCode, message, hr)
{}
{
}
 
protected wasDAVConflictException(SerializationInfo info, StreamingContext context)
: base(info, context)
{}
{
}
}
}
/wasDAVClient/Helpers/wasDAVException.cs
@@ -12,35 +12,42 @@
 
public wasDAVException(string message)
: base(message)
{}
{
}
 
public wasDAVException(string message, int hr)
: base(message, hr)
{}
{
}
 
public wasDAVException(string message, Exception innerException)
: base(message, innerException)
{}
{
}
 
public wasDAVException(int httpCode, string message, Exception innerException)
: base(httpCode, message, innerException)
{}
{
}
 
public wasDAVException(int httpCode, string message)
: base(httpCode, message)
{}
{
}
 
public wasDAVException(int httpCode, string message, int hr)
: base(httpCode, message, hr)
{}
{
}
 
protected wasDAVException(SerializationInfo info, StreamingContext context)
: base(info, context)
{}
{
}
 
public override string ToString()
{
var s = string.Format("HttpStatusCode: {0}", base.GetHttpCode());
var s = string.Format("HttpStatusCode: {0}", GetHttpCode());
s += Environment.NewLine + string.Format("ErrorCode: {0}", ErrorCode);
s += Environment.NewLine + string.Format("Message: {0}", Message);
s += Environment.NewLine + base.ToString();
/wasDAVClient/IClient.cs
@@ -26,6 +26,7 @@
/// Specify the UserAgent (and UserAgent version) string to use in requests
/// </summary>
string UserAgent { get; set; }
 
/// <summary>
/// Specify the UserAgent (and UserAgent version) string to use in requests
/// </summary>
/wasDAVClient/Properties/AssemblyInfo.cs
@@ -1,10 +1,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
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("wasDAV")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
@@ -17,9 +17,11 @@
// 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("8c9a5d56-e5a1-4e18-bd00-fff55a2ae01d")]
 
// Version information for an assembly consists of the following four values:
@@ -32,5 +34,6 @@
// 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")]
[assembly: AssemblyFileVersion("1.0.0.0")]