Commit 9d52aadcab6e57e4a91e7831dd292246a56d1b30

Thomas de Grivel 2024-09-16T03:35:45

httpd: 405 Method Not Allowed

diff --git a/lib/kc3/0.1/http/response.kc3 b/lib/kc3/0.1/http/response.kc3
index a46a557..6e4c13f 100644
--- a/lib/kc3/0.1/http/response.kc3
+++ b/lib/kc3/0.1/http/response.kc3
@@ -10,6 +10,7 @@ defmodule HTTP.Response do
 
   def default_messages = [{(U16) 200, "OK"},
                           {(U16) 404, "Not Found"},
+                          {(U16) 405, "Method Not Allowed"},
                           {(U16) 500, "Internal Server Error"}]
 
   # buf_parse(buf_w, parse_body) => response
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index ef4fb0e..5c795b7 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -41,8 +41,12 @@ defmodule HTTPd do
       req = void
       req = HTTP.Request.buf_parse(client.buf_rw.r)
       if req do
-        router = route_request(req)
-        response = router(req)
+        if (type(req.method) == Str) do
+          response = error_405_page(req)
+        else
+          router = route_request(req)
+          response = router(req)
+        end
         response = HTTP.Response.set_header(response,
           "Connection", "Keep-Alive")
         response = HTTP.Response.set_header(response,
@@ -162,6 +166,22 @@ defmodule HTTPd do
     %HTTP.Response{code: 404, body: body}
   }
 
+  def error_405_page = fn (request) {
+    body = "<html>
+  <head>
+    <title>405 Method Not Allowed</title>
+  </head>
+  <body>
+    <h1>405 Method Not Allowed</h1>
+    <p>
+      The requested method #{HTML.escape(inspect(request.method))} is not accepted on this server.
+    </p>
+  </body>
+</html>
+"
+    %HTTP.Response{code: 405, body: body}
+  }
+
   def directory_page = fn (request) {
     files = List.sort(File.list(root_dir + request.url))
     file_li = HTTPd.fn (file) {