OLD | NEW |
(Empty) | |
| 1 from urllib import urlencode |
| 2 from urlparse import urlparse |
| 3 |
| 4 def main(request, response): |
| 5 stashed_data = {'count': 0, 'preflight': "0"} |
| 6 status = 302 |
| 7 headers = [("Content-Type", "text/plain"), |
| 8 ("Cache-Control", "no-cache"), |
| 9 ("Pragma", "no-cache"), |
| 10 ("Access-Control-Allow-Origin", "*")] |
| 11 token = None |
| 12 |
| 13 if "token" in request.GET: |
| 14 token = request.GET.first("token") |
| 15 data = request.server.stash.take(token) |
| 16 if data: |
| 17 stashed_data = data |
| 18 |
| 19 if request.method == "OPTIONS": |
| 20 if "allow_headers" in request.GET: |
| 21 headers.append(("Access-Control-Allow-Headers", request.GET['allow_h
eaders'])) |
| 22 stashed_data['preflight'] = "1" |
| 23 #Preflight is not redirected: return 200 |
| 24 if not "redirect_preflight" in request.GET: |
| 25 if token: |
| 26 request.server.stash.put(request.GET.first("token"), stashed_data) |
| 27 return 200, headers, "" |
| 28 |
| 29 if "redirect_status" in request.GET: |
| 30 status = int(request.GET['redirect_status']) |
| 31 |
| 32 stashed_data['count'] += 1 |
| 33 |
| 34 if "location" in request.GET: |
| 35 url = request.GET['location'] |
| 36 scheme = urlparse(url).scheme |
| 37 if scheme == "" or scheme == "http" or scheme == "https": |
| 38 url += "&" if '?' in url else "?" |
| 39 #keep url parameters in location |
| 40 url_parameters = {} |
| 41 for item in request.GET.items(): |
| 42 url_parameters[item[0]] = item[1][0] |
| 43 url += urlencode(url_parameters) |
| 44 #make sure location changes during redirection loop |
| 45 url += "&count=" + str(stashed_data['count']) |
| 46 headers.append(("Location", url)) |
| 47 |
| 48 if "redirect_referrerpolicy" in request.GET: |
| 49 headers.append(("Referrer-Policy", request.GET['redirect_referrerpolicy'
])) |
| 50 |
| 51 if token: |
| 52 request.server.stash.put(request.GET.first("token"), stashed_data) |
| 53 if "max_count" in request.GET: |
| 54 max_count = int(request.GET['max_count']) |
| 55 #stop redirecting and return count |
| 56 if stashed_data['count'] > max_count: |
| 57 # -1 because the last is not a redirection |
| 58 return str(stashed_data['count'] - 1) |
| 59 |
| 60 return status, headers, "" |
OLD | NEW |