##// END OF EJS Templates
hgweb: minimize scope of a try-block in staticfile()...
Martin von Zweigbergk -
r45940:f0735f2c default
parent child Browse files
Show More
@@ -197,18 +197,14 b' def staticfile(templatepath, directory, '
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:
@@ -219,6 +215,10 b' def staticfile(templatepath, directory, '
219 HTTP_SERVER_ERROR, encoding.strtolocal(err.strerror)
215 HTTP_SERVER_ERROR, encoding.strtolocal(err.strerror)
220 )
216 )
221
217
218 res.headers[b'Content-Type'] = ct
219 res.setbodybytes(data)
220 return res
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"""
General Comments 0
You need to be logged in to leave comments. Login now