Add server content data and mountableserver
This commit is contained in:
16
include/TessesFramework/Http/CallbackServer.hpp
Normal file
16
include/TessesFramework/Http/CallbackServer.hpp
Normal 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();
|
||||
};
|
||||
}
|
||||
@ -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 {
|
||||
|
||||
25
include/TessesFramework/Http/MountableServer.hpp
Normal file
25
include/TessesFramework/Http/MountableServer.hpp
Normal 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();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user