Overhaul cmake configuration, add console api, fix http code that caused issues with cgi-bin

This commit is contained in:
2026-05-27 03:02:16 -05:00
parent 266ef5f830
commit 8413c67ec6
177 changed files with 20088 additions and 17948 deletions

View File

@@ -1,18 +1,39 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "HttpServer.hpp"
namespace Tesses::Framework::Http {
class BasicAuthServer : public Tesses::Framework::Http::IHttpServer
{
public:
std::shared_ptr<IHttpServer> server;
std::function<bool(std::string username, std::string password)> authorization;
std::string realm;
class BasicAuthServer : public Tesses::Framework::Http::IHttpServer {
public:
std::shared_ptr<IHttpServer> server;
std::function<bool(std::string username, std::string password)>
authorization;
std::string realm;
BasicAuthServer();
BasicAuthServer(std::shared_ptr<IHttpServer> server, std::function<bool(std::string username, std::string password)> auth,std::string realm="Protected Content");
bool Handle(ServerContext& ctx);
BasicAuthServer();
BasicAuthServer(
std::shared_ptr<IHttpServer> server,
std::function<bool(std::string username, std::string password)> auth,
std::string realm = "Protected Content");
bool Handle(ServerContext &ctx);
static bool GetCreds(ServerContext& ctx, std::string& user, std::string& pass);
};
}
static bool GetCreds(ServerContext &ctx, std::string &user,
std::string &pass);
};
} // namespace Tesses::Framework::Http

View File

@@ -1,23 +1,42 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "../Filesystem/VFS.hpp"
#include "../Filesystem/VFSFix.hpp"
#include "HttpServer.hpp"
#include <optional>
namespace Tesses::Framework::Http {
struct CGIParams {
std::optional<Tesses::Framework::Filesystem::VFSPath> document_root;
Tesses::Framework::Filesystem::VFSPath program;
std::optional<std::string> adminEmail;
std::optional<Tesses::Framework::Filesystem::VFSPath> workingDirectory;
};
class CGIServer : public Tesses::Framework::Http::IHttpServer {
Tesses::Framework::Filesystem::VFSPath dir;
public:
std::optional<Tesses::Framework::Filesystem::VFSPath> document_root;
std::optional<std::string> adminEmail;
std::optional<Tesses::Framework::Filesystem::VFSPath> workingDirectory;
CGIServer(Tesses::Framework::Filesystem::VFSPath dir);
bool Handle(ServerContext& ctx);
static bool ServeCGIRequest(ServerContext& ctx, CGIParams& params);
};
}
struct CGIParams {
std::optional<Tesses::Framework::Filesystem::VFSPath> document_root;
Tesses::Framework::Filesystem::VFSPath program;
std::optional<std::string> adminEmail;
std::optional<Tesses::Framework::Filesystem::VFSPath> workingDirectory;
};
class CGIServer : public Tesses::Framework::Http::IHttpServer {
Tesses::Framework::Filesystem::VFSPath dir;
public:
std::optional<Tesses::Framework::Filesystem::VFSPath> document_root;
std::optional<std::string> adminEmail;
std::optional<Tesses::Framework::Filesystem::VFSPath> workingDirectory;
CGIServer(Tesses::Framework::Filesystem::VFSPath dir);
bool Handle(ServerContext &ctx);
static bool ServeCGIRequest(ServerContext &ctx, CGIParams &params);
};
} // namespace Tesses::Framework::Http

View File

@@ -1,16 +1,34 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#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();
};
}
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();
};
} // namespace Tesses::Framework::Http

View File

@@ -1,14 +1,31 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#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();
};
}
namespace Tesses::Framework::Http {
class ChangeableServer {
public:
ChangeableServer();
ChangeableServer(std::shared_ptr<IHttpServer> original);
std::shared_ptr<IHttpServer> server;
bool Handle(ServerContext &ctx);
~ChangeableServer();
};
} // namespace Tesses::Framework::Http

View File

@@ -1,13 +1,30 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "../Common.hpp"
namespace Tesses::Framework::Http
{
class ContentDisposition {
public:
std::string filename;
std::string type;
std::string fieldName;
static bool TryParse(std::string str, ContentDisposition& cd);
std::string ToString();
};
}
namespace Tesses::Framework::Http {
class ContentDisposition {
public:
std::string filename;
std::string type;
std::string fieldName;
static bool TryParse(std::string str, ContentDisposition &cd);
std::string ToString();
};
} // namespace Tesses::Framework::Http

View File

