Commit 7ae1547928bd9b4ed94b9b647667cd60ba95f038

Baptiste 2024-08-07T17:04:10

httpd: send headers with mime type based on extension of request url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index 533010e..7cf0840 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -101,10 +101,14 @@ defmodule HTTPd do
   }
 
   def show_page = fn (request) {
+    ext = File.ext(request.url)
     path = root_dir + request.url
     if File.exists?(path) do
       body = File.read(path)
-      %HTTP.Response{body: body}
+      mime = HTTP.mime_type(ext)
+      puts("HTTPd.show_page: mime = #{mime}")
+      headers = [{"Content-Type", (Str) mime}]
+      %HTTP.Response{body: body, headers: headers}
     else
       error_404_page(request)
     end