Opened 17 years ago
Closed 12 years ago
#6543 closed Bug (duplicate)
serving files/iterated content with setting.USE_ETAGS = True
Reported by: | pcicman | Owned by: | nobody |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Normal | Keywords: | USE_ETAGS etag file zip md5 iterator |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
It seems there's a bug in django.core.servers.basehttp.FileWrapper. FileWraper should do seek(0) before new interation.
Problem comes only with using Etags and this line (in django.middleware.common.CommonMiddleware):
etag = md5.new(response.content).hexdigest()
this calls code in HttpResponse object:
def _get_content(self): if self.has_header('Content-Encoding'): return ''.join(self._container) return smart_str(''.join(self._container), self._charset)
and join iterates over FileWrapper object:
def next(self): data = self.filelike.read(self.blksize) if data: return data raise StopIteration
but there's no return back to file begining. md5 Makes first iteration, and next iteration is called when response gets converted to string and set back to client.
Solution can probably be:
def next(self): data = self.filelike.read(self.blksize) if data: return data self.filelike.seek(0) raise StopIteration
Better solution
Add some temporary cache variable to HttpResponse object, so content gets iterated only once (i think this can be much more faster solution, shorter exec time).
In this case there's no seek(0) required.
Attachments (1)
Change History (8)
comment:1 by , 17 years ago
comment:2 by , 17 years ago
Has patch: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
I understand this problem. I wrote a patch which checks first, if
the filelike object has a seek method.
Is FileWrapper documented somewhere? It is the first time I heard of it.
by , 17 years ago
comment:3 by , 17 years ago
See bug #6527, which has a patch for iterating only once. In fact, it may make sense to mark this bug as a duplicate of that one.
comment:4 by , 14 years ago
Type: | → Bug |
---|
comment:5 by , 14 years ago
Easy pickings: | unset |
---|---|
Needs tests: | set |
Severity: | → Normal |
comment:7 by , 12 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
This is a duplicate of #6027.
Wrong indentation used in solution :S