Make streams and vfs and http shared_ptr

This commit is contained in:
2025-09-29 02:22:27 -05:00
parent 71d0e36a5c
commit d785508571
61 changed files with 541 additions and 951 deletions

View File

@@ -6,8 +6,8 @@ namespace Tesses::Framework::Http
{
class FileServer : public IHttpServer
{
Tesses::Framework::Filesystem::VFS* vfs;
bool ownsVFS;
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
bool SendFile(ServerContext& ctx,Tesses::Framework::Filesystem::VFSPath path);
@@ -17,8 +17,8 @@ namespace Tesses::Framework::Http
std::vector<std::string> defaultNames;
FileServer(std::filesystem::path path,bool allowListing,bool spa);
FileServer(std::filesystem::path path,bool allowListing, bool spa, std::vector<std::string> defaultNames);
FileServer(Tesses::Framework::Filesystem::VFS* fs, bool owns, bool allowListing, bool spa);
FileServer(Tesses::Framework::Filesystem::VFS* fs, bool owns, bool allowListing, bool spa, std::vector<std::string> defaultNames);
FileServer(std::shared_ptr<Tesses::Framework::Filesystem::VFS> fs, bool allowListing, bool spa);
FileServer(std::shared_ptr<Tesses::Framework::Filesystem::VFS> fs, bool allowListing, bool spa, std::vector<std::string> defaultNames);
bool Handle(ServerContext& ctx);
~FileServer();
};

View File

@@ -10,18 +10,18 @@ namespace Tesses::Framework::Http
class HttpRequestBody {
public:
virtual void HandleHeaders(HttpDictionary& dict);
virtual void Write(Tesses::Framework::Streams::Stream* strm)=0;
virtual void Write(std::shared_ptr<Tesses::Framework::Streams::Stream> strm)=0;
virtual ~HttpRequestBody();
};
class StreamHttpRequestBody : public HttpRequestBody {
Tesses::Framework::Streams::Stream* strm;
bool owns;
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
std::string mimeType;
public:
StreamHttpRequestBody(Tesses::Framework::Streams::Stream* strm, bool owns, std::string mimeType);
StreamHttpRequestBody(std::shared_ptr<Tesses::Framework::Streams::Stream> strm, std::string mimeType);
void HandleHeaders(HttpDictionary& dict);
void Write(Tesses::Framework::Streams::Stream* strm);
void Write(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
~StreamHttpRequestBody();
};
@@ -31,7 +31,7 @@ namespace Tesses::Framework::Http
public:
TextHttpRequestBody(std::string text, std::string mimeType);
void HandleHeaders(HttpDictionary& dict);
void Write(Tesses::Framework::Streams::Stream* strm);
void Write(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
~TextHttpRequestBody();
};
@@ -49,52 +49,47 @@ namespace Tesses::Framework::Http
HttpDictionary requestHeaders;
HttpRequestBody* body;
static Tesses::Framework::Streams::Stream* EstablishConnection(Uri uri,bool ignoreSSLErrors,std::string trusted_root_cert_bundle);
static Tesses::Framework::Streams::Stream* EstablishUnixPathConnection(std::string unixPath, Uri uri, bool ignoreSSLErrors, std::string trusted_root_cert_bundle);
static std::shared_ptr<Tesses::Framework::Streams::Stream> EstablishConnection(Uri uri,bool ignoreSSLErrors,std::string trusted_root_cert_bundle);
static std::shared_ptr<Tesses::Framework::Streams::Stream> EstablishUnixPathConnection(std::string unixPath, Uri uri, bool ignoreSSLErrors, std::string trusted_root_cert_bundle);
void SendRequest(Tesses::Framework::Streams::Stream* strm);
void SendRequest(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
};
class HttpResponse {
private:
bool owns;
Tesses::Framework::Streams::Stream* handleStrm;
std::shared_ptr<Tesses::Framework::Streams::Stream> handleStrm;
public:
HttpResponse(Tesses::Framework::Streams::Stream* strm, bool owns);
HttpResponse(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
HttpResponse(HttpRequest& request);
std::string version;
StatusCode statusCode;
HttpDictionary responseHeaders;
std::string ReadAsString();
Tesses::Framework::Streams::Stream* ReadAsStream();
void CopyToStream(Tesses::Framework::Streams::Stream* strm);
Tesses::Framework::Streams::Stream* GetInternalStream();
std::shared_ptr<Tesses::Framework::Streams::Stream> ReadAsStream();
void CopyToStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
std::shared_ptr<Tesses::Framework::Streams::Stream> GetInternalStream();
~HttpResponse();
};
void DownloadToStreamSimple(std::string url, Tesses::Framework::Streams::Stream* strm);
void DownloadToStreamSimple(std::string url, Tesses::Framework::Streams::Stream& strm);
void DownloadToStreamSimple(std::string url, std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
void DownloadToFileSimple(std::string url, std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path);
void DownloadToFileSimple(std::string url, Tesses::Framework::Filesystem::VFS* vfs, Tesses::Framework::Filesystem::VFSPath path);
void DownloadToFileSimple(std::string url, Tesses::Framework::Filesystem::VFS& vfs, Tesses::Framework::Filesystem::VFSPath path);
void DownloadToFileSimple(std::string url, Tesses::Framework::Filesystem::VFSPath path);
std::string DownloadToStringSimple(std::string url);
bool WebSocketClientSuccessDefault(HttpDictionary& dict,bool v);
void WebSocketClient(std::string url, HttpDictionary& requestHeaders, WebSocketConnection& wsc, std::function<bool(HttpDictionary&,bool)> cb=WebSocketClientSuccessDefault);
void WebSocketClient(std::string url, HttpDictionary& requestHeaders, WebSocketConnection* wsc, std::function<bool(HttpDictionary&,bool)> cb=WebSocketClientSuccessDefault);
void WebSocketClient(std::string url, HttpDictionary& requestHeaders, std::shared_ptr<WebSocketConnection> wsc, std::function<bool(HttpDictionary&,bool)> cb=WebSocketClientSuccessDefault);
void DownloadUnixSocketToStreamSimple(std::string unixSocket,std::string url, Tesses::Framework::Streams::Stream* strm);
void DownloadUnixSocketToStreamSimple(std::string unixSocket,std::string url, Tesses::Framework::Streams::Stream& strm);
void DownloadUnixSocketToStreamSimple(std::string unixSocket,std::string url, std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
void DownloadUnixSocketToFileSimple(std::string unixSocket,std::string url, Tesses::Framework::Filesystem::VFS* vfs, Tesses::Framework::Filesystem::VFSPath path);
void DownloadUnixSocketToFileSimple(std::string unixSocket,std::string url, Tesses::Framework::Filesystem::VFS& vfs, Tesses::Framework::Filesystem::VFSPath path);
void DownloadUnixSocketToFileSimple(std::string unixSocket,std::string url, std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path);
void DownloadUnixSocketToFileSimple(std::string unixSocket,std::string url, Tesses::Framework::Filesystem::VFSPath path);
std::string DownloadUnixSocketToStringSimple(std::string unixSocket,std::string url);
void WebSocketUnixSocketClient(std::string unixSocket,std::string url, HttpDictionary& requestHeaders, WebSocketConnection& wsc, std::function<bool(HttpDictionary&,bool)> cb=WebSocketClientSuccessDefault);
void WebSocketUnixSocketClient(std::string unixSocket,std::string url,HttpDictionary& requestHeaders, WebSocketConnection* wsc, std::function<bool(HttpDictionary&,bool)> cb=WebSocketClientSuccessDefault);
void WebSocketUnixSocketClient(std::string unixSocket,std::string url, HttpDictionary& requestHeaders, std::shared_ptr<WebSocketConnection> wsc, std::function<bool(HttpDictionary&,bool)> cb=WebSocketClientSuccessDefault);
}

View File

@@ -17,7 +17,7 @@ namespace Tesses::Framework::Http
class ServerContext {
bool sent;
Tesses::Framework::Streams::Stream* strm;
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
std::map<std::string,ServerContextData*> data;
public:
HttpDictionary requestHeaders;
@@ -31,27 +31,25 @@ namespace Tesses::Framework::Http
uint16_t port;
std::string version;
bool encrypted;
ServerContext(Tesses::Framework::Streams::Stream* strm);
ServerContext(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
~ServerContext();
Tesses::Framework::Streams::Stream& GetStream();
std::shared_ptr<Tesses::Framework::Streams::Stream> GetStream();
std::string GetOriginalPathWithQuery();
std::string GetUrlWithQuery();
bool Sent();
bool NeedToParseFormData();
void ParseFormData(std::function<Tesses::Framework::Streams::Stream*(std::string mime, std::string filename, std::string name)> cb);
void ReadStream(Tesses::Framework::Streams::Stream& strm);
void ReadStream(Tesses::Framework::Streams::Stream* strm);
void ParseFormData(std::function<std::shared_ptr<Tesses::Framework::Streams::Stream>(std::string mime, std::string filename, std::string name)> cb);
void ReadStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
std::string ReadString();
void SendBytes(std::vector<uint8_t> buffer);
void SendText(std::string text);
void SendStream(Tesses::Framework::Streams::Stream& strm);
void SendStream(Tesses::Framework::Streams::Stream* strm);
void SendStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
void SendErrorPage(bool showPath);
void SendNotFound();
void SendBadRequest();
void SendException(std::exception& ex);
Tesses::Framework::Streams::Stream* OpenResponseStream();
Tesses::Framework::Streams::Stream* OpenRequestStream();
std::shared_ptr<Tesses::Framework::Streams::Stream> OpenResponseStream();
std::shared_ptr<Tesses::Framework::Streams::Stream> OpenRequestStream();
ServerContext& WithLastModified(Date::DateTime time);
ServerContext& WithHeader(std::string key, std::string value);
ServerContext& WithSingleHeader(std::string key, std::string value);
@@ -59,7 +57,7 @@ namespace Tesses::Framework::Http
ServerContext& WithContentDisposition(std::string filename, bool isInline);
ServerContext& WriteHeaders();
void StartWebSocketSession(std::function<void(std::function<void(WebSocketMessage&)>,std::function<void()>,std::function<void()>)> onOpen, std::function<void(WebSocketMessage&)> onReceive, std::function<void(bool)> onClose);
void StartWebSocketSession(WebSocketConnection& connection);
void StartWebSocketSession(std::shared_ptr<WebSocketConnection> connection);
template<class T>
T* GetServerContentData(std::string tag)
@@ -81,27 +79,20 @@ namespace Tesses::Framework::Http
};
class HttpServer {
Tesses::Framework::Streams::TcpServer* server;
IHttpServer* http;
std::shared_ptr<Tesses::Framework::Streams::TcpServer> server;
std::shared_ptr<IHttpServer> http;
Tesses::Framework::Threading::Thread* thrd;
bool ownsTCP;
bool ownsHttp;
bool showIPs;
bool showARTL;
public:
HttpServer(Tesses::Framework::Streams::TcpServer& tcpServer, IHttpServer& http, bool showIPs=true);
HttpServer(Tesses::Framework::Streams::TcpServer* tcpServer, bool ownsTCP, IHttpServer& http, bool showIPs=true);
HttpServer(Tesses::Framework::Streams::TcpServer& tcpServer, IHttpServer* http, bool ownsHttpServer, bool showIPs=true);
HttpServer(Tesses::Framework::Streams::TcpServer* tcpServer, bool ownsTCP, IHttpServer* http, bool ownsHttpServer, bool showIPs=true);
HttpServer(uint16_t port, IHttpServer& http, bool showIPs=true);
HttpServer(uint16_t port, IHttpServer* http, bool owns, bool showIPs=true);
HttpServer(std::string unixPath, IHttpServer& http);
HttpServer(std::string unixPath, IHttpServer* http, bool owns);
HttpServer(std::shared_ptr<Tesses::Framework::Streams::TcpServer> tcpServer, std::shared_ptr<IHttpServer> http, bool showIPs=true);
HttpServer(uint16_t port, std::shared_ptr<IHttpServer> http, bool showIPs=true);
HttpServer(std::string unixPath, std::shared_ptr<IHttpServer> http);
uint16_t GetPort();
void StartAccepting();
static void Process(Tesses::Framework::Streams::Stream& strm, IHttpServer& server, std::string ip, uint16_t port, bool encrypted);
static void Process(std::shared_ptr<Tesses::Framework::Streams::Stream> strm, std::shared_ptr<IHttpServer> server, std::string ip, uint16_t port, bool encrypted);
~HttpServer();
};
}

View File

@@ -4,22 +4,20 @@
namespace Tesses::Framework::Http
{
class HttpStream : public Tesses::Framework::Streams::Stream {
Tesses::Framework::Streams::Stream* strm;
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
size_t offset;
size_t read;
int64_t length;
int64_t position;
bool owns;
bool recv;
bool http1_1;
bool done;
public:
HttpStream(Tesses::Framework::Streams::Stream* strm, bool owns, int64_t length, bool recv, bool http1_1);
HttpStream(Tesses::Framework::Streams::Stream& strm, int64_t length, bool recv,bool http1_1);
HttpStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm, int64_t length, bool recv, bool http1_1);
bool CanRead();
bool CanWrite();
bool EndOfStream();

View File

@@ -7,17 +7,14 @@ namespace Tesses::Framework::Http
{
class MountableServer : public IHttpServer
{
IHttpServer* root;
bool owns;
std::vector<std::pair<std::string,std::pair<bool,IHttpServer*>>> servers;
std::shared_ptr<IHttpServer> root;
std::vector<std::pair<std::string,std::shared_ptr<IHttpServer>>> servers;
std::string Subpath(Filesystem::VFSPath fullPath, Filesystem::VFSPath offsetPath);
bool StartsWith(Filesystem::VFSPath fullPath, Filesystem::VFSPath offsetPath);
public:
MountableServer();
MountableServer(IHttpServer* root, bool owns);
MountableServer(IHttpServer& root);
void Mount(std::string path, IHttpServer* server, bool owns);
void Mount(std::string path, IHttpServer& server);
MountableServer(std::shared_ptr<IHttpServer> root);
void Mount(std::string path, std::shared_ptr<IHttpServer> server);
void Unmount(std::string path);
bool Handle(ServerContext& ctx);
~MountableServer();