diff options
author | harlowja <harlowja@virtualbox.rhel> | 2012-06-17 18:23:24 -0700 |
---|---|---|
committer | harlowja <harlowja@virtualbox.rhel> | 2012-06-17 18:23:24 -0700 |
commit | b6f158b8a55f37d9d2854ff0e566298b85cc0c89 (patch) | |
tree | 706d434f19edf151d51072a32c41f3baa26093cc /cloudinit/url_helper.py | |
parent | bc5322d2bc81b2421ae8dfe1bb02fa2fd61fed51 (diff) | |
download | vyos-cloud-init-b6f158b8a55f37d9d2854ff0e566298b85cc0c89.tar.gz vyos-cloud-init-b6f158b8a55f37d9d2854ff0e566298b85cc0c89.zip |
1. Add a url response class that urlreading now returns (instead of a tuple).
a. This allows for more properties to be added as needed in the future, instead of being very restrictive.
2. Fix up all uses of the url reading to now use this new response object.
3. Also fixup user data including, such that if no response actual occurs the url content is not further processed.
Diffstat (limited to 'cloudinit/url_helper.py')
-rw-r--r-- | cloudinit/url_helper.py | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py index 95de9c7a..56649c1b 100644 --- a/cloudinit/url_helper.py +++ b/cloudinit/url_helper.py @@ -34,11 +34,35 @@ from cloudinit import version LOG = logging.getLogger(__name__) -def ok_http_code(st, redirects_ok=False): - if redirects_ok: - return st in xrange(200, 400) - else: - return st in xrange(200, 300) +class UrlResponse(object): + def __init__(self, status_code, contents=None, headers=None): + self._status_code = status_code + self._contents = contents + self._headers = headers + + @property + def code(self): + return self._status_code + + @property + def contents(self): + return self._contents + + @property + def headers(self): + return self._headers + + def __str__(self): + if not self.contents: + return '' + else: + return str(self.contents) + + def ok(self, redirects_ok=False): + if redirects_ok: + return self.code in xrange(200, 400) + else: + return self.code in xrange(200, 300) def readurl(url, data=None, timeout=None, @@ -74,9 +98,12 @@ def readurl(url, data=None, timeout=None, if status is None: # This seems to happen when files are read... status = 200 + headers = {} + if rh.headers: + headers = dict(rh.headers) LOG.info("Read from %s (%s, %sb) after %s attempts", url, status, len(content), (i + 1)) - return (content, status) + return UrlResponse(status, content, headers) except urllib2.HTTPError as e: excepts.append(e) except urllib2.URLError as e: @@ -162,11 +189,11 @@ def wait_for_url(urls, max_wait=None, timeout=None, else: headers = {} - (resp, sc) = readurl(url, headers=headers, timeout=timeout) - if not resp: - reason = "empty response [%s]" % sc - elif not ok_http_code(sc): - reason = "bad status code [%s]" % sc + resp = readurl(url, headers=headers, timeout=timeout) + if not resp.contents: + reason = "empty response [%s]" % (resp.code) + elif not resp.ok(): + reason = "bad status code [%s]" % (resp.code) else: return url except urllib2.HTTPError as e: |