Skip to content

Commit

Permalink
Tidy network files (#2824)
Browse files Browse the repository at this point in the history
The Network Component files are laid out in a fairly predictable way, with a few exceptions whose headers and source modules are all in the root directory:

- FtpServer
- HttpClient
- HttpServer
- WebsocketClient
- MqttClient

This PR moves those files into the appropriate subdirectory but replaces the headers with stubs.
This should make maintenance easier as the actual code and header are kept together in the expected location.
  • Loading branch information
mikee47 authored Jun 23, 2024
1 parent 34ec9b2 commit 63877bb
Show file tree
Hide file tree
Showing 15 changed files with 699 additions and 637 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
****/

#include "FtpServer.h"
#include "Ftp/FtpServerConnection.h"
#include "FtpServerConnection.h"

TcpConnection* CustomFtpServer::createClient(tcp_pcb* clientTcp)
{
Expand Down
85 changes: 85 additions & 0 deletions Sming/Components/Network/src/Network/Ftp/FtpServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* FtpServer.h
*
****/

#pragma once

#include "../TcpServer.h"
#include <WHashMap.h>
#include <FileSystem.h>

class FtpServerConnection;

/** @defgroup ftpserver FTP server
* @ingroup tcpserver
* @brief Base implementation for FTP server
*/
class CustomFtpServer : public TcpServer
{
friend class FtpServerConnection;

public:
CustomFtpServer(IFS::FileSystem* fileSystem = nullptr) : fileSystem(fileSystem)
{
setTimeOut(900);
}

/**
* @brief Validate user
* @param login User name
* @param pass User password
* @retval IFS::UserRole Returns assigned user role, None if user not validated
*/
virtual IFS::UserRole validateUser(const char* login, const char* pass) = 0;

protected:
TcpConnection* createClient(tcp_pcb* clientTcp) override;

/**
* @brief Handle an incoming command
* @param cmd The command identifier, e.g. LIST
* @param data Any command arguments
* @param connection The associated TCP connection to receive any response
* @retval bool true if command handled and response sent
*/
virtual bool onCommand(String cmd, String data, FtpServerConnection& connection)
{
return false;
}

IFS::FileSystem* getFileSystem() const
{
return fileSystem ?: ::getFileSystem();
}

private:
IFS::FileSystem* fileSystem;
};

/**
* @ingroup ftpserver
* @brief Provides FTP server
*/
class FtpServer : public CustomFtpServer
{
public:
void addUser(const String& login, const String& pass, IFS::UserRole userRole = IFS::UserRole::Admin);
IFS::UserRole validateUser(const char* login, const char* pass) override;

protected:
bool onCommand(String cmd, String data, FtpServerConnection& connection) override;

private:
struct User {
String password;
IFS::UserRole role;
};
using UserList = HashMap<String, User>;
UserList users;
};
74 changes: 1 addition & 73 deletions Sming/Components/Network/src/Network/FtpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,4 @@

#pragma once

#include "TcpServer.h"
#include "WHashMap.h"
#include <FileSystem.h>

class FtpServerConnection;

/** @defgroup ftpserver FTP server
* @ingroup tcpserver
* @brief Base implementation for FTP server
*/
class CustomFtpServer : public TcpServer
{
friend class FtpServerConnection;

public:
CustomFtpServer(IFS::FileSystem* fileSystem = nullptr) : fileSystem(fileSystem)
{
setTimeOut(900);
}

/**
* @brief Validate user
* @param login User name
* @param pass User password
* @retval IFS::UserRole Returns assigned user role, None if user not validated
*/
virtual IFS::UserRole validateUser(const char* login, const char* pass) = 0;

protected:
TcpConnection* createClient(tcp_pcb* clientTcp) override;

/**
* @brief Handle an incoming command
* @param cmd The command identifier, e.g. LIST
* @param data Any command arguments
* @param connection The associated TCP connection to receive any response
* @retval bool true if command handled and response sent
*/
virtual bool onCommand(String cmd, String data, FtpServerConnection& connection)
{
return false;
}

IFS::FileSystem* getFileSystem() const
{
return fileSystem ?: ::getFileSystem();
}

private:
IFS::FileSystem* fileSystem;
};

/**
* @ingroup ftpserver
* @brief Provides FTP server
*/
class FtpServer : public CustomFtpServer
{
public:
void addUser(const String& login, const String& pass, IFS::UserRole userRole = IFS::UserRole::Admin);
IFS::UserRole validateUser(const char* login, const char* pass) override;

protected:
bool onCommand(String cmd, String data, FtpServerConnection& connection) override;

private:
struct User {
String password;
IFS::UserRole role;
};
using UserList = HashMap<String, User>;
UserList users;
};
#include "Ftp/FtpServer.h"
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
****/

#include "HttpClient.h"
#include "Data/Stream/FileStream.h"
#include <Data/Stream/FileStream.h>

HttpClient::HttpConnectionPool HttpClient::httpConnectionPool;
SimpleTimer HttpClient::cleanUpTimer;
Expand Down
146 changes: 146 additions & 0 deletions Sming/Components/Network/src/Network/Http/HttpClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* HttpClient.h
*
* Modified: 2017 - Slavey Karadzhov <[email protected]>
*
****/

/** @defgroup httpclient HTTP client
* @brief Provides HTTP/S client
* @ingroup tcpclient
* @{
*/

#pragma once

#include "../TcpClient.h"
#include "HttpCommon.h"
#include "HttpRequest.h"
#include "HttpClientConnection.h"
#include <Data/Stream/LimitedMemoryStream.h>
#include <SimpleTimer.h>

class HttpClient
{
public:
/**
* @brief HttpClient destructor
* @note DON'T call cleanup.
* If you want to free all resources from HttpClients the correct sequence will be to
* 1. Delete all instances of HttpClient
* 2. Call the static method HttpClient::cleanup();
*/
virtual ~HttpClient()
{
}

/* High-Level Methods */

bool sendRequest(const Url& url, RequestCompletedDelegate requestComplete)
{
return send(createRequest(url)->setMethod(HTTP_GET)->onRequestComplete(requestComplete));
}

bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers,
RequestCompletedDelegate requestComplete)
{
return send(createRequest(url)->setMethod(method)->setHeaders(headers)->onRequestComplete(requestComplete));
}

bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers, const String& body,
RequestCompletedDelegate requestComplete)
{
return send(createRequest(url)->setMethod(method)->setHeaders(headers)->setBody(body)->onRequestComplete(
requestComplete));
}

bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers, String&& body,
RequestCompletedDelegate requestComplete) noexcept
{
return send(createRequest(url)
->setMethod(method)
->setHeaders(headers)
->setBody(std::move(body))
->onRequestComplete(requestComplete));
}

