Opened 16 years ago

Closed 13 years ago

Last modified 13 years ago

#7535 closed (fixed)

django.views.static.serve does not properly set Content-Encoding header

Reported by: Kevin Hunter <hunteke@…> Owned by: nobody
Component: Generic views Version: dev
Severity: Keywords: content-encoding static view development manage.py
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Amidst trying to gzip a javascript file, I noted that the interaction between my browser (FF3/Linux) and the manage.py server only showed me the compressed version of the requested file. I believe it's due to not properly setting the Content-Encoding header.

This patch adds the Content-Encoding header to django.views.static.serve as returned by mimetypes.guess_type .

Attachments (3)

static.py.patch (905 bytes ) - added by Kevin Hunter <hunteke@… 16 years ago.
Content-Encoding patch for django.views.static.serve
static.py.2.patch (936 bytes ) - added by Kevin Hunter <hunteke@…> 16 years ago.
Content-Encoding patch for django.views.static.serve, revision 2
static.py.3.patch (907 bytes ) - added by Kevin Hunter <hunteke@…> 16 years ago.
Content-Encoding patch for django.views.static.serve, revision 3, hopefully final

Download all attachments as: .zip

Change History (15)

by Kevin Hunter <hunteke@…, 16 years ago

Attachment: static.py.patch added

Content-Encoding patch for django.views.static.serve

comment:1 by edgarsj, 16 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Bastian Kleineidam <calvin@…>, 16 years ago

Patch needs improvement: set

The patch will break if mimetypes cannot guess the content type or the encoding. Specifically this expression:

mimetypes.guess_type(fullpath) or mimetype

will never use the second mimetype variable since .guess_type() returns a 2-tuple which has a boolean value of True.

It is easier (albeit a little longer) to check both content-type and -encoding separately for None and use appropriate fallbacks.

by Kevin Hunter <hunteke@…>, 16 years ago

Attachment: static.py.2.patch added

Content-Encoding patch for django.views.static.serve, revision 2

comment:3 by Kevin Hunter <hunteke@…>, 16 years ago

Heh, length I am not worried about; I'm just learning. For whatever reason, mimetypes and encodings confuse me. In any event, I was attempting to follow the "compressed" look of the code.

Does this second revision of the patch work then?

comment:4 by Bastian Kleineidam <calvin@…>, 16 years ago

The patch looks ok. You can shorten the code a bit by assigning a tuple to multiple variables:

mimetype, encoding = mimetypes.guess_type(fullpath) 
if mimetype is None:
    mimetype = 'application/octet-stream'

in reply to:  4 comment:5 by Kevin Hunter <hunteke@…>, 16 years ago

Ah cool, and even one more line?
mimetype = mimetype or 'application/octet-stream'

comment:6 by Bastian Kleineidam <calvin@…>, 16 years ago

Yes, the "or" expression would save another line :-)

comment:7 by Kevin Hunter <hunteke@…>, 16 years ago

Is this patch good then? I note the "Patch needs improvement" bit is still set so is there anything else I need to do while this is still fresh in my mind? Should I submit .3 of the patch with minification discussed?

comment:8 by Bastian Kleineidam <calvin@…>, 16 years ago

Yes, add a .3 patch (or replace the .2 one) and delete the improvement checkbox. Thanks for the work.

by Kevin Hunter <hunteke@…>, 16 years ago

Attachment: static.py.3.patch added

Content-Encoding patch for django.views.static.serve, revision 3, hopefully final

in reply to:  8 comment:9 by Kevin Hunter <hunteke@…>, 16 years ago

Patch needs improvement: unset

Alright, thar she blows. v3, hopefully final patch, added. Pleasure doin' business with ya.

comment:10 by Adam Nelson, 14 years ago

Patch needs improvement: set

Patch needs to be ported to current version of source:/django/trunk/django/views/static.py.

comment:11 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: newclosed

(In [13868]) Fixed #7535 -- Correctly set Content-Encoding header in static files serving view. Thanks for the report and patch, Kevin Hunter.

comment:12 by Jannis Leidel, 13 years ago

(In [13869]) [1.2.X] Fixed #7535 -- Correctly set Content-Encoding header in static files serving view. Thanks for the report and patch, Kevin Hunter.

Backport from trunk (r13868).

Note: See TracTickets for help on using tickets.
Back to Top