emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] scratch/with-url 42354bb 1/4: Implement ftp: and file:


From: Lars Ingebrigtsen
Subject: [Emacs-diffs] scratch/with-url 42354bb 1/4: Implement ftp: and file:
Date: Sat, 21 Jan 2017 20:01:59 +0000 (UTC)

branch: scratch/with-url
commit 42354bb6012d61c359207faa795885191f5088f2
Author: Lars Ingebrigtsen <address@hidden>
Commit: Lars Ingebrigtsen <address@hidden>

    Implement ftp: and file:
---
 lisp/url/with-url.el |   39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/lisp/url/with-url.el b/lisp/url/with-url.el
index da28ee6..17b1a16 100644
--- a/lisp/url/with-url.el
+++ b/lisp/url/with-url.el
@@ -201,6 +201,12 @@ If given, return the value in BUFFER instead."
     (setf (url-request-url req) (url-request-original-url req)))
   (setf (url-request-parsed-url req)
         (url-generic-parse-url (url-request-url req)))
+  (pcase (url-type (url-request-parsed-url req))
+    ((or "http" "https") (with-url--fetch-http req))
+    ("ftp" (with-url--fetch-ftp req))
+    ("file" (with-url--fetch-file req))))
+
+(defun with-url--fetch-http (req)
   (when (or (url-request-timeout req)
             (url-request-read-timeout req))
     (setf (url-request-timer req)
@@ -232,6 +238,39 @@ If given, return the value in BUFFER instead."
              :filter #'with-url--filter)))
       (setf (url-request-process req) process))))
 
+(defun with-url--fetch-ftp (req)
+  (let ((parsed (url-request-parsed-url req)))
+    ;; Transform the URL into Tramp syntax and let it worry about it.
+    (with-url--fetch-file
+     (concat "/"
+             (and (url-user parsed)
+                  (format "%s@" (url-user parsed)))
+             (url-host)
+             (and (url-port parsed)
+                  (format "#s" (url-port parsed)))
+             ":"
+             (url-filename parsed)))))
+
+(defun with-url--fetch-file (req)
+  (with-current-buffer (generate-new-buffer "*request*")
+    (set-buffer-multibyte nil)
+    (let ((coding-system-for-read 'binary)
+          (coding-system-for-write 'binary)
+          (buffer (current-buffer)))
+      (condition-case err
+          (insert-file-contents-literally
+           (url-filename (url-request-parsed-url req)))
+        (error
+         (setq-local with-url--status
+                     (list 500 (format "Error occurred while fetching file: %s"
+                                       err)))))
+      (when (or (not (url-request-ignore-errors req))
+                (url-okp))
+        (goto-char (point-min))
+        (unwind-protect
+            (funcall (url-request-callback req))
+          (kill-buffer buffer))))))
+
 (defun with-url--timer (req)
   (let ((now (float-time)))
     ;; There are two possible timeouts: One for the overall time of



reply via email to

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