Skip to content

Commit 923ee01

Browse files
Rémy HUBSCHERhonzakral
authored andcommitted
Support RFC-1738 URLs
Fixes #149
1 parent 03a8176 commit 923ee01

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

elasticsearch/client/__init__.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from __future__ import unicode_literals
12
import weakref
23
import logging
34

45
from ..transport import Transport
56
from ..exceptions import NotFoundError, TransportError
6-
from ..compat import string_types
7+
from ..compat import string_types, urlparse
78
from .indices import IndicesClient
89
from .cluster import ClusterClient
910
from .cat import CatClient
@@ -30,22 +31,27 @@ def _normalize_hosts(hosts):
3031
# normalize hosts to dicts
3132
for i, host in enumerate(hosts):
3233
if isinstance(host, string_types):
33-
host = host.strip('/')
34-
# remove schema information
35-
if '://' in host:
34+
if '://' not in host:
35+
host = "//%s" % host
36+
37+
parsed_url = urlparse(host)
38+
h = {"host": parsed_url.hostname}
39+
40+
if parsed_url.port:
41+
h["port"] = parsed_url.port
42+
43+
if parsed_url.scheme == "https":
44+
h['port'] = parsed_url.port or 443
45+
h['use_ssl'] = True
46+
elif parsed_url.scheme == "http":
3647
logger.warning(
3748
"List of nodes should not include schema information (http://): %r.",
3849
host
3950
)
40-
host = host[host.index('://') + 3:]
41-
42-
h = {"host": host}
43-
if ':' in host:
44-
# TODO: detect auth urls
45-
host, port = host.rsplit(':', 1)
46-
if port.isdigit():
47-
port = int(port)
48-
h = {"host": host, "port": port}
51+
52+
if parsed_url.username or parsed_url.password:
53+
h['http_auth'] = '%s:%s' % (parsed_url.username, parsed_url.password)
54+
4955
out.append(h)
5056
else:
5157
out.append(host)

elasticsearch/compat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
if PY2:
66
string_types = basestring,
77
from urllib import quote_plus, urlencode
8+
from urlparse import urlparse
89
from itertools import imap as map
910
else:
1011
string_types = str, bytes
11-
from urllib.parse import quote_plus, urlencode
12+
from urllib.parse import quote_plus, urlencode, urlparse
1213
map = map

test_elasticsearch/test_client/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ def test_none_uses_defaults(self):
1313
def test_strings_are_used_as_hostnames(self):
1414
self.assertEquals([{"host": "elasticsearch.org"}], _normalize_hosts(["elasticsearch.org"]))
1515

16-
def test_strings_are_parsed_for_port(self):
16+
def test_strings_are_parsed_for_port_and_user(self):
1717
self.assertEquals(
18-
[{"host": "elasticsearch.org", "port": 42}, {"host": "user:secret@elasticsearch.com"}],
18+
[{"host": "elasticsearch.org", "port": 42}, {"host": "elasticsearch.com", "http_auth": "user:secret"}],
1919
_normalize_hosts(["elasticsearch.org:42", "user:[email protected]"])
2020
)
2121

22+
def test_strings_are_parsed_for_scheme(self):
23+
self.assertEquals(
24+
[{"host": "elasticsearch.org", "port": 42, "use_ssl": True},
25+
{"host": "elasticsearch.com", "http_auth": "user:secret", "use_ssl": True, "port": 443}],
26+
_normalize_hosts(["https://elasticsearch.org:42", "https://user:[email protected]"])
27+
)
28+
2229
def test_dicts_are_left_unchanged(self):
2330
self.assertEquals([{"host": "local", "extra": 123}], _normalize_hosts([{"host": "local", "extra": 123}]))
2431

0 commit comments

Comments
 (0)