forked from hayamiz/twittering-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwittering-urllib.el
199 lines (182 loc) · 7.38 KB
/
twittering-urllib.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
;;; twittering-urllib.el --- Functions for using urllib with twittering-mode
;; Copyright (C) 2009-2015 Tadashi MATSUO
;; 2007, 2009-2011 Yuto Hayamizu.
;; 2008 Tsuyoshi CHO
;; 2014, 2015 Xavier Maillard
;; Author: Tadashi MATSUO <[email protected]>
;; Y. Hayamizu <[email protected]>
;; Tsuyoshi CHO <[email protected]>
;; Alberto Garcia <[email protected]>
;; Xavier Maillard <[email protected]>
;; Created: Sep 4, 2007
;; Version: HEAD
;; Identity: $Id: a2fc6eb695ad0994e986ab0413e53f335d9a947b $
;; Keywords: twitter web
;; URL: http://twmode.sf.net/
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Functions for using urllib with twittering-mode
;;; Code:
(require 'twittering-http)
(defcustom twittering-url-show-status nil
"*If non-nil, show a running total of bytes transferred by urllib.
This has effect only if either \"urllib-httpp\" or \"urllib-https\" is used
as the connection method."
:group 'twittering-mode
:type 'boolean)
(defun twittering-start-http-session-urllib-p ()
"Return t if url library is available, otherwise nil."
(require 'url nil t))
(defun twittering-start-http-session-urllib-https-p ()
"Return t if url library can be used for HTTPS, otherwise nil."
(and (not twittering-proxy-use)
(require 'url nil t)
(cond
((<= 22 emacs-major-version)
;; On Emacs22 and later, `url' requires `tls'.
(twittering-start-http-session-native-tls-p))
((require 'ssl nil t)
;; On Emacs21, `url' requires `ssl'.
t)
((or (and (fboundp 'open-ssl-stream)
;; Since `url-gw' (required by `url') defines autoload of
;; `open-ssl-stream' from "ssl",
;; (fboundp 'open-ssl-stream) will be non-nil even if
;; "ssl" cannot be loaded and `open-ssl-stream' is
;; unavailable.
;; Here, the availability is confirmed by `documentation'.
(documentation 'open-ssl-stream))
;; On Emacs21, `url' requires `ssl' in order to use
;; `open-ssl-stream', which is included in `ssl.el'.
;; Even if `ssl' cannot be loaded, `open-tls-stream' can be
;; used as an alternative of the function.
(and (twittering-start-http-session-native-tls-p)
(defalias 'open-ssl-stream 'open-tls-stream)))
(provide 'ssl)
t)
(t
nil))))
(defun twittering-send-http-request-urllib (name buffer connection-info sentinel)
(let* ((request (cdr (assq 'request connection-info)))
(method (cdr (assq 'method request)))
(scheme (cdr (assq 'scheme request)))
(uri (cdr (assq 'uri request)))
(header-list (cdr (assq 'header-list request)))
(post-body (cdr (assq 'post-body request)))
(use-proxy (cdr (assq 'use-proxy connection-info)))
(proxy-server (cdr (assq 'proxy-server connection-info)))
(proxy-port (cdr (assq 'proxy-port connection-info)))
(proxy-user (cdr (assq 'proxy-user connection-info)))
(proxy-password (cdr (assq 'proxy-password connection-info)))
(proxy-credentials
(when (and proxy-user proxy-password)
(concat "Basic "
(base64-encode-string
(concat proxy-user ":" proxy-password)))))
(coding-system-for-read 'binary)
(coding-system-for-write 'binary)
(url-proxy-services
(when use-proxy
`((,scheme . ,(format "%s:%s" proxy-server proxy-port)))))
(url-request-method method)
(url-request-extra-headers
;; Remove some headers that should be configured by url library.
;; They may break redirections by url library because
;; `url-request-extra-headers' overwrites the new headers
;; that are adapted to redirected connection.
(apply 'append
(mapcar (lambda (pair)
(if (member (car pair)
'("Host" "Content-Length"))
nil
`(,pair)))
(if proxy-credentials
(cons
`("Proxy-Authorization" ,proxy-credentials)
header-list)
header-list))))
(url-request-data post-body)
(url-show-status twittering-url-show-status)
(url-http-attempt-keepalives nil)
(tls-program twittering-tls-program)
(coding-system-for-read 'binary)
(coding-system-for-write 'binary))
(lexical-let ((sentinel sentinel)
(buffer buffer))
(let ((result-buffer
(url-retrieve
uri
(lambda (&rest args)
(let ((proc url-http-process)
(url-buffer (current-buffer))
(status-str
(if (and (< emacs-major-version 22)
(boundp 'url-http-end-of-headers)
url-http-end-of-headers)
"urllib-finished"
"finished")))
;; Callback may be called multiple times.
;; (as filter and sentinel?)
(unless (local-variable-if-set-p 'twittering-retrieved)
(set (make-local-variable 'twittering-retrieved)
'not-completed)
(with-current-buffer buffer
(set-buffer-multibyte nil)
(insert-buffer-substring url-buffer))
(set-process-buffer proc buffer)
(unwind-protect
(apply sentinel proc status-str nil)
(set-process-buffer proc url-buffer)
(if (eq twittering-retrieved 'exited)
(url-mark-buffer-as-dead url-buffer)
(setq twittering-retrieved 'completed))))
(when (memq (process-status proc)
'(nil closed exit failed signal))
;; Mark `url-buffer' as dead when the process exited
;; and `sentinel' is completed.
;; If this `lambda' is evaluated via a filter, the
;; process may exit before it is finished to evaluate
;; `(apply sentinel ...)'. In the case, `buffer' should
;; not be killed. It should be killed after the
;; evaluation of `sentinel'.
(if (eq twittering-retrieved 'completed)
(url-mark-buffer-as-dead url-buffer)
(setq twittering-retrieved 'exited))))))))
(when (buffer-live-p result-buffer)
(with-current-buffer result-buffer
(set (make-local-variable 'url-show-status)
twittering-url-show-status)
;; Make `url-http-attempt-keepalives' buffer-local
;; in order to send the current value of the variable
;; to the sentinel invoked for HTTP redirection,
(make-local-variable 'url-http-attempt-keepalives))
(get-buffer-process result-buffer))))))
(defun twittering-pre-process-buffer-urllib (proc buffer connection-info)
(with-current-buffer buffer
(save-excursion
(goto-char (point-max))
(cond
((search-backward-regexp
"- Peer has closed the GNUTLS connection\r?\n\\'"
nil t)
(let ((beg (match-beginning 0))
(end (match-end 0)))
(delete-region beg end)))
((search-backward-regexp "closed\r?\n\\'" nil t)
(let ((beg (match-beginning 0))
(end (match-end 0)))
(delete-region beg end)))
(t nil)))))
(provide 'twittering-urllib)
;;; twittering-urllib.el ends here