Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MIME charset based on Emacs coding system #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions simple-httpd.el
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,26 @@ element is the fragment."
(httpd-serve-root proc httpd-root uri-path request)
(httpd-error proc 403)))

(defun httpd-get-mime (ext)
(defun httpd-get-mime (ext &optional buffer-name)
"Combine MIME type and charset into MIME value."
(let ((mime-type (httpd-get-mime-type ext))
(mime-charset (httpd-get-mime-charset buffer-name)))
(if mime-charset
(concat mime-type ";charset=" mime-charset)
mime-type)))

(defun httpd-get-mime-charset (&optional buffer-name)
"Let Emacs guess a reasonable MIME charset parameter for buffer."
(with-current-buffer (or buffer-name (current-buffer))
(let ((type (coding-system-type buffer-file-coding-system)))
(cond
((eq type 'utf-8) "utf-8")
((eq type 'charset)
(symbol-name (car (coding-system-charset-list buffer-file-coding-system))))
((eq type 'undecided) nil)
(t nil)))))

(defun httpd-get-mime-type (ext)
"Fetch MIME type given the file extention."
(or (and ext (cdr (assoc (downcase ext) httpd-mime-types)))
"application/octet-stream"))
Expand Down Expand Up @@ -797,7 +816,7 @@ the `httpd-current-proc' as the process."
(httpd-send-header proc "text/plain" 304))
(httpd-log `(file ,path))
(with-temp-buffer
(set-buffer-multibyte nil)
(set-buffer-multibyte t) ;; Required to be able to guess MIME charset
(insert-file-contents path)
(httpd-send-header proc (httpd-get-mime (file-name-extension path))
200 :Last-Modified mtime :ETag etag)))))
Expand Down