Patchwork D2789: hgweb: port static file handling to new response API

login
register
mail settings
Submitter phabricator
Date March 12, 2018, 9:33 p.m.
Message ID <7010e8b4f4686ac2958de25cf16af6f7@localhost.localdomain>
Download mbox | patch
Permalink /patch/29370/
State Not Applicable
Headers show

Comments

phabricator - March 12, 2018, 9:33 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG98baf8dea553: hgweb: port static file handling to new response API (authored by indygreg, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D2789?vs=6850&id=6925

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

AFFECTED FILES
  mercurial/hgweb/common.py
  mercurial/hgweb/hgwebdir_mod.py
  mercurial/hgweb/webcommands.py

CHANGE DETAILS




To: indygreg, #hg-reviewers, durin42
Cc: mercurial-devel

Patch

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -1232,8 +1232,9 @@ 
         if isinstance(tp, str):
             tp = [tp]
         static = [os.path.join(p, 'static') for p in tp]
-    staticfile(static, fname, req)
-    return []
+
+    staticfile(static, fname, web.res)
+    return web.res
 
 @webcommand('graph')
 def graph(web, req, tmpl):
diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py
+++ b/mercurial/hgweb/hgwebdir_mod.py
@@ -230,19 +230,25 @@ 
 
     def _runwsgi(self, wsgireq):
         req = wsgireq.req
+        res = wsgireq.res
 
         try:
             self.refresh()
 
             csp, nonce = cspvalues(self.ui)
             if csp:
+                res.headers['Content-Security-Policy'] = csp
                 wsgireq.headers.append(('Content-Security-Policy', csp))
 
             virtual = wsgireq.env.get("PATH_INFO", "").strip('/')
             tmpl = self.templater(wsgireq, nonce)
             ctype = tmpl('mimetype', encoding=encoding.encoding)
             ctype = templater.stringify(ctype)
 
+            # Global defaults. These can be overridden by any handler.
+            res.status = '200 Script output follows'
+            res.headers['Content-Type'] = ctype
+
             # a static file
             if virtual.startswith('static/') or 'static' in req.qsparams:
                 if virtual.startswith('static/'):
@@ -256,8 +262,9 @@ 
                     if isinstance(tp, str):
                         tp = [tp]
                     static = [os.path.join(p, 'static') for p in tp]
-                staticfile(static, fname, wsgireq)
-                return []
+
+                staticfile(static, fname, res)
+                return res.sendresponse()
 
             # top-level index
 
diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py
--- a/mercurial/hgweb/common.py
+++ b/mercurial/hgweb/common.py
@@ -153,7 +153,7 @@ 
 
     return True
 
-def staticfile(directory, fname, req):
+def staticfile(directory, fname, res):
     """return a file inside directory with guessed Content-Type header
 
     fname always uses '/' as directory separator and isn't allowed to
@@ -178,7 +178,9 @@ 
         with open(path, 'rb') as fh:
             data = fh.read()
 
-        req.respond(HTTP_OK, ct, body=data)
+        res.headers['Content-Type'] = ct
+        res.setbodybytes(data)
+        return res
     except TypeError:
         raise ErrorResponse(HTTP_SERVER_ERROR, 'illegal filename')
     except OSError as err: