Add server content data and mountableserver
This commit is contained in:
22
src/Http/CallbackServer.cpp
Normal file
22
src/Http/CallbackServer.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include "TessesFramework/Http/CallbackServer.hpp"
|
||||
|
||||
namespace Tesses::Framework::Http
|
||||
{
|
||||
CallbackServer::CallbackServer(std::function<bool(ServerContext&)> cb) : CallbackServer(cb,[]()->void{})
|
||||
{
|
||||
|
||||
}
|
||||
CallbackServer::CallbackServer(std::function<bool(ServerContext&)> cb,std::function<void()> destroy)
|
||||
{
|
||||
this->cb = cb;
|
||||
this->destroy=destroy;
|
||||
}
|
||||
bool CallbackServer::Handle(ServerContext& ctx)
|
||||
{
|
||||
return this->cb(ctx);
|
||||
}
|
||||
CallbackServer::~CallbackServer()
|
||||
{
|
||||
this->destroy();
|
||||
}
|
||||
}
|
||||
@ -379,10 +379,23 @@ namespace Tesses::Framework::Http
|
||||
{
|
||||
if(strm == nullptr) return;
|
||||
SendStream(*strm);
|
||||
}
|
||||
ServerContext::~ServerContext()
|
||||
{
|
||||
for(auto item : this->data)
|
||||
{
|
||||
delete item.second;
|
||||
}
|
||||
}
|
||||
ServerContextData::~ServerContextData()
|
||||
{
|
||||
|
||||
}
|
||||
void ServerContext::SendStream(Stream& strm)
|
||||
{
|
||||
if(sent) return;
|
||||
if(!strm.CanRead()) throw TextException("Cannot read from stream");
|
||||
if(strm.EndOfStream()) throw TextException("End of stream");
|
||||
if(strm.CanSeek())
|
||||
{
|
||||
int64_t len=strm.GetLength();
|
||||
@ -666,6 +679,11 @@ namespace Tesses::Framework::Http
|
||||
{
|
||||
ctx.SendException(ex);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
TextException ex("An unknown error occurred");
|
||||
ctx.SendException(ex);
|
||||
}
|
||||
|
||||
if(ctx.version != "HTTP/1.1" ) return;
|
||||
|
||||
|
||||
85
src/Http/MountableServer.cpp
Normal file
85
src/Http/MountableServer.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
#include "TessesFramework/Http/MountableServer.hpp"
|
||||
|
||||
namespace Tesses::Framework::Http {
|
||||
std::string MountableServer::Subpath(Filesystem::VFSPath fullPath, Filesystem::VFSPath offsetPath)
|
||||
{
|
||||
if(fullPath.path.size() < offsetPath.path.size()) return {}; //this shouldn't happen but here just in case
|
||||
Filesystem::VFSPath p;
|
||||
p.relative=false;
|
||||
|
||||
for(size_t i = offsetPath.path.size(); i < fullPath.path.size(); i++)
|
||||
{
|
||||
p.path.push_back(fullPath.path[i]);
|
||||
}
|
||||
return p.ToString();
|
||||
}
|
||||
bool MountableServer::StartsWith(Filesystem::VFSPath fullPath, Filesystem::VFSPath offsetPath)
|
||||
{
|
||||
if(fullPath.path.size() < offsetPath.path.size()) return false;
|
||||
for(size_t i = 0; i < offsetPath.path.size(); i++)
|
||||
{
|
||||
if(fullPath.path[i] != offsetPath.path[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MountableServer::MountableServer() : MountableServer(nullptr,false)
|
||||
{
|
||||
|
||||
}
|
||||
MountableServer::MountableServer(IHttpServer* root, bool owns)
|
||||
{
|
||||
this->root = root;
|
||||
this->owns = owns;
|
||||
}
|
||||
MountableServer::MountableServer(IHttpServer& root) : MountableServer(&root,false)
|
||||
{
|
||||
|
||||
}
|
||||
void MountableServer::Mount(std::string path, IHttpServer* server, bool owns)
|
||||
{
|
||||
this->servers.insert(this->servers.begin(), std::pair<std::string,std::pair<bool,IHttpServer*>>(path, std::pair<bool,IHttpServer*>(owns,server)));
|
||||
}
|
||||
void MountableServer::Mount(std::string path, IHttpServer& server)
|
||||
{
|
||||
Mount(path,&server,false);
|
||||
}
|
||||
void MountableServer::Unmount(std::string path)
|
||||
{
|
||||
for(auto i = this->servers.begin(); i != this->servers.end(); i++)
|
||||
{
|
||||
auto& item = *i;
|
||||
if(item.first == path)
|
||||
{
|
||||
if(item.second.first) delete item.second.second;
|
||||
this->servers.erase(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
bool MountableServer::Handle(ServerContext& ctx)
|
||||
{
|
||||
std::string oldPath = ctx.path;
|
||||
for(auto item : this->servers)
|
||||
{
|
||||
if(StartsWith(oldPath, item.first))
|
||||
{
|
||||
ctx.path = Subpath(oldPath, item.first);
|
||||
if(item.second.second->Handle(ctx))
|
||||
{
|
||||
ctx.path = oldPath;
|
||||
return true;
|
||||
}
|
||||
ctx.path = oldPath;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ctx.path=oldPath;
|
||||
if(this->root != nullptr && this->root->Handle(ctx)) return true;
|
||||
return false;
|
||||
}
|
||||
MountableServer::~MountableServer()
|
||||
{
|
||||
if(this->owns) delete this->root;
|
||||
for(auto svr : this->servers) if(svr.second.first) delete svr.second.second;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user