-
Notifications
You must be signed in to change notification settings - Fork 13
/
org-cliplink-transport.el
133 lines (113 loc) · 5.61 KB
/
org-cliplink-transport.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
125
126
127
128
129
130
131
132
133
;;; org-cliplink-transport.el --- insert org-mode links from the clipboard -*- lexical-binding: t -*-
;; Copyright (C) 2014 Alexey Kutepov a.k.a rexim
;; Author: Alexey Kutepov <reximkut@gmail.com>
;; Maintainer: Alexey Kutepov <reximkut@gmail.com>
;; URL: http://github.com/rexim/org-cliplink
;; Version: 0.2
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use, copy,
;; modify, merge, publish, distribute, sublicense, and/or sell copies
;; of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
(require 'url-parse)
(require 'org-cliplink-string)
(defvar org-cliplink-block-authorization nil
"Flag whether to block url.el's usual interactive authorisation procedure")
(defadvice url-http-handle-authentication (around org-cliplink-fix)
(unless org-cliplink-block-authorization
ad-do-it))
(ad-activate 'url-http-handle-authentication)
(defun org-cliplink-credentials-to-basic-auth (username password)
(concat "Basic " (base64-encode-string
(concat username ":" password))))
(defun org-cliplink-shadow-basic-auth-credentials (basic-auth-credentials)
(when basic-auth-credentials
(list :username "***"
:password "***")))
(defun org-cliplink-curl-prepare-response-buffer-name (url)
(format " *curl-%s-%x*"
(url-host (url-generic-parse-url url))
(random)))
(defun org-cliplink-build-curl-arguments (url basic-auth-credentials extra-curl-arguments)
(append extra-curl-arguments
(list "--include"
"--silent"
"--show-error"
"-X"
"GET")
(when basic-auth-credentials
(let ((username (plist-get basic-auth-credentials :username))
(password (plist-get basic-auth-credentials :password)))
(list "--user"
(format "%s:%s" username password))))
(list url)))
(defun org-cliplink-build-curl-sentinel (response-buffer-name callback)
(lambda (process event)
(ignore event)
(when (not (process-live-p process))
(if (zerop (process-exit-status process))
(when callback
(with-current-buffer response-buffer-name
(funcall callback nil)))
(with-current-buffer response-buffer-name
(error (buffer-string)))))))
(defun org-cliplink-start-curl-process (response-buffer-name curl-arguments)
(let ((curl-executable (executable-find "curl")))
(apply #'start-process
"curl"
response-buffer-name
curl-executable
curl-arguments)))
(defun org-cliplink-log-curl-command (url basic-auth-credentials extra-curl-arguments)
(message "curl %s"
(org-cliplink-join-string
(org-cliplink-build-curl-arguments
url
(org-cliplink-shadow-basic-auth-credentials basic-auth-credentials)
extra-curl-arguments))))
(defun org-cliplink-http-get-request--curl (url callback &optional basic-auth-credentials extra-curl-arguments)
(let* ((response-buffer-name (org-cliplink-curl-prepare-response-buffer-name url))
(curl-arguments (org-cliplink-build-curl-arguments url
basic-auth-credentials
extra-curl-arguments)))
(org-cliplink-log-curl-command url basic-auth-credentials
extra-curl-arguments)
(set-process-sentinel
(org-cliplink-start-curl-process response-buffer-name
curl-arguments)
(org-cliplink-build-curl-sentinel response-buffer-name
callback))))
(defun org-cliplink-http-get-request--url-el (url callback &optional basic-auth-credentials)
(let* (;; Sometimes url-retrieve invokes the callback multiple
;; times. Looks like it is a bug in url.el. For more
;; information see
;; https://github.com/rexim/org-cliplink/issues/34
(block-title-callback-invocation nil)
(url-retrieve-callback
(lambda (status)
(when (not block-title-callback-invocation)
(setq block-title-callback-invocation t)
(funcall callback status)))))
(if basic-auth-credentials
(let* ((org-cliplink-block-authorization t)
(username (plist-get basic-auth-credentials :username))
(password (plist-get basic-auth-credentials :password))
(url-request-extra-headers
`(("Authorization" . ,(org-cliplink-credentials-to-basic-auth
username password)))))
(url-retrieve url url-retrieve-callback))
(url-retrieve url url-retrieve-callback nil nil t))))
(provide 'org-cliplink-transport)
;;; org-cliplink-transport.el ends here