Added and reworked a few methods in the WEBServer and FTPServer class

This commit is contained in:
Anatole SCHRAMM 2022-10-03 14:15:13 +02:00
parent 8e655ed8fe
commit 59be94afb9
2 changed files with 66 additions and 14 deletions

View File

@ -16,18 +16,18 @@ class FTPServer : public TCPServer<T>
enum FTPClientDataTransfer {NONE = 0, LIST_DF, NLST_DF, RETR_DF, STOR_DF, APPE_DF}; enum FTPClientDataTransfer {NONE = 0, LIST_DF, NLST_DF, RETR_DF, STOR_DF, APPE_DF};
enum FileTransferStatus {OK, NOT_FOUND, NO_FILE_NAME, MALLOC_ERROR}; enum FileTransferStatus {OK, NOT_FOUND, NO_FILE_NAME, MALLOC_ERROR};
enum BinaryFlag {OFF = 0, ON}; enum BinaryFlag {OFF = 0, ON};
enum FTPMsgCode {_150, _200, _215, _220, _221, _230, _226, _227, _250, _257, _331, _350, _451, _5_502, _504, _530, _550 }; enum FTPMsgCode {_150, _200, _215, _220, _221, _230, _226, _227, _250, _257, _331, _350, _451, _502, _504, _530, _550 };
FTPServer(uint16_t port = 21, SDClass *sdClass = NULL, const char *login = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize), FTPServer(uint16_t port = 21, SDClass *sdClass = NULL, const char *username = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize),
_dataServer(_dataPort), _dataServer(_dataPort),
_sdClass(sdClass) _sdClass(sdClass)
{ {
if (login != NULL) if (username != NULL)
{ {
if (strlen(login)) if (strlen(username))
{ {
_login = (char *)malloc((sizeof(char) * strlen(login)) + 1); _username = (char *)malloc((sizeof(char) * strlen(username)) + 1);
strcpy(_login, login); strcpy(_username, username);
} }
} }
@ -50,7 +50,7 @@ class FTPServer : public TCPServer<T>
virtual ~FTPServer() virtual ~FTPServer()
{ {
_dataServer.stop(); _dataServer.stop();
free(_login); free(_password); free(_FTPDir); free(_username); free(_password); free(_FTPDir);
} }
virtual void stop() virtual void stop()
@ -68,7 +68,51 @@ class FTPServer : public TCPServer<T>
{ {
free(_FTPDir); free(_FTPDir);
_FTPDir = (char *)malloc((strlen(FTPDir) * sizeof(char)) + 1); _FTPDir = (char *)malloc((strlen(FTPDir) * sizeof(char)) + 1);
strcpy(_FTPDir, FTPDir); if(_FTPDir)
{
strcpy(_FTPDir, FTPDir);
}
}
else
{
free(_FTPDir);
_FTPDir = nullptr;
}
}
virtual void setUsername(const char *username)
{
if(username)
{
free(_username);
_username = (char *)malloc((strlen(username) * sizeof(char)) + 1);
if(_username)
{
strcpy(_username, username);
}
}
else
{
free(_username);
_username = nullptr;
}
}
virtual void setPassword(const char *password)
{
if(password)
{
free(_password);
_password = (char *)malloc((strlen(password) * sizeof(char)) + 1);
if(_password)
{
strcpy(_password, password);
}
}
else
{
free(_password);
_password = nullptr;
} }
} }
@ -334,7 +378,7 @@ class FTPServer : public TCPServer<T>
if (strcmp(client->_ftpCommand, "USER") == 0) if (strcmp(client->_ftpCommand, "USER") == 0)
{ {
//We check if we set a login and a password : //We check if we set a login and a password :
if (_login != NULL && _password != NULL) if (_username != NULL && _password != NULL)
{ {
if (client->_cmdParameters->count() > 0) if (client->_cmdParameters->count() > 0)
{ {
@ -355,7 +399,7 @@ class FTPServer : public TCPServer<T>
//We now check if the username and password correspond //We now check if the username and password correspond
if (client->_cmdParameters->count() > 0) if (client->_cmdParameters->count() > 0)
{ {
if (strcmp(_login, client->_username) == 0 && strcmp(_password, client->_cmdParameters->getAt(0)->getString()) == 0) if (strcmp(_username, client->_username) == 0 && strcmp(_password, client->_cmdParameters->getAt(0)->getString()) == 0)
{ {
client->_loggedIn = true; client->_loggedIn = true;
client->_client.println("230 User logged in, proceed."); client->_client.println("230 User logged in, proceed.");
@ -792,7 +836,7 @@ class FTPServer : public TCPServer<T>
}*/ }*/
else else
{ {
client->_client.println("502 Command not implemented."); sendInfoResponse(_502, client);
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.println("Command not implemented"); Serial.println("Command not implemented");
#endif #endif
@ -1015,7 +1059,10 @@ class FTPServer : public TCPServer<T>
client->_client.printf_P(PSTR("200 Command okay. %s\r\n"), msg); client->_client.printf_P(PSTR("200 Command okay. %s\r\n"), msg);
break; break;
case _221: case _221:
client->_client.printf_P(PSTR("221 Service closing control connection. %s\r\n")); client->_client.printf_P(PSTR("221 Service closing control connection. %s\r\n"), msg);
break;
case _502:
client->_client.printf_P(PSTR("502 Command not implemented. %s\r\n"), msg);
break; break;
case _530: case _530:
client->_client.printf_P(PSTR("530 Password required. %s\r\n"), msg); client->_client.printf_P(PSTR("530 Password required. %s\r\n"), msg);
@ -1028,7 +1075,7 @@ class FTPServer : public TCPServer<T>
} }
} }
char *_login = nullptr; char *_username = nullptr;
char *_password = nullptr; char *_password = nullptr;
char *_FTPDir = nullptr; char *_FTPDir = nullptr;
uint16_t _dataPort = 1024; uint16_t _dataPort = 1024;

View File

@ -80,12 +80,17 @@ class WEBServer : public TCPServer<T>, public HttpConstants
void setWWWDir(const char *WWWDir) void setWWWDir(const char *WWWDir)
{ {
if(WWWDir != nullptr) if(WWWDir)
{ {
free(_WWWDir); free(_WWWDir);
_WWWDir = (char *)malloc((strlen(WWWDir) * sizeof(char)) + 1); _WWWDir = (char *)malloc((strlen(WWWDir) * sizeof(char)) + 1);
strcpy(_WWWDir, WWWDir); strcpy(_WWWDir, WWWDir);
} }
else
{
free(_WWWDir);
_WWWDir = nullptr;
}
} }
protected: protected:
private: private: