[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
- [elpa] 10/119: some simple examples, (continued)
- [elpa] 10/119: some simple examples, Eric Schulte, 2014/03/10
- [elpa] 15/119: more lenient parsing of multipart forms, Eric Schulte, 2014/03/10
- [elpa] 12/119: compiling to .elc, Eric Schulte, 2014/03/10
- [elpa] 13/119: tasks, Eric Schulte, 2014/03/10
- [elpa] 18/119: 404 not found helper, Eric Schulte, 2014/03/10
- [elpa] 17/119: handling errors, Eric Schulte, 2014/03/10
- [elpa] 14/119: better requirements for test, Eric Schulte, 2014/03/10
- [elpa] 16/119: tasks, Eric Schulte, 2014/03/10
- [elpa] 20/119: don't require non-nil return to close connection, Eric Schulte, 2014/03/10
- [elpa] 19/119: simply (name . content) for form elements, Eric Schulte, 2014/03/10
- [elpa] 21/119: helper to send a file with mime type,
Eric Schulte <=
- [elpa] 22/119: file serving example, Eric Schulte, 2014/03/10
- [elpa] 23/119: handle close-connection thrown during header parse, Eric Schulte, 2014/03/10
- [elpa] 25/119: seems to be generally usable, added a README, Eric Schulte, 2014/03/10
- [elpa] 24/119: parse application/x-www-form-urlencoded post data, Eric Schulte, 2014/03/10
- [elpa] 26/119: thread proc through some functions, Eric Schulte, 2014/03/10
- [elpa] 28/119: testing a hello-world server, Eric Schulte, 2014/03/10
- [elpa] 27/119: run test processes asynch, Eric Schulte, 2014/03/10
- [elpa] 29/119: update examples, Eric Schulte, 2014/03/10
- [elpa] 31/119: match more HTTP methods, Eric Schulte, 2014/03/10
- [elpa] 30/119: fleshed out some more tests, Eric Schulte, 2014/03/10