-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
15 changed files
with
699 additions
and
637 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
|
||
/** @} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.