def get_file_http(self, url, path, revision): logging.info('Fetching file from %s' % url) try: request = URLRequest(url) if self.username: auth_string = base64.b64encode('%s:%s' % (self.username, self.password)) request.add_header('Authorization', 'Basic %s' % auth_string) return urlopen(request).read() except HTTPError as e: if e.code == 404: logging.error('404') raise FileNotFoundError(path, revision) else: msg = "HTTP error code %d when fetching file from %s: %s" % \ (e.code, url, e) logging.error(msg) raise SCMError(msg) except Exception as e: msg = "Unexpected error fetching file from %s: %s" % (url, e) logging.error(msg) raise SCMError(msg)
def get_file_http(self, url, path, revision): """Return the contents of a file from an HTTP(S) URL. This is a convenience for looking up the contents of files that are referenced in diffs through an HTTP(S) request. Authentication is performed using the username and password provided (if any). Args: url (unicode): The URL to fetch the file contents from. path (unicode): The path of the file, as referenced in the diff. revision (Revision): The revision of the file, as referenced in the diff. Returns: bytes: The contents of the file. Raises: reviewboard.scmtools.errors.FileNotFoundError: The file could not be found. reviewboard.scmtools.errors.SCMError: Unexpected error in fetching the file. This may be an unexpected HTTP status code. """ logging.info('Fetching file from %s' % url) try: request = URLRequest(url) if self.username: auth_string = base64.b64encode('%s:%s' % (self.username, self.password)) request.add_header('Authorization', 'Basic %s' % auth_string) return urlopen(request).read() except HTTPError as e: if e.code == 404: logging.error('404') raise FileNotFoundError(path, revision) else: msg = "HTTP error code %d when fetching file from %s: %s" % \ (e.code, url, e) logging.error(msg) raise SCMError(msg) except Exception as e: msg = "Unexpected error fetching file from %s: %s" % (url, e) logging.error(msg) raise SCMError(msg)
def _build_request(self, url, body=None, headers={}, username=None, password=None): r = URLRequest(url, body, headers) if username is not None and password is not None: auth_key = username + ':' + password r.add_header( HTTPBasicAuthHandler.auth_header, 'Basic %s' % base64.b64encode(auth_key.encode('utf-8'))) return r
def get_file_http(self, url, path, revision, mime_type=None): """Return the contents of a file from an HTTP(S) URL. This is a convenience for looking up the contents of files that are referenced in diffs through an HTTP(S) request. Authentication is performed using the username and password provided (if any). Args: url (unicode): The URL to fetch the file contents from. path (unicode): The path of the file, as referenced in the diff. revision (Revision): The revision of the file, as referenced in the diff. mime_type (unicode): The expected content type of the file. If not specified, this will default to accept everything. Returns: bytes: The contents of the file if content type matched, otherwise None. Raises: reviewboard.scmtools.errors.FileNotFoundError: The file could not be found. reviewboard.scmtools.errors.SCMError: Unexpected error in fetching the file. This may be an unexpected HTTP status code. """ logging.info('Fetching file from %s' % url) try: request = URLRequest(url) if self.username: credentials = '%s:%s' % (self.username, self.password) auth_string = \ force_text(base64.b64encode(credentials.encode('utf-8'))) request.add_header(force_str('Authorization'), force_str('Basic %s' % auth_string)) response = urlopen(request) if (mime_type is None or response.info()['Content-Type'] == mime_type): return force_bytes(response.read()) return None except HTTPError as e: if e.code == 404: raise FileNotFoundError(path, revision) else: msg = "HTTP error code %d when fetching file from %s: %s" % \ (e.code, url, e) logging.error(msg) raise SCMError(msg) except Exception as e: msg = "Unexpected error fetching file from %s: %s" % (url, e) logging.error(msg) raise SCMError(msg)