diff --git a/lib/chibi/mime-test.sld b/lib/chibi/mime-test.sld index 8091f769..87ae6d2e 100644 --- a/lib/chibi/mime-test.sld +++ b/lib/chibi/mime-test.sld @@ -144,5 +144,10 @@ Content-Transfer-Encoding: binary --BbC04y-- --AaB03x-- ")))) - + (test "text/html; charset=utf-8" + (mime-type-from-extension "html")) + (test "text/plain; charset=utf-8" + (mime-type-from-extension "scm")) + (test #f + (mime-type-from-extension "foo")) (test-end)))) diff --git a/lib/chibi/mime.scm b/lib/chibi/mime.scm index ec3a5858..a782aac4 100644 --- a/lib/chibi/mime.scm +++ b/lib/chibi/mime.scm @@ -21,6 +21,20 @@ (cond ((and (pair? ls) (pair? (car ls)) (assq key ls)) => cdr) (else (and (pair? o) (car o))))) +;; most of these are plain text for easier viewing in the browser +(define (mime-type-from-extension ext) + (assq-ref + '((htm . "text/html; charset=utf-8") + (html . "text/html; charset=utf-8") + (scm . "text/plain; charset=utf-8") + (sld . "text/plain; charset=utf-8") + (c . "text/plain; charset=utf-8") + (h . "text/plain; charset=utf-8") + (txt . "text/plain; charset=utf-8") + (org . "text/plain; charset=utf-8") + (md . "text/plain; charset=utf-8")) + (and (string? ext) (string->symbol ext)))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; simple matching instead of regexps diff --git a/lib/chibi/mime.sld b/lib/chibi/mime.sld index 0ac13681..42844bc4 100644 --- a/lib/chibi/mime.sld +++ b/lib/chibi/mime.sld @@ -2,7 +2,8 @@ (define-library (chibi mime) (export assq-ref mime-header-fold mime-headers->list mime-parse-content-type mime-decode-header - mime-message-fold mime-message->sxml mime-write-headers) + mime-message-fold mime-message->sxml mime-write-headers + mime-type-from-extension) (import (scheme base) (scheme char) (scheme write) (chibi base64) (chibi quoted-printable) (chibi string)) diff --git a/lib/chibi/net/http-server.scm b/lib/chibi/net/http-server.scm index 3cb2c110..7563f80e 100644 --- a/lib/chibi/net/http-server.scm +++ b/lib/chibi/net/http-server.scm @@ -129,8 +129,13 @@ (define (http-send-file request path) (cond ((file-exists? path) - (servlet-respond request 200 "OK") - (send-file path (request-out request))) + (let ((headers + (cond + ((mime-type-from-extension (path-extension path)) + => (lambda (type) `((Content-Type . ,type)))) + (else '())))) + (servlet-respond request 200 "OK" headers) + (send-file path (request-out request)))) (else (servlet-respond request 404 "Not Found"))))