[z3-checkins] r52124 - in z3/deliverance/trunk/deliverance: . test-data/aggregate2

ianb at codespeak.net ianb at codespeak.net
Mon Mar 3 22:01:57 CET 2008


Author: ianb
Date: Mon Mar  3 22:01:56 2008
New Revision: 52124

Modified:
   z3/deliverance/trunk/deliverance/test-data/aggregate2/expected.html
   z3/deliverance/trunk/deliverance/wsgimiddleware.py
Log:
Add a <meta http-equiv=content-type> to pages that don't already have it

Modified: z3/deliverance/trunk/deliverance/test-data/aggregate2/expected.html
==============================================================================
--- z3/deliverance/trunk/deliverance/test-data/aggregate2/expected.html	(original)
+++ z3/deliverance/trunk/deliverance/test-data/aggregate2/expected.html	Mon Mar  3 22:01:56 2008
@@ -1,6 +1,7 @@
 <html>
+<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
 <body>
     <div class="foo" >
     </div>
 </body>
-</html>
\ No newline at end of file
+</html>

Modified: z3/deliverance/trunk/deliverance/wsgimiddleware.py
==============================================================================
--- z3/deliverance/trunk/deliverance/wsgimiddleware.py	(original)
+++ z3/deliverance/trunk/deliverance/wsgimiddleware.py	Mon Mar  3 22:01:56 2008
@@ -359,7 +359,7 @@
         fetcher.environ['HTTP_CACHE_CONTROL'] = 'no-cache'
         
 
-        status, headers, body = fetcher.wsgi_get()         
+        status, headers, body = fetcher.wsgi_get()
         
         if not status.startswith('200'): 
             path_info = uri 
@@ -373,6 +373,8 @@
                 % (construct_url(environ), path_info, status,
                    loc))
 
+        body = fixup_meta_content_type(headers, body)
+
         environ[DELIVERANCE_CACHE][uri] = (status, headers, body)
 
         return body
@@ -507,3 +509,27 @@
         app, theme_uri, rule_uri,
         renderer=renderer)
 
+_http_equiv_re = re.compile(r'<meta\s+[^>]*http-equiv="?content-type"?[^>]*>', re.I|re.S)
+_head_re = re.compile(r'<head[^>]*>', re.I|re.S)
+_html_re = re.compile(r'<html[^>]*>', re.I|re.S)
+
+def fixup_meta_content_type(headers, body):
+    """
+    This, in a somewhat hacky fashion, adds <meta
+    http-equiv=content-type> to pages that do not already have it.
+    """
+    ## FIXME: the existance of this function is a total hack
+    content_type = header_value(headers, 'content-type')
+    if not content_type or not content_type.startswith('text/html'):
+        return body
+    if _http_equiv_re.search(body):
+        # Already has the tag
+        return body
+    http_equiv = '<meta http-equiv="content-type" content="%s">\n' % content_type
+    match = _head_re.search(body)
+    if not match:
+        match = _html_re.search(body)
+        if not match:
+            # Doesn't look like html
+            return body
+    return body[:match.end()] + http_equiv + body[match.end():]


More information about the z3-checkins mailing list