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 @@ -6,7 +6,7 @@ # This software may be used and distributed according to the terms # of the GNU General Public License, incorporated herein by reference. -import os, mimetypes, re +import os, mimetypes, re, mimetools, cStringIO from mercurial.node import hex, nullid, short from mercurial.repo import RepoError from mercurial import mdiff, ui, hg, util, archival, patch, hook @@ -226,8 +226,17 @@ class hgweb(object): try: tmpl = self.templater(req) - ctype = tmpl('mimetype', encoding=self.encoding) - ctype = templater.stringify(ctype) + try: + ctype = tmpl('mimetype', encoding=self.encoding) + ctype = templater.stringify(ctype) + except KeyError: + # old templates with inline HTTP headers? + if 'mimetype' in tmpl: + raise + header = tmpl('header', encoding=self.encoding) + header_file = cStringIO.StringIO(templater.stringify(header)) + msg = mimetools.Message(header_file, 0) + ctype = msg['content-type'] if cmd == '': req.form['cmd'] = [tmpl.cache['default']] @@ -282,7 +291,13 @@ class hgweb(object): # some functions for the templater def header(**map): - yield tmpl('header', encoding=self.encoding, **map) + header = tmpl('header', encoding=self.encoding, **map) + if 'mimetype' not in tmpl: + # old template with inline HTTP headers + header_file = cStringIO.StringIO(templater.stringify(header)) + msg = mimetools.Message(header_file, 0) + header = header_file.read() + yield header def footer(**map): yield tmpl("footer", **map) 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 @@ -6,7 +6,7 @@ # This software may be used and distributed according to the terms # of the GNU General Public License, incorporated herein by reference. -import os +import os, mimetools, cStringIO from mercurial.i18n import gettext as _ from mercurial.repo import RepoError from mercurial import ui, hg, util, templater, templatefilters @@ -81,8 +81,17 @@ class hgwebdir(object): virtual = req.env.get("PATH_INFO", "").strip('/') tmpl = self.templater(req) - ctype = tmpl('mimetype', encoding=util._encoding) - ctype = templater.stringify(ctype) + try: + ctype = tmpl('mimetype', encoding=util._encoding) + ctype = templater.stringify(ctype) + except KeyError: + # old templates with inline HTTP headers? + if 'mimetype' in tmpl: + raise + header = tmpl('header', encoding=util._encoding) + header_file = cStringIO.StringIO(templater.stringify(header)) + msg = mimetools.Message(header_file, 0) + ctype = msg['content-type'] # a static file if virtual.startswith('static/') or 'static' in req.form: @@ -246,7 +255,13 @@ class hgwebdir(object): def templater(self, req): def header(**map): - yield tmpl('header', encoding=util._encoding, **map) + header = tmpl('header', encoding=util._encoding, **map) + if 'mimetype' not in tmpl: + # old template with inline HTTP headers + header_file = cStringIO.StringIO(templater.stringify(header)) + msg = mimetools.Message(header_file, 0) + header = header_file.read() + yield header def footer(**map): yield tmpl("footer", **map)