Add changeable server

This commit is contained in:
2025-10-14 23:12:43 -05:00
parent 1a419d9575
commit 931cf49320
13 changed files with 139 additions and 13 deletions

View File

@@ -21,12 +21,7 @@ namespace Tesses::Framework::Filesystem
for(auto item : this->directories) delete item;
}
void MountableFilesystem::Close()
{
this->root=nullptr;
for(auto item : this->directories) delete item;
this->directories.clear();
}
void MountableFilesystem::GetFS(VFSPath srcPath, VFSPath& destRoot, VFSPath& destPath, std::shared_ptr<VFS>& vfs)

View File

@@ -0,0 +1,22 @@
#include "TessesFramework/Http/ChangeableServer.hpp"
namespace Tesses::Framework::Http {
ChangeableServer::ChangeableServer() : ChangeableServer(nullptr)
{
}
ChangeableServer::ChangeableServer(std::shared_ptr<IHttpServer> original)
{
this->server = original;
}
bool ChangeableServer::Handle(ServerContext& ctx)
{
if(this->server) this->server->Handle(ctx);
return false;
}
ChangeableServer::~ChangeableServer()
{
}
}

View File

@@ -10,6 +10,8 @@
#include "TessesFramework/Threading/Mutex.hpp"
#include "TessesFramework/Common.hpp"
#include "TessesFramework/TextStreams/StdIOWriter.hpp"
#include "TessesFramework/Filesystem/VFSFix.hpp"
#include "TessesFramework/Filesystem/VFS.hpp"
#include <iostream>
using FileStream = Tesses::Framework::Streams::FileStream;
@@ -801,7 +803,7 @@ namespace Tesses::Framework::Http
this->strm->WriteBlock(buffer,read);
begin += read;
} while(read > 0);
} while(read > 0 && !this->strm->EndOfStream());
}
@@ -858,6 +860,26 @@ namespace Tesses::Framework::Http
this->responseHeaders.SetValue("Content-Disposition",cd.ToString());
return *this;
}
ServerContext& ServerContext::WithLocationHeader(std::string url)
{
return WithSingleHeader("Location", this->MakeAbsolute(url));
}
ServerContext& ServerContext::WithLocationHeader(std::string url,StatusCode sc)
{
this->statusCode = sc;
return WithSingleHeader("Location", this->MakeAbsolute(url));
}
void ServerContext::SendRedirect(std::string url)
{
WithLocationHeader(url);
this->WriteHeaders();
}
void ServerContext::SendRedirect(std::string url,StatusCode sc)
{
WithLocationHeader(url,sc);
this->WriteHeaders();
}
void ServerContext::SendNotFound()
{
if(sent) return;
@@ -1053,5 +1075,22 @@ namespace Tesses::Framework::Http
svr.Start();
thrd.Join();
}
std::string ServerContext::GetServerRoot()
{
if(this->originalPath == this->path) return "/";
Tesses::Framework::Filesystem::VFSPath originalPath=this->originalPath;
Tesses::Framework::Filesystem::VFSPath path=this->path;
if(originalPath.path.size() <= path.path.size()) return "/";
originalPath.path.resize(originalPath.path.size() - path.path.size());
return originalPath.ToString();
}
std::string ServerContext::MakeAbsolute(std::string path)
{
if(path.find("http://") == 0 || path.find("https://") == 0 || path.find("/") == 0) return path;
Tesses::Framework::Filesystem::VFSPath path2 = GetServerRoot();
path2 = path2 / path;
return path2.CollapseRelativeParents().ToString();
}
}

View File

@@ -111,7 +111,7 @@ namespace Tesses::Framework::Streams
if(!f) return;
if(this->owns)
{
fclose(this->f);
fclose(this->f);
f=NULL;
}
}

View File

@@ -53,7 +53,7 @@ namespace Tesses::Framework::Streams {
buffer += read;
len -= read;
} while(read > 0);
} while(read > 0 && !this->EndOfStream());
}
bool Stream::CanRead()
{
@@ -96,6 +96,27 @@ namespace Tesses::Framework::Streams {
{
}
void Stream::CopyToLimit(std::shared_ptr<Stream> strm,uint64_t len, size_t buffSize)
{
size_t read;
uint8_t* buffer = new uint8_t[buffSize];
uint64_t offset = 0;
do {
if(offset >= len) break;
read = (size_t)std::min(len-offset,(uint64_t)buffSize);
read = this->Read(buffer,read);
strm->WriteBlock(buffer, read);
offset += read;
} while(read > 0 && !strm->EndOfStream());
strm->Flush();
delete[] buffer;
}
void Stream::CopyTo(std::shared_ptr<Stream> strm, size_t buffSize)
{
@@ -107,7 +128,7 @@ namespace Tesses::Framework::Streams {
read = this->Read(buffer,buffSize);
strm->WriteBlock(buffer, read);
} while(read > 0);
} while(read > 0 && !strm->EndOfStream());
strm->Flush();
delete[] buffer;