Mercurial > hg
changeset 45380:f0735f2ce542
hgweb: minimize scope of a try-block in staticfile()
I think the exceptions are only relevant for the `os.stat()` and
`open()` calls, and maybe to the `fh.read()` call.
Differential Revision: https://phab.mercurial-scm.org/D8936
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Mon, 03 Aug 2020 23:41:50 -0700 |
parents | ec2fc4d038c2 |
children | ede4c121239e |
files | mercurial/hgweb/common.py |
diffstat | 1 files changed, 7 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/hgweb/common.py Mon Aug 03 23:38:50 2020 -0700 +++ b/mercurial/hgweb/common.py Mon Aug 03 23:41:50 2020 -0700 @@ -197,18 +197,14 @@ directory = os.path.join(tp, b'static') fpath = os.path.join(*fname.split(b'/')) + ct = pycompat.sysbytes( + mimetypes.guess_type(pycompat.fsdecode(fpath))[0] or r"text/plain" + ) path = os.path.join(directory, fpath) try: os.stat(path) - ct = pycompat.sysbytes( - mimetypes.guess_type(pycompat.fsdecode(fpath))[0] or r"text/plain" - ) with open(path, b'rb') as fh: data = fh.read() - - res.headers[b'Content-Type'] = ct - res.setbodybytes(data) - return res except TypeError: raise ErrorResponse(HTTP_SERVER_ERROR, b'illegal filename') except OSError as err: @@ -219,6 +215,10 @@ HTTP_SERVER_ERROR, encoding.strtolocal(err.strerror) ) + res.headers[b'Content-Type'] = ct + res.setbodybytes(data) + return res + def paritygen(stripecount, offset=0): """count parity of horizontal stripes for easier reading"""