set-cookie header lost on 302 redirect python

604 Views Asked by At

I am using BaseHTTPServer.BaseHTTPRequestHandler in python to handle a redirect request and would like to set a cookie on the redirect. However, whenever I set the header, it is not saved in the browser cookies and therefore crashes my program. Here is how I perform the redirect:

    def redirect(self, destination, urid):
    self.send_response(302)
    self.send_header('Location', destination)

    if urid:
        expires = time.time() + 14 * 24 * 3600 # 14 days from now
        t = time.strftime("%a %d-%b-%Y %T GMT", time.gmtime(expires))
        cookie = Cookie.SimpleCookie()
        cookie['URID'] = str(urid)
        cookie['URID']['path'] = '/'
        cookie['URID']['expires'] = str(t)
        self.send_header('set-cookie', cookie.output(header = ''))

    self.end_headers()
    return None

Here 'urid' is simply supposed to be the cookie value, if it is not present, I shouldn't be setting a cookie. I am using Firefox to test my code, and I heard that this might be a Firefox issue, in that it rejects cookies and writes it's own header for redirects. If that is the case, how to do get around it, and if not, what can I possibly be doing wrong?

0

There are 0 best solutions below