/
libcurl.el
124 lines (110 loc) · 4.17 KB
/
libcurl.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
;; libcurl.el - Provides a library that makes it easy to interact with "curl"
;; Author: Christopher Miles <twitch@nervestaple.com>
;; This file is not a part of GNU Emacs
(defgroup libcurl '()
"A small package that makes it easy to interact with the
utility 'curl'. curl is a command line tool for transferring
files with URL syntax. It supports FTP, FTPS, HTTP, HTTPS and
more.")
;; I wrote this package because I was having problems getting the
;; built-in URL support that comes with Emacs to work through the
;; SOCKS4 proxy that I use at work. This is my first stab at elisp
;; code, there's most likely many bits that could be improved.
;; Installation: Put this file somewhere in your load-path and then
;; add the following to your emacs initialization file:
;; (require 'libcurl)
;; There aren't any end-user facing functions in this package, it is
;; meant to be used by developers who need to interact with curl.
(defun libcurl-parse-headers (headers)
"Returns a formatted list of headers that can be passed to curl."
(let ((headers-out nil)
(headers-in headers)
(index 0))
(while headers-in
(setq headers-out
(append headers-out
(list "--header" (car headers-in))))
(setq headers-in (cdr headers-in))
(setq index (1+ index)))
headers-out))
(defun libcurl-parse-data (data-in)
"Parses a property list of data and returns a list that can be
handed to curl when posting data to a URL."
(let ((libcurl-data data-in)
(libcurl-post-data nil)
(index 0))
(while (<= index (length libcurl-data))
(let ((item (car libcurl-data)))
(setq libcurl-post-data
(concat libcurl-post-data
(if (not (null libcurl-post-data))
"&")
(url-hexify-string (car item))
"="
(url-hexify-string (car (cdr item))))))
(setq libcurl-data (cdr libcurl-data))
(setq index (1+ index)))
libcurl-post-data))
(defun libcurl (args url)
"Invokes curl with the given arguments and the provided url. A
new buffer with the result will be returned. Note that all calls
to curl are made with the '--silent' and '--show-error' flagsp,
this is so the output is predictable."
(let ((buffer-temp (generate-new-buffer "libcurl-result")))
(save-excursion
(apply 'call-process
(append
(list "curl" nil buffer-temp nil)
(append '("--silent" "--show-error") args)
(list url))))
buffer-temp))
(defun libcurl-test (args url)
"Invokes curl with the given arguments and the provided
url. This is meant to be used to test that you are getting what
you expect from curl."
(let ((buffer-temp (generate-new-buffer "libcurl-result")))
(save-excursion
(apply 'call-process
(append
(list "curl" nil buffer-temp nil)
(append '("--verbose") args)
(list url))))
buffer-temp))
(defun libcurl-errorp (buffer-in)
"Checks a buffer of curl output and returns true if the buffer
contains a curl error message."
(if (bufferp buffer-in)
(save-excursion
(set-buffer buffer-in)
(goto-char (point-min))
(skip-chars-forward "\t\r\n\f\b ")
(let ((start-pos (point)) word)
(forward-word)
(forward-char)
(setq word (buffer-substring-no-properties start-pos (point)))
(if (string= "curl:" word) t nil)))
(signal 'wrong-type-argument '("The argument provided is not a buffer"))))
(defun libcurl-error-code (buffer-in)
"Returns the error code in the buffer of curl result data."
(if (bufferp buffer-in)
(save-excursion
(set-buffer buffer-in)
(goto-char (point-min))
(goto-char (search-forward "curl: ("))
(let ((start-pos (point)))
(forward-word)
(buffer-substring-no-properties start-pos (point))))
(signal 'wrong-type-argument '("The argument provided is not a buffer"))))
(defun libcurl-error-code-description (buffer-in)
"Returns the descriptive description of the error code in the
buffer of curl result data"
(if (bufferp buffer-in)
(save-excursion
(set-buffer buffer-in)
(goto-char (point-min))
(goto-char (re-search-forward "curl: ([0-9]*) "))
(let ((start-pos (point)))
(end-of-line)
(buffer-substring-no-properties start-pos (point))))
(signal 'wrong-type-argument '("The argument provided is not a buffer"))))
(provide 'libcurl)