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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# Copyright (C) 2012 Aleksey Lim
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
# pylint: disable-msg=E1103
import os
import cgi
import json
import time
import shutil
import logging
import hashlib
import tempfile
from os.path import isdir, exists, dirname, join
import requests
from requests.sessions import Session
from M2Crypto import DSA
import active_document as ad
from sugar_network.zerosugar import Bundle
from active_toolkit.sockets import decode_multipart, BUFFER_SIZE
from sugar_network.toolkit import sugar
from sugar_network import local
from active_toolkit import coroutine, enforce
# Let toolkit.http work in concurrence
from gevent import monkey
# XXX No DNS because `toolkit.network.res_init()` doesn't work otherwise
monkey.patch_socket(dns=False)
monkey.patch_select()
monkey.patch_ssl()
monkey.patch_time()
_RECONNECTION_NUMBER = 1
_RECONNECTION_TIMEOUT = 3
_logger = logging.getLogger('http')
class Client(object):
def __init__(self, api_url='', sugar_auth=False, **kwargs):
self.api_url = api_url
self.params = kwargs
self._sugar_auth = sugar_auth
verify = True
if local.no_check_certificate.value:
verify = False
elif local.certfile.value:
verify = local.certfile.value
headers = {'Accept-Language': ad.default_lang()}
if self._sugar_auth:
uid = sugar.uid()
headers['sugar_user'] = uid
headers['sugar_user_signature'] = _sign(uid)
self._session = Session(headers=headers, verify=verify, prefetch=False)
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
def close(self):
self._session.close()
def exists(self, path):
response = self.request('GET', path, allowed=[404], params=self.params)
return response.status_code != 404
def get(self, path_=None, **kwargs):
kwargs.update(self.params)
response = self.request('GET', path_, params=kwargs)
return self._decode_response(response)
def post(self, path_=None, data_=None, **kwargs):
kwargs.update(self.params)
response = self.request('POST', path_, data_,
headers={'Content-Type': 'application/json'}, params=kwargs)
return self._decode_response(response)
def put(self, path_=None, data_=None, **kwargs):
kwargs.update(self.params)
response = self.request('PUT', path_, data_,
headers={'Content-Type': 'application/json'}, params=kwargs)
return self._decode_response(response)
def delete(self, path_=None, **kwargs):
kwargs.update(self.params)
response = self.request('DELETE', path_, params=kwargs)
return self._decode_response(response)
def request(self, method, path=None, data=None, headers=None, allowed=None,
**kwargs):
if not path:
path = ['']
if not isinstance(path, basestring):
path = '/'.join([i.strip('/') for i in [self.api_url] + path])
if data is not None and headers and \
headers.get('Content-Type') == 'application/json':
data = json.dumps(data)
while True:
try:
response = requests.request(method, path, data=data,
headers=headers, session=self._session, **kwargs)
except requests.exceptions.SSLError:
_logger.warning('Use --no-check-certificate to avoid checks')
raise
if response.status_code != 200:
if response.status_code == 401:
enforce(self._sugar_auth,
'Operation is not available in anonymous mode')
_logger.info('User is not registered on the server, '
'registering')
self._register()
continue
if allowed and response.status_code in allowed:
return response
content = response.content
try:
error = json.loads(content)
except Exception:
_logger.debug('Got %s HTTP error for %r request:\n%s',
response.status_code, path, content)
response.raise_for_status()
else:
raise RuntimeError(error['error'])
return response
def call(self, request, response=None):
params = request.copy()
method = params.pop('method')
document = params.pop('document')
guid = params.pop('guid') if 'guid' in params else None
prop = params.pop('prop') if 'prop' in params else None
path = [document]
if guid:
path.append(guid)
if prop:
path.append(prop)
reply = self.request(method, path, data=request.content,
params=params, headers={'Content-Type': 'application/json'})
if response is not None:
response.content_type = reply.headers['Content-Type']
return self._decode_response(reply)
def download(self, url_path, out_path, seqno=None, extract=False):
if isdir(out_path):
shutil.rmtree(out_path)
elif not exists(dirname(out_path)):
os.makedirs(dirname(out_path))
params = {}
if seqno:
params['seqno'] = seqno
response = self.request('GET', url_path, allow_redirects=True,
params=params, allowed=[404])
if response.status_code != 200:
return 'application/octet-stream'
mime_type = response.headers.get('Content-Type') or \
'application/octet-stream'
content_length = response.headers.get('Content-Length')
content_length = int(content_length) if content_length else 0
if seqno and not content_length:
# Local cacheed versions is not stale
return mime_type
def fetch(f):
_logger.debug('Download %r BLOB to %r',
'/'.join(url_path), out_path)
chunk_size = min(content_length, BUFFER_SIZE)
empty = True
for chunk in response.iter_content(chunk_size=chunk_size):
empty = False
f.write(chunk)
return not empty
def fetch_multipart(stream, size, boundary):
stream.readline = None
for filename, content in decode_multipart(stream, size, boundary):
dst_path = join(out_path, filename)
if not exists(dirname(dst_path)):
os.makedirs(dirname(dst_path))
shutil.move(content.name, dst_path)
content_type, params = cgi.parse_header(mime_type)
if content_type.split('/', 1)[0] == 'multipart':
try:
fetch_multipart(response.raw, content_length,
params['boundary'])
except Exception:
shutil.rmtree(out_path, ignore_errors=True)
raise
elif extract:
tmp_file = tempfile.NamedTemporaryFile(delete=False)
try:
if fetch(tmp_file):
tmp_file.close()
with Bundle(tmp_file.name, 'application/zip') as bundle:
bundle.extractall(out_path)
finally:
if exists(tmp_file.name):
os.unlink(tmp_file.name)
else:
with file(out_path, 'w') as f:
if not fetch(f):
os.unlink(out_path)
return mime_type
def subscribe(self):
def handshake():
_logger.debug('Subscribe to %r', self.api_url)
return self.request('GET', params={'cmd': 'subscribe'}).raw
def pull_events(stream):
retries = _RECONNECTION_NUMBER
while True:
start_time = time.time()
try:
if stream is None:
stream = handshake()
for line in _readlines(stream):
if line.startswith('data: '):
yield json.loads(line.split(' ', 1)[1])
except Exception:
if time.time() - start_time > _RECONNECTION_TIMEOUT * 10:
retries = _RECONNECTION_NUMBER
if retries <= 0:
raise
_logger.debug('Re-subscribe to %r in %s second(s)',
self.api_url, _RECONNECTION_TIMEOUT)
self.close()
coroutine.sleep(_RECONNECTION_TIMEOUT)
retries -= 1
stream = None
return pull_events(handshake())
def _register(self):
self.request('POST', ['user'],
headers={
'Content-Type': 'application/json',
},
data={
'name': sugar.nickname() or '',
'color': sugar.color() or '#000000,#000000',
'machine_sn': sugar.machine_sn() or '',
'machine_uuid': sugar.machine_uuid() or '',
'pubkey': sugar.pubkey(),
},
)
def _decode_response(self, response):
if response.headers.get('Content-Type') == 'application/json':
return json.loads(response.content)
else:
return response
def _sign(data):
key = DSA.load_key(sugar.privkey_path())
return key.sign_asn1(hashlib.sha1(data).digest()).encode('hex')
def _readlines(stream):
line = ''
while True:
char = stream.read(1)
if not char:
break
if char == '\n':
yield line
line = ''
else:
line += char
|