def encode_response(self, status, headers, exc_info=None):
        """
        Zappa-ify our application response!

        This means:
            - Updating any existing cookies.
            - Packing all our cookies into a single ZappaCookie.
            - Injecting redirect HTML if setting a Cookie on a redirect.

        """

        # All the non-cookie headers should be sent unharmed.
        new_headers = [(header[0], header[1]) for header in headers if header[0] != 'Set-Cookie']

        # Filter the headers for Set-Cookie header
        cookie_dicts = [{header[1].split('=', 1)[0].strip():header[1].split('=',1)[1:][0]} for header in headers if header[0] == 'Set-Cookie']
        
        # Flatten cookies_dicts to one dict. If there are multiple occuring
        # cookies, the last one present in the headers wins.
        new_cookies = dict()
        map(new_cookies.update, cookie_dicts)

        # Update request_cookies with cookies from the response.
        for name, value in new_cookies.items():
            self.request_cookies[name] = value

        # Get the oldest expire time, and set the Zappa cookie age to that.
        # Else, let this be a session cookie.
        expires = None
        for name, value in self.request_cookies.items():
            # Is this Unicode safe?
            cookie = (name + '=' + value).encode('utf-8')
            if cookie.count('=') is 1:
                continue

            kvps = cookie.split(';')
            for kvp in kvps:
                kvp = kvp.strip()
                if 'expires' in kvp.lower():
                    exp = time.strptime(kvp.split('=')[1], "%a, %d-%b-%Y %H:%M:%S GMT")
                    if exp > expires:
                        expires = exp
                    break

        # JSON-ify the cookie and encode it.
        pack_s = json.dumps(self.request_cookies)
        encoded = base58.b58encode(pack_s)
        
        # Set the result as the zappa cookie
        new_headers.append(
            ('Set-Cookie', dump_cookie('zappa', 
                        value=encoded, 
                        expires=expires)
            )
        )

        # If setting cookie on a 301/2,
        # return 200 and replace the content with a javascript redirector
        if status != '200 OK':
            for key, value in new_headers:
                if key != 'Location':
                    continue
                self.redirect_content = REDIRECT_HTML.replace('REDIRECT_ME', value)
                status = '200 OK'
                break

        return self.start_response(status, new_headers, exc_info)
dataset/ETHPy150Open Miserlou/Zappa/zappa/middleware.py/ZappaWSGIMiddleware.encode_response/original