Add mbed helpers

This commit is contained in:
2025-01-23 13:34:38 -06:00
parent f235ab11f6
commit 1336d84006
11 changed files with 763 additions and 4 deletions

View File

@ -0,0 +1,56 @@
#pragma once
#include "../Streams/Stream.hpp"
namespace Tesses::Framework::Crypto
{
bool HaveCrypto();
std::string Base64_Encode(std::vector<uint8_t> data);
std::vector<uint8_t> Base64_Decode(std::string str);
class Sha1 {
void* inner;
public:
Sha1();
bool Start();
bool Update(const uint8_t* buffer, size_t sz);
bool Update(Tesses::Framework::Streams::Stream* strm);
bool Update(Tesses::Framework::Streams::Stream& strm);
std::vector<uint8_t> Finish();
~Sha1();
static std::vector<uint8_t> ComputeHash(const uint8_t* buffer, size_t len);
static std::vector<uint8_t> ComputeHash(Tesses::Framework::Streams::Stream* strm);
static std::vector<uint8_t> ComputeHash(Tesses::Framework::Streams::Stream& strm);
};
class Sha256 {
void* inner;
bool is224;
public:
Sha256();
bool Start(bool is224=false);
bool Is224();
bool Update(const uint8_t* buffer, size_t sz);
bool Update(Tesses::Framework::Streams::Stream* strm);
bool Update(Tesses::Framework::Streams::Stream& strm);
std::vector<uint8_t> Finish();
~Sha256();
static std::vector<uint8_t> ComputeHash(const uint8_t* buffer, size_t len,bool is224=false);
static std::vector<uint8_t> ComputeHash(Tesses::Framework::Streams::Stream* strm,bool is224=false);
static std::vector<uint8_t> ComputeHash(Tesses::Framework::Streams::Stream& strm,bool is224=false);
};
class Sha512 {
void* inner;
bool is384;
public:
Sha512();
bool Start(bool is384=false);
bool Is384();
bool Update(const uint8_t* buffer, size_t sz);
bool Update(Tesses::Framework::Streams::Stream* strm);
bool Update(Tesses::Framework::Streams::Stream& strm);
std::vector<uint8_t> Finish();
~Sha512();
static std::vector<uint8_t> ComputeHash(const uint8_t* buffer, size_t len,bool is384=false);
static std::vector<uint8_t> ComputeHash(Tesses::Framework::Streams::Stream* strm,bool is384=false);
static std::vector<uint8_t> ComputeHash(Tesses::Framework::Streams::Stream& strm,bool is384=false);
};
}

View File

@ -11,6 +11,37 @@ namespace Tesses::Framework::Http
virtual ~ServerContextData();
};
class WebSocketMessage {
public:
std::vector<uint8_t> data;
bool isBinary;
WebSocketMessage();
WebSocketMessage(std::vector<uint8_t> data);
WebSocketMessage(const void* data, size_t len);
WebSocketMessage(std::string message);
std::string ToString();
};
class WebSocketConnection {
public:
virtual void OnOpen(std::function<void(WebSocketMessage&)> sendMessage, std::function<void()> ping)=0;
virtual void OnReceive(WebSocketMessage& message)=0;
virtual void OnClose(bool clean)=0;
virtual ~WebSocketConnection();
};
class CallbackWebSocketConnection : public WebSocketConnection {
public:
std::function<void(std::function<void(WebSocketMessage&)>,std::function<void()>)> onOpen;
std::function<void(WebSocketMessage&)> onReceive;
std::function<void(bool)> onClose;
CallbackWebSocketConnection();
CallbackWebSocketConnection(std::function<void(std::function<void(WebSocketMessage&)>,std::function<void()>)> onOpen, std::function<void(WebSocketMessage&)> onReceive, std::function<void(bool)> onClose);
void OnOpen(std::function<void(WebSocketMessage&)> sendMessage, std::function<void()> ping);
void OnReceive(WebSocketMessage& message);
void OnClose(bool clean);
};
class ServerContext {
bool sent;
Tesses::Framework::Streams::Stream* strm;
@ -53,6 +84,8 @@ namespace Tesses::Framework::Http
ServerContext& WithMimeType(std::string mime);
ServerContext& WithContentDisposition(std::string filename, bool isInline);
ServerContext& WriteHeaders();
void StartWebSocketSession(std::function<void(std::function<void(WebSocketMessage&)>,std::function<void()>)> onOpen, std::function<void(WebSocketMessage&)> onReceive, std::function<void(bool)> onClose);
void StartWebSocketSession(WebSocketConnection& connection);
template<class T>
T* GetServerContentData(std::string tag)

View File

@ -1,5 +1,6 @@
#pragma once
#include "../Common.hpp"
#include <algorithm>
namespace Tesses::Framework::Http
{
@ -67,9 +68,18 @@ namespace Tesses::Framework::Http
NotExtended=510,
NetworkAuthenticationRequired=511
} StatusCode;
struct CaseInsensitiveLess {
bool operator() (const std::string& s1, const std::string& s2) const {
std::string str1(s1.length(),' ');
std::string str2(s2.length(),' ');
std::transform(s1.begin(), s1.end(), str1.begin(), tolower);
std::transform(s2.begin(), s2.end(), str2.begin(), tolower);
return str1 < str2;
}
};
class HttpDictionary {
public:
std::map<std::string,std::vector<std::string>> kvp;
std::map<std::string,std::vector<std::string>,CaseInsensitiveLess> kvp;
void Clear();
void Clear(std::string key, bool kvpExistsAfter);
void SetValue(std::string key, std::string value);
@ -97,6 +107,8 @@ namespace Tesses::Framework::Http
bool TryGetFirstDouble(std::string key, double& value);
bool GetFirstBoolean(std::string key);
bool AnyEquals(std::string key, std::string value);
};
class Uri {

View File

@ -39,5 +39,6 @@ namespace Tesses::Framework::Streams
size_t WriteTo(const uint8_t* buff, size_t sz, std::string ip, uint16_t port);
static std::vector<std::pair<std::string,std::string>> GetIPs(bool ipV6=false);
~NetworkStream();
void SetNoDelay(bool noDelay);
};
}

View File

@ -20,4 +20,5 @@
#include "Filesystem/MountableFilesystem.hpp"
#include "Filesystem/MemoryFilesystem.hpp"
#include "Crypto/ClientTLSStream.hpp"
#include "Crypto/MbedHelpers.hpp"
#include "Lazy.hpp"