Skip to content

Commit

Permalink
[unzip-http] handle errors opening URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
midichef committed Dec 27, 2024
1 parent 8143b11 commit 21d078b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions visidata/loaders/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def openZipFile(self, fp, *args, **kwargs):
pwd = vd.input(f'{args[0].filename} is encrypted, enter password: ', display=False)
return zip_open(*args, **kwargs, pwd=pwd.encode('utf-8'))
vd.exceptionCaught(err)
except urllib.error.HTTPError as err:
if err.status is None:
vd.fail(f'cannot open URL: {err.msg}')
else:
vd.fail(f'cannot open URL: HTTP Error {err.status}: {err.msg}')


def openRow(self, row):
fi, zpath = row
Expand Down
24 changes: 22 additions & 2 deletions visidata/loaders/unzip_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import argparse
import pathlib
import urllib.parse
import urllib.error
from visidata import vd


Expand Down Expand Up @@ -149,7 +150,20 @@ def namelist(self):
return list(r.filename for r in self.infoiter())

def infoiter(self):
resp = self.http.request('HEAD', self.url)
urllib3 = vd.importExternal('urllib3')
try:
resp = self.http.request('HEAD', self.url)
except urllib3.exceptions.HTTPError as e:
code = None
msg = f'urllib3.error.{e.__name__}'
hdrs = fp = None
# transform to HTTPError in urllib, instead of urllib3, to avoid caller needing urllib3
raise urllib.error.HTTPError(self.url, code, msg, hdrs, fp)
if not (200 <= resp.status <= 299):
code = resp.status
msg = 'HEAD request status not in range 200-299'
hdrs = fp = None
raise urllib.error.HTTPError(self.url, code, msg, hdrs, fp)
r = resp.headers.get('Accept-Ranges', '')
if r != 'bytes':
hostname = urllib.parse.urlparse(self.url).netloc
Expand Down Expand Up @@ -231,7 +245,13 @@ def extractall(self, path=None, members=None, pwd=None):
self.extract(fn, path, pwd=pwd)

def get_range(self, start, n):
return self.http.request('GET', self.url, headers={'Range': f'bytes={start}-{start+n-1}'}, preload_content=False)
try:
return self.http.request('GET', self.url, headers={'Range': f'bytes={start}-{start+n-1}'}, preload_content=False)
except urllib3.exceptions.HTTPError as e:
code = None
msg = f'urllib3.error.{e.__name__}'
hdrs = fp = None
raise urllib.error.HTTPError(self.url, code, msg, hdrs, fp)

def matching_files(self, *globs):
for f in self.files.values():
Expand Down

0 comments on commit 21d078b

Please sign in to comment.