@@ -1,25 +1,45 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "../Filesystem/VFS.hpp"
#include "HttpServer.hpp"
namespace Tesses::Framework::Http
{
class FileServer : public IHttpServer
{
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
namespace Tesses::Framework::Http {
class FileServer : public IHttpServer {
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
bool SendFile(ServerContext& ctx,Tesses::Framework::Filesystem::VFSPath path);
public:
bool allowListing;
bool spa;
std::vector<std::string> defaultNames;
FileServer(std::filesystem::path path,bool allowListing,bool spa);
FileServer(std::filesystem::path path,bool allowListing, bool spa, std::vector<std::string> defaultNames);
FileServer(std::shared_ptr<Tesses::Framework::Filesystem::VFS> fs, bool allowListing, bool spa);
FileServer(std::shared_ptr<Tesses::Framework::Filesystem::VFS> fs, bool allowListing, bool spa, std::vector<std::string> defaultNames);
bool Handle(ServerContext& ctx);
~FileServer();
};
}
bool SendFile(ServerContext &ctx,
Tesses::Framework::Filesystem::VFSPath path);
public:
bool allowListing;
bool spa;
std::vector<std::string> defaultNames;
FileServer(std::filesystem::path path, bool allowListing, bool spa);
FileServer(std::filesystem::path path, bool allowListing, bool spa,
std::vector<std::string> defaultNames);
FileServer(std::shared_ptr<Tesses::Framework::Filesystem::VFS> fs,
bool allowListing, bool spa);
FileServer(std::shared_ptr<Tesses::Framework::Filesystem::VFS> fs,
bool allowListing, bool spa,
std::vector<std::string> defaultNames);
bool Handle(ServerContext &ctx);
~FileServer();
};
} // namespace Tesses::Framework::Http

View File

