[Zope3-checkins] SVN: Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/ Only guess content type for browser responses

Jim Fulton jim at zope.com
Fri Sep 2 21:15:08 EDT 2005


Log message for revision 38285:
  Only guess content type for browser responses
  

Changed:
  U   Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/browser.py
  U   Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/http.py

-=-
Modified: Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/browser.py
===================================================================
--- Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/browser.py	2005-09-03 01:14:08 UTC (rev 38284)
+++ Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/browser.py	2005-09-03 01:15:07 UTC (rev 38285)
@@ -661,6 +661,15 @@
         )
 
     def _implicitResult(self, body):
+        content_type = self.getHeader('content-type')
+        if content_type is None:
+            if isHTML(body):
+                content_type = 'text/html'
+            else:
+                content_type = 'text/plain'
+            self.setHeader('x-content-type-warning', 'guessed from content')
+            self.setHeader('content-type', content_type)
+        
         body, headers = super(BrowserResponse, self)._implicitResult(body)
         body = self.__insertBase(body)
         return body, headers
@@ -733,6 +742,18 @@
         self.outstream.write(''.join(self.result.body))
     
 
+def isHTML(str):
+     """Try to determine whether str is HTML or not."""
+     s = str.lstrip().lower()
+     if s.startswith('<!doctype html'):
+         return True
+     if s.startswith('<html') and (s[5:6] in ' >'):
+         return True
+     if s.startswith('<!--'):
+         idx = s.find('<html')
+         return idx > 0 and (s[idx+5:idx+6] in ' >')
+     else:
+         return False
 
 def normalize_lang(lang):
     lang = lang.strip().lower()

Modified: Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/http.py
===================================================================
--- Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/http.py	2005-09-03 01:14:08 UTC (rev 38284)
+++ Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/http.py	2005-09-03 01:15:07 UTC (rev 38285)
@@ -566,6 +566,7 @@
 
         super(HTTPResponse, self).__init__()
         self.reset()
+        
 
     def reset(self):
         'See IResponse'
@@ -576,6 +577,7 @@
         self._reason = 'No status set'
         self._status_set = False
         self._charset = None
+        self.authUser = '-'
 
     def setStatus(self, status, reason=None):
         'See IHTTPResponse'
@@ -723,19 +725,16 @@
         encoding = getCharsetUsingRequest(self._request) or 'utf-8'
         content_type = self.getHeader('content-type')
 
-        if content_type is None:
-            if isHTML(body):
-                content_type = 'text/html'
-            else:
-                content_type = 'text/plain'
-            self.setHeader('x-content-type-warning', 'guessed from content')
-
         if isinstance(body, unicode):
+            try:
+                if not content_type.startswith('text/'):
+                    raise ValueError(
+                        'Unicode results must have a text content type.')
+            except AttributeError:
+                    raise ValueError(
+                        'Unicode results must have a text content type.')
+                
 
-            if not content_type.startswith('text/'):
-                raise ValueError(
-                    'Unicode results must have a text content type.')
-
             major, minor, params = contenttype.parse(content_type)
 
             if 'charset' in params:
@@ -745,8 +744,11 @@
 
             body = body.encode(encoding)
 
-        headers = [('content-type', content_type),
-                   ('content-length', len(body))]
+        if content_type:
+            headers = [('content-type', content_type),
+                       ('content-length', str(len(body)))]
+        else:
+            headers = [('content-length', str(len(body)))]
 
         return body, headers
 
@@ -888,20 +890,6 @@
         return [c[1] for c in charsets]
 
 
-def isHTML(str):
-     """Try to determine whether str is HTML or not."""
-     s = str.lstrip().lower()
-     if s.startswith('<!doctype html'):
-         return True
-     if s.startswith('<html') and (s[5:6] in ' >'):
-         return True
-     if s.startswith('<!--'):
-         idx = s.find('<html')
-         return idx > 0 and (s[idx+5:idx+6] in ' >')
-     else:
-         return False
-
-
 def getCharsetUsingRequest(request):
     'See IHTTPResponse'
     envadapter = IUserPreferredCharsets(request, None)



More information about the Zope3-Checkins mailing list