[CMF-checkins] CVS: Products/CMFDefault - Document.py:1.40.14.1 Link.py:1.14.2.1 utils.py:1.8.24.1

Tres Seaver tseaver@zope.com
Tue, 8 Jan 2002 13:19:46 -0500


Update of /cvs-repository/Products/CMFDefault
In directory cvs.zope.org:/tmp/cvs-serv3440

Modified Files:
      Tag: tseaver-tracker_407-branch
	Document.py Link.py utils.py 
Log Message:


  - Clean up emission of RFC822-style headers (Tracker #407):

    o Headers must be terminated with CRLF;

    o Header values with embedded newlines must pad continuation lines
      with leading whitespace).

  - Refactor test_Link to use 'assertEquals'.


=== Products/CMFDefault/Document.py 1.40 => 1.40.14.1 ===
 from Products.CMFCore.WorkflowCore import WorkflowAction
 from Products.CMFCore.utils import _format_stx, keywordsplitter
-from utils import parseHeadersBody, SimpleHTMLParser, bodyfinder, _dtmldir
+from utils import parseHeadersBody, formatRFC822Headers
+from utils import SimpleHTMLParser, bodyfinder, _dtmldir
 
 factory_type_information = ( { 'id'             : 'Document'
                              , 'meta_type'      : 'Document'
@@ -400,9 +401,8 @@
                 'body': self.EditableBody(),
                 }
         else:
-            hdrtext = join(map(lambda x: '%s: %s' % (
-                x[0], x[1]), hdrlist), '\n')
-            bodytext = '%s\n\n%s' % ( hdrtext, self.text )
+            hdrtext = formatRFC822Headers( hdrlist )
+            bodytext = '%s\r\n\r\n%s' % ( hdrtext, self.text )
 
         return bodytext
 


=== Products/CMFDefault/Link.py 1.14 => 1.14.2.1 ===
 
 from DublinCore import DefaultDublinCoreImpl
-from utils import parseHeadersBody, _dtmldir
+from utils import formatRFC822Headers, parseHeadersBody, _dtmldir
 
 factory_type_information = ( { 'id'             : 'Link'
                              , 'meta_type'      : 'Link'
@@ -88,6 +88,7 @@
                      )
 
     meta_type = 'Link'
+    URL_FORMAT = format = 'text/url'
     effective_date = expiration_date = None
     _isDiscussable = 1
 
@@ -158,7 +159,7 @@
         lines = string.split( body, '\n' )
         self.edit( lines[0] )
 
-        headers['Format'] = 'text/url'
+        headers['Format'] = self.URL_FORMAT
         new_subject = keywordsplitter(headers)
         headers['Subject'] = new_subject or self.Subject()
         haveheader = headers.has_key
@@ -197,8 +198,7 @@
         join = string.join
         lower = string.lower
         hdrlist = self.getMetadataHeaders()
-        hdrtext = join( map( lambda x: '%s: %s' % ( x[0], x[1] )
-                           , hdrlist), '\n' )
+        hdrtext = formatRFC822Headers( hdrlist )
         bodytext = '%s\n\n%s' % ( hdrtext, self.getRemoteUrl() )
 
         return bodytext


=== Products/CMFDefault/utils.py 1.8 => 1.8.24.1 ===
 _dtmldir = os.path.join( package_home( globals() ), 'dtml' )
 
+def formatRFC822Headers( headers ):
+    """
+        Convert the key-value pairs in 'headers' to valid RFC822-style
+        headers, including adding leading whitespace to elements which
+        contain newlines in order to preserve continuation-line semantics.
+    """
+    munged = []
+    linesplit = re.compile( r'[\n\r]+?' )
+
+    for key, value in headers:
+
+        vallines = linesplit.split( value )
+        munged.append( '%s: %s' % ( key, join( vallines, '\r\n  ' ) ) )
+
+    return join( munged, '\r\n' )
+
+
 def parseHeadersBody( body, headers=None ):
     """
         Parse any leading 'RFC-822'-ish headers from an uploaded