diff --git a/.ikc3_history b/.ikc3_history
index 6466141..1d6c508 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,4 +1,3 @@
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, op]], fn (fact) { puts(fact.object); 1 })
op
op = ?
Facts.with(Facts.env_facts(), quote [[KC3, :operator, op]], fn (fact) { puts(fact.object); 1 })
@@ -97,3 +96,4 @@ Facts.first_with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { fact.obj
first_operator = Facts.first_with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { fact.object })
first_operator
List.append([1, 2, 3], 4)
+HTTPd.routes
diff --git a/ikc3/.ikc3_history b/ikc3/.ikc3_history
index bed1738..f76e1d9 100644
--- a/ikc3/.ikc3_history
+++ b/ikc3/.ikc3_history
@@ -97,3 +97,4 @@ quote a = ? <- 1 ; 2
KC3.Operator.find(:xxx)
KC3.Operator.find(:+)
quote 1 + 20 / 3 * 4 - 5
+
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index a254ee1..0bff7d0 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -1,16 +1,18 @@
defmodule HTTPd do
require Event
+ require Facts
require File
require HTTP
require HTTP.Request
require HTTP.Response
+ require HTTPd.Route
require List
require Socket
require Socket.Buf
require Str
- def root_dir = "./public"
+ def root_dir = "./static"
def http_client = fn (socket, events, client_ev, client) do
if List.has?(events, :read) do
@@ -53,18 +55,22 @@ defmodule HTTPd do
end
Socket.close(socket)
}
-
+
def main = fn {
() {
- HTTP.mime_type_load("mime.types")
host = getenv("KC3_HTTPD_HOST")
port = getenv("KC3_HTTPD_PORT")
event_base = Event.base_new()
+ HTTP.mime_type_load("config/mime.types")
+ if (File.exists?("config/routes.kc3")) do
+ load("config/routes")
+ end
server(host, port)
}
(host, port) {
HTTP.mime_type_load("mime.types")
event_base = Event.base_new()
+ load("config/routes")
server(host, port)
}
}
@@ -155,10 +161,35 @@ defmodule HTTPd do
error_404_page(request)
end
}
+
+ def routes = []
+
+ def def_route = fn (path, controller) {
+ tmp = List.append(HTTPd.routes,
+ %HTTPd.Route{path: path,
+ controller: controller})
+ def HTTPd.routes = tmp
+ }
- def route_request = fn (request) {
+ def route_request = fn {
+ (request) {
+ route_request(request, routes)
+ }
+ (request, []) {
+ error_404_page
+ }
+ (request, [route | next_routes]) {
+ if (controller = HTTPd.Route.match(route, request)) do
+ controller
+ else
+ route_request(request, next_routes)
+ end
+ }
+ }
+
+ def static_controller = fn (request) {
path = root_dir + request.url
- if File.exists?(path) do
+ render = if File.exists?(path) do
if File.is_directory?(path) do
directory_page
else
@@ -167,6 +198,9 @@ defmodule HTTPd do
else
error_404_page
end
+ render(request)
}
+ def_route("/", static_controller)
+
end
diff --git a/lib/kc3/0.1/httpd/route.kc3 b/lib/kc3/0.1/httpd/route.kc3
new file mode 100644
index 0000000..a854375
--- /dev/null
+++ b/lib/kc3/0.1/httpd/route.kc3
@@ -0,0 +1,14 @@
+defmodule HTTPd.Route do
+
+ require Str
+
+ defstruct [path: "/",
+ controller: ?]
+
+ def match = fn (route, request) {
+ if Str.starts_with?(request.url, route.path) do
+ route.controller
+ end
+ }
+
+end
diff --git a/libkc3/data.c b/libkc3/data.c
index 1222776..d0dcaf1 100644
--- a/libkc3/data.c
+++ b/libkc3/data.c
@@ -477,6 +477,8 @@ bool data_hash_update (const s_sym *type, t_hash *hash, const void *data)
return hash_update_sw(hash, data);
if (type == &g_sym_Sym)
return hash_update_sym(hash, data);
+ if (type == &g_sym_Tag)
+ return hash_update_tag(hash, data);
if (type == &g_sym_Tuple)
return hash_update_tuple(hash, data);
if (type == &g_sym_U8)
diff --git a/libkc3/hash.c b/libkc3/hash.c
index cd51cec..65e3087 100644
--- a/libkc3/hash.c
+++ b/libkc3/hash.c
@@ -409,7 +409,9 @@ bool hash_update_struct (t_hash *hash, const s_struct *s)
while (i < s->type->map.count) {
if (! hash_update_tag(hash, s->type->map.key + i))
return false;
- if (! tag_type(s->type->map.value + i, &sym))
+ if (s->type->map.value[i].type == TAG_VAR)
+ sym = s->type->map.value[i].data.var.type;
+ else if (! tag_type(s->type->map.value + i, &sym))
return false;
if (s->data) {
data = (s8 *) s->data + s->type->offset[i];
diff --git a/test/httpd/config/mime.types b/test/httpd/config/mime.types
new file mode 100644
index 0000000..c6cc75a
--- /dev/null
+++ b/test/httpd/config/mime.types
@@ -0,0 +1,102 @@
+
+types {
+ text/html html htm shtml;
+ text/css css;
+ text/xml xml;
+ image/gif gif;
+ image/jpeg jpeg jpg;
+ application/javascript js;
+ application/atom+xml atom;
+ application/rss+xml rss;
+
+ text/mathml mml;
+ text/plain txt;
+ text/vnd.sun.j2me.app-descriptor jad;
+ text/vnd.wap.wml wml;
+ text/x-component htc;
+
+ image/avif avif;
+ image/png png;
+ image/svg+xml svg svgz;
+ image/tiff tif tiff;
+ image/vnd.wap.wbmp wbmp;
+ image/webp webp;
+ image/x-icon ico;
+ image/x-jng jng;
+ image/x-ms-bmp bmp;
+
+ font/woff woff;
+ font/woff2 woff2;
+
+ application/java-archive jar war ear;
+ application/json json;
+ application/mac-binhex40 hqx;
+ application/msword doc;
+ application/pdf pdf;
+ application/postscript ps eps ai;
+ application/rtf rtf;
+ application/vnd.apple.mpegurl m3u8;
+ application/vnd.google-earth.kml+xml kml;
+ application/vnd.google-earth.kmz kmz;
+ application/vnd.ms-excel xls;
+ application/vnd.ms-fontobject eot;
+ application/vnd.ms-powerpoint ppt;
+ application/vnd.oasis.opendocument.graphics odg;
+ application/vnd.oasis.opendocument.presentation odp;
+ application/vnd.oasis.opendocument.spreadsheet ods;
+ application/vnd.oasis.opendocument.text odt;
+ application/vnd.openxmlformats-officedocument.presentationml.presentation
+ pptx;
+ application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
+ xlsx;
+ application/vnd.openxmlformats-officedocument.wordprocessingml.document
+ docx;
+ application/vnd.wap.wmlc wmlc;
+ application/wasm wasm;
+ application/x-7z-compressed 7z;
+ application/x-cocoa cco;
+ application/x-java-archive-diff jardiff;
+ application/x-java-jnlp-file jnlp;
+ application/x-makeself run;
+ application/x-ns-proxy-autoconfig pac;
+ application/x-perl pl pm;
+ application/x-pilot prc pdb;
+ application/x-rar-compressed rar;
+ application/x-redhat-package-manager rpm;
+ application/x-sea sea;
+ application/x-shockwave-flash swf;
+ application/x-stuffit sit;
+ application/x-tcl tcl tk;
+ application/x-x509-ca-cert der pem crt;
+ application/x-xpinstall xpi;
+ application/xhtml+xml xhtml;
+ application/xspf+xml xspf;
+ application/zip zip;
+
+ application/octet-stream bin exe dll;
+ application/octet-stream deb;
+ application/octet-stream dmg;
+ application/octet-stream iso img;
+ application/octet-stream msi msp msm;
+
+ audio/basic au snd;
+ audio/midi mid midi kar;
+ audio/mpeg mp3;
+ audio/ogg ogg;
+ audio/x-m4a m4a;
+ audio/x-realaudio ra;
+
+ video/3gpp 3gpp 3gp;
+ video/mp2t ts;
+ video/mp4 mp4;
+ video/mpeg mpeg mpg;
+ video/quicktime mov;
+ video/webm webm;
+ video/x-flv flv;
+ video/x-m4v m4v;
+ video/x-matroska mkv;
+ video/x-mng mng;
+ video/x-ms-asf asx asf;
+ video/x-ms-wmv wmv;
+ video/x-msvideo avi;
+}
diff --git a/test/httpd/mime.types b/test/httpd/mime.types
deleted file mode 100644
index c6cc75a..0000000
--- a/test/httpd/mime.types
+++ /dev/null
@@ -1,102 +0,0 @@
-
-types {
- text/html html htm shtml;
- text/css css;
- text/xml xml;
- image/gif gif;
- image/jpeg jpeg jpg;
- application/javascript js;
- application/atom+xml atom;
- application/rss+xml rss;
-
- text/mathml mml;
- text/plain txt;
- text/vnd.sun.j2me.app-descriptor jad;
- text/vnd.wap.wml wml;
- text/x-component htc;
-
- image/avif avif;
- image/png png;
- image/svg+xml svg svgz;
- image/tiff tif tiff;
- image/vnd.wap.wbmp wbmp;
- image/webp webp;
- image/x-icon ico;
- image/x-jng jng;
- image/x-ms-bmp bmp;
-
- font/woff woff;
- font/woff2 woff2;
-
- application/java-archive jar war ear;
- application/json json;
- application/mac-binhex40 hqx;
- application/msword doc;
- application/pdf pdf;
- application/postscript ps eps ai;
- application/rtf rtf;
- application/vnd.apple.mpegurl m3u8;
- application/vnd.google-earth.kml+xml kml;
- application/vnd.google-earth.kmz kmz;
- application/vnd.ms-excel xls;
- application/vnd.ms-fontobject eot;
- application/vnd.ms-powerpoint ppt;
- application/vnd.oasis.opendocument.graphics odg;
- application/vnd.oasis.opendocument.presentation odp;
- application/vnd.oasis.opendocument.spreadsheet ods;
- application/vnd.oasis.opendocument.text odt;
- application/vnd.openxmlformats-officedocument.presentationml.presentation
- pptx;
- application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
- xlsx;
- application/vnd.openxmlformats-officedocument.wordprocessingml.document
- docx;
- application/vnd.wap.wmlc wmlc;
- application/wasm wasm;
- application/x-7z-compressed 7z;
- application/x-cocoa cco;
- application/x-java-archive-diff jardiff;
- application/x-java-jnlp-file jnlp;
- application/x-makeself run;
- application/x-ns-proxy-autoconfig pac;
- application/x-perl pl pm;
- application/x-pilot prc pdb;
- application/x-rar-compressed rar;
- application/x-redhat-package-manager rpm;
- application/x-sea sea;
- application/x-shockwave-flash swf;
- application/x-stuffit sit;
- application/x-tcl tcl tk;
- application/x-x509-ca-cert der pem crt;
- application/x-xpinstall xpi;
- application/xhtml+xml xhtml;
- application/xspf+xml xspf;
- application/zip zip;
-
- application/octet-stream bin exe dll;
- application/octet-stream deb;
- application/octet-stream dmg;
- application/octet-stream iso img;
- application/octet-stream msi msp msm;
-
- audio/basic au snd;
- audio/midi mid midi kar;
- audio/mpeg mp3;
- audio/ogg ogg;
- audio/x-m4a m4a;
- audio/x-realaudio ra;
-
- video/3gpp 3gpp 3gp;
- video/mp2t ts;
- video/mp4 mp4;
- video/mpeg mpeg mpg;
- video/quicktime mov;
- video/webm webm;
- video/x-flv flv;
- video/x-m4v m4v;
- video/x-matroska mkv;
- video/x-mng mng;
- video/x-ms-asf asx asf;
- video/x-ms-wmv wmv;
- video/x-msvideo avi;
-}
diff --git a/test/httpd/public/release b/test/httpd/public/release
deleted file mode 120000
index 2577df2..0000000
--- a/test/httpd/public/release
+++ /dev/null
@@ -1 +0,0 @@
-../../../release
\ No newline at end of file
diff --git a/test/httpd/public/test.html b/test/httpd/public/test.html
deleted file mode 100644
index e2f733c..0000000
--- a/test/httpd/public/test.html
+++ /dev/null
@@ -1,13 +0,0 @@
-<!doctype html>
-<html>
- <head>
- <title>HTML test</title>
- </head>
- <body>
- <h1>HTML test</h1>
-
- <p>
- Test <span style="color: #0f0;"><b>OK</b></span> !
- </p>
- </body>
-</html>
diff --git a/test/httpd/public/test.txt b/test/httpd/public/test.txt
deleted file mode 100644
index 85cad51..0000000
--- a/test/httpd/public/test.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Test
-
-OK !
diff --git a/test/httpd/static/release b/test/httpd/static/release
new file mode 120000
index 0000000..2577df2
--- /dev/null
+++ b/test/httpd/static/release
@@ -0,0 +1 @@
+../../../release
\ No newline at end of file
diff --git a/test/httpd/static/test.txt b/test/httpd/static/test.txt
new file mode 100644
index 0000000..85cad51
--- /dev/null
+++ b/test/httpd/static/test.txt
@@ -0,0 +1,3 @@
+Test
+
+OK !