Opened 7 years ago

Closed 3 years ago

#6137 closed Bug (worksforme)

Dev Web Server Calling View For Each HTTP/1.1 Chunk

Reported by: todd1814@… Owned by: nobody
Component: HTTP handling Version: master
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 mtredinnick)

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 (7)

comment:1 Changed 7 years ago by todd1814 <todd1814@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by Simon Greenhill <dev@…>

  • Triage Stage changed from Unreviewed to Accepted

Hmm.. can someone confirm/replicate this?

comment:3 Changed 7 years ago by mtredinnick

  • 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.

comment:4 Changed 4 years ago by gabrielhurley

  • Severity set to Normal
  • Type set to Bug

comment:5 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:6 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:7 Changed 3 years ago by claudep

  • Resolution set to worksforme
  • Status changed from new to closed

5 years without feedback, we can assume this does not affect people (or might affect an obsolete IE version). Feel free to reopen if you have any new data to provide.

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