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

@@ -15,6 +15,7 @@ src/Http/HttpClient.cpp
src/Http/HttpStream.cpp
src/Http/ContentDisposition.cpp
src/Http/WebSocket.cpp
src/Http/ChangeableServer.cpp
src/Mail/Smtp.cpp
src/Serialization/Json.cpp
src/Serialization/SQLite.cpp

View File

@@ -32,6 +32,22 @@ class MyWebServer : public IHttpServer {
.SendStream(fs);
return true;
}
else if(ctx.path == "/mypath.html")
{
std::string txt = "<h1>Root: " + HttpUtils::HtmlEncode(ctx.GetServerRoot()) + "</h1>";
ctx.WithMimeType("text/html").SendText(txt);
return true;
}
else if(ctx.path == "/getabsolute.html")
{
std::string path;
if(ctx.queryParams.TryGetFirst("path",path))
{
std::string txt = "<h1>Path: " + HttpUtils::HtmlEncode(ctx.MakeAbsolute(path)) + "</h1>";
ctx.WithMimeType("text/html").SendText(txt);
return true;
}
}
else if(ctx.path == "/streaming.html")
{
StreamWriter writer(ctx.OpenResponseStream());
@@ -109,6 +125,12 @@ class MyOtherWebServer : public IHttpServer
ctx.SendText(data->text);
return true;
}
else if(ctx.path == "/mypath.html")
{
std::string txt = "<h1>Root: " + HttpUtils::HtmlEncode(ctx.GetServerRoot()) + "</h1>";
ctx.WithMimeType("text/html").SendText(txt);
return true;
}
return false;
}

View File

@@ -43,7 +43,10 @@ namespace Tesses::Framework
mtx.Lock();
for(std::shared_ptr<Event<TArgs...>>& item : this->items)
{
if(item.get() == event.get()) return;
if(item.get() == event.get()) {
mtx.Unlock();
return;
}
}
this->items.push_back(event);
mtx.Unlock();
@@ -56,10 +59,11 @@ namespace Tesses::Framework
if(i->get() == event.get())
{
this->items.erase(i);
mtx.Unlock();
return;
}
}
mtx.Lock();
mtx.Unlock();
}
void Invoke(TArgs... args)
{

View File

@@ -57,6 +57,5 @@ namespace Tesses::Framework::Filesystem
void Chmod(VFSPath path, uint32_t mode);
void Close();
};
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include "HttpServer.hpp"
namespace Tesses::Framework::Http
{
class ChangeableServer {
public:
ChangeableServer();
ChangeableServer(std::shared_ptr<IHttpServer> original);
std::shared_ptr<IHttpServer> server;
bool Handle(ServerContext& ctx);
~ChangeableServer();
};
}

View File

@@ -56,8 +56,15 @@ namespace Tesses::Framework::Http
ServerContext& WithMimeType(std::string mime);
ServerContext& WithContentDisposition(std::string filename, bool isInline);
ServerContext& WriteHeaders();
ServerContext& WithLocationHeader(std::string url);
ServerContext& WithLocationHeader(std::string url,StatusCode sc);
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(std::shared_ptr<WebSocketConnection> connection);
std::string GetServerRoot();
std::string MakeAbsolute(std::string path);
void SendRedirect(std::string url);
void SendRedirect(std::string url, StatusCode sc);
template<class T>
T* GetServerContentData(std::string tag)

View File

@@ -24,6 +24,7 @@ namespace Tesses::Framework::Streams
virtual void Flush();
virtual void Seek(int64_t pos, SeekOrigin whence);
void CopyTo(std::shared_ptr<Stream> strm, size_t buffSize=1024);
void CopyToLimit(std::shared_ptr<Stream> strm,uint64_t len, size_t buffSize=1024);
virtual void Close();
virtual ~Stream();
};

View File

@@ -6,6 +6,7 @@
#include "Http/CallbackServer.hpp"
#include "Http/MountableServer.hpp"
#include "Http/ContentDisposition.hpp"
#include "Http/ChangeableServer.hpp"
#include "Streams/FileStream.hpp"
#include "Streams/MemoryStream.hpp"
#include "Streams/NetworkStream.hpp"

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;