'Transform headers (and cookies) into self.header_list. (Core)'

def finalize(self):

    try:
        (code, reason, _) = httputil.valid_status(self.status)
    except ValueError:
        raise cherrypy.HTTPError(500, sys.exc_info()[1].args[0])
    headers = self.headers
    self.status = ('%s    %s' % (code, reason))
    self.output_status = ((ntob(str(code), 'ascii') + ntob('    ')) + headers.encode(reason))
    if self.stream:
        if (dict.get(headers, 'Content-Length') is None):
            dict.pop(headers, 'Content-Length', None)
    elif ((code < 200) or (code in (204, 205, 304))):
        dict.pop(headers, 'Content-Length', None)
        self.body = ntob('')
    elif (dict.get(headers, 'Content-Length') is None):
        content = self.collapse_body()
        dict.__setitem__(headers, 'Content-Length', len(content))
    self.header_list = h = headers.output()
    cookie = self.cookie.output()
    if cookie:
        for line in cookie.split('\n'):
            if line.endswith('\r'):
                line = line[:(-1)]
            (name, value) = line.split(':    ', 1)
            if isinstance(name, unicodestr):
                name = name.encode('ISO-8859-1')
            if isinstance(value, unicodestr):
                value = headers.encode(value)
            h.append((name, value))
