How to serve a non-ASCII file name with Django and Apache using xsendfile

151 Views Asked by At

I wanted to serve protected contents in Django, so I tried to set xsendfile in both Django and Apache. It works for ASCII file names, but when I tried with non-ASCII file name the Apache server responds with 404. What’s going on here?

Django:

def media_xsendfile_pdf(request):
    file = Media.objects.last()
    response = HttpResponse()
    response['Content-Type'] = 'application/pdf'
    response['X-Sendfile'] = smart_str(f"{settings.BASE_DIR}{file.file.url}")
    return response

Apache:

AllowEncodedSlashes On
XSendFile On
XsendFilePath /path-to-the-protected-resource
1

There are 1 best solutions below

0
Gopinath On

I was able to find a solution for this. In the response header, we need set the X-sendfile like below:

from urllib.parse import unquote
response["X-sendfile"] = unquote(<url>).encode('utf-8')