#!/usr/bin/python
import os, re, time, sys

def GetCookie(field):
    try:
        cookies = re.compile('([=;])').split(os.environ['HTTP_COOKIE'])
        for i in range(len(cookies)-2):
            if cookies[i] == field and cookies[i+1] == '=':
                return cookies[i+2]
    except KeyError:
        pass
    return None

def SetCookie(field, value, path, exp=''):
    print 'Set-Cookie: %s=%s;path=%s;' % (field, value, path),
    if len(exp):
        print 'expires=%s' % exp
    else:
        print '\n',

def ExpireCookie(field, value, path):
    t = list(time.gmtime(time.time()))
    t[0] = t[0]-1
    SetCookie(field, value, path, 
        time.strftime('%d-%m-%Y 00:00:00 GMT', tuple(t))) 

def SetCookieForYear(field, value, path):
    t = list(time.gmtime(time.time()))
    t[0] = t[0]+1
    SetCookie(field, value, path, 
        time.strftime('%d-%m-%Y 00:00:00 GMT', tuple(t))) 
