first commit

This commit is contained in:
2024-12-06 04:58:55 -06:00
commit 856373b396
61 changed files with 5920 additions and 0 deletions

16
examples/copyfile.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "TessesFramework/TessesFramework.hpp"
using namespace Tesses::Framework::Filesystem;
int main(int argc, char** argv)
{
LocalFilesystem fs;
VFSPath src = fs.SystemToVFSPath(argv[1]);
VFSPath dest = fs.SystemToVFSPath(argv[2]);
auto srcs = fs.OpenFile(src,"rb");
auto dests = fs.OpenFile(dest,"wb");
srcs->CopyTo(*dests);
delete srcs;
delete dests;
}

22
examples/download.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "TessesFramework/TessesFramework.hpp"
#include <iostream>
using namespace Tesses::Framework;
using namespace Tesses::Framework::Streams;
using namespace Tesses::Framework::Http;
int main(int argc, char** argv)
{
if(argc < 3)
{
printf("USAGE: %s <url> <path>\n",argv[0]);
return 1;
}
FileStream strm(argv[2],"wb");
HttpRequest req;
req.url = argv[1];
HttpResponse resp(req);
resp.CopyToStream(&strm);
return 0;
}

23
examples/fileserver.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "TessesFramework/TessesFramework.hpp"
#include <iostream>
using namespace Tesses::Framework;
using namespace Tesses::Framework::Http;
using namespace Tesses::Framework::Streams;
using namespace Tesses::Framework::TextStreams;
using namespace Tesses::Framework::Threading;
int main(int argc, char** argv)
{
TF_Init();
if(argc < 1)
{
printf("USAGE: %s <dir>\n",argv[0]);
return 0;
}
FileServer fs(argv[1],true,false);
HttpServer server(10000U,fs);
server.StartAccepting();
TF_RunEventLoop();
std::cout << "Closing server" << std::endl;
return 0;
}

View File

@ -0,0 +1,63 @@
#include "TessesFramework/TessesFramework.hpp"
#include <iostream>
using namespace Tesses::Framework;
using namespace Tesses::Framework::Filesystem;
using namespace Tesses::Framework::Streams;
int main(int argc, char** argv)
{
if(argc < 2)
{
printf("USAGE: %s <command> <args...>\n",argv[0]);
return 1;
}
LocalFilesystem lfs;
std::string root = "./root";
std::string mountDemi = "./demi";
std::string mountJoelSlashJim = "./joelslashjim";
SubdirFilesystem rootdir(&lfs,root,false);
SubdirFilesystem mountDemidir(&lfs,mountDemi,false);
SubdirFilesystem mountjohnslashjim(&lfs,mountJoelSlashJim,false);
MountableFilesystem fs(&rootdir,false);
fs.Mount(std::string("/demi"), &mountDemidir,false);
fs.Mount(std::string("/joel/jim"), &mountjohnslashjim,false);
std::string command = argv[1];
if(command == "ls")
{
std::string dir = "/";
if(argc > 2) dir = argv[2];
std::vector<VFSPath> paths;
fs.GetPaths(dir, paths);
for(auto item : paths)
{
std::cout << item.GetFileName() << std::endl;
}
}
else if(command == "cat")
{
FileStream strm(stdout, false,"wb",false);
for(int a = 2; a < argc; a++)
{
std::string path = argv[a];
auto f = fs.OpenFile(path,"rb");
if(f != nullptr)
{
f->CopyTo(strm);
delete f;
}
}
}
}

19
examples/safesubpath.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "TessesFramework/TessesFramework.hpp"
#include <iostream>
using namespace Tesses::Framework::Filesystem;
int main(int argc, char** argv)
{
if(argc < 3)
{
std::cout << "USAGE: " << argv[0] << " <parent> <subpath>\n";
return 1;
}
VFSPath parent(argv[1]);
VFSPath subpath(argv[2]);
VFSPath newPath(parent,subpath.CollapseRelativeParents());
std::cout << newPath.ToString() << "\n";
}

100
examples/webserverex.cpp Normal file
View File

@ -0,0 +1,100 @@
#include "TessesFramework/TessesFramework.hpp"
#include <iostream>
using namespace Tesses::Framework;
using namespace Tesses::Framework::Http;
using namespace Tesses::Framework::Streams;
using namespace Tesses::Framework::TextStreams;
using namespace Tesses::Framework::Threading;
class MyWebServer : public IHttpServer {
public:
bool Handle(ServerContext& ctx)
{
if(ctx.path == "/")
{
FileStream fs("index.html","rb");
ctx
.WithMimeType("text/html")
.SendStream(fs);
return true;
}
else if(ctx.path == "/streaming.html")
{
StreamWriter writer(ctx.OpenResponseStream(),true);
writer.WriteLine("<html>");
writer.WriteLine("<head><title>Streaming</title></head>");
writer.WriteLine("<body>");
writer.WriteLine("<h1>Streaming</h1>");
writer.WriteLine("<ul>");
for(size_t i=0;i<10000; i++)
{
writer.WriteLine("<li>" + std::to_string(i) + "</li>");
}
writer.WriteLine("</ul>");
writer.WriteLine("</body>");
writer.WriteLine("</html>");
return true;
}
else if(ctx.path == "/main.js")
{
FileStream fs("main.js","rb");
ctx
.WithMimeType("text/js")
.SendStream(fs);
return true;
}
else if(ctx.path == "/upload")
{
ctx.ParseFormData([](std::string mime, std::string filename, std::string name)->Tesses::Framework::Streams::Stream*{
return new FileStream(filename,"wb");
});
}
return false;
}
bool Handle1(ServerContext& ctx)
{
std::string name;
if(ctx.queryParams.TryGetFirst("name",name))
{
std::cout << name << std::endl;
ctx
.WithMimeType("text/plain")
.WithContentDisposition(HttpUtils::Sanitise(name) + ".txt",false)
.SendText(name + " is cool.");
//do something with q
return true;
}
return false;
}
};
int main(int argc, char** argv)
{
TF_Init();
TcpServer server(9985U,10);
MyWebServer svr;
while(true)
{
std::string ip;
uint16_t port;
auto res = server.GetStream(ip, port);
if(res == nullptr) return 0;
std::cout << "Got from " << ip << ":" << port << std::endl;
Thread thrd([ip,port,res,&svr]()->void {
HttpServer::Process(*res, svr, ip,port, false);
delete res;
});
thrd.Detach();
}
}