Skip to content

Commit 6da0b76

Browse files
committed
Merge pull request #33 from rasky/master
Complete SSL support by using a HTTPSConnection when needed.
2 parents f84651c + 9f20c11 commit 6da0b76

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

pusher/__init__.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
from urllib import quote
1515
import re
1616
import socket
17+
import urlparse
1718

1819
if sys.version < '3':
1920
text_type = unicode
2021
else:
2122
text_type = str
2223

2324
host = 'api.pusherapp.com'
24-
port = 80
2525
app_id = None
2626
key = None
2727
secret = None
@@ -30,32 +30,33 @@
3030
app_id_re = re.compile('^[0-9]+$')
3131

3232
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+
}
4444

4545
def pusher_from_url(url=None):
4646
url = url or os.environ['PUSHER_URL']
4747
return Pusher(**url2options(url))
4848

4949
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):
5151
_globals = globals()
5252
self.app_id = str(app_id or _globals['app_id'])
5353
if not app_id_re.match(self.app_id):
5454
raise NameError("Invalid app id")
5555
self.key = key or _globals['key']
5656
self.secret = secret or _globals['secret']
5757
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
5960
self.encoder = encoder
6061
self._channels = {}
6162

@@ -108,7 +109,10 @@ def compose_querystring(self, event, json_data, socket_id):
108109
return ret
109110

110111
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)
112116
client.request('POST', signed_path, data_string, {'Content-Type': 'application/json'})
113117
resp = client.getresponse()
114118
return resp.status, resp.read()

0 commit comments

Comments
 (0)