@@ -1,95 +1,139 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "../Streams/Stream.hpp"
#include "HttpUtils.hpp"
#include "WebSocket.hpp"
#include "TessesFramework/Filesystem/LocalFS.hpp"
#include "TessesFramework/Filesystem/VFSFix.hpp"
namespace Tesses::Framework::Http
{
class HttpRequestBody {
public:
virtual void HandleHeaders(HttpDictionary& dict);
virtual void Write(std::shared_ptr<Tesses::Framework::Streams::Stream> strm)=0;
virtual ~HttpRequestBody();
};
#include "WebSocket.hpp"
namespace Tesses::Framework::Http {
class StreamHttpRequestBody : public HttpRequestBody {
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
std::string mimeType;
public:
StreamHttpRequestBody(std::shared_ptr<Tesses::Framework::Streams::Stream> strm, std::string mimeType);
void HandleHeaders(HttpDictionary& dict);
void Write(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
~StreamHttpRequestBody();
};
class HttpRequestBody {
public:
virtual void HandleHeaders(HttpDictionary &dict);
virtual void
Write(std::shared_ptr<Tesses::Framework::Streams::Stream> strm) = 0;
virtual ~HttpRequestBody();
};
class TextHttpRequestBody : public HttpRequestBody {
std::string text;
std::string mimeType;
public:
TextHttpRequestBody(std::string text, std::string mimeType);
void HandleHeaders(HttpDictionary& dict);
void Write(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
~TextHttpRequestBody();
};
class StreamHttpRequestBody : public HttpRequestBody {
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
std::string mimeType;
class HttpRequest {
public:
HttpRequest();
std::string trusted_root_cert_bundle;
bool ignoreSSLErrors;
bool followRedirects;
public:
StreamHttpRequestBody(
std::shared_ptr<Tesses::Framework::Streams::Stream> strm,
std::string mimeType);
void HandleHeaders(HttpDictionary &dict);
void Write(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
~StreamHttpRequestBody();
};
std::string method;
std::string url;
std::string unixSocket;
HttpDictionary requestHeaders;
HttpRequestBody* body;
class TextHttpRequestBody : public HttpRequestBody {
std::string text;
std::string mimeType;
static std::shared_ptr<Tesses::Framework::Streams::Stream> EstablishConnection(Uri uri,bool ignoreSSLErrors,std::string trusted_root_cert_bundle);
static std::shared_ptr<Tesses::Framework::Streams::Stream> EstablishUnixPathConnection(std::string unixPath, Uri uri, bool ignoreSSLErrors, std::string trusted_root_cert_bundle);
public:
TextHttpRequestBody(std::string text, std::string mimeType);
void HandleHeaders(HttpDictionary &dict);
void Write(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
~TextHttpRequestBody();
};
void SendRequest(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
};
class HttpRequest {
public:
HttpRequest();
std::string trusted_root_cert_bundle;
bool ignoreSSLErrors;
bool followRedirects;
class HttpResponse {
private:
bool owns;
std::shared_ptr<Tesses::Framework::Streams::Stream> handleStrm;
public:
HttpResponse(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
HttpResponse(HttpRequest& request);
std::string version;
StatusCode statusCode;
HttpDictionary responseHeaders;
std::string ReadAsString();
std::shared_ptr<Tesses::Framework::Streams::Stream> ReadAsStream();
void CopyToStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
std::shared_ptr<Tesses::Framework::Streams::Stream> GetInternalStream();
~HttpResponse();
};
std::string method;
std::string url;
std::string unixSocket;
HttpDictionary requestHeaders;
HttpRequestBody *body;
void DownloadToStreamSimple(std::string url, std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
static std::shared_ptr<Tesses::Framework::Streams::Stream>
EstablishConnection(Uri uri, bool ignoreSSLErrors,
std::string trusted_root_cert_bundle);
static std::shared_ptr<Tesses::Framework::Streams::Stream>
EstablishUnixPathConnection(std::string unixPath, Uri uri,
bool ignoreSSLErrors,
std::string trusted_root_cert_bundle);
void DownloadToFileSimple(std::string url, std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path);
void SendRequest(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
};
void DownloadToFileSimple(std::string url, Tesses::Framework::Filesystem::VFSPath path);
std::string DownloadToStringSimple(std::string url);
bool WebSocketClientSuccessDefault(HttpDictionary& dict,bool v);
void WebSocketClient(std::string url, HttpDictionary& requestHeaders, std::shared_ptr<WebSocketConnection> wsc, std::function<bool(HttpDictionary&,bool)> cb=WebSocketClientSuccessDefault);
void DownloadUnixSocketToStreamSimple(std::string unixSocket,std::string url, std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
class HttpResponse {
private:
bool owns;
std::shared_ptr<Tesses::Framework::Streams::Stream> handleStrm;
void DownloadUnixSocketToFileSimple(std::string unixSocket,std::string url, std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path);
void DownloadUnixSocketToFileSimple(std::string unixSocket,std::string url, Tesses::Framework::Filesystem::VFSPath path);
std::string DownloadUnixSocketToStringSimple(std::string unixSocket,std::string url);
public:
HttpResponse(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
HttpResponse(HttpRequest &request);
std::string version;
StatusCode statusCode;
HttpDictionary responseHeaders;
std::string ReadAsString();
std::shared_ptr<Tesses::Framework::Streams::Stream> ReadAsStream();
void CopyToStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
std::shared_ptr<Tesses::Framework::Streams::Stream> GetInternalStream();
~HttpResponse();
};
void DownloadToStreamSimple(
std::string url, std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
void WebSocketUnixSocketClient(std::string unixSocket,std::string url, HttpDictionary& requestHeaders, std::shared_ptr<WebSocketConnection> wsc, std::function<bool(HttpDictionary&,bool)> cb=WebSocketClientSuccessDefault);
}
void DownloadToFileSimple(
std::string url, std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs,
Tesses::Framework::Filesystem::VFSPath path);
void DownloadToFileSimple(std::string url,
Tesses::Framework::Filesystem::VFSPath path);
std::string DownloadToStringSimple(std::string url);
bool WebSocketClientSuccessDefault(HttpDictionary &dict, bool v);
void WebSocketClient(std::string url, HttpDictionary &requestHeaders,
std::shared_ptr<WebSocketConnection> wsc,
std::function<bool(HttpDictionary &, bool)> cb =
WebSocketClientSuccessDefault);
void DownloadUnixSocketToStreamSimple(
std::string unixSocket, std::string url,
std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
void DownloadUnixSocketToFileSimple(
std::string unixSocket, std::string url,
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs,
Tesses::Framework::Filesystem::VFSPath path);
void DownloadUnixSocketToFileSimple(
std::string unixSocket, std::string url,
Tesses::Framework::Filesystem::VFSPath path);
std::string DownloadUnixSocketToStringSimple(std::string unixSocket,
std::string url);
void WebSocketUnixSocketClient(std::string unixSocket, std::string url,
HttpDictionary &requestHeaders,
std::shared_ptr<WebSocketConnection> wsc,
std::function<bool(HttpDictionary &, bool)> cb =
WebSocketClientSuccessDefault);
} // namespace Tesses::Framework::Http

View File

@@ -1,138 +1,170 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "../Streams/NetworkStream.hpp"
#include "HttpUtils.hpp"
#include "../Threading/Thread.hpp"
#include "../Date/Date.hpp"
#include <unordered_map>
#include "../Threading/Thread.hpp"
#include "HttpUtils.hpp"
#include "WebSocket.hpp"
#include <queue>
namespace Tesses::Framework::Http
{
class ServerContextData {
public:
virtual ~ServerContextData();
};
class ServerContext;
class ServerSentEvents {
std::vector<std::shared_ptr<Tesses::Framework::Streams::Stream>> strms;
Tesses::Framework::Threading::Mutex mtx;
private:
void SendEventRaw(const std::string& evt);
public:
void SendRetry(uint32_t ms);
void SendRetry(std::chrono::milliseconds ms);
void SendRetry(Tesses::Framework::Date::TimeSpan ts);
void SendData(const std::string& message);
void SendData(const std::string& message, const std::string& dataType);
void SendId(const std::string& idVal);
void SendCustomEvent(const std::string& type, const std::string& value);
void SendComment(const std::string& comment);
friend class ServerContext;
};
#include <unordered_map>
namespace Tesses::Framework::Http {
class ServerContextData {
public:
virtual ~ServerContextData();
};
class ServerContext;
class ServerSentEvents {
std::vector<std::shared_ptr<Tesses::Framework::Streams::Stream>> strms;
Tesses::Framework::Threading::Mutex mtx;
private:
void SendEventRaw(const std::string &evt);
class ServerContext {
bool sent;
bool debug;
std::vector<std::shared_ptr<ServerSentEvents>> sse;
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
std::map<std::string,ServerContextData*> data;
std::queue<std::function<bool(ServerContext& ctx)>> headerhandlers;
public:
HttpDictionary requestHeaders;
HttpDictionary responseHeaders;
HttpDictionary queryParams;
//used by path
HttpDictionary pathArguments;
std::string path;
std::string originalPath;
std::string method;
StatusCode statusCode;
std::string ip;
uint16_t port;
uint16_t serverPort;
std::string version;
bool encrypted;
ServerContext(std::shared_ptr<Tesses::Framework::Streams::Stream> strm, bool debug=false);
~ServerContext();
std::shared_ptr<Tesses::Framework::Streams::Stream> GetStream();
std::string GetOriginalPathWithQuery();
std::string GetUrlWithQuery();
bool Sent();
bool NeedToParseFormData();
void ParseFormData(std::function<std::shared_ptr<Tesses::Framework::Streams::Stream>(std::string mime, std::string filename, std::string name)> cb);
void ReadStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
std::string ReadString();
void SendBytes(std::vector<uint8_t> buffer);
void SendText(std::string text);
void SendStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
void SendErrorPage(bool showPath);
void SendNotFound();
void SendBadRequest();
void SendException(std::exception& ex);
void SendServerSentEvents(std::shared_ptr<ServerSentEvents> sse);
std::shared_ptr<Tesses::Framework::Streams::Stream> OpenResponseStream();
std::shared_ptr<Tesses::Framework::Streams::Stream> OpenRequestStream();
ServerContext& WithStatusCode(StatusCode code);
ServerContext& WithLastModified(Date::DateTime time);
ServerContext& WithHeader(std::string key, std::string value);
ServerContext& WithSingleHeader(std::string key, std::string value);
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);
ServerContext& WithHeaderIntercepter(std::function<bool(ServerContext&)> cb);
ServerContext& WithDebug(bool debug=true);
bool Debug();
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);
public:
void SendRetry(uint32_t ms);
void SendRetry(std::chrono::milliseconds ms);
void SendRetry(Tesses::Framework::Date::TimeSpan ts);
void SendData(const std::string &message);
void SendData(const std::string &message, const std::string &dataType);
void SendId(const std::string &idVal);
void SendCustomEvent(const std::string &type, const std::string &value);
void SendComment(const std::string &comment);
friend class ServerContext;
};
class ServerContext {
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;
}
friend class ServerSentEvents;
bool sent;
bool debug;
std::vector<std::shared_ptr<ServerSentEvents>> sse;
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
std::map<std::string, ServerContextData *> data;
std::queue<std::function<bool(ServerContext &ctx)>> headerhandlers;
};
public:
HttpDictionary requestHeaders;
HttpDictionary responseHeaders;
HttpDictionary queryParams;
// used by path
HttpDictionary pathArguments;
std::string path;
std::string originalPath;
std::string method;
StatusCode statusCode;
std::string ip;
uint16_t port;
uint16_t serverPort;
std::string version;
bool encrypted;
ServerContext(std::shared_ptr<Tesses::Framework::Streams::Stream> strm,
bool debug = false);
~ServerContext();
std::shared_ptr<Tesses::Framework::Streams::Stream> GetStream();
std::string GetOriginalPathWithQuery();
std::string GetUrlWithQuery();
bool Sent();
bool NeedToParseFormData();
void ParseFormData(
std::function<std::shared_ptr<Tesses::Framework::Streams::Stream>(
std::string mime, std::string filename, std::string name)>
cb);
void ReadStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
std::string ReadString();
void SendBytes(std::vector<uint8_t> buffer);
void SendText(std::string text);
void SendStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
void SendErrorPage(bool showPath);
void SendNotFound();
void SendBadRequest();
void SendException(std::exception &ex);
void SendServerSentEvents(std::shared_ptr<ServerSentEvents> sse);
std::shared_ptr<Tesses::Framework::Streams::Stream> OpenResponseStream();
std::shared_ptr<Tesses::Framework::Streams::Stream> OpenRequestStream();
ServerContext &WithStatusCode(StatusCode code);
ServerContext &WithLastModified(Date::DateTime time);
ServerContext &WithHeader(std::string key, std::string value);
ServerContext &WithSingleHeader(std::string key, std::string value);
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);
ServerContext &
WithHeaderIntercepter(std::function<bool(ServerContext &)> cb);
ServerContext &WithDebug(bool debug = true);
bool Debug();
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);
class IHttpServer {
public:
virtual bool Handle(ServerContext& ctx)=0;
virtual ~IHttpServer();
};
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;
}
friend class ServerSentEvents;
};
class HttpServer {
std::shared_ptr<Tesses::Framework::Streams::TcpServer> server;
std::shared_ptr<IHttpServer> http;
Tesses::Framework::Threading::Thread* thrd;
class IHttpServer {
public:
virtual bool Handle(ServerContext &ctx) = 0;
virtual ~IHttpServer();
};
bool showIPs;
bool showARTL;
bool debug;
class HttpServer {
std::shared_ptr<Tesses::Framework::Streams::TcpServer> server;
std::shared_ptr<IHttpServer> http;
Tesses::Framework::Threading::Thread *thrd;
public:
HttpServer(std::shared_ptr<Tesses::Framework::Streams::TcpServer> tcpServer, std::shared_ptr<IHttpServer> http, bool showIPs=true, bool debug=false);
HttpServer(uint16_t port, std::shared_ptr<IHttpServer> http, bool showIPs=true, bool debug=false);
HttpServer(std::string unixPath, std::shared_ptr<IHttpServer> http, bool debug=false);
uint16_t GetPort();
void StartAccepting();
static void Process(std::shared_ptr<Tesses::Framework::Streams::Stream> strm, std::shared_ptr<IHttpServer> server, std::string ip, uint16_t port,uint16_t serverPort, bool encrypted, bool debug=false);
~HttpServer();
};
}
bool showIPs;
bool showARTL;
bool debug;
public:
HttpServer(std::shared_ptr<Tesses::Framework::Streams::TcpServer> tcpServer,
std::shared_ptr<IHttpServer> http, bool showIPs = true,
bool debug = false);
HttpServer(uint16_t port, std::shared_ptr<IHttpServer> http,
bool showIPs = true, bool debug = false);
HttpServer(std::string unixPath, std::shared_ptr<IHttpServer> http,
bool debug = false);
uint16_t GetPort();
void StartAccepting();
static void
Process(std::shared_ptr<Tesses::Framework::Streams::Stream> strm,
std::shared_ptr<IHttpServer> server, std::string ip, uint16_t port,
uint16_t serverPort, bool encrypted, bool debug = false);
~HttpServer();
};
} // namespace Tesses::Framework::Http

View File

@@ -1,31 +1,49 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "../Streams/Stream.hpp"
namespace Tesses::Framework::Http
{
class HttpStream : public Tesses::Framework::Streams::Stream {
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
size_t offset;
size_t read;
int64_t length;
int64_t position;
bool recv;
bool http1_1;
bool done;
namespace Tesses::Framework::Http {
class HttpStream : public Tesses::Framework::Streams::Stream {
std::shared_ptr<Tesses::Framework::Streams::Stream> strm;
public:
HttpStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm, int64_t length, bool recv, bool http1_1);
bool CanRead();
bool CanWrite();
bool EndOfStream();
int64_t GetLength();
int64_t GetPosition();
size_t Read(uint8_t* buffer, size_t len);
size_t Write(const uint8_t* buffer, size_t len);
void Close();
~HttpStream();
};
}
size_t offset;
size_t read;
int64_t length;
int64_t position;
bool recv;
bool http1_1;
bool done;
public:
HttpStream(std::shared_ptr<Tesses::Framework::Streams::Stream> strm,
int64_t length, bool recv, bool http1_1);
bool CanRead();
bool CanWrite();
bool EndOfStream();
int64_t GetLength();
int64_t GetPosition();
size_t Read(uint8_t *buffer, size_t len);
size_t Write(const uint8_t *buffer, size_t len);
void Close();
~HttpStream();
};
} // namespace Tesses::Framework::Http

View File

@@ -1,181 +1,196 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "../Common.hpp"
#include "../Date/Date.hpp"
#include <algorithm>
namespace Tesses::Framework::Http
{
typedef enum StatusCode {
Continue=100,
SwitchingProtocols=101,
Processing=102,
EarlyHints=103,
OK=200,
Created=201,
Accepted=202,
NonAuthoritativeInformation=203,
NoContent=204,
ResetContent=205,
PartialContent=206,
MultiStatus=207,
AlreadyReported=208,
IMUsed=226,
MultipleChoices=300,
MovedPermanently=301,
Found=302,
SeeOther=303,
NotModified=304,
UseProxy=305,
TemporaryRedirect=307,
PermanentRedirect=308,
BadRequest=400,
Unauthorized=401,
PaymentRequired=402,
Forbidden=403,
NotFound=404,
MethodNotAllowed=405,
NotAcceptable=406,
ProxyAuthenticationRequired=407,
RequestTimeout=408,
Conflict=409,
Gone=410,
LengthRequired=411,
PreconditionFailed=412,
PayloadTooLarge=413,
URITooLong=414,
UnsupportedMediaType=415,
RangeNotSatisfiable=416,
ExpectationFailed=417,
ImATeapot=418,
MisdirectedRequest=421,
UnprocessableContent=422,
Locked=423,
FailedDependency=424,
TooEarly=425,
UpgradeRequired=426,
PreconditionRequired=428,
TooManyRequests=429,
RequestHeaderFieldsTooLarge=431,
UnavailableForLegalReasons=451,
InternalServerError=500,
NotImplemented=501,
BadGateway=502,
ServiceUnavailable=503,
GatewayTimeout=504,
HTTPVersionNotSupported=505,
VariantAlsoNegotiates=506,
InsufficientStorage=507,
LoopDetected=508,
NotExtended=510,
NetworkAuthenticationRequired=511
namespace Tesses::Framework::Http {
typedef enum StatusCode {
Continue = 100,
SwitchingProtocols = 101,
Processing = 102,
EarlyHints = 103,
OK = 200,
Created = 201,
Accepted = 202,
NonAuthoritativeInformation = 203,
NoContent = 204,
ResetContent = 205,
PartialContent = 206,
MultiStatus = 207,
AlreadyReported = 208,
IMUsed = 226,
MultipleChoices = 300,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
UseProxy = 305,
TemporaryRedirect = 307,
PermanentRedirect = 308,
BadRequest = 400,
Unauthorized = 401,
PaymentRequired = 402,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
ProxyAuthenticationRequired = 407,
RequestTimeout = 408,
Conflict = 409,
Gone = 410,
LengthRequired = 411,
PreconditionFailed = 412,
PayloadTooLarge = 413,
URITooLong = 414,
UnsupportedMediaType = 415,
RangeNotSatisfiable = 416,
ExpectationFailed = 417,
ImATeapot = 418,
MisdirectedRequest = 421,
UnprocessableContent = 422,
Locked = 423,
FailedDependency = 424,
TooEarly = 425,
UpgradeRequired = 426,
PreconditionRequired = 428,
TooManyRequests = 429,
RequestHeaderFieldsTooLarge = 431,
UnavailableForLegalReasons = 451,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HTTPVersionNotSupported = 505,
VariantAlsoNegotiates = 506,
InsufficientStorage = 507,
LoopDetected = 508,
NotExtended = 510,
NetworkAuthenticationRequired = 511
} StatusCode;
struct CaseInsensitiveLess {
CaseInsensitiveLess(const CaseInsensitiveLess& str);
CaseInsensitiveLess(const CaseInsensitiveLess &str);
CaseInsensitiveLess();
CaseInsensitiveLess* offset;
CaseInsensitiveLess *offset;
bool caseSensitive;
bool operator() (const std::string& s1, const std::string& s2) const;
bool operator()(const std::string &s1, const std::string &s2) const;
};
class HttpDictionary {
public:
HttpDictionary();
HttpDictionary(bool caseSensitive);
std::map<std::string,std::vector<std::string>,CaseInsensitiveLess> kvp;
void SetCaseSensitive(bool isCaseSensitive);
void Clear();
void Clear(std::string key, bool kvpExistsAfter);
void SetValue(std::string key, std::string value);
void SetValue(std::string key, int64_t v);
void SetValue(std::string key, double v);
void SetValue(std::string key, Date::DateTime v);
void SetValue(std::string key, std::vector<std::string> value);
template<typename Itterator>
void SetValue(std::string key, Itterator begin, Itterator end)
{
Clear(key,true);
AddValue(key, begin, end);
}
void AddValue(std::string key, std::string value);
void AddValue(std::string key, int64_t v);
void AddValue(std::string key, double v);
void AddValue(std::string key, Date::DateTime v);
void AddValue(std::string key, std::vector<std::string> value);
template<typename Itterator>
void AddValue(std::string key, Itterator begin, Itterator end)
{
auto& ls = kvp[key];
ls.insert(ls.end(), begin, end);
}
bool TryGetFirst(std::string key, std::string& value);
class HttpDictionary {
public:
HttpDictionary();
HttpDictionary(bool caseSensitive);
std::map<std::string, std::vector<std::string>, CaseInsensitiveLess> kvp;
void SetCaseSensitive(bool isCaseSensitive);
void Clear();
void Clear(std::string key, bool kvpExistsAfter);
void SetValue(std::string key, std::string value);
void SetValue(std::string key, int64_t v);
void SetValue(std::string key, double v);
void SetValue(std::string key, Date::DateTime v);
void SetValue(std::string key, std::vector<std::string> value);
template <typename Itterator>
void SetValue(std::string key, Itterator begin, Itterator end) {
Clear(key, true);
AddValue(key, begin, end);
}
void AddValue(std::string key, std::string value);
void AddValue(std::string key, int64_t v);
void AddValue(std::string key, double v);
void AddValue(std::string key, Date::DateTime v);
void AddValue(std::string key, std::vector<std::string> value);
bool TryGetFirstInt(std::string key, int64_t& value);
template <typename Itterator>
void AddValue(std::string key, Itterator begin, Itterator end) {
auto &ls = kvp[key];
ls.insert(ls.end(), begin, end);
}
bool TryGetFirstDouble(std::string key, double& value);
bool TryGetFirstDate(std::string key, Date::DateTime& value);
bool TryGetFirst(std::string key, std::string &value);
bool GetFirstBoolean(std::string key);
bool TryGetFirstInt(std::string key, int64_t &value);
bool AnyEquals(std::string key, std::string value);
};
bool TryGetFirstDouble(std::string key, double &value);
bool TryGetFirstDate(std::string key, Date::DateTime &value);
class Uri {
public:
Uri();
std::string GetQuery();
std::string GetPathAndQuery();
uint16_t GetPort();
std::string HostPort();
bool Relative(std::string url, Uri& uri);
std::string ToString();
static bool TryParse(std::string url, Uri& uri);
std::string scheme;
std::string host;
uint16_t port;
std::string path;
HttpDictionary query;
std::string hash;
};
bool GetFirstBoolean(std::string key);
class HttpUtils
{
public:
static char NibbleToHex(uint8_t b, bool isUppercase);
static char NibbleToHex(uint8_t nibble);
static uint8_t HexToNibble(char c);
static std::string BytesToHex(const std::vector<uint8_t>& data);
static void BytesToHex(std::string& text,const std::vector<uint8_t>& data);
bool AnyEquals(std::string key, std::string value);
};
static std::string BytesToHex(const std::vector<uint8_t>& data,bool isUppercase);
static void BytesToHex(std::string& text,const std::vector<uint8_t>& data, bool isUppercase);
static std::vector<uint8_t> HexToBytes(const std::string& text);
static void HexToBytes(std::vector<uint8_t>& data,const std::string& text);
static std::string MimeType(std::filesystem::path p);
static bool Invalid(char c);
static std::string Sanitise(std::string text);
static void QueryParamsDecode(HttpDictionary& dict,std::string query);
static std::string Join(std::string joinStr, std::vector<std::string> ents);
static std::string QueryParamsEncode(HttpDictionary& dict);
static std::string UrlDecode(std::string v);
static std::string UrlEncode(std::string v);
static std::string UrlPathDecode(std::string v);
static std::string UrlPathEncode(std::string v, bool ignoreSpace=false);
static std::string HtmlEncode(std::string v);
static std::string HtmlP(std::string text);
static std::string HtmlDecodeOnlyEntityNumber(std::string v);
static std::vector<std::string> SplitString(std::string text, std::string delimiter,std::size_t maxCnt = std::string::npos);
static std::string Replace(std::string str, std::string find, std::string replace);
static std::string StatusCodeString(StatusCode code);
static std::string ToLower(std::string str);
static std::string ToUpper(std::string str);
static std::string LeftPad(std::string text, int count, char c);
};
class Uri {
public:
Uri();
std::string GetQuery();
std::string GetPathAndQuery();
uint16_t GetPort();
std::string HostPort();
bool Relative(std::string url, Uri &uri);
std::string ToString();
static bool TryParse(std::string url, Uri &uri);
std::string scheme;
std::string host;
uint16_t port;
std::string path;
HttpDictionary query;
std::string hash;
};
class HttpUtils {
public:
static char NibbleToHex(uint8_t b, bool isUppercase);
static char NibbleToHex(uint8_t nibble);
static uint8_t HexToNibble(char c);
static std::string BytesToHex(const std::vector<uint8_t> &data);
static void BytesToHex(std::string &text, const std::vector<uint8_t> &data);
}
static std::string BytesToHex(const std::vector<uint8_t> &data,
bool isUppercase);
static void BytesToHex(std::string &text, const std::vector<uint8_t> &data,
bool isUppercase);
static std::vector<uint8_t> HexToBytes(const std::string &text);
static void HexToBytes(std::vector<uint8_t> &data, const std::string &text);
static std::string MimeType(std::filesystem::path p);
static bool Invalid(char c);
static std::string Sanitise(std::string text);
static void QueryParamsDecode(HttpDictionary &dict, std::string query);
static std::string Join(std::string joinStr, std::vector<std::string> ents);
static std::string QueryParamsEncode(HttpDictionary &dict);
static std::string UrlDecode(std::string v);
static std::string UrlEncode(std::string v);
static std::string UrlPathDecode(std::string v);
static std::string UrlPathEncode(std::string v, bool ignoreSpace = false);
static std::string HtmlEncode(std::string v);
static std::string HtmlP(std::string text);
static std::string HtmlDecodeOnlyEntityNumber(std::string v);
static std::vector<std::string>
SplitString(std::string text, std::string delimiter,
std::size_t maxCnt = std::string::npos);
static std::string Replace(std::string str, std::string find,
std::string replace);
static std::string StatusCodeString(StatusCode code);
static std::string ToLower(std::string str);
static std::string ToUpper(std::string str);
static std::string LeftPad(std::string text, int count, char c);
};
} // namespace Tesses::Framework::Http

View File

@@ -1,22 +1,41 @@
#pragma once
#include "HttpServer.hpp"
#include "../Filesystem/VFSFix.hpp"
#include "../Filesystem/VFS.hpp"
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
namespace Tesses::Framework::Http
{
class MountableServer : public IHttpServer
{
std::shared_ptr<IHttpServer> root;
std::vector<std::pair<std::string,std::shared_ptr<IHttpServer>>> servers;
std::string Subpath(Filesystem::VFSPath fullPath, Filesystem::VFSPath offsetPath);
bool StartsWith(Filesystem::VFSPath fullPath, Filesystem::VFSPath offsetPath);
public:
MountableServer();
MountableServer(std::shared_ptr<IHttpServer> root);
void Mount(std::string path, std::shared_ptr<IHttpServer> server);
void Unmount(std::string path);
bool Handle(ServerContext& ctx);
~MountableServer();
};
}
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "../Filesystem/VFS.hpp"
#include "../Filesystem/VFSFix.hpp"
#include "HttpServer.hpp"
namespace Tesses::Framework::Http {
class MountableServer : public IHttpServer {
std::shared_ptr<IHttpServer> root;
std::vector<std::pair<std::string, std::shared_ptr<IHttpServer>>> servers;
std::string Subpath(Filesystem::VFSPath fullPath,
Filesystem::VFSPath offsetPath);
bool StartsWith(Filesystem::VFSPath fullPath,
Filesystem::VFSPath offsetPath);
public:
MountableServer();
MountableServer(std::shared_ptr<IHttpServer> root);
void Mount(std::string path, std::shared_ptr<IHttpServer> server);
void Unmount(std::string path);
bool Handle(ServerContext &ctx);
~MountableServer();
};
} // namespace Tesses::Framework::Http

View File

@@ -1,41 +1,59 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "HttpServer.hpp"
#include "../Filesystem/VFSFix.hpp"
#include "../Filesystem/VFS.hpp"
#include "../Filesystem/VFSFix.hpp"
#include "HttpServer.hpp"
namespace Tesses::Framework::Http
{
using ServerRequestHandler = std::function<bool(ServerContext&)>;
class RouteServer : public IHttpServer
{
class RouteServerRoute {
public:
std::vector<std::pair<std::string,bool>> parts;
std::string method;
ServerRequestHandler handler;
namespace Tesses::Framework::Http {
using ServerRequestHandler = std::function<bool(ServerContext &)>;
RouteServerRoute() = default;
RouteServerRoute(std::string route, std::string method, ServerRequestHandler handler);
bool Equals(Tesses::Framework::Filesystem::VFSPath& path, HttpDictionary& args);
};
std::vector<RouteServerRoute> routes;
std::shared_ptr<IHttpServer> root;
public:
RouteServer() = default;
RouteServer(std::shared_ptr<IHttpServer> root);
void Get(std::string pattern, ServerRequestHandler handler);
void Post(std::string pattern, ServerRequestHandler handler);
void Put(std::string pattern, ServerRequestHandler handler);
void Patch(std::string pattern, ServerRequestHandler handler);
class RouteServer : public IHttpServer {
class RouteServerRoute {
public:
std::vector<std::pair<std::string, bool>> parts;
std::string method;
ServerRequestHandler handler;
void Delete(std::string pattern, ServerRequestHandler handler);
void Trace(std::string pattern, ServerRequestHandler handler);
void Options(std::string pattern, ServerRequestHandler handler);
void Add(std::string method, std::string pattern, ServerRequestHandler handler);
bool Handle(ServerContext& ctx);
RouteServerRoute() = default;
RouteServerRoute(std::string route, std::string method,
ServerRequestHandler handler);
bool Equals(Tesses::Framework::Filesystem::VFSPath &path,
HttpDictionary &args);
};
}
std::vector<RouteServerRoute> routes;
std::shared_ptr<IHttpServer> root;
public:
RouteServer() = default;
RouteServer(std::shared_ptr<IHttpServer> root);
void Get(std::string pattern, ServerRequestHandler handler);
void Post(std::string pattern, ServerRequestHandler handler);
void Put(std::string pattern, ServerRequestHandler handler);
void Patch(std::string pattern, ServerRequestHandler handler);
void Delete(std::string pattern, ServerRequestHandler handler);
void Trace(std::string pattern, ServerRequestHandler handler);
void Options(std::string pattern, ServerRequestHandler handler);
void Add(std::string method, std::string pattern,
ServerRequestHandler handler);
bool Handle(ServerContext &ctx);
};
} // namespace Tesses::Framework::Http

View File

@@ -1,43 +1,68 @@
/*
TessesFramework a library to make C++ easier for me, used in CrossLang:
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <functional>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <string>
#include <vector>
#include <cstddef>
namespace Tesses::Framework::Http
{
class WebSocketMessage {
public:
std::vector<uint8_t> data;
bool isBinary;
WebSocketMessage();
WebSocketMessage(std::vector<uint8_t> data);
WebSocketMessage(const void* data, size_t len);
WebSocketMessage(std::string message);
std::string ToString();
namespace Tesses::Framework::Http {
class WebSocketMessage {
public:
std::vector<uint8_t> data;
bool isBinary;
WebSocketMessage();
WebSocketMessage(std::vector<uint8_t> data);
WebSocketMessage(const void *data, size_t len);
WebSocketMessage(std::string message);
std::string ToString();
};
void SendWebSocketMessage(std::function<void(WebSocketMessage &)> cb,
std::string text);
class WebSocketConnection {
};
void SendWebSocketMessage(std::function<void(WebSocketMessage&)> cb, std::string text);
class WebSocketConnection {
public:
virtual void OnOpen(std::function<void(WebSocketMessage&)> sendMessage, std::function<void()> ping,std::function<void()> close)=0;
virtual void OnReceive(WebSocketMessage& message)=0;
virtual void OnClose(bool clean)=0;
virtual ~WebSocketConnection();
};
class CallbackWebSocketConnection : public WebSocketConnection {
public:
std::function<void(std::function<void(WebSocketMessage&)>,std::function<void()>,std::function<void()>)> onOpen;
std::function<void(WebSocketMessage&)> onReceive;
std::function<void(bool)> onClose;
CallbackWebSocketConnection();
CallbackWebSocketConnection(std::function<void(std::function<void(WebSocketMessage&)>,std::function<void()>,std::function<void()>)> onOpen, std::function<void(WebSocketMessage&)> onReceive, std::function<void(bool)> onClose);
public:
virtual void OnOpen(std::function<void(WebSocketMessage &)> sendMessage,
std::function<void()> ping,
std::function<void()> close) = 0;
virtual void OnReceive(WebSocketMessage &message) = 0;
virtual void OnClose(bool clean) = 0;
virtual ~WebSocketConnection();
};
class CallbackWebSocketConnection : public WebSocketConnection {
public:
std::function<void(std::function<void(WebSocketMessage &)>,
std::function<void()>, std::function<void()>)>
onOpen;
std::function<void(WebSocketMessage &)> onReceive;
std::function<void(bool)> onClose;
CallbackWebSocketConnection();
CallbackWebSocketConnection(
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 OnOpen(std::function<void(WebSocketMessage&)> sendMessage, std::function<void()> ping,std::function<void()> closeFn);
void OnReceive(WebSocketMessage& message);
void OnClose(bool clean);
};
}
void OnOpen(std::function<void(WebSocketMessage &)> sendMessage,
std::function<void()> ping, std::function<void()> closeFn);
void OnReceive(WebSocketMessage &message);
void OnClose(bool clean);
};
} // namespace Tesses::Framework::Http