imgIfMatch.py (1065B)
1 from cookies.resources import helpers 2 3 def main(request, response): 4 """Respond to `/cookie/imgIfMatch?name={name}&value={value}` with a 404 if 5 the cookie isn't present, and a transparent GIF otherwise.""" 6 headers = helpers.setNoCacheAndCORSHeaders(request, response) 7 name = helpers.readParameter(request, paramName=u"name", requireValue=True) 8 value = helpers.readParameter(request, paramName=u"value", requireValue=True) 9 cookiesWithMatchingNames = request.cookies.get_list(name) 10 for cookie in cookiesWithMatchingNames: 11 if cookie.value == value: 12 # From https://github.com/mathiasbynens/small/blob/master/gif-transparent.gif 13 headers.append((b"Content-Type", b"image/gif")) 14 gif = b"\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xFF\xFF\xFF\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B" 15 return headers, gif 16 return 500, headers, b'{"error": {"message": "The cookie\'s value did not match the given value."}}'