import mimetypes import os import pathlib from . import hdrs from .helpers import set_exception, set_result from .http_writer import StreamWriter from .log import server_logger from .web_exceptions import (HTTPNotModified, HTTPOk, HTTPPartialContent, HTTPRequestRangeNotSatisfiable) from .web_response import StreamResponse __all__ = ('FileResponse',) NOSENDFILE = bool(os.environ.get("AIOHTTP_NOSENDFILE")) class SendfileStreamWriter(StreamWriter): def __init__(self, *args, **kwargs): self._sendfile_buffer = [] super().__init__(*args, **kwargs) def _write(self, chunk): # we overwrite StreamWriter._write, so nothing can be appended to # _buffer, and nothing is written to the transport directly by the # parent class self.output_size += len(chunk) self._sendfile_buffer.append(chunk) def _sendfile_cb(self, fut, out_fd, in_fd, offset, count, loop, registered): if registered: loop.remove_writer(out_fd) if fut.cancelled(): return try: n = os.sendfile(out_fd, in_fd, offset, count) if n == 0: # EOF reached n = count except (BlockingIOError, InterruptedError): n = 0 except Exception as exc: set_exception(fut, exc) return if n < count: loop.add_writer(out_fd, self._sendfile_cb, fut, out_fd, in_fd, offset + n, count - n, loop, True) else: set_result(fut, None) async def sendfile(self, fobj, count): out_socket = self.transport.get_extra_info('socket').dup() out_socket.setblocking(False) out_fd = out_socket.fileno() in_fd = fobj.fileno() offset = fobj.tell() loop = self.loop data = b''.join(self._sendfile_buffer) try: await loop.sock_sendall(out_socket, data) fut = loop.create_future() self._sendfile_cb(fut, out_fd, in_fd, offset, count, loop, False) await fut except Exception: server_logger.debug('Socket error') self.transport.close() finally: out_socket.close() self.output_size += count await super().write_eof() async def write_eof(self, chunk=b''): pass class FileResponse(StreamResponse): """A response object can be used to send files.""" def __init__(self, path, chunk_size=256*1024, *args, **kwargs): super().__init__(*args, **kwargs) if isinstance(path, str): path = pathlib.Path(path) self._path = path self._chunk_size = chunk_size async def _sendfile_system(self, request, fobj, count): # Write count bytes of fobj to resp using # the os.sendfile system call. # # For details check # https://github.com/KeepSafe/aiohttp/issues/1177 # See https://github.com/KeepSafe/aiohttp/issues/958 for details # # request should be a aiohttp.web.Request instance. # fobj should be an open file object. # count should be an integer > 0. transport = request.transport if (transport.get_extra_info("sslcontext") or transport.get_extra_info("socket") is None): writer = await self._sendfile_fallback(request, fobj, count) else: writer = SendfileStreamWriter( request.protocol, transport, request.loop ) request._payload_writer = writer await super().prepare(request) await writer.sendfile(fobj, count) return writer async def _sendfile_fallback(self, request, fobj, count): # Mimic the _sendfile_system() method, but without using the # os.sendfile() system call. This should be used on systems # that don't support the os.sendfile(). # To avoid blocking the event loop & to keep memory usage low, # fobj is transferred in chunks controlled by the # constructor's chunk_size argument. writer = (await super().prepare(request)) chunk_size = self._chunk_size chunk = fobj.read(chunk_size) while True: await writer.write(chunk) count = count - chunk_size if count <= 0: break chunk = fobj.read(min(chunk_size, count)) await writer.drain() return writer if hasattr(os, "sendfile") and not NOSENDFILE: # pragma: no cover _sendfile = _sendfile_system else: # pragma: no cover _sendfile = _sendfile_fallback async def prepare(self, request): filepath = self._path gzip = False if 'gzip' in request.headers.get(hdrs.ACCEPT_ENCODING, ''): gzip_path = filepath.with_name(filepath.name + '.gz') if gzip_path.is_file(): filepath = gzip_path gzip = True st = filepath.stat() modsince = request.if_modified_since if modsince is not None and st.st_mtime <= modsince.timestamp(): self.set_status(HTTPNotModified.status_code) self._length_check = False return await super().prepare(request) if hdrs.CONTENT_TYPE not in self.headers: ct, encoding = mimetypes.guess_type(str(filepath)) if not ct: ct = 'application/octet-stream' should_set_ct = True else: encoding = 'gzip' if gzip else None should_set_ct = False status = HTTPOk.status_code file_size = st.st_size count = file_size try: rng = request.http_range start = rng.start end = rng.stop except ValueError: self.set_status(HTTPRequestRangeNotSatisfiable.status_code) return await super().prepare(request) # If a range request has been made, convert start, end slice notation # into file pointer offset and count if start is not None or end is not None: if start is None and end < 0: # return tail of file start = file_size + end count = -end else: count = (end or file_size) - start if start + count > file_size: # rfc7233:If the last-byte-pos value is # absent, or if the value is greater than or equal to # the current length of the representation data, # the byte range is interpreted as the remainder # of the representation (i.e., the server replaces the # value of last-byte-pos with a value that is one less than # the current length of the selected representation). count = file_size - start if start >= file_size: count = 0 if count != file_size: status = HTTPPartialContent.status_code self.set_status(status) if should_set_ct: self.content_type = ct if encoding: self.headers[hdrs.CONTENT_ENCODING] = encoding if gzip: self.headers[hdrs.VARY] = hdrs.ACCEPT_ENCODING self.last_modified = st.st_mtime self.content_length = count if count: with filepath.open('rb') as fobj: if start: fobj.seek(start) return await self._sendfile(request, fobj, count) return await super().prepare(request)
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
__init__.py | File | 1.35 KB | 0644 |
|
_cparser.pxd | File | 3.87 KB | 0644 |
|
_frozenlist.c | File | 279.05 KB | 0644 |
|
_frozenlist.cpython-35m-x86_64-linux-gnu.so | File | 63.73 KB | 0755 |
|
_frozenlist.pyx | File | 2.54 KB | 0644 |
|
_http_parser.c | File | 592.13 KB | 0644 |
|
_http_parser.cpython-35m-x86_64-linux-gnu.so | File | 155.33 KB | 0755 |
|
_http_parser.pyx | File | 20.6 KB | 0644 |
|
_websocket.c | File | 125.34 KB | 0644 |
|
_websocket.cpython-35m-x86_64-linux-gnu.so | File | 24.38 KB | 0755 |
|
_websocket.pyx | File | 1.52 KB | 0644 |
|
abc.py | File | 3.31 KB | 0644 |
|
client.py | File | 31.81 KB | 0644 |
|
client_exceptions.py | File | 5.28 KB | 0644 |
|
client_proto.py | File | 5.74 KB | 0644 |
|
client_reqrep.py | File | 27.95 KB | 0644 |
|
client_ws.py | File | 8.69 KB | 0644 |
|
connector.py | File | 31.81 KB | 0644 |
|
cookiejar.py | File | 9.99 KB | 0644 |
|
formdata.py | File | 5.22 KB | 0644 |
|
frozenlist.py | File | 1.73 KB | 0644 |
|
hdrs.py | File | 3.28 KB | 0644 |
|
helpers.py | File | 22.36 KB | 0644 |
|
http.py | File | 1.26 KB | 0644 |
|
http_exceptions.py | File | 1.93 KB | 0644 |
|
http_parser.py | File | 23.77 KB | 0644 |
|
http_websocket.py | File | 21.49 KB | 0644 |
|
http_writer.py | File | 3.91 KB | 0644 |
|
locks.py | File | 946 B | 0644 |
|
log.py | File | 326 B | 0644 |
|
multipart.py | File | 28.89 KB | 0644 |
|
payload.py | File | 8.2 KB | 0644 |
|
payload_streamer.py | File | 1.49 KB | 0644 |
|
pytest_plugin.py | File | 9.54 KB | 0644 |
|
resolver.py | File | 3.23 KB | 0644 |
|
signals.py | File | 933 B | 0644 |
|
streams.py | File | 16.31 KB | 0644 |
|
tcp_helpers.py | File | 1.38 KB | 0644 |
|
test_utils.py | File | 15.75 KB | 0644 |
|
tracing.py | File | 9.32 KB | 0644 |
|
web.py | File | 7.03 KB | 0644 |
|
web_app.py | File | 9.88 KB | 0644 |
|
web_exceptions.py | File | 8.38 KB | 0644 |
|
web_fileresponse.py | File | 7.51 KB | 0644 |
|
web_middlewares.py | File | 2.6 KB | 0644 |
|
web_protocol.py | File | 17.57 KB | 0644 |
|
web_request.py | File | 20.42 KB | 0644 |
|
web_response.py | File | 20.97 KB | 0644 |
|
web_runner.py | File | 8.05 KB | 0644 |
|
web_server.py | File | 1.29 KB | 0644 |
|
web_urldispatcher.py | File | 32.67 KB | 0644 |
|
web_ws.py | File | 14.34 KB | 0644 |
|
worker.py | File | 6.92 KB | 0644 |
|