[ZCM] [ZC] 348/ 3 Comment "Method.py / decapitate works only on '\n' as newline"

Collector: Zope Bugs, Features, and Patches ... zope-coders@zope.org
Fri, 19 Apr 2002 02:51:03 -0400


Issue #348 Update (Comment) "Method.py / decapitate works only on '\n' as newline"
 Status Accepted, Zope/bug low
To followup, visit:
  http://collector.zope.org/Zope/348

==============================================================
= Comment - Entry #3 by mjablonski on Apr 19, 2002 2:50 am


Uploaded:  "DTMLMethod.py"
 - http://collector.zope.org/Zope/348/DTMLMethod.py/view
 Here's my patch for the header-problem. tested on linux & win2k 
 for all combinations of "mixed-newlines" (examples):

 Content-type: text/html\r\n\r\n
 Content-type: text/html\n\n
 Content-type: text/html\r\n\n
 Content-type: text/html\n\r\n

 main idea behind the patch: first test for '\r\n', then for '\n'

 ------------------------------------

 def decapitate(html, RESPONSE=None):
    headers = []
    spos  = 0
    eolen = 1
    while 1:
        m = hdr_start(html, spos)
        if not m:
            if html[spos:spos+2] == '\r\n':
                eolen = 2
                break
            if html[spos:spos+1] == '\n':
                eolen = 1
                break
            return html
        header = list(m.groups())
        headers.append(header)
        spos = m.end() + 1
        while spos < len(html) and html[spos] in ' \t':
            eol = find(html, '\r\n', spos)            
            if eol <> -1:
                eolen = 2
            else:
                eol = find(html, '\n', spos)                
                if eol < 0: return html
                eolen = 1
            header.append(strip(html[spos:eol]))
            spos = eol + eolen
    if RESPONSE is not None:
        for header in headers:
            hkey = header.pop(0)
            RESPONSE.setHeader(hkey, ' '.join(header).strip())
    return html[spos + eolen:]
________________________________________
= Accept - Entry #2 by ajung on Apr 18, 2002 9:25 am

 Status: Pending => Accepted

 Supporters added: ajung

Can you provide a patch?

-aj
________________________________________
= Request - Entry #1 by mjablonski on Apr 15, 2002 10:10 am

HTTP-RFC's allow '\r\n' as newline for headers. Zope's Method.py / 'decapitate' parses headers only for '\n'. 

Test:

This script generates two DTMLMethod's with different outputs: the first one as expected, the second one 'fails.'

context.manage_addDTMLMethod("test1","",
"""Content-type: text/html\n\n<h1>Hello</h1>""")

context.manage_addDTMLMethod("test2","",
"""Content-type: text/html\r\n\r\n<h1>Hello</h1>""")

The behavior of 'decapitate' is a problem when you get output from external processors (like 'php' or 'pdf-generators') which return '\r\n'-headers.
==============================================================