diff --git a/spl/http/server.spl b/spl/httpserver/base.spl similarity index 92% rename from spl/http/server.spl rename to spl/httpserver/base.spl index 4c1b3d6..8da45cb 100644 --- a/spl/http/server.spl +++ b/spl/httpserver/base.spl @@ -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 + } } diff --git a/spl/httpserver/static.spl b/spl/httpserver/static.spl new file mode 100644 index 0000000..c8779b1 --- /dev/null +++ b/spl/httpserver/static.spl @@ -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: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; + } + this:write-ok:write-content-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: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 diff --git a/src/stdlib.rs b/src/stdlib.rs index b3e59c3..34b294b 100644 --- a/src/stdlib.rs +++ b/src/stdlib.rs @@ -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); } } diff --git a/test.spl b/test.spl index d724728..639de80 100644 --- a/test.spl +++ b/test.spl @@ -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