/**
* @brief Queue request to download content as string (in memory)
* @param url URL from which the content will be fetched
* @param requestComplete Completion callback
* @param maxLength maximum bytes to store in memory. If the response is bigger than `maxLength` then the rest bytes will be discarded.
* Use this parameter wisely as setting the value too high may consume all available RAM resulting in
* device restart and Denial-Of-Service
*/
bool downloadString(const Url& url, RequestCompletedDelegate requestComplete,
size_t maxLength = NETWORK_SEND_BUFFER_SIZE)
{
return send(createRequest(url)
->setMethod(HTTP_GET)
->setResponseStream(new LimitedMemoryStream(maxLength))
->onRequestComplete(requestComplete));
}

bool downloadFile(const Url& url, RequestCompletedDelegate requestComplete = nullptr)
{
return downloadFile(url, nullptr, requestComplete);
}

/**
* @brief Queue request to download a file
* @param url Source of file data
* @param saveFileName Path to save file to. Optional: specify nullptr to use name from url
* @param requestComplete Completion callback
*/
bool downloadFile(const Url& url, const String& saveFileName, RequestCompletedDelegate requestComplete = nullptr);

/* Low Level Methods */

/*
* @brief This method queues a request and sends it, once it is connected to the remote server.
* @param HttpRequest* request The request object will be freed inside of the method.
* Do not try to reuse it outside of the send method as it will lead to unpredicted results
*
* @retval bool true if the request was queued, false otherwise.
*
*/
bool send(HttpRequest* request);

/** @brief Helper function to create a new request on a URL
* @param url
* @retval HttpRequest*
*/
HttpRequest* createRequest(const Url& url)
{
return new HttpRequest(url);
}

/**
* @brief Use this method to clean all object pools
*/
static void cleanup()
{
httpConnectionPool.clear();
}

protected:
String getCacheKey(const Url& url)
{
return url.Host + ':' + url.getPort();
}

protected:
using HttpConnectionPool = ObjectMap<String, HttpClientConnection>;
static HttpConnectionPool httpConnectionPool;

private:
static SimpleTimer cleanUpTimer;
static void cleanInactive();
};

/** @} */
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
****/

#include "HttpServer.h"
#include "TcpClient.h"
#include "WString.h"

void HttpServer::configure(const HttpServerSettings& settings)
{
Expand Down
Loading

0 comments on commit 63877bb

Please sign in to comment.