|
14 | 14 | from urllib import quote
|
15 | 15 | import re
|
16 | 16 | import socket
|
| 17 | +import urlparse |
17 | 18 |
|
18 | 19 | if sys.version < '3':
|
19 | 20 | text_type = unicode
|
20 | 21 | else:
|
21 | 22 | text_type = str
|
22 | 23 |
|
23 | 24 | host = 'api.pusherapp.com'
|
24 |
| -port = 80 |
25 | 25 | app_id = None
|
26 | 26 | key = None
|
27 | 27 | secret = None
|
|
30 | 30 | app_id_re = re.compile('^[0-9]+$')
|
31 | 31 |
|
32 | 32 | def url2options(url):
|
33 |
| - if url.startswith('http://'): |
34 |
| - url = url[7:] |
35 |
| - elif url.startswith('https://'): |
36 |
| - url = url[8:] |
37 |
| - else: |
38 |
| - assert False, "invalid URL" |
39 |
| - key, url = url.split(':', 1) |
40 |
| - secret, url = url.split('@', 1) |
41 |
| - host, url = url.split('/', 1) |
42 |
| - url, app_id = url.split('/', 1) |
43 |
| - return {'key': key, 'secret': secret, 'host': host, 'app_id': app_id} |
| 33 | + p = urlparse.urlsplit(url) |
| 34 | + if not p.path.startswith("/apps/"): |
| 35 | + raise ValueError("invalid URL path") |
| 36 | + return { |
| 37 | + 'key': p.username, |
| 38 | + 'secret': p.password, |
| 39 | + 'host': p.hostname, |
| 40 | + 'app_id': p.path[6:], |
| 41 | + 'port': p.port, |
| 42 | + 'secure': p.scheme == 'https', |
| 43 | + } |
44 | 44 |
|
45 | 45 | def pusher_from_url(url=None):
|
46 | 46 | url = url or os.environ['PUSHER_URL']
|
47 | 47 | return Pusher(**url2options(url))
|
48 | 48 |
|
49 | 49 | class Pusher(object):
|
50 |
| - def __init__(self, app_id=None, key=None, secret=None, host=None, port=None, encoder=None): |
| 50 | + def __init__(self, app_id=None, key=None, secret=None, host=None, port=None, encoder=None, secure=False): |
51 | 51 | _globals = globals()
|
52 | 52 | self.app_id = str(app_id or _globals['app_id'])
|
53 | 53 | if not app_id_re.match(self.app_id):
|
54 | 54 | raise NameError("Invalid app id")
|
55 | 55 | self.key = key or _globals['key']
|
56 | 56 | self.secret = secret or _globals['secret']
|
57 | 57 | self.host = host or _globals['host']
|
58 |
| - self.port = port or _globals['port'] |
| 58 | + self.port = port or (443 if secure else 80) |
| 59 | + self.secure = secure |
59 | 60 | self.encoder = encoder
|
60 | 61 | self._channels = {}
|
61 | 62 |
|
@@ -108,7 +109,10 @@ def compose_querystring(self, event, json_data, socket_id):
|
108 | 109 | return ret
|
109 | 110 |
|
110 | 111 | def send_request(self, signed_path, data_string, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
|
111 |
| - client = httplib.HTTPConnection(self.pusher.host, self.pusher.port, timeout=timeout) |
| 112 | + if not self.pusher.secure: |
| 113 | + client = httplib.HTTPConnection(self.pusher.host, self.pusher.port, timeout=timeout) |
| 114 | + else: |
| 115 | + client = httplib.HTTPSConnection(self.pusher.host, self.pusher.port, timeout=timeout) |
112 | 116 | client.request('POST', signed_path, data_string, {'Content-Type': 'application/json'})
|
113 | 117 | resp = client.getresponse()
|
114 | 118 | return resp.status, resp.read()
|
|
0 commit comments