Commit 4db028327a33106f1d5ebec10446d5bbcb92bd49

Thomas de Grivel 2024-09-05T17:40:41

wip httpd routes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
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 !