python - How to verify HttpResponseRedirect is to an external site? -
i'm using following middleware snippet:
class ajaxredirect(object): """ instead of returning 302 ajax requests, instead should return 200 location redirect jquery can forward user needed. """ def process_response(self, request, response): if request.is_ajax(): if type(response) == httpresponseredirect: logger.warn(response.__dict__) redirect = '%s&ajaxredirect=1' % response['location'] logger.info('returned ajaxredirect %s' % redirect) return httpresponse( json.dumps( {'redirect': redirect}, indent=4, sort_keys=false), content_type='application/json; charset=utf-8'); return response however if redirect external site forego query string parameter. there reliable way find information given have both httprequest , httpresponseredirect instances?
solution
add following check:
if request.get_host() in redirect or not redirect.startswith('http'): redirect = '%s&ajaxredirect=1' % redirect
not sure if it's best option, can compare request.get_host() response['location'] (or response.url in django dev version).
hope helps.
Comments
Post a Comment