OLD | NEW |
(Empty) | |
| 1 """CherryPy Library""" |
| 2 |
| 3 # Deprecated in CherryPy 3.2 -- remove in CherryPy 3.3 |
| 4 from cherrypy.lib.reprconf import unrepr, modules, attributes |
| 5 |
| 6 class file_generator(object): |
| 7 """Yield the given input (a file object) in chunks (default 64k). (Core)""" |
| 8 |
| 9 def __init__(self, input, chunkSize=65536): |
| 10 self.input = input |
| 11 self.chunkSize = chunkSize |
| 12 |
| 13 def __iter__(self): |
| 14 return self |
| 15 |
| 16 def __next__(self): |
| 17 chunk = self.input.read(self.chunkSize) |
| 18 if chunk: |
| 19 return chunk |
| 20 else: |
| 21 if hasattr(self.input, 'close'): |
| 22 self.input.close() |
| 23 raise StopIteration() |
| 24 next = __next__ |
| 25 |
| 26 def file_generator_limited(fileobj, count, chunk_size=65536): |
| 27 """Yield the given file object in chunks, stopping after `count` |
| 28 bytes has been emitted. Default chunk size is 64kB. (Core) |
| 29 """ |
| 30 remaining = count |
| 31 while remaining > 0: |
| 32 chunk = fileobj.read(min(chunk_size, remaining)) |
| 33 chunklen = len(chunk) |
| 34 if chunklen == 0: |
| 35 return |
| 36 remaining -= chunklen |
| 37 yield chunk |
| 38 |
| 39 def set_vary_header(response, header_name): |
| 40 "Add a Vary header to a response" |
| 41 varies = response.headers.get("Vary", "") |
| 42 varies = [x.strip() for x in varies.split(",") if x.strip()] |
| 43 if header_name not in varies: |
| 44 varies.append(header_name) |
| 45 response.headers['Vary'] = ", ".join(varies) |
OLD | NEW |