Add CGI support

This commit is contained in:
2025-12-05 18:08:09 -06:00
parent 993c5655cc
commit bbf122a7eb
4 changed files with 259 additions and 90 deletions

View File

@@ -227,6 +227,14 @@ namespace Tesses::CrossLang
} }
return false; return false;
} }
static std::string EnsureSafeVariable(LexToken token)
{
if(token.text.find("__compGen") == 0)
{
throw SyntaxException(token.lineInfo,"__compGen* is reserved for compilers (this error is enforced by the compiler)");
}
return token.text;
}
void Parser::EnsureSymbol(std::string txt) void Parser::EnsureSymbol(std::string txt)
{ {
if(i < tokens.size()) if(i < tokens.size())
@@ -842,6 +850,7 @@ namespace Tesses::CrossLang
if(i >= tokens.size()) throw std::out_of_range("End of file"); if(i >= tokens.size()) throw std::out_of_range("End of file");
auto variable = tokens[i]; auto variable = tokens[i];
i++; i++;
if(variable.type == LexTokenType::Symbol && variable.text == ".") if(variable.type == LexTokenType::Symbol && variable.text == ".")
{ {
@@ -861,7 +870,7 @@ namespace Tesses::CrossLang
else if(variable.type != LexTokenType::Identifier) throw SyntaxException(variable.lineInfo, "Expected an identifier got a " + LexTokenType_ToString(variable.type) + " \"" + variable.text + "\""); else if(variable.type != LexTokenType::Identifier) throw SyntaxException(variable.lineInfo, "Expected an identifier got a " + LexTokenType_ToString(variable.type) + " \"" + variable.text + "\"");
else else
{ {
node = AdvancedSyntaxNode::Create(DeclareExpression,true,{variable.text}); node = AdvancedSyntaxNode::Create(DeclareExpression,true,{EnsureSafeVariable(variable)});
} }
} }
else if(IsIdentifier("const")) else if(IsIdentifier("const"))
@@ -888,7 +897,7 @@ namespace Tesses::CrossLang
else if(variable.type != LexTokenType::Identifier) throw SyntaxException(variable.lineInfo, "Expected an identifier got a " + LexTokenType_ToString(variable.type) + " \"" + variable.text + "\""); else if(variable.type != LexTokenType::Identifier) throw SyntaxException(variable.lineInfo, "Expected an identifier got a " + LexTokenType_ToString(variable.type) + " \"" + variable.text + "\"");
else else
{ {
node = AdvancedSyntaxNode::Create(ConstExpression,true,{variable.text}); node = AdvancedSyntaxNode::Create(ConstExpression,true,{EnsureSafeVariable(variable)});
} }
} }
else if(IsIdentifier("comptime")) else if(IsIdentifier("comptime"))
@@ -956,37 +965,37 @@ namespace Tesses::CrossLang
} }
else if(tokens[i].type == LexTokenType::Identifier) else if(tokens[i].type == LexTokenType::Identifier)
{ {
std::string token = tokens[i].text; auto token=tokens[i];
i++; i++;
bool hasNumber=true; bool hasNumber=true;
int64_t lngNum = 0; int64_t lngNum = 0;
if(token.size() == 1 && token[0] == '0') if(token.text.size() == 1 && token.text[0] == '0')
{ {
lngNum = 0; lngNum = 0;
} }
else else
if(token.size() > 0 && token[0] == '0') if(token.text.size() > 0 && token.text[0] == '0')
{ {
if(token.size() > 1 && token[1] == 'x') if(token.text.size() > 1 && token.text[1] == 'x')
{ {
lngNum = std::stoll(token.substr(2),nullptr,16); lngNum = std::stoll(token.text.substr(2),nullptr,16);
} }
else if(token.size() > 1 && token[1] == 'b') else if(token.text.size() > 1 && token.text[1] == 'b')
{ {
lngNum = std::stoll(token.substr(2),nullptr,2); lngNum = std::stoll(token.text.substr(2),nullptr,2);
} }
else else
{ {
lngNum = std::stoll(token.substr(1),nullptr,8); lngNum = std::stoll(token.text.substr(1),nullptr,8);
} }
} }
else if(token.size() > 0 && token[0] >= '0' && token[0] <= '9') else if(token.text.size() > 0 && token.text[0] >= '0' && token.text[0] <= '9')
{ {
lngNum=std::stoll(token,nullptr,10); lngNum=std::stoll(token.text,nullptr,10);
} }
else else
{ {
@@ -1018,16 +1027,16 @@ namespace Tesses::CrossLang
if(!hasNumber) if(!hasNumber)
{ {
if(token == "true") if(token.text == "true")
node = true; node = true;
else if(token == "false") else if(token.text == "false")
node = false; node = false;
else if(token == "null") else if(token.text == "null")
node = nullptr; node = nullptr;
else if(token == "undefined") else if(token.text == "undefined")
node = Undefined(); node = Undefined();
else { else {
node = AdvancedSyntaxNode::Create(GetVariableExpression,true,{token}); node = AdvancedSyntaxNode::Create(GetVariableExpression,true,{EnsureSafeVariable(token)});
} }
} }
} }

View File

@@ -728,7 +728,7 @@ namespace Tesses::CrossLang
} }
return Undefined(); return Undefined();
} }
static TObject Net_NetworkStream(GCList& ls, std::vector<TObject> args) static TObject New_NetworkStream(GCList& ls, std::vector<TObject> args)
{ {
bool ipv6; bool ipv6;
bool datagram; bool datagram;
@@ -770,7 +770,7 @@ namespace Tesses::CrossLang
} }
}; };
static TObject Net_Http_HttpServer(GCList& ls, std::vector<TObject> args,TRootEnvironment* env) static TObject New_HttpServer(GCList& ls, std::vector<TObject> args,TRootEnvironment* env)
{ {
int64_t port; int64_t port;
std::string pathStr; std::string pathStr;
@@ -1440,13 +1440,102 @@ namespace Tesses::CrossLang
} }
return ""; return "";
} }
static TObject New_FileServer(GCList& ls, std::vector<TObject> args)
{
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
bool allowlisting;
bool spa;
if(GetArgument(args,0,vfs) && GetArgument(args,1,allowlisting) && GetArgument(args,2,spa))
{
return std::make_shared<FileServer>(vfs,allowlisting,spa);
}
return nullptr;
}
static TObject New_BasicAuthServer(GCList& ls, std::vector<TObject> args)
{
auto bAuth = std::make_shared<BasicAuthServer>();
TCallable* cb;
if(!args.empty())
bAuth->server = ToHttpServer(ls.GetGC(),args[0]);
if(GetArgumentHeap(args,1,cb))
{
auto marked= CreateMarkedTObject(ls, cb);
bAuth->authorization = [marked](std::string user,std::string password)->bool {
GCList ls(marked->GetGC());
TCallable* callable;
if(GetObjectHeap(marked->GetObject(), callable))
{
return ToBool(callable->Call(ls,{user,password}));
}
return false;
};
}
GetArgument(args,2,bAuth->realm);
return bAuth;
}
static TObject New_MountableServer(GCList& ls, std::vector<TObject> args)
{
if(args.empty()) return Undefined();
return std::make_shared<MountableServer>(ToHttpServer(ls.GetGC(),args[0]));
}
static TObject New_StreamHttpRequestBody(GCList& ls, std::vector<TObject> args)
{
std::shared_ptr<Stream> strm;
std::string mimeType;
if(GetArgument(args,0,strm) && GetArgument(args, 1, mimeType))
{
return TNativeObject::Create<THttpRequestBody>(ls, new StreamHttpRequestBody(strm, mimeType));
}
return nullptr;
}
static TObject New_TextHttpRequestBody(GCList& ls, std::vector<TObject> args)
{
std::string text;
std::string mimeType;
if(GetArgument(args, 0, text) && GetArgument(args, 1, mimeType))
{
return TNativeObject::Create<THttpRequestBody>(ls,new TextHttpRequestBody(text, mimeType));
}
return nullptr;
}
static TObject New_JsonHttpRequestBody(GCList& ls, std::vector<TObject> args)
{
if(!args.empty())
{
return TNativeObject::Create<THttpRequestBody>(ls,new TextHttpRequestBody(Json_Encode(args[0]), "application/json"));
}
return nullptr;
}
void TStd::RegisterNet(GC* gc, TRootEnvironment* env) void TStd::RegisterNet(GC* gc, TRootEnvironment* env)
{ {
env->permissions.canRegisterNet=true; env->permissions.canRegisterNet=true;
GCList ls(gc); GCList ls(gc);
TDictionary* dict = TDictionary::Create(ls);
gc->BarrierBegin();
TDictionary* dict = env->EnsureDictionary(gc,"Net");
TDictionary* _new = env->EnsureDictionary(gc,"New");
_new->DeclareFunction(gc, "StreamHttpRequestBody","Create a stream request body",{"stream","mimeType"},New_StreamHttpRequestBody);
_new->DeclareFunction(gc, "TextHttpRequestBody","Create a text request body",{"text","mimeType"},New_TextHttpRequestBody);
_new->DeclareFunction(gc, "JsonHttpRequestBody","Create a text request body",{"json"},New_JsonHttpRequestBody);
_new->DeclareFunction(gc, "HttpServer", "Create a http server (allows multiple)",{"server","portOrUnixPath","$printIPs"},[env](GCList& ls, std::vector<TObject> args)->TObject{
return New_HttpServer(ls,args,env);
});
_new->DeclareFunction(gc, "FileServer","Create a file server",{"vfs","allowlisting","spa"}, New_FileServer);
_new->DeclareFunction(gc, "BasicAuthServer", "Create a basic auth server", {"$server","$auth","$realm"},New_BasicAuthServer);
_new->DeclareFunction(gc, "MountableServer","Create a server you can mount to, must mount parents before child",{"root"}, New_MountableServer);
_new->DeclareFunction(gc, "NetworkStream","Create a network stream",{"ipv6","datagram"},New_NetworkStream);
TDictionary* http = TDictionary::Create(ls); TDictionary* http = TDictionary::Create(ls);
TDictionary* smtp = TDictionary::Create(ls); TDictionary* smtp = TDictionary::Create(ls);
@@ -1461,88 +1550,40 @@ namespace Tesses::CrossLang
http->DeclareFunction(gc, "MimeType","Get mimetype from extension",{"ext"},Net_Http_MimeType); http->DeclareFunction(gc, "MimeType","Get mimetype from extension",{"ext"},Net_Http_MimeType);
//http->DeclareFunction(gc, "ProcessServer","Process HTTP server connection",{"networkstream","server","ip","port","encrypted"},, Net_ProcessServer); http->DeclareFunction(gc, "StreamHttpRequestBody","Create a stream request body",{"stream","mimeType"},New_StreamHttpRequestBody);
http->DeclareFunction(gc, "StreamHttpRequestBody","Create a stream request body",{"stream","mimeType"},[](GCList& ls, std::vector<TObject> args)->TObject { http->DeclareFunction(gc, "TextHttpRequestBody","Create a text request body",{"text","mimeType"},New_TextHttpRequestBody);
std::shared_ptr<Stream> strm; http->DeclareFunction(gc, "JsonHttpRequestBody","Create a text request body",{"json"},New_JsonHttpRequestBody);
std::string mimeType;
if(GetArgument(args,0,strm) && GetArgument(args, 1, mimeType))
{
return TNativeObject::Create<THttpRequestBody>(ls, new StreamHttpRequestBody(strm, mimeType));
}
return nullptr;
});
http->DeclareFunction(gc, "TextHttpRequestBody","Create a text request body",{"text","mimeType"},[](GCList& ls, std::vector<TObject> args)->TObject {
std::string text;
std::string mimeType;
if(GetArgument(args, 0, text) && GetArgument(args, 1, mimeType))
{
return TNativeObject::Create<THttpRequestBody>(ls,new TextHttpRequestBody(text, mimeType));
}
return nullptr;
});
http->DeclareFunction(gc, "JsonHttpRequestBody","Create a text request body",{"json"},[](GCList& ls, std::vector<TObject> args)->TObject {
if(!args.empty())
{
return TNativeObject::Create<THttpRequestBody>(ls,new TextHttpRequestBody(Json_Encode(args[0]), "application/json"));
}
return nullptr;
});
http->DeclareFunction(gc, "MakeRequest", "Create an http request", {"url","$extra"}, [env](GCList& ls, std::vector<TObject> args)->TObject {return Net_Http_MakeRequest(ls,args,env);}); http->DeclareFunction(gc, "MakeRequest", "Create an http request", {"url","$extra"}, [env](GCList& ls, std::vector<TObject> args)->TObject {return Net_Http_MakeRequest(ls,args,env);});
http->DeclareFunction(gc, "WebSocketClient", "Create a websocket connection",{"url","headers","conn","$successCB"},Net_Http_WebSocketClient); http->DeclareFunction(gc, "WebSocketClient", "Create a websocket connection",{"url","headers","conn","$successCB"},Net_Http_WebSocketClient);
http->DeclareFunction(gc, "DownloadToString","Return the http file's contents as a string",{"url"},Net_Http_DownloadToString); http->DeclareFunction(gc, "DownloadToString","Return the http file's contents as a string",{"url"},Net_Http_DownloadToString);
http->DeclareFunction(gc, "DownloadToStream","Download file to stream",{"url","stream"},Net_Http_DownloadToStream); http->DeclareFunction(gc, "DownloadToStream","Download file to stream",{"url","stream"},Net_Http_DownloadToStream);
http->DeclareFunction(gc, "DownloadToFile","Download file to file in vfs",{"url","vfs","path"},Net_Http_DownloadToFile); http->DeclareFunction(gc, "DownloadToFile","Download file to file in vfs",{"url","vfs","path"},Net_Http_DownloadToFile);
http->DeclareFunction(gc, "HttpServer", "Create a http server (allows multiple)",{"server","portOrUnixPath","$printIPs"},[env](GCList& ls, std::vector<TObject> args)->TObject{ http->DeclareFunction(gc, "HttpServer", "Create a http server (allows multiple)",{"server","portOrUnixPath","$printIPs"},[env](GCList& ls, std::vector<TObject> args)->TObject{
return Net_Http_HttpServer(ls,args,env); return New_HttpServer(ls,args,env);
}); });
http->DeclareFunction(gc, "ListenSimpleWithLoop", "Listen (creates application loop)", {"server","port"},Net_Http_ListenSimpleWithLoop); http->DeclareFunction(gc, "ListenSimpleWithLoop", "Listen (creates application loop)", {"server","port"},Net_Http_ListenSimpleWithLoop);
http->DeclareFunction(gc, "ListenOnUnusedPort","Listen on unused localhost port and print Port: theport",{"server"},Net_Http_ListenOnUnusedPort); http->DeclareFunction(gc, "ListenOnUnusedPort","Listen on unused localhost port and print Port: theport",{"server"},Net_Http_ListenOnUnusedPort);
//FileServer svr() //FileServer svr()
http->DeclareFunction(gc, "FileServer","Create a file server",{"vfs","allowlisting","spa"}, [](GCList& ls, std::vector<TObject> args)->TObject{ http->DeclareFunction(gc, "FileServer","Create a file server",{"vfs","allowlisting","spa"}, New_FileServer);
http->DeclareFunction(gc, "BasicAuthServer", "Create a basic auth server", {"$server","$auth","$realm"},New_BasicAuthServer);
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs; http->DeclareFunction(gc, "BasicAuthGetCreds","Get creds from str",{"ctx"},[](GCList& ls, std::vector<TObject> args)->TObject {
bool allowlisting; TServerContext* sc;
bool spa; if(GetArgumentHeap(args,0,sc))
if(GetArgument(args,0,vfs) && GetArgument(args,1,allowlisting) && GetArgument(args,2,spa))
{ {
return std::make_shared<FileServer>(vfs,allowlisting,spa); std::string user;
std::string pass;
if(sc->IsAvailable() && BasicAuthServer::GetCreds(*sc->GetContext(),user,pass))
{
return TDictionary::Create(ls,{
TDItem("Username",user),
TDItem("Password",pass)
});
}
} }
return nullptr; return nullptr;
}); });
http->DeclareFunction(gc, "MountableServer","Create a server you can mount to, must mount parents before child",{"root"}, [](GCList& ls, std::vector<TObject> args)->TObject{ http->DeclareFunction(gc, "MountableServer","Create a server you can mount to, must mount parents before child",{"root"}, New_MountableServer);
TCallable* call; dict->DeclareFunction(gc, "NetworkStream","Create a network stream",{"ipv6","datagram"},New_NetworkStream);
TDictionary* dict;
TClassObject* cls;
std::shared_ptr<Tesses::Framework::Http::IHttpServer> mySvr;
if(GetArgumentHeap(args,0,call))
{
auto svr = std::make_shared<TObjectHttpServer>(ls.GetGC(), call);
return std::make_shared<MountableServer>(svr);
}
else if(GetArgumentHeap(args,0,dict))
{
auto svr = std::make_shared<TObjectHttpServer>(ls.GetGC(), dict);
return std::make_shared<MountableServer>(svr);
}
else if(GetArgumentHeap(args,0,cls))
{
auto svr = std::make_shared<TObjectHttpServer>(ls.GetGC(), cls);
return std::make_shared<MountableServer>(svr);
}
else if(GetArgument(args,0,mySvr))
{
return std::make_shared<MountableServer>(mySvr);
}
return nullptr;
});
dict->DeclareFunction(gc, "NetworkStream","Create a network stream",{"ipv6","datagram"},Net_NetworkStream);
smtp->DeclareFunction(gc, "Send","Send email via smtp server",{"messageStruct"},Net_Smtp_Send); smtp->DeclareFunction(gc, "Send","Send email via smtp server",{"messageStruct"},Net_Smtp_Send);
dict->DeclareFunction(gc, "getIPAddresses","Get the ip addresses of this machine",{"$ipv6"},[](GCList& ls, std::vector<TObject> args)->TObject{ dict->DeclareFunction(gc, "getIPAddresses","Get the ip addresses of this machine",{"$ipv6"},[](GCList& ls, std::vector<TObject> args)->TObject{
TList* a = TList::Create(ls); TList* a = TList::Create(ls);
@@ -1559,10 +1600,8 @@ namespace Tesses::CrossLang
ls.GetGC()->BarrierEnd();; ls.GetGC()->BarrierEnd();;
return a; return a;
}); });
gc->BarrierBegin();
dict->SetValue("Http", http); dict->SetValue("Http", http);
dict->SetValue("Smtp", smtp); dict->SetValue("Smtp", smtp);
env->DeclareVariable("Net", dict);
gc->BarrierEnd(); gc->BarrierEnd();
} }
} }

