comparison mercurial/hgweb/common.py @ 45400: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 89a2afe31e82
comparison
equal deleted inserted replaced
45399:ec2fc4d038c2 45400:f0735f2ce542
195 tp = templatepath or templater.templatedir() 195 tp = templatepath or templater.templatedir()
196 if tp is not None: 196 if tp is not None:
197 directory = os.path.join(tp, b'static') 197 directory = os.path.join(tp, b'static')
198 198
199 fpath = os.path.join(*fname.split(b'/')) 199 fpath = os.path.join(*fname.split(b'/'))
200 ct = pycompat.sysbytes(
201 mimetypes.guess_type(pycompat.fsdecode(fpath))[0] or r"text/plain"
202 )
200 path = os.path.join(directory, fpath) 203 path = os.path.join(directory, fpath)
201 try: 204 try:
202 os.stat(path) 205 os.stat(path)
203 ct = pycompat.sysbytes(
204 mimetypes.guess_type(pycompat.fsdecode(fpath))[0] or r"text/plain"
205 )
206 with open(path, b'rb') as fh: 206 with open(path, b'rb') as fh:
207 data = fh.read() 207 data = fh.read()
208
209 res.headers[b'Content-Type'] = ct
210 res.setbodybytes(data)
211 return res
212 except TypeError: 208 except TypeError:
213 raise ErrorResponse(HTTP_SERVER_ERROR, b'illegal filename') 209 raise ErrorResponse(HTTP_SERVER_ERROR, b'illegal filename')
214 except OSError as err: 210 except OSError as err:
215 if err.errno == errno.ENOENT: 211 if err.errno == errno.ENOENT:
216 raise ErrorResponse(HTTP_NOT_FOUND) 212 raise ErrorResponse(HTTP_NOT_FOUND)
217 else: 213 else:
218 raise ErrorResponse( 214 raise ErrorResponse(
219 HTTP_SERVER_ERROR, encoding.strtolocal(err.strerror) 215 HTTP_SERVER_ERROR, encoding.strtolocal(err.strerror)
220 ) 216 )
217
218 res.headers[b'Content-Type'] = ct
219 res.setbodybytes(data)
220 return res
221 221
222 222
223 def paritygen(stripecount, offset=0): 223 def paritygen(stripecount, offset=0):
224 """count parity of horizontal stripes for easier reading""" 224 """count parity of horizontal stripes for easier reading"""
225 if stripecount and offset: 225 if stripecount and offset: