Patchwork D8936: hgweb: minimize scope of a try-block in staticfile()

login
register
mail settings
Submitter phabricator
Date Aug. 19, 2020, 4:42 a.m.
Message ID <differential-rev-PHID-DREV-taakvhrh6hjc2kfxa3d2-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47035/
State Superseded
Headers show

Comments

phabricator - Aug. 19, 2020, 4:42 a.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  I think the exceptions are only relevant for the `os.stat()` and
  `open()` calls, and maybe to the `fh.read()` call.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8936

AFFECTED FILES
  mercurial/hgweb/common.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py
--- a/mercurial/hgweb/common.py
+++ b/mercurial/hgweb/common.py
@@ -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"""