Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why are python double-quotes converted to hyphen in filename?

I'm generating some pdfs using ReportLab in Django. I followed and experimented with the answer given to this question, and realised that the double-quotes therein don't make sense:

response['Content-Disposition'] = 'inline; filename=constant_"%s_%s".pdf'\
% ('foo','bar')

gives filename constant_-foo_bar-.pdf

response['Content-Disposition'] = 'inline; filename=constant_%s_%s.pdf' \
% ('foo','bar')

gives filename constant_foo_bar.pdf

Why is this? Is it just to do with slug-esque sanitisation for filesystems?

like image 599
nimasmi Avatar asked Nov 14 '22 00:11

nimasmi


1 Answers

It seems from the research in this question that it's actually the browser doing the encoding/escaping. I used cURL to confirm that Django itself does not escape these headers. First, I set up a minimal test view:

# views.py 
def index(request):
    response = render(request, 'template.html')
    response['Content-Disposition'] = 'inline; filename=constant"a_b".html'
    return response

then ran:

carl@chaffinch:~$ HEAD http://localhost:8003
200 OK
Date: Thu, 16 Aug 2012 19:28:54 GMT
Server: WSGIServer/0.1 Python/2.7.3
Vary: Cookie
Content-Type: text/html; charset=utf-8
Client-Date: Thu, 16 Aug 2012 19:28:54 GMT
Client-Peer: 127.0.0.1:8003
Client-Response-Num: 1
Content-Disposition: inline; filename=constant"a_b".html

Check out the header: filename=constant"a_b".html. The quotes are still there!

like image 90
supervacuo Avatar answered Dec 06 '22 21:12

supervacuo