Add server content data and mountableserver

This commit is contained in:
2025-01-12 19:32:19 -06:00
parent c80a5c503a
commit 4ce3526047
11 changed files with 265 additions and 19 deletions

View File

@ -0,0 +1,16 @@
#pragma once
#include "HttpServer.hpp"
namespace Tesses::Framework::Http
{
class CallbackServer : public IHttpServer
{
std::function<bool(ServerContext&)> cb;
std::function<void()> destroy;
public:
CallbackServer(std::function<bool(ServerContext&)> cb);
CallbackServer(std::function<bool(ServerContext&)> cb,std::function<void()> destroy);
bool Handle(ServerContext& ctx);
~CallbackServer();
};
}

View File

@ -3,12 +3,18 @@
#include "HttpUtils.hpp"
#include "../Threading/Thread.hpp"
#include <unordered_map>
namespace Tesses::Framework::Http
{
class ServerContextData {
public:
virtual ~ServerContextData();
};
class ServerContext {
bool sent;
Tesses::Framework::Streams::Stream* strm;
std::map<std::string,ServerContextData*> data;
public:
HttpDictionary requestHeaders;
HttpDictionary responseHeaders;
@ -22,6 +28,7 @@ namespace Tesses::Framework::Http
std::string version;
bool encrypted;
ServerContext(Tesses::Framework::Streams::Stream* strm);
~ServerContext();
Tesses::Framework::Streams::Stream& GetStream();
std::string GetOriginalPathWithQuery();
std::string GetUrlWithQuery();
@ -46,6 +53,18 @@ namespace Tesses::Framework::Http
ServerContext& WithMimeType(std::string mime);
ServerContext& WithContentDisposition(std::string filename, bool isInline);
ServerContext& WriteHeaders();
template<class T>
T* GetServerContentData(std::string tag)
{
std::string name = typeid(T).name();
name.push_back(' ');
name.append(tag);
if(data.count(name) > 0) return dynamic_cast<T*>(data[name]);
T* item = new T();
data[name] = item;
return item;
}
};
class IHttpServer {

View File

@ -0,0 +1,25 @@
#pragma once
#include "HttpServer.hpp"
#include "../Filesystem/VFSFix.hpp"
#include "../Filesystem/VFS.hpp"
namespace Tesses::Framework::Http
{
class MountableServer : public IHttpServer
{
IHttpServer* root;
bool owns;
std::vector<std::pair<std::string,std::pair<bool,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);
void Unmount(std::string path);
bool Handle(ServerContext& ctx);
~MountableServer();
};
}