0

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gopinath
  • 56
  • 5

1 Answers1

1

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')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gopinath
  • 56
  • 5