Commit 1e2da975272bbc7b7a4b720a5a663ed730c6800e

Thomas de Grivel 2018-06-17T17:01:26

fix file content-type

diff --git a/mime.lisp b/mime.lisp
index 9400f0d..85da5ee 100644
--- a/mime.lisp
+++ b/mime.lisp
@@ -13,7 +13,7 @@
 (defmethod ext ((x string))
   (cond ((string= "" x))
         ((char= #\. (char x 0))
-         (let ((sym (intern x :file-extensions)))
+         (let ((sym (intern (string-upcase x) :file-extensions)))
            (export sym :file-extensions)
            sym))
         (t
@@ -42,21 +42,25 @@
       (cl:read stream nil eof))))
 
 (defun load-mime.types (path)
-  (let ((in (cl:open path)))
-    (unwind-protect
-         (let ((eof (gensym))
-               (mime-type nil))
-           (loop
-              (let ((sym (safe-read in eof)))
-                (cond ((eq eof sym) (return))
-                      ((not (symbolp sym)))
-                      ((mime-type-p sym) (setf mime-type sym))
-                      (mime-type (let ((ext (ext sym)))
-                                   (when (debug-p :mime)
-                                     (msg mime ext " " mime-type))
-                                   (setf (mime-type ext)
-                                         mime-type)))))))
-      (cl:close in))))
-
-#+openbsd
-(load-mime.types "/usr/share/misc/mime.types")
+  (when (probe-file path)
+    (msg info "loading mime types from " path)
+    (let ((in (cl:open path :if-does-not-exist nil)))
+      (unwind-protect
+           (let ((eof (gensym))
+                 (mime-type nil))
+             (loop
+                (let ((sym (safe-read in eof)))
+                  (cond ((eq eof sym) (return))
+                        ((not (symbolp sym)))
+                        ((mime-type-p sym) (setf mime-type sym))
+                        (mime-type (let ((ext (ext sym)))
+                                     (when (debug-p :mime)
+                                       (msg mime ext " " mime-type))
+                                     (setf (mime-type ext)
+                                           mime-type)))))))
+        (cl:close in)))))
+
+(defun configure-mime ()
+  (load-mime.types "/etc/mime.types")
+  #+openbsd
+  (load-mime.types "/usr/share/misc/mime.types"))
diff --git a/thot.lisp b/thot.lisp
index c85eb75..404e749 100644
--- a/thot.lisp
+++ b/thot.lisp
@@ -452,20 +452,23 @@ The requested url "
 (defun path-name (path)
   (declare (type simple-string path))
   (let ((start (position #\/ path :from-end t)))
-    (when start
-      (subseq path (1+ start)))))
+    (if start
+        (subseq path (1+ start))
+        path)))
 
 (defun path-extension (path)
   (declare (type simple-string path))
   (let* ((name (the simple-string (path-name path)))
          (start (position #\. name :from-end t)))
     (when start
-      (ext (subseq path start)))))
+      (ext (subseq name start)))))
 
 (defun file (path)
   (let* ((ext (path-extension path))
          (type (mime-type ext)))
-    (header "Content-Type: " type)
+    (when (debug-p :file)
+      (msg debug "path " path " ext " ext " type " type))
+    (header "Content-Type: " (string-downcase (symbol-name type)))
     (with-stream (in (unistd-stream-open path :read t))
       (let ((size (stream-file-size in)))
         (header "Content-Length: " size))
@@ -562,6 +565,7 @@ The requested url "
   (setq *stop* nil)
   (let ((*host* host)
         (*port* port))
+    (configure-mime)
     (socket:with-socket (fd socket:+af-inet+
                             socket:+sock-stream+
                             0)