View File

@@ -291,6 +291,14 @@ namespace Tesses::CrossLang
env->SetVariable("Process",dict); env->SetVariable("Process",dict);
auto process = env->EnsureDictionary(gc,"New"); auto process = env->EnsureDictionary(gc,"New");
process->DeclareFunction(gc, "Process", "Create process",{},New_Process); process->DeclareFunction(gc, "Process", "Create process",{},New_Process);
process->DeclareFunction(gc, "CGIServer", "Create a CGI Server",{"path"},[](GCList& ls, std::vector<TObject> args)->TObject{
Tesses::Framework::Filesystem::VFSPath path;
if(GetArgumentAsPath(args,0,path))
{
return std::make_shared<Tesses::Framework::Http::CGIServer>(path);
}
return Undefined();
});
gc->BarrierEnd(); gc->BarrierEnd();
} }
} }

View File

@@ -3763,6 +3763,7 @@ namespace Tesses::CrossLang {
if(svr != nullptr) if(svr != nullptr)
{ {
auto mountable = std::dynamic_pointer_cast<Tesses::Framework::Http::MountableServer>(svr); auto mountable = std::dynamic_pointer_cast<Tesses::Framework::Http::MountableServer>(svr);
if(mountable != nullptr) if(mountable != nullptr)
{ {
if(key == "Mount") if(key == "Mount")
@@ -6256,6 +6257,118 @@ namespace Tesses::CrossLang {
cse.back()->Push(gc,Undefined()); cse.back()->Push(gc,Undefined());
return false; return false;
} }
if(std::holds_alternative<std::shared_ptr<Tesses::Framework::Http::IHttpServer>>(instance))
{
auto svr = std::get<std::shared_ptr<Tesses::Framework::Http::IHttpServer>>(instance);
auto bas = std::dynamic_pointer_cast<Tesses::Framework::Http::BasicAuthServer>(svr);
auto cgi = std::dynamic_pointer_cast<Tesses::Framework::Http::CGIServer>(svr);
auto changable = std::dynamic_pointer_cast<Tesses::Framework::Http::ChangeableServer>(svr);
if(changable != nullptr)
{
if(key == "Server")
{
bas->server = ToHttpServer(gc,value);
stk->Push(gc,value);
return false;
}
}
if(bas != nullptr)
{
if(key == "Realm")
{
bool val;
if(GetObject(value,val))
{
bas->realm = val;
stk->Push(gc,val );
return false;
}
}
if(key == "Server")
{
bas->server = ToHttpServer(gc,value);
stk->Push(gc,value);
return false;
}
if(key == "Authorization")
{
TCallable* val;
if(GetObjectHeap(value,val))
{
auto marked= CreateMarkedTObject(ls, val);
bas->authorization = [marked](std::string user,std::string password)->bool {
GCList ls(marked->GetGC());
TCallable* callable;
if(GetObjectHeap(marked->GetObject(), callable))
{
return ToBool(callable->Call(ls,{user,password}));
}
return false;
};
stk->Push(gc,val);
return false;
}
}
}
if(cgi != nullptr)
{
if(key == "WorkingDirectory")
{
Tesses::Framework::Filesystem::VFSPath path;
if(GetObjectAsPath(value,path))
{
cgi->workingDirectory = path;
stk->Push(gc,path);
return false;
}
else {
cgi->workingDirectory = std::nullopt;
stk->Push(gc,nullptr);
return false;
}
}
if(key == "DocumentRoot")
{
Tesses::Framework::Filesystem::VFSPath path;
if(GetObjectAsPath(value,path))
{
cgi->document_root= path;
stk->Push(gc,path);
return false;
}
else {
cgi->document_root= std::nullopt;
stk->Push(gc,nullptr);
return false;
}
}
if(key == "AdminEmail")
{
std::string str;
if(GetObject(value,str))
{
cgi->adminEmail = str;
stk->Push(gc,str);
return false;
}
else {
cgi->adminEmail= std::nullopt;
stk->Push(gc,nullptr);
return false;
}
}
}
stk->Push(gc, Undefined());
return false;
}
if(std::holds_alternative<std::shared_ptr<Tesses::Framework::Streams::Stream>>(instance)) if(std::holds_alternative<std::shared_ptr<Tesses::Framework::Streams::Stream>>(instance))
{ {
auto strm = std::get<std::shared_ptr<Tesses::Framework::Streams::Stream>>(instance); auto strm = std::get<std::shared_ptr<Tesses::Framework::Streams::Stream>>(instance);