changeset 6379:d2bb66a8a435

hgweb: add compatibility code for old templates Up to changeset 3340aa5a64f7, HTTP headers were expected to be embedded in the "headers" template. Since that changeset, the content-type is supposed to be defined as the "mimetype" template in the map file. This changeset makes sure the old templates still work.
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Mon, 24 Mar 2008 13:45:01 -0300
parents f0f830114a9b
children a632a9a57821
files mercurial/hgweb/hgweb_mod.py mercurial/hgweb/hgwebdir_mod.py
diffstat 2 files changed, 38 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/hgweb_mod.py	Mon Mar 24 11:12:57 2008 -0500
+++ b/mercurial/hgweb/hgweb_mod.py	Mon Mar 24 13:45:01 2008 -0300
@@ -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 @@
         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 @@
         # 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)
--- a/mercurial/hgweb/hgwebdir_mod.py	Mon Mar 24 11:12:57 2008 -0500
+++ b/mercurial/hgweb/hgwebdir_mod.py	Mon Mar 24 13:45:01 2008 -0300
@@ -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 @@
 
                 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 @@
     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)