redirect.py (977B)
1 def main(request, response): 2 """Simple handler that causes redirection. 3 4 The request should typically have two query parameters: 5 status - The status to use for the redirection. Defaults to 302. 6 location - The resource to redirect to. 7 8 This utility optionally supports CORS (iff the `enable-cors` query param is 9 present). 10 """ 11 status = 302 12 if b"status" in request.GET: 13 try: 14 status = int(request.GET.first(b"status")) 15 except ValueError: 16 pass 17 18 response.status = status 19 20 location = request.GET.first(b"location") 21 22 response.headers.set(b"Location", location) 23 24 if request.GET.get(b"enable-cors") is not None: 25 origin = request.headers.get(b"Origin") 26 if origin: 27 response.headers.set(b"Content-Type", b"text/plain") 28 response.headers.set(b"Access-Control-Allow-Origin", origin) 29 response.headers.set(b"Access-Control-Allow-Credentials", 'true')