diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py +++ b/mercurial/hgweb/hgweb_mod.py @@ -206,12 +206,17 @@ class hgweb(object): 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 @@ class hgweb(object): 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 @@ class hgweb(object): "header": header, "footer": footer, "motd": motd, - "rawfileheader": rawfileheader, "sessionvars": sessionvars }) return tmpl diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py --- a/mercurial/hgweb/webcommands.py +++ b/mercurial/hgweb/webcommands.py @@ -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 @@ def log(web, req, tmpl): 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: diff --git a/templates/raw/map b/templates/raw/map --- a/templates/raw/map +++ b/templates/raw/map @@ -8,7 +8,6 @@ diffline = '#line#' changesetparent = '# Parent #node#' changesetchild = '# Child #node#' filenodelink = '' -filerevision = '#rawfileheader##raw#' fileline = '#line#' diffblock = '#lines#' filediff = filediff.tmpl