vendor/tramline

changeset 3:5265ba1c57bd gracinet-fix-range

Better handling of filter flush and close. Reduce mem footprint.
author gracinet
date Tue, 10 Feb 2009 13:48:46 +0000
parents dee3a43cd538
children b7abc7431525
files src/tramline/core.py
diffstat 1 files changed, 10 insertions(+), 7 deletions(-) [+]
line diff
     1.1 --- a/src/tramline/core.py
     1.2 +++ b/src/tramline/core.py
     1.3 @@ -11,6 +11,7 @@
     1.4        s = filter.read()
     1.5        while s:
     1.6            filter.write(s)
     1.7 +          filter.flush()
     1.8            s = filter.read()
     1.9      except IOError:
    1.10         s = None 
    1.11 @@ -139,7 +140,6 @@
    1.12      # we're done if we're in a subrequset
    1.13      if filter.req.main is not None:
    1.14          pass_on(filter)
    1.15 -        filter.flush()
    1.16          return
    1.17  
    1.18      # in case of post request, we may need to do a commit/abort
    1.19 @@ -154,7 +154,6 @@
    1.20          return
    1.21      
    1.22      pass_on(filter)
    1.23 -    filter.flush()
    1.24      
    1.25  def outputfilter_post(filter):
    1.26      # get id
    1.27 @@ -163,7 +162,6 @@
    1.28      if id is None:
    1.29          # we're done now, just pass along data
    1.30          pass_on(filter)
    1.31 -        filter.flush()
    1.32          return
    1.33  
    1.34      # reuse existing processor instance based on id
    1.35 @@ -185,13 +183,11 @@
    1.36  
    1.37      # now pass along the data.
    1.38      pass_on(filter)
    1.39 -    filter.flush()
    1.40  
    1.41  def outputfilter_get(filter):
    1.42      # check whether we want to do file serving using tramline
    1.43      if not filter.req.headers_out.has_key('tramline_file'):
    1.44          pass_on(filter)
    1.45 -        filter.flush()
    1.46          return
    1.47  
    1.48      status = filter.req.status
    1.49 @@ -231,6 +227,7 @@
    1.50      f = open(p, 'rb')
    1.51      dump_file_range(f, 0, size-1, filter)
    1.52      f.close()
    1.53 +    filter.close()
    1.54  
    1.55  def serve_ranges(path, ranges, filter, boundary=None):
    1.56      size = os.stat(path).st_size
    1.57 @@ -250,13 +247,15 @@
    1.58               hout['Content-Range'] = 'bytes */%d' % size
    1.59               hout['Content-Length'] = str(size)
    1.60               filter.req.status = 416
    1.61 +             filter.close()
    1.62               return
    1.63  
    1.64  	log("serve_file_ranges: one chunk, %d-%d" % (start, end), filter.req)
    1.65    	hout['Content-Length'] = str(end - start + 1)
    1.66          hout['Content-Range'] = 'bytes %d-%d/%d' % (start, end, size)
    1.67  	dump_file_range(fd, start, end, filter)
    1.68 -	return
    1.69 +        filter.close()
    1.70 +        return	
    1.71  
    1.72      del hout['Content-Range'] # If present, was from app server and can't be ok
    1.73      if boundary is None:
    1.74 @@ -298,7 +297,11 @@
    1.75          filter.write(data)
    1.76          # flush the data out as soon as possible, so we don't
    1.77          # waste memory
    1.78 -        filter.flush()
    1.79 +        try:
    1.80 +	    filter.flush()
    1.81 +	except IOError:
    1.82 +	    # Happens if user cancels download
    1.83 +	    return
    1.84  
    1.85      
    1.86  class ProcessorRegistry: