Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 2047

Show
Ignore:
Timestamp:
09/28/08 02:21:29
Author:
visteya
Message:

Change lib.static.serve_file() to use a file generator for requests with Ranges, rather than read entire range into memory. Closes #859.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/__init__.py

    r2033 r2047  
    144144 
    145145 
     146def file_generator_limited(fileobj, count, chunk_size=65536): 
     147    """Yield the given file object in chunks, stopping after `count` 
     148    bytes has been emitted.  Default chunk size is 64kB. (Core) 
     149    """ 
     150    remaining = count 
     151    while remaining > 0: 
     152        chunk = fileobj.read(min(chunk_size, remaining)) 
     153        chunklen = len(chunk) 
     154        if chunklen == 0: 
     155            return 
     156        remaining -= chunklen 
     157        yield chunk 
    146158 
  • trunk/cherrypy/lib/static.py

    r1975 r2047  
    1111 
    1212import cherrypy 
    13 from cherrypy.lib import cptools, http 
     13from cherrypy.lib import cptools, http, file_generator_limited 
    1414 
    1515 
     
    8484                # Return a single-part response. 
    8585                start, stop = r[0] 
     86                if stop > c_len: 
     87                    stop = c_len 
    8688                r_len = stop - start 
    8789                response.status = "206 Partial Content" 
     
    9092                response.headers['Content-Length'] = r_len 
    9193                bodyfile.seek(start) 
    92                 response.body = bodyfile.read(r_len) 
     94                response.body = file_generator_limited(bodyfile, r_len) 
    9395            else: 
    9496                # Return a multipart/byteranges response. 
     
    112114                               % (start, stop - 1, c_len)) 
    113115                        bodyfile.seek(start) 
    114                         yield bodyfile.read(stop - start) 
     116                        for chunk in file_generator_limited(bodyfile, stop-start): 
     117                            yield chunk 
    115118                        yield "\r\n" 
    116119                    # Final boundary 

Hosted by WebFaction

Log in as guest/cpguest to create tickets