diff mercurial/hgweb/hgwebdir_mod.py @ 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 c86207d41512
children a1007f7b9b7b 8189e03adb44
line wrap: on
line diff
--- 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)