﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
6137	Dev Web Server Calling View For Each HTTP/1.1 Chunk	todd1814@…	nobody	"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.

{{{
#!python
'''
	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

}}}
"		new	HTTP handling	dev					Accepted	0	0	0	0		
