Skip to content

Commit 9f20c11

Browse files
committed
Rewrite URL parsing code to use urlparse.
1 parent 3a0dbef commit 9f20c11

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

pusher/__init__.py

Lines changed: 16 additions & 18 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,35 +30,33 @@
3030
app_id_re = re.compile('^[0-9]+$')
3131

3232
def url2options(url):
33-
port = None
34-
if url.startswith('http://'):
35-
url = url[7:]
36-
port = 80
37-
elif url.startswith('https://'):
38-
url = url[8:]
39-
port = 443
40-
else:
41-
assert False, "invalid URL"
42-
key, url = url.split(':', 1)
43-
secret, url = url.split('@', 1)
44-
host, url = url.split('/', 1)
45-
url, app_id = url.split('/', 1)
46-
return {'key': key, 'secret': secret, 'host': host, 'app_id': app_id, 'port': port }
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+
}
4744

4845
def pusher_from_url(url=None):
4946
url = url or os.environ['PUSHER_URL']
5047
return Pusher(**url2options(url))
5148

5249
class Pusher(object):
53-
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):
5451
_globals = globals()
5552
self.app_id = str(app_id or _globals['app_id'])
5653
if not app_id_re.match(self.app_id):
5754
raise NameError("Invalid app id")
5855
self.key = key or _globals['key']
5956
self.secret = secret or _globals['secret']
6057
self.host = host or _globals['host']
61-
self.port = port or _globals['port']
58+
self.port = port or (443 if secure else 80)
59+
self.secure = secure
6260
self.encoder = encoder
6361
self._channels = {}
6462

@@ -111,7 +109,7 @@ def compose_querystring(self, event, json_data, socket_id):
111109
return ret
112110

113111
def send_request(self, signed_path, data_string, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
114-
if self.pusher.port == 443:
112+
if not self.pusher.secure:
115113
client = httplib.HTTPConnection(self.pusher.host, self.pusher.port, timeout=timeout)
116114
else:
117115
client = httplib.HTTPSConnection(self.pusher.host, self.pusher.port, timeout=timeout)

0 commit comments

Comments
 (0)