Skip to content

Commit

Permalink
.net core support almost ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh Gupta committed Feb 23, 2017
1 parent c484877 commit b239b27
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 44 deletions.
2 changes: 1 addition & 1 deletion FluentFTP/FluentFTP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Extensions\GetChecksum.cs" />
<Compile Include="Extensions\MD5.cs" />
<Compile Include="FtpClient.cs" />
Expand Down
21 changes: 21 additions & 0 deletions FluentFTP/FluentFTP.xproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>

<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>117f9ba2-711b-4c71-92ec-002220b157dd</ProjectGuid>
<RootNamespace>FluentFTP</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
</PropertyGroup>

<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
132 changes: 89 additions & 43 deletions FluentFTP/FtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,11 @@ public bool DataConnectionEncryption {
}
}

#if CORE
private SslProtocols m_SslProtocols = SslProtocols.Tls11 | SslProtocols.Ssl3;
#else
private SslProtocols m_SslProtocols = SslProtocols.Default;
#endif
/// <summary>
/// Encryption protocols to use. Only valid if EncryptionMode property is not equal to FtpSslMode.None.
/// Default value is .NET Framework defaults from SslStream class.
Expand Down Expand Up @@ -583,9 +587,9 @@ public int TransferChunkSize {
// ADD PROPERTIES THAT NEED TO BE CLONED INTO
// FtpClient.CloneConnection()

#endregion
#endregion

#region Core
#region Core

/// <summary>
/// Performs a bitwise and to check if the specified
Expand Down Expand Up @@ -628,12 +632,16 @@ protected T GetAsyncDelegate<T>(IAsyncResult ar) {
throw new InvalidOperationException("The specified IAsyncResult could not be located.");

if (!(m_asyncmethods[ar] is T)) {
#if CORE
throw new InvalidCastException("The AsyncResult cannot be matched to the specified delegate. ");
#else
StackTrace st = new StackTrace(1);

throw new InvalidCastException("The AsyncResult cannot be matched to the specified delegate. " +
string.Format("Are you sure you meant to call {0} and not another method?",
st.GetFrame(0).GetMethod().Name)
);
#endif
}

func = (T)m_asyncmethods[ar];
Expand Down Expand Up @@ -697,6 +705,10 @@ protected FtpClient CloneConnection() {
return conn;
}

/// <summary>
/// Creates a new instance of this class. Useful in FTP proxy classes.
/// </summary>
/// <returns></returns>
protected virtual FtpClient Create() {
return new FtpClient();
}
Expand Down Expand Up @@ -827,9 +839,9 @@ public FtpReply EndExecute(IAsyncResult ar) {
return GetAsyncDelegate<AsyncExecute>(ar).EndInvoke(ar);
}

#endregion
#endregion

#region Connection
#region Connection

/// <summary>
/// Connect to the server. Throws ObjectDisposedException if this object has been disposed.
Expand Down Expand Up @@ -915,10 +927,17 @@ public virtual void Connect() {
}
}

/// <summary>
/// Connect to the FTP server. Overwritten in proxy classes.
/// </summary>
/// <param name="stream"></param>
protected virtual void Connect(FtpSocketStream stream) {
stream.Connect(Host, Port, InternetProtocolVersions);
}

/// <summary>
/// Connect to the FTP server. Overwritten in proxy classes.
/// </summary>
protected virtual void Connect(FtpSocketStream stream, string host, int port, FtpIpVersion ipVersions) {
stream.Connect(host, port, ipVersions);
}
Expand Down Expand Up @@ -1129,9 +1148,9 @@ public void EndDisconnect(IAsyncResult ar) {
GetAsyncDelegate<AsyncDisconnect>(ar).EndInvoke(ar);
}

#endregion
#endregion

#region File I/O
#region File I/O

/// <summary>
/// Opens the specified type of passive data stream
Expand Down Expand Up @@ -1725,9 +1744,9 @@ public Stream EndOpenAppend(IAsyncResult ar) {
return GetAsyncDelegate<AsyncOpenAppend>(ar).EndInvoke(ar);
}

#endregion
#endregion

#region File Upload/Download
#region File Upload/Download

/// <summary>
/// Uploads the specified file directly onto the server.
Expand Down Expand Up @@ -1775,7 +1794,7 @@ public bool UploadFile(string localPath, string remotePath, bool overwrite = tru

// close the file stream
try {
fileStream.Close();
fileStream.Dispose();
} catch (Exception) { }
return ok;
}
Expand Down Expand Up @@ -1879,7 +1898,7 @@ public bool DownloadFile(string localPath, string remotePath, bool overwrite = t

// close the file stream
try {
outStream.Close();
outStream.Dispose();
} catch (Exception) { }
return ok;
}
Expand Down Expand Up @@ -1963,8 +1982,13 @@ private bool UploadFileInternal(Stream fileData, string remotePath, bool createR
// resume if server disconnects midway (fixes #39)
if (ex.InnerException != null) {
var iex = ex.InnerException as System.Net.Sockets.SocketException;
if (iex != null && iex.ErrorCode == 10054) {
upStream.Close();
#if CORE
int code = (int)iex.SocketErrorCode;
#else
int code = iex.ErrorCode;
#endif
if (iex != null && code == 10054) {
upStream.Dispose();
upStream = OpenAppend(remotePath);
upStream.Position = offset;
} else throw;
Expand All @@ -1977,16 +2001,33 @@ private bool UploadFileInternal(Stream fileData, string remotePath, bool createR
while (upStream.Position < upStream.Length) {
}

// disconnect FTP stream before exiting
upStream.Dispose();

// FIX : if this is not added, there appears to be "stale data" on the socket
// listen for a success/failure reply
if (!m_threadSafeDataChannels) {
FtpReply status = GetReply();
}

// fixes #30. the file can have some bytes at the end missing
// on proxy servers, so we write those missing bytes now.
if (IsProxy()) {
if (upStream.Position < len && fileData.CanSeek) {

// seek to the point of the file
fileData.Seek(upStream.Position, SeekOrigin.Begin);
// get the current length of the remote file
// and check if any bytes are missing
long curLen = GetFileSize(remotePath);
if (curLen > 0 && curLen < len && fileData.CanSeek) {

// seek to the point of the local file
offset = curLen;
fileData.Position = offset;

// open the remote file for appending
upStream = OpenAppend(remotePath, FtpDataType.Binary);
upStream.Position = offset;

// read a chunk of bytes from the file
offset = upStream.Position;
int readBytes;
while ((readBytes = fileData.Read(buffer, 0, buffer.Length)) > 0) {

Expand All @@ -1995,16 +2036,16 @@ private bool UploadFileInternal(Stream fileData, string remotePath, bool createR
upStream.Flush();
offset += readBytes;
}
}
}

// disconnect FTP stream before exiting
upStream.Close();
// disconnect FTP stream before exiting
upStream.Dispose();

// FIX : if this is not added, there appears to be "stale data" on the socket
// listen for a success/failure reply
if (!m_threadSafeDataChannels) {
FtpReply status = GetReply();
// FIX : if this is not added, there appears to be "stale data" on the socket
// listen for a success/failure reply
if (!m_threadSafeDataChannels) {
FtpReply status = GetReply();
}
}
}

return true;
Expand All @@ -2013,7 +2054,7 @@ private bool UploadFileInternal(Stream fileData, string remotePath, bool createR

// close stream before throwing error
try {
upStream.Close();
upStream.Dispose();
} catch (Exception) { }

// catch errors during upload
Expand All @@ -2038,7 +2079,7 @@ private bool DownloadFileInternal(string remotePath, Stream outStream) {

// close stream before throwing error
try {
downStream.Close();
downStream.Dispose();
} catch (Exception) { }

throw new FtpException("Cannot download file since file has length of 0. Use the FtpDataType.Binary data type and try again.");
Expand Down Expand Up @@ -2076,8 +2117,13 @@ private bool DownloadFileInternal(string remotePath, Stream outStream) {
// resume if server disconnects midway (fixes #39)
if (ex.InnerException != null) {
var ie = ex.InnerException as System.Net.Sockets.SocketException;
if (ie != null && ie.ErrorCode == 10054) {
downStream.Close();
#if CORE
int code = (int)ie.SocketErrorCode;
#else
int code = ie.ErrorCode;
#endif
if (ie != null && code == 10054) {
downStream.Dispose();
downStream = OpenRead(remotePath, restart: offset);
} else throw;
} else throw;
Expand All @@ -2089,7 +2135,7 @@ private bool DownloadFileInternal(string remotePath, Stream outStream) {

// disconnect FTP stream before exiting
outStream.Flush();
downStream.Close();
downStream.Dispose();

// FIX : if this is not added, there appears to be "stale data" on the socket
// listen for a success/failure reply
Expand All @@ -2103,7 +2149,7 @@ private bool DownloadFileInternal(string remotePath, Stream outStream) {

// close stream before throwing error
try {
downStream.Close();
downStream.Dispose();
} catch (Exception) { }

// absorb "file does not exist" exceptions and simply return false
Expand All @@ -2116,9 +2162,9 @@ private bool DownloadFileInternal(string remotePath, Stream outStream) {
}
}

#endregion
#endregion

#region File Management
#region File Management

/// <summary>
/// Deletes a file on the server
Expand Down Expand Up @@ -2640,9 +2686,9 @@ public void EndRename(IAsyncResult ar) {
}


#endregion
#endregion

#region Link Dereferencing
#region Link Dereferencing

/// <summary>
/// Recursively dereferences a symbolic link. See the
Expand Down Expand Up @@ -2756,9 +2802,9 @@ public FtpListItem EndDereferenceLink(IAsyncResult ar) {
return GetAsyncDelegate<AsyncDereferenceLink>(ar).EndInvoke(ar);
}

#endregion
#endregion

#region File Listing
#region File Listing

/// <summary>
/// Returns information about a file system object. You should check the Capabilities
Expand Down Expand Up @@ -3088,9 +3134,9 @@ public FtpListItem[] EndGetListing(IAsyncResult ar) {
return GetAsyncDelegate<AsyncGetListing>(ar).EndInvoke(ar);
}

#endregion
#endregion

#region Name Listing
#region Name Listing

/// <summary>
/// Returns a file/directory listing using the NLST command.
Expand Down Expand Up @@ -3195,9 +3241,9 @@ public string[] EndGetNameListing(IAsyncResult ar) {
return GetAsyncDelegate<AsyncGetNameListing>(ar).EndInvoke(ar);
}

#endregion
#endregion

#region Misc Methods
#region Misc Methods

private static string DecodeUrl(string url) {
#if CORE
Expand Down Expand Up @@ -3807,9 +3853,9 @@ private bool IsProxy() {
return (this is FtpClientProxy);
}

#endregion
#endregion

#region Static API
#region Static API

/// <summary>
/// Connects to the specified URI. If the path specified by the URI ends with a
Expand Down Expand Up @@ -4020,7 +4066,7 @@ private static void CheckURI(Uri uri) {
}
}

#endregion
#endregion

}
}
Loading

0 comments on commit b239b27

Please sign in to comment.