Skip to content

Commit

Permalink
Merge pull request #1039 from FanDjango/ExecuteTweak
Browse files Browse the repository at this point in the history
Separate clean command and postexecute functionality
  • Loading branch information
FanDjango authored Nov 12, 2022
2 parents 5ed03bc + 90bb6b9 commit b35a466
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
5 changes: 3 additions & 2 deletions FluentFTP/Client/AsyncClient/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FluentFTP.Helpers;
using System.Threading;
using System.Threading.Tasks;
using FluentFTP.Client.Modules;

namespace FluentFTP {
public partial class AsyncFtpClient {
Expand Down Expand Up @@ -41,9 +42,9 @@ public async Task<FtpReply> Execute(string command, CancellationToken token) {
}

// hide sensitive data from logs
string commandTxt = OnPostExecute(command);
string commandClean = LogMaskModule.MaskCommand(this, command);

Log(FtpTraceLevel.Info, "Command: " + commandTxt);
Log(FtpTraceLevel.Info, "Command: " + commandClean);

// send command to FTP server
await m_stream.WriteLineAsync(m_textEncoding, command, token);
Expand Down
3 changes: 2 additions & 1 deletion FluentFTP/Client/AsyncClient/GetReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using FluentFTP.Client.Modules;

namespace FluentFTP {
public partial class AsyncFtpClient {
Expand Down Expand Up @@ -62,7 +63,7 @@ protected async Task<FtpReply> GetReplyAsyncInternal(CancellationToken token, st
LogWithPrefix(FtpTraceLevel.Verbose, "Waiting for a response");
}
else {
LogWithPrefix(FtpTraceLevel.Verbose, "Waiting for response to: " + OnPostExecute(command));
LogWithPrefix(FtpTraceLevel.Verbose, "Waiting for response to: " + LogMaskModule.MaskCommand(this, command));
}

if (!IsConnected) {
Expand Down
26 changes: 10 additions & 16 deletions FluentFTP/Client/BaseClient/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,35 @@ FtpReply IInternalFtpClient.ExecuteInternal(string command) {
}

// hide sensitive data from logs
string commandClean = OnPostExecute(command);
string commandClean = LogMaskModule.MaskCommand(this, command);

Log(FtpTraceLevel.Info, "Command: " + commandClean);

// send command to FTP server
m_stream.WriteLine(m_textEncoding, command);
LastCommandTimestamp = DateTime.UtcNow;
reply = GetReplyInternal(command);
}
if (reply.Success) {
OnPostExecute(command);
}

return reply;
return reply;
}
}

protected string OnPostExecute(string command) {

// hide sensitive data from logs
command = LogMaskModule.MaskCommand(this, command);
protected void OnPostExecute(string command) {

// A CWD will invalidate the cached value.
// Update stored values
if (command.StartsWith("CWD ", StringComparison.Ordinal)) {
Status.LastWorkingDir = null;
Status.LastWorkingDir = command.Substring(4).Trim();
}

// A TYPE I could invalidate the cached value.
else if (command.StartsWith("TYPE I", StringComparison.Ordinal)) {
Status.CurrentDataType = FtpDataType.Binary;
}

// A TYPE A could invalidate the cached value.
else if (command.StartsWith("TYPE A", StringComparison.Ordinal)) {
Status.CurrentDataType = FtpDataType.ASCII;
}

return command;
}

}
}
}
2 changes: 1 addition & 1 deletion FluentFTP/Client/BaseClient/GetReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected FtpReply GetReplyInternal(string command, bool exhaustNoop, int timeOu
LogWithPrefix(FtpTraceLevel.Verbose, "Waiting for a response");
}
else {
LogWithPrefix(FtpTraceLevel.Verbose, "Waiting for response to: " + OnPostExecute(command));
LogWithPrefix(FtpTraceLevel.Verbose, "Waiting for response to: " + LogMaskModule.MaskCommand(this, command));
}

if (!IsConnected) {
Expand Down

0 comments on commit b35a466

Please sign in to comment.