diff --git a/glance_store/_drivers/http.py b/glance_store/_drivers/http.py index a2073de..d258102 100644 --- a/glance_store/_drivers/http.py +++ b/glance_store/_drivers/http.py @@ -18,6 +18,8 @@ import logging import socket import urlparse +import requests + from glance_store import capabilities import glance_store.driver from glance_store import exceptions @@ -175,11 +177,11 @@ class Store(glance_store.driver.Store): "redirects.") % MAX_REDIRECTS) LOG.debug(reason) raise exceptions.MaxRedirectsExceeded(message=reason) + loc = location.store_location - conn_class = self._get_conn_class(loc) - conn = conn_class(loc.netloc) - conn.request(verb, loc.path, "", {}) - resp = conn.getresponse() + session = new_session() + conn = session.request(verb, loc.get_uri(), stream=True) + resp = conn.raw # Check for bad status codes if resp.status >= 400: @@ -194,29 +196,11 @@ class Store(glance_store.driver.Store): LOG.debug(reason) raise exceptions.BadStoreUri(message=reason) - location_header = resp.getheader("location") - if location_header: - if resp.status not in (301, 302): - reason = (_("The HTTP URL %(url)s attempted to redirect " - "with an invalid %(status)s status code.") % - dict(url=loc.path, status=resp.status)) - LOG.info(reason) - raise exceptions.BadStoreUri(message=reason) - location_class = glance_store.location.Location - new_loc = location_class(location.store_name, - location.store_location.__class__, - self.conf, - uri=location_header, - image_id=location.image_id, - store_specs=location.store_specs) - return self._query(new_loc, verb, depth + 1) content_length = int(resp.getheader('content-length', 0)) return (conn, resp, content_length) - def _get_conn_class(self, loc): - """ - Returns connection class for accessing the resource. Useful - for dependency injection and stubouts in testing... - """ - return {'http': httplib.HTTPConnection, - 'https': httplib.HTTPSConnection}[loc.scheme] + +def new_session(): + s = requests.Session() + s.max_redirects = MAX_REDIRECTS + return s diff --git a/requirements.txt b/requirements.txt index 8ff82a6..c27e98d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,5 +13,7 @@ six>=1.4.1 jsonschema>=2.0.0,<3.0.0 +requests>=2.2.0,!=2.4.0 + # py2.6 compat ordereddict