Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a referring URL via a custom HTTP header?

I am currently using the following function to get a referring view:

def get_referer_view(request, default=None):   
    referer = request.META.get('HTTP_REFERER')
    if not referer:
        return default

    # remove the protocol and split the url at the slashes
    referer = re.sub('^https?:\/\/', '', referer).split('/')
    if referer[0] != request.META.get('SERVER_NAME'):
        return default

    # add the slash at the relative path's view and finished
    referer = u'/' + u'/'.join(referer[1:])
    return referer

If I redirected the view as a result of programmatic logic, e.g...

return HttpResponseRedirect('dashboard')

...is there a way to get the referring view without using HTTP_REFERER so that I can use that variable in the redirected view? This is not always set in the headers of the browser.

Note because the views are redirected pro grammatically, I can't use POST to collect the data.

Perhaps its possible to set and retrieve a custom header somehow?

like image 838
alias51 Avatar asked Jan 10 '20 20:01

alias51


People also ask

How do I get a referrer URL?

$_SERVER['HTTP_REFERER'] will give you the referrer page's URL if there exists any. If users use a bookmark or directly visit your site by manually typing in the URL, http_referer will be empty.

Can you set Referer header?

You cannot set Referer header manually but you can use location. href to set the referer header to the link used in href but it will cause reloading of the page.

What is referrer in HTTP header?

The Referer header allows a server to identify a page where people are visiting it from. This data can be used for analytics, logging, optimized caching, and more. When you follow a link, the Referer contains the address of the page that owns the link.

Can request URL be spoofed?

Yes, the HTTP referer header can be spoofed. A common way to play with HTTP headers is to use a tool like cURL: Sending headers using cURL: How to send a header using a HTTP request through a curl call?


1 Answers

Use Django's middleware component.

https://docs.djangoproject.com/en/3.0/topics/http/middleware/

Something like this should work:

class HTTPReferer:

    def __init__(self, get_response):
        self.get_response = get_response

def __call__old(self, request):
    # old
    referer = request.META.get('HTTP_REFERER', None)
    request.referer = referer
    # other business logic as you like it
    response = self.get_response(request)
    return response

def __call__(self, request):
    # reflecting last edit
    path = request.path
    response = self.get_response(request)
    response['previous_path'] = path
    return response

So you could tie any information you need to every request/response cycle in Django (you could also set custom headers, etc...)

In the example above HTTP_REFERER will be available in request object as referer.

EDIT: I think, you concern is that HTTP_REFERER is not always populated by the client; so you could tie HttpRequest.path to every request made to a custom header. If path is not enough, you could save the request args too. That's all, I think. Then you have a custom header populated by the last path. Further on, if this is not enough, you could use Django's URL resolver.

like image 101
jazz Avatar answered Sep 20 '22 04:09

jazz