Skip to content

Commit

Permalink
Fix #1101 - Properly handle a <base> without a href
Browse files Browse the repository at this point in the history
  • Loading branch information
dstufft committed Jun 13, 2014
1 parent a2c008f commit e05a759
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -51,6 +51,9 @@
* Fixed :issue:`1319`. Allow use of Zip64 extension in Wheels and other zip
files. (:pull:`1868`)

* Fixed :issue:`1101`. Properly handle an index or --find-links target which
has a <base> without a href attribute. (:pull:`1869`)


**1.5.7**

Expand Down
9 changes: 6 additions & 3 deletions pip/index.py
Expand Up @@ -820,9 +820,12 @@ def api_version(self):
@property
def base_url(self):
if not hasattr(self, "_base_url"):
base = self.parsed.find(".//base")
if base is not None and base.get("href"):
self._base_url = base.get("href")
bases = [
x for x in self.parsed.findall(".//base")
if x.get("href") is not None
]
if bases and bases[0].get("href"):
self._base_url = bases[0].get("href")
else:
self._base_url = self.url
return self._base_url
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_index.py
@@ -1,3 +1,5 @@
import pytest

from pip.download import PipSession
from pip.index import package_to_requirement, HTMLPage
from pip.index import PackageFinder, Link, INSTALLED_VERSION
Expand Down Expand Up @@ -80,3 +82,27 @@ def test_ext_fragment(self):

def test_ext_query(self):
assert '.whl' == Link('http://yo/wheel.whl?a=b').ext


@pytest.mark.parametrize(
("html", "url", "expected"),
[
("<html></html>", "https://example.com/", "https://example.com/"),
(
"<html><head>"
"<base href=\"https://foo.example.com/\">"
"</head></html>",
"https://example.com/",
"https://foo.example.com/",
),
(
"<html><head>"
"<base><base href=\"https://foo.example.com/\">"
"</head></html>",
"https://example.com/",
"https://foo.example.com/",
),
],
)
def test_base_url(html, url, expected):
assert HTMLPage(html, url).base_url == expected

0 comments on commit e05a759

Please sign in to comment.