emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] 21/119: helper to send a file with mime type


From: Eric Schulte
Subject: [elpa] 21/119: helper to send a file with mime type
Date: Mon, 10 Mar 2014 16:57:05 +0000

eschulte pushed a commit to branch master
in repository elpa.

commit 11c6bb1d757666444ee56948ae9d56f9115624f1
Author: Eric Schulte <address@hidden>
Date:   Wed Dec 18 23:16:01 2013 -0700

    helper to send a file with mime type
---
 NOTES               |    6 ++----
 emacs-web-server.el |   16 +++++++++++++++-
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/NOTES b/NOTES
index 7e5dbcd..2024910 100644
--- a/NOTES
+++ b/NOTES
@@ -22,14 +22,12 @@ parse more than just the content-type headers.
 ** TODO non-multipart form data
 e.g., parameter strings
 
-** TODO some more convenience functionality [3/6]
+** TODO some more convenience functionality [4/5]
 - [ ] strip and parse URL query string
 - [X] think about defaulting to (name . content) for form elements
 - [X] maybe don't require a non-nil return to cancel the connection,
       instead only keep open if :keep-open is returned
-  [ ] rather only keep on an explicit :keep-alive
-- [ ] function to send a file (with mime handling)
-- [ ] function to send a string (with header handling)
+- [X] function to send a file (with mime handling)
 - [X] send a 404 with some default text
 
 ** TODO Lazy header processing
diff --git a/emacs-web-server.el b/emacs-web-server.el
index 5af2ab2..b085723 100644
--- a/emacs-web-server.el
+++ b/emacs-web-server.el
@@ -8,7 +8,8 @@
 
 ;;; Code:
 (require 'emacs-web-server-status-codes)
-(require 'mail-parse)
+(require 'mail-parse)             ; to parse multipart data in headers
+(require 'mm-encode)              ; to look-up mime types for files
 (require 'eieio)
 (eval-when-compile (require 'cl))
 (require 'cl-lib)
@@ -241,5 +242,18 @@ Currently CODE should be an HTTP status code, see
                               "404 Not Found"))
   (throw 'close-connection nil))
 
+(defun ews-send-file (proc path &optional mime-type)
+  "Send PATH to PROC.
+Optionally explicitly set MIME-TYPE, otherwise it is guessed by
+`mm-default-file-encoding'."
+  (let ((mime (or mime-type
+                  (mm-default-file-encoding path)
+                  "application/octet-stream")))
+    (ews-response-header proc 200 (cons "Content-type" mime))
+    (process-send-string proc
+      (with-temp-buffer
+        (insert-file-contents-literally path)
+        (buffer-string)))))
+
 (provide 'emacs-web-server)
 ;;; emacs-web-server.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]