hgweb: fast path for sending raw files
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Fri, 18 Jan 2008 19:53:38 +0100
changeset 5890 a0e20a5eba3c
parent 5889 209577095f20
child 5891 e7ec2217f2d8
hgweb: fast path for sending raw files
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/webcommands.py
templates/raw/map
--- a/mercurial/hgweb/hgweb_mod.py	Fri Jan 18 19:53:38 2008 +0100
+++ b/mercurial/hgweb/hgweb_mod.py	Fri Jan 18 19:53:38 2008 +0100
@@ -206,12 +206,17 @@
                 method = getattr(protocol, cmd)
                 method(self, req)
             else:
+
                 tmpl = self.templater(req)
                 if cmd == '':
                     req.form['cmd'] = [tmpl.cache['default']]
                     cmd = req.form['cmd'][0]
-                method = getattr(webcommands, cmd)
-                method(self, req, tmpl)
+
+                if cmd == 'file' and 'raw' in req.form['style']:
+                    webcommands.rawfile(self, req, tmpl)
+                else:
+                    getattr(webcommands, cmd)(self, req, tmpl)
+
                 del tmpl
 
         except revlog.LookupError, err:
@@ -254,12 +259,6 @@
             req.header(msg.items())
             yield header_file.read()
 
-        def rawfileheader(**map):
-            req.header([('Content-type', map['mimetype']),
-                        ('Content-disposition', 'filename=%s' % map['file']),
-                        ('Content-length', str(len(map['raw'])))])
-            yield ''
-
         def footer(**map):
             yield tmpl("footer", **map)
 
@@ -300,7 +299,6 @@
                                              "header": header,
                                              "footer": footer,
                                              "motd": motd,
-                                             "rawfileheader": rawfileheader,
                                              "sessionvars": sessionvars
                                             })
         return tmpl
--- a/mercurial/hgweb/webcommands.py	Fri Jan 18 19:53:38 2008 +0100
+++ b/mercurial/hgweb/webcommands.py	Fri Jan 18 19:53:38 2008 +0100
@@ -5,8 +5,8 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-import os
-from mercurial import revlog
+import os, mimetypes
+from mercurial import revlog, util
 from common import staticfile
 
 def log(web, req, tmpl):
@@ -15,6 +15,27 @@
     else:
         changelog(web, req, tmpl)
 
+def rawfile(web, req, tmpl):
+    path = web.cleanpath(req.form.get('file', [''])[0])
+    if not path:
+        req.write(web.manifest(tmpl, web.changectx(req), path))
+        return
+
+    try:
+        fctx = web.filectx(req)
+    except revlog.LookupError:
+        req.write(web.manifest(tmpl, web.changectx(req), path))
+        return
+
+    path = fctx.path()
+    text = fctx.data()
+    mt = mimetypes.guess_type(path)[0]
+    if util.binary(text):
+        mt = mt or 'application/octet-stream'
+
+    req.httphdr(mt, path, len(text))
+    req.write(text)
+
 def file(web, req, tmpl):
     path = web.cleanpath(req.form.get('file', [''])[0])
     if path:
--- a/templates/raw/map	Fri Jan 18 19:53:38 2008 +0100
+++ b/templates/raw/map	Fri Jan 18 19:53:38 2008 +0100
@@ -8,7 +8,6 @@
 changesetparent = '# Parent #node#'
 changesetchild = '# Child #node#'
 filenodelink = ''
-filerevision = '#rawfileheader##raw#'
 fileline = '#line#'
 diffblock = '#lines#'
 filediff = filediff.tmpl