improve httpserver, add httpserver/static.spl

This commit is contained in:
Daniella 2024-09-23 15:45:52 +02:00
parent 416a310072
commit b454fe9dee
4 changed files with 60 additions and 5 deletions

View file

@ -16,16 +16,21 @@ construct net:http:Server {
this
}
accept { net:http:server:Request | with this ;
this:stream:accept net:http:server:Request:new
this this:stream:accept net:http:server:Request:new
}
close { | with this ; this:stream:close; }
}
construct net:http:server namespace {
Request
;
register { | with name this ;
name "net:http:server" register-field
}
}
construct net:http:server:Request {
server
stream
head
body
@ -34,7 +39,8 @@ construct net:http:server:Request {
headers
wrote-body
;
construct { this | with stream this ;
construct { this | with server stream this ;
server this:=server
stream this:=stream
0 anew this:=head
0 anew this:=body
@ -144,4 +150,7 @@ construct net:http:server:Request {
}
this:stream:close;
}
is-open { bool | with this ;
this:wrote-body not
}
}

43
spl/httpserver/static.spl Normal file
View file

@ -0,0 +1,43 @@
"base.spl" import
"_static_ext_server" net:http:server:register
"_static_ext_Request" net:http:server:register
construct net:http:server:_static_ext_server {
cached-files
;
get-cached-files { cached-files | with this ;
this:cached-files dup not if { pop MicroMap:new dup this:=cached-files }
}
}
construct net:http:server:_static_ext_Request {
;
serve-file { this | with filepath path type this ;
this:path path eq if {
filepath read-file this:write-ok:write-content-type<type>:write-str-body:finish;
}
this
}
serve-file-cached { this | with filepath path type this ;
this:path path eq if {
filepath this:server:get-cached-files:get dup not if {
pop filepath read-file dup this:server:cached-files:set;<filepath>
}
this:write-ok:write-content-type<type>:write-str-body:finish;
}
this
}
serve-string { this | with string path type this ;
this:path path eq if {
string this:write-ok:write-content-type<type>:write-str-body:finish;
}
this
}
serve-html { this | with this ; "text/html" this:serve-file }
serve-html-cached { this | with this ; "text/html" this:serve-file-cached }
serve-html-string { this | with this ; "text/html" this:serve-string }
}
include net:http:server:_static_ext_server in net:http:Server
include net:http:server:_static_ext_Request in net:http:server:Request

View file

@ -13,7 +13,8 @@ pub const REPL: &str = include_str!("../spl/repl.spl");
pub const PURE: &str = include_str!("../spl/pure.spl");
pub const TIME: &str = include_str!("../spl/time.spl");
pub const SERVER: &str = include_str!("../spl/server.spl");
pub const HTTP_SERVER: &str = include_str!("../spl/http/server.spl");
pub const HTTP_SERVER: &str = include_str!("../spl/httpserver/base.spl");
pub const HTTP_SERVER_STATIC: &str = include_str!("../spl/httpserver/static.spl");
pub const NOP: &str = "";
pub fn register(runtime: &mut Runtime) {
@ -31,7 +32,8 @@ pub fn register(runtime: &mut Runtime) {
insert("pure.spl", PURE);
insert("time.spl", TIME);
insert("server.spl", SERVER);
insert("http/server.spl", HTTP_SERVER);
insert("httpserver/base.spl", HTTP_SERVER);
insert("httpserver/static.spl", HTTP_SERVER_STATIC);
insert("nop.spl", NOP);
}
}

View file

@ -4,7 +4,8 @@
"#messaging.spl" import
"#server.spl" import
"#time.spl" import
"#http/server.spl" import
"#httpserver/base.spl" import
"SPL tester" =program-name