Multi-platform http server (#646)
- Add server support to `http-module` - Ensure `DevelopmentService` works on all platforms (including posix) - Ensure the built-in web server with dashboard works on all platforms (inluding posix). It still has some issues with certain featuers, but the basics work.
This commit is contained in:
committed by
GitHub
parent
a0b2ee7ebc
commit
1b16184a72
@@ -1,70 +0,0 @@
|
||||
#pragma once
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include <Tactility/RecursiveMutex.h>
|
||||
#include <esp_http_server.h>
|
||||
|
||||
namespace tt::network {
|
||||
|
||||
class HttpServer {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief Function for URI matching used by server.
|
||||
*
|
||||
* @param[in] referenceUri URI/template with respect to which the other URI is matched
|
||||
* @param[in] uriToCheck URI/template being matched to the reference URI/template
|
||||
* @param[in] matchUpTo For specifying the actual length of `uri_to_match` up to
|
||||
* which the matching algorithm is to be applied (The maximum
|
||||
* value is `strlen(uri_to_match)`, independent of the length
|
||||
* of `reference_uri`)
|
||||
* @return true on match
|
||||
*/
|
||||
typedef bool (*UriMatchFunction)(const char* referenceUri, const char* uriToCheck, size_t matchUpTo);
|
||||
|
||||
private:
|
||||
|
||||
const uint32_t port;
|
||||
const std::string address;
|
||||
const uint32_t stackSize;
|
||||
const UriMatchFunction matchUri;
|
||||
|
||||
std::vector<httpd_uri_t> handlers;
|
||||
|
||||
RecursiveMutex mutex;
|
||||
httpd_handle_t server = nullptr;
|
||||
|
||||
bool startInternal();
|
||||
void stopInternal();
|
||||
|
||||
public:
|
||||
|
||||
HttpServer(
|
||||
uint32_t port,
|
||||
const std::string& address,
|
||||
std::vector<httpd_uri_t> handlers,
|
||||
uint32_t stackSize = 5120,
|
||||
UriMatchFunction matchUri = httpd_uri_match_wildcard
|
||||
) :
|
||||
port(port),
|
||||
address(address),
|
||||
stackSize(stackSize),
|
||||
matchUri(matchUri),
|
||||
handlers(std::move(handlers))
|
||||
{}
|
||||
|
||||
bool start();
|
||||
|
||||
void stop();
|
||||
|
||||
bool isStarted() const {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
return server != nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <http/server.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
// Helper functions for HttpServerRequest from http-module
|
||||
namespace tt::network {
|
||||
|
||||
bool getHeaderOrSendError(struct HttpServerRequest* request, const std::string& name, std::string& value);
|
||||
|
||||
bool getMultiPartBoundaryOrSendError(struct HttpServerRequest* request, std::string& boundary);
|
||||
|
||||
bool getQueryOrSendError(struct HttpServerRequest* request, std::string& query);
|
||||
|
||||
/** @return the received text up to and including @a terminator, or "" if the connection failed
|
||||
* or the preamble exceeded its bounded maximum length without finding @a terminator. */
|
||||
std::string receiveTextUntil(struct HttpServerRequest* request, const std::string& terminator);
|
||||
|
||||
bool readAndDiscardOrSendError(struct HttpServerRequest* request, const std::string& toRead);
|
||||
|
||||
size_t receiveFile(struct HttpServerRequest* request, size_t length, const std::string& filePath);
|
||||
|
||||
}
|
||||
@@ -1,31 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include <esp_http_server.h>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace tt::network {
|
||||
|
||||
bool getHeaderOrSendError(httpd_req_t* request, const std::string& name, std::string& value);
|
||||
|
||||
bool getMultiPartBoundaryOrSendError(httpd_req_t* request, std::string& boundary);
|
||||
|
||||
bool getQueryOrSendError(httpd_req_t* request, std::string& query);
|
||||
|
||||
std::unique_ptr<char[]> receiveByteArray(httpd_req_t* request, size_t length, size_t& bytesRead);
|
||||
|
||||
std::string receiveTextUntil(httpd_req_t* request, const std::string& terminator);
|
||||
|
||||
/** Pure string parsing, no request I/O */
|
||||
std::map<std::string, std::string> parseContentDisposition(const std::vector<std::string>& input);
|
||||
|
||||
bool readAndDiscardOrSendError(httpd_req_t* request, const std::string& toRead);
|
||||
|
||||
size_t receiveFile(httpd_req_t* request, size_t length, const std::string& filePath);
|
||||
|
||||
}
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
@@ -5,6 +5,12 @@
|
||||
|
||||
namespace tt::settings::webserver {
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
constexpr uint16_t DEFAULT_PORT = 80;
|
||||
#else
|
||||
constexpr uint16_t DEFAULT_PORT = 8080;
|
||||
#endif
|
||||
|
||||
enum class WiFiMode : uint8_t {
|
||||
Station = 0, // Connect to existing WiFi network
|
||||
AccessPoint = 1 // Create own WiFi network
|
||||
@@ -23,7 +29,7 @@ struct WebServerSettings {
|
||||
|
||||
// Web Server Settings
|
||||
bool webServerEnabled = false;
|
||||
uint16_t webServerPort = 80; // Default: 80
|
||||
uint16_t webServerPort = DEFAULT_PORT;
|
||||
|
||||
// Optional HTTP Basic Auth
|
||||
bool webServerAuthEnabled = false;
|
||||
|
||||
Reference in New Issue
Block a user