Opened 17 years ago

Last modified 12 years ago

#6137 closed

Dev Web Server Calling View For Each HTTP/1.1 Chunk — at Version 3

Reported by: todd1814@… Owned by: nobody
Component: HTTP handling Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Malcolm Tredinnick)

The development web server (manage.py runserver) calls the view for each chunk request it receives. Django users would logically expect a single call to the view. Attached is an example. If a large file is being returned to the client, the dev web server will chunk it in pieces. The view is called for each piece and Django runs the code in the view each time. Apache doesn't do this with the same test files I'm using.

I only see this problem using IE and I'm only seeing it while transferring mp3 files. If I transfer the entire album in a .zip file then it doesn't happen. I can only guess that the mime type as well as IE plays a part in this. Perhaps Firefox doesn't chunk the data. I've tried watching the results in fiddlertool as well but no further clues.

I would like to at least detect the chunking so I don't run the view code multiple times. Ideally, the framework would detect this situation and prevent calling the view again.

My apologies for pasting code - I'm on a public access computer and can't attach files.

'''
        The view 'song' below is typically called 3 times when serving a file
        This is confirmed when 3 rows are added to the table 'Download'
        The development server will also show 3 calls with a status of 200
'''

def song(request,song_slug):
        if request.user.is_authenticated():
                return AudioFile(request,song_slug)
        else:
                song = models.Song.objects.get(slug=song_slug)
                return HttpResponseRedirect('/login?next=/album/' + song.album.slug)

''' 
        Code below is not located directly in the view but is included here as an example
        Code creates a response containing the contents of an mp3 file and returns it
        Also "logs" what user has downloaded by inserting row in "Download" table
'''

def AudioFile(request,song_slug):
        etag = md5.new("song" + song_slug).hexdigest()
        if request.META.get("HTTP_IF_NONE_MATCH") == etag:
                response = HttpResponseNotModified()
        else:
                song = models.Song.objects.get(slug=song_slug)
                if song:
                        response = HttpResponse(mimetype='audio/mp3')
                        response['Content-Disposition'] = 'filename=' + os.path.split(song.filepath)[1]
                        response['Cache-Control'] = 'public'
                        response['ETag'] = etag

                        size = os.path.getsize(song.filepath)
                        filestream = open(song.filepath,'rb')
                        buffer = filestream.read(size)
                        response.write(buffer)
                        filestream.close()
                        Downloaded(song)
                else:
                        response = HttpResponse()

        return response

def Downloaded(content):
        download = models.Download()
        download.content = content
        download.modifiedon = datetime.datetime.today()
        download.save()
        return

Change History (3)

comment:1 by todd1814 <todd1814@…>, 17 years ago

comment:2 by Simon Greenhill <dev@…>, 17 years ago

Triage Stage: UnreviewedAccepted

Hmm.. can someone confirm/replicate this?

comment:3 by Malcolm Tredinnick, 17 years ago

Description: modified (diff)

What headers is IE sending back on requests after the first one? This sounds like a violation of HTTP, since the server does send headers to say 'continue' requests are permitted.

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