Add date code

This commit is contained in:
2025-05-10 19:52:23 -05:00
parent 1cb9bc93ee
commit 21b0418926
31 changed files with 21614 additions and 78 deletions

View File

@@ -43,7 +43,9 @@ namespace Tesses::Framework::Http
bool retVal = false;
if(strm != nullptr)
{
ctx.WithMimeType(HttpUtils::MimeType(path.GetFileName())).SendStream(strm);
Date::DateTime lw,la;
this->vfs->GetDate(path,lw,la);
ctx.WithLastModified(lw).WithMimeType(HttpUtils::MimeType(path.GetFileName())).SendStream(strm);
retVal = true;
}

View File

@@ -507,18 +507,42 @@ namespace Tesses::Framework::Http
while(parseSection(this, ct, cb));
}
HttpServer::HttpServer(uint16_t port, IHttpServer* http, bool owns)
{
this->server = new TcpServer(port, 10);
this->http = http;
this->owns = owns;
this->thrd=nullptr;
this->port = port;
}
HttpServer::HttpServer(uint16_t port, IHttpServer& http) : HttpServer(port,&http,false)
HttpServer::HttpServer(Tesses::Framework::Streams::TcpServer& tcpServer, IHttpServer& http, bool showIPs) : HttpServer(&tcpServer,false,&http,false,showIPs)
{
}
HttpServer::HttpServer(Tesses::Framework::Streams::TcpServer* tcpServer, bool ownsTCP, IHttpServer& http, bool showIPs) : HttpServer(tcpServer,ownsTCP,&http,false,showIPs)
{
}
HttpServer::HttpServer(Tesses::Framework::Streams::TcpServer& tcpServer, IHttpServer* http, bool ownsHttpServer, bool showIPs) : HttpServer(&tcpServer,false,http,ownsHttpServer,showIPs)
{
}
HttpServer::HttpServer(Tesses::Framework::Streams::TcpServer* tcpServer, bool ownsTCP, IHttpServer* http, bool ownsHttpServer, bool showIPs)
{
this->server = tcpServer;
this->ownsTCP = ownsTCP;
this->http = http;
this->ownsHttp = ownsHttpServer;
this->showIPs = showIPs;
this->thrd=nullptr;
}
HttpServer::HttpServer(uint16_t port, IHttpServer* http, bool owns, bool showIPs) : HttpServer(new TcpServer(port,10),true,http,owns,showIPs)
{
}
HttpServer::HttpServer(uint16_t port, IHttpServer& http,bool showIPs) : HttpServer(port,&http,false,showIPs)
{
}
uint16_t HttpServer::GetPort()
{
if(server != nullptr)
return server->GetPort();
return 0;
}
Stream* ServerContext::OpenResponseStream()
{
@@ -543,7 +567,7 @@ namespace Tesses::Framework::Http
void HttpServer::StartAccepting()
{
fflush(stdout);
if(http == nullptr) return;
if(http == nullptr || server == nullptr) return;
auto svr=this->server;
auto http = this->http;
TF_LOG("Before Creating Thread");
@@ -574,31 +598,37 @@ namespace Tesses::Framework::Http
TF_LOG("After attach");
}
});
TF_LOG("Before printing interfaces");
std::cout << "\x1B[34mInterfaces:\n";
for(auto _ip : NetworkStream::GetIPs())
if(this->showIPs)
{
std::cout << "\x1B[32m";
std::cout << _ip.first << ": ";
std::cout << "\x1B[35mhttp://";
std::cout << _ip.second << ":" << std::to_string(this->port) << "/\n";
TF_LOG("Before printing interfaces");
std::cout << "\x1B[34mInterfaces:\n";
for(auto _ip : NetworkStream::GetIPs())
{
std::cout << "\x1B[32m";
std::cout << _ip.first << ": ";
std::cout << "\x1B[35mhttp://";
std::cout << _ip.second << ":" << std::to_string(this->GetPort()) << "/\n";
}
if(!svr->IsValid()) std::cout << "\x1B[31mError, we failed to bind or something\x1B[39m\n" << std::endl;
std::cout << "\x1B[31mAlmost Ready to Listen\x1B[39m\n";
}
if(!svr->IsValid()) std::cout << "\x1B[31mError, we failed to bind or something\x1B[39m\n" << std::endl;
std::cout << "\x1B[31mAlmost Ready to Listen\x1B[39m\n";
TF_LOG("After printing interfaces");
}
HttpServer::~HttpServer()
{
auto port = this->GetPort();
this->server->Close();
TF_ConnectToSelf(this->port);
TF_ConnectToSelf(port);
if(this->thrd != nullptr)
{
this->thrd->Join();
delete this->thrd;
}
if(this->owns)
if(this->ownsHttp)
delete http;
if(this->ownsTCP)
delete this->server;
}
IHttpServer::~IHttpServer()
@@ -622,6 +652,12 @@ namespace Tesses::Framework::Http
strm.GetBuffer() = buff;
SendStream(strm);
}
ServerContext& ServerContext::WithLastModified(Date::DateTime dt)
{
this->responseHeaders.SetValue("Last-Modified",dt);
return *this;
}
void ServerContext::SendText(std::string text)
{
MemoryStream strm(false);

View File

@@ -1,5 +1,7 @@
#include "TessesFramework/Http/HttpUtils.hpp"
#include "TessesFramework/Filesystem/VFS.hpp"
#include <iostream>
#include <algorithm>
using VFSPath = Tesses::Framework::Filesystem::VFSPath;
namespace Tesses::Framework::Http {
@@ -158,6 +160,38 @@ namespace Tesses::Framework::Http {
uri.append(this->GetPathAndQuery());
return uri;
}
std::string HttpUtils::Replace(std::string text, std::string find, std::string replace)
{
std::string dest;
while(text.length() > 0)
{
std::size_t index= text.find(find);
if(index == std::string::npos)
{
dest.append(text);
break;
}
else
{
std::string left = text.substr(0,index);
text = text.substr(index+find.size());
dest.append(left);
dest.append(replace);
}
}
return dest;
}
std::string HttpUtils::LeftPad(std::string text, int count, char c)
{
std::stringstream strm(std::ios_base::out);
strm << std::setfill(c) << std::setw(count) << text;
return strm.str();
}
char HttpUtils::NibbleToHex(uint8_t b)
{
b %= 16;
@@ -672,6 +706,18 @@ namespace Tesses::Framework::Http {
{
kvp[key] = {value};
}
void HttpDictionary::SetValue(std::string key, int64_t value)
{
kvp[key] = {std::to_string(value)};
}
void HttpDictionary::SetValue(std::string key, double value)
{
kvp[key] = {std::to_string(value)};
}
void HttpDictionary::SetValue(std::string key, Date::DateTime value)
{
kvp[key] = {value.ToHttpDate()};
}
void HttpDictionary::SetValue(std::string key, std::vector<std::string> value)
{
kvp[key] = value;
@@ -680,6 +726,20 @@ namespace Tesses::Framework::Http {
{
kvp[key].push_back(value);
}
void HttpDictionary::AddValue(std::string key, int64_t value)
{
kvp[key].push_back(std::to_string(value));
}
void HttpDictionary::AddValue(std::string key, double value)
{
kvp[key].push_back(std::to_string(value));
}
void HttpDictionary::AddValue(std::string key, Date::DateTime value)
{
kvp[key].push_back(value.ToHttpDate());
}
void HttpDictionary::AddValue(std::string key, std::vector<std::string> value)
{
auto& ls = kvp[key];
@@ -711,7 +771,12 @@ namespace Tesses::Framework::Http {
}
return true;
}
bool HttpDictionary::TryGetFirstDate(std::string key, Date::DateTime& dt)
{
std::string val;
if(!TryGetFirst(key,val)) return false;
return Date::DateTime::TryParseHttpDate(val,dt);
}
bool HttpDictionary::TryGetFirstDouble(std::string key, double& value)
{
std::string val;