Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/compile config delay sending 226 #63

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fineftp-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ target_compile_options(${PROJECT_NAME} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
/W4>)

# The below is used to configure an optional delay when sending the 226 response code when a file has been fetched.
# The background for this option is that an FTP client implementation has been observed to close the data connection
# as soon as it receives the 226 status code - even though it hasn't received/ all data, yet. To improve
# interoperability with such buggy clients, sending of the 226 status code can be delayed a bit. The delay is specified
# in milliseconds. If the delay is 0, no delay is introduced.
set(FINEFTP_SERVER_DELAY_226_RESP_MS 0 CACHE STRING
"An optional delay (in ms) for the 226 response when a file has been fetched. Used to improve interoperability with buggy clients.")
target_compile_definitions(${PROJECT_NAME} PRIVATE DELAY_226_RESP_MS=${FINEFTP_SERVER_DELAY_226_RESP_MS})

# Add own public include directory
target_include_directories(${PROJECT_NAME}
Expand Down
32 changes: 29 additions & 3 deletions fineftp-server/src/ftp_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>
#include <cassert> // assert
#include <cctype> // std::iscntrl, toupper
#include <chrono>
#include <cstddef>
#include <cstdio>
#include <fstream>
Expand Down Expand Up @@ -47,6 +48,7 @@ namespace fineftp
, ftp_working_directory_("/")
, data_acceptor_ (io_service)
, data_socket_strand_ (io_service)
, timer_ (io_service)
{
}

Expand Down Expand Up @@ -454,9 +456,9 @@ namespace fineftp
// Form reply string
std::stringstream stream;
stream << "(";
for (const char byte : ip_bytes)
for (const auto byte : ip_bytes)
{
stream << static_cast<int>(byte) << ",";
stream << static_cast<unsigned int>(byte) << ",";
}
stream << ((port >> 8) & 0xff) << "," << (port & 0xff) << ")";

Expand Down Expand Up @@ -1302,7 +1304,31 @@ namespace fineftp
}
else
{
me->sendFtpMessage(FtpReplyCode::CLOSING_DATA_CONNECTION, "Done");
// Close Data Socket properly
{
asio::error_code errc;
data_socket->shutdown(asio::socket_base::shutdown_both, errc);
data_socket->close(errc);
}

// Ugly work-around:
// An FTP client implementation has been observed to close the data connection
// as soon as it receives the 226 status code - even though it hasn't received
// all data, yet. To improve interoperability with such buggy clients, sending
// of the 226 status code can be delayed a bit. The delay is defined through a
// preprocessor definition. If the delay is 0, no delay is introduced at all.
#if (0 == DELAY_226_RESP_MS)
me->sendFtpMessage(FtpReplyCode::CLOSING_DATA_CONNECTION, "Done");
#else
me->timer_.expires_after(std::chrono::milliseconds{DELAY_226_RESP_MS});
me->timer_.async_wait(me->data_socket_strand_.wrap([me](const asio::error_code& ec)
{
if (ec != asio::error::operation_aborted)
{
me->sendFtpMessage(FtpReplyCode::CLOSING_DATA_CONNECTION, "Done");
}
}));
#endif
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions fineftp-server/src/ftp_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,7 @@ namespace fineftp
asio::io_service::strand data_socket_strand_;
std::weak_ptr<asio::ip::tcp::socket> data_socket_weakptr_;
std::deque<std::shared_ptr<std::vector<char>>> data_buffer_;

asio::steady_timer timer_;
};
}