set-cookie.py (1368B)
1 from datetime import date 2 3 def main(request, response): 4 """ 5 Returns cookie name and path from query params in a Set-Cookie header. 6 7 e.g. 8 9 > GET /cookies/resources/set-cookie.py?name=match-slash&path=%2F HTTP/1.1 10 > Host: localhost:8000 11 > User-Agent: curl/7.43.0 12 > Accept: */* 13 > 14 < HTTP/1.1 200 OK 15 < Content-Type: application/json 16 < Set-Cookie: match-slash=1; Path=/; Expires=09 Jun 2021 10:18:14 GMT 17 < Server: BaseHTTP/0.3 Python/2.7.12 18 < Date: Tue, 04 Oct 2016 18:16:06 GMT 19 < Content-Length: 80 20 """ 21 22 name = request.GET[b'name'] 23 path = request.GET[b'path'] 24 samesite = request.GET.get(b'samesite') 25 secure = b'secure' in request.GET 26 expiry_year = date.today().year + 1 27 cookie = b"%s=1; Path=%s; Expires=09 Jun %d 10:18:14 GMT" % (name, path, expiry_year) 28 if samesite: 29 cookie += b";SameSite=%s" % samesite 30 if secure: 31 cookie += b";Secure" 32 33 headers = [ 34 (b"Content-Type", b"application/json"), 35 (b"Set-Cookie", cookie) 36 ] 37 38 # Set the cors enabled headers. 39 origin = request.headers.get(b"Origin") 40 if origin is not None and origin != b"null": 41 headers.append((b"Access-Control-Allow-Origin", origin)) 42 headers.append((b"Access-Control-Allow-Credentials", 'true')) 43 44 body = b"var dummy='value';" 45 return headers, body