in bedrock/firefox/views.py [0:0]
def stub_attribution_code(request):
"""Return a JSON response containing the HMAC signed stub attribution value"""
if not request.headers.get("x-requested-with") == "XMLHttpRequest":
return JsonResponse({"error": "Resource only available via XHR"}, status=400)
response = None
if not settings.STUB_ATTRIBUTION_RATE:
# return as though it was rate limited, since it was
response = JsonResponse({"error": "rate limited"}, status=429)
elif not settings.STUB_ATTRIBUTION_HMAC_KEY:
response = JsonResponse({"error": "service not configured"}, status=403)
if response:
patch_response_headers(response, 300) # 5 min
return response
data = request.GET
codes = OrderedDict()
has_value = False
for name, default_value in STUB_VALUE_NAMES:
val = data.get(name, "")
# remove utm_
if name.startswith("utm_"):
name = name[4:]
if val and STUB_VALUE_RE.match(val):
codes[name] = val
has_value = True
else:
codes[name] = default_value
if codes["source"] == "(not set)" and "referrer" in data:
try:
domain = urlparse(data["referrer"]).netloc
if domain and STUB_VALUE_RE.match(domain):
codes["source"] = domain
codes["medium"] = "referral"
has_value = True
except Exception:
# any problems and we should just ignore it
pass
if not has_value:
codes["source"] = "www.mozilla.org"
codes["medium"] = "(none)"
code_data = sign_attribution_codes(codes)
if code_data:
response = JsonResponse(code_data)
else:
response = JsonResponse({"error": "Invalid code"}, status=400)
patch_response_headers(response, 300) # 5 min
return response