You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
10 KiB

4 years ago
  1. import mimetypes
  2. import os
  3. import pathlib
  4. from . import hdrs
  5. from .helpers import set_exception, set_result
  6. from .http_writer import StreamWriter
  7. from .log import server_logger
  8. from .web_exceptions import (HTTPNotModified, HTTPOk, HTTPPartialContent,
  9. HTTPPreconditionFailed,
  10. HTTPRequestRangeNotSatisfiable)
  11. from .web_response import StreamResponse
  12. __all__ = ('FileResponse',)
  13. NOSENDFILE = bool(os.environ.get("AIOHTTP_NOSENDFILE"))
  14. class SendfileStreamWriter(StreamWriter):
  15. def __init__(self, *args, **kwargs):
  16. self._sendfile_buffer = []
  17. super().__init__(*args, **kwargs)
  18. def _write(self, chunk):
  19. # we overwrite StreamWriter._write, so nothing can be appended to
  20. # _buffer, and nothing is written to the transport directly by the
  21. # parent class
  22. self.output_size += len(chunk)
  23. self._sendfile_buffer.append(chunk)
  24. def _sendfile_cb(self, fut, out_fd, in_fd,
  25. offset, count, loop, registered):
  26. if registered:
  27. loop.remove_writer(out_fd)
  28. if fut.cancelled():
  29. return
  30. try:
  31. n = os.sendfile(out_fd, in_fd, offset, count)
  32. if n == 0: # EOF reached
  33. n = count
  34. except (BlockingIOError, InterruptedError):
  35. n = 0
  36. except Exception as exc:
  37. set_exception(fut, exc)
  38. return
  39. if n < count:
  40. loop.add_writer(out_fd, self._sendfile_cb, fut, out_fd, in_fd,
  41. offset + n, count - n, loop, True)
  42. else:
  43. set_result(fut, None)
  44. async def sendfile(self, fobj, count):
  45. out_socket = self.transport.get_extra_info('socket').dup()
  46. out_socket.setblocking(False)
  47. out_fd = out_socket.fileno()
  48. in_fd = fobj.fileno()
  49. offset = fobj.tell()
  50. loop = self.loop
  51. data = b''.join(self._sendfile_buffer)
  52. try:
  53. await loop.sock_sendall(out_socket, data)
  54. fut = loop.create_future()
  55. self._sendfile_cb(fut, out_fd, in_fd, offset, count, loop, False)
  56. await fut
  57. except Exception:
  58. server_logger.debug('Socket error')
  59. self.transport.close()
  60. finally:
  61. out_socket.close()
  62. self.output_size += count
  63. await super().write_eof()
  64. async def write_eof(self, chunk=b''):
  65. pass
  66. class FileResponse(StreamResponse):
  67. """A response object can be used to send files."""
  68. def __init__(self, path, chunk_size=256*1024, *args, **kwargs):
  69. super().__init__(*args, **kwargs)
  70. if isinstance(path, str):
  71. path = pathlib.Path(path)
  72. self._path = path
  73. self._chunk_size = chunk_size
  74. async def _sendfile_system(self, request, fobj, count):
  75. # Write count bytes of fobj to resp using
  76. # the os.sendfile system call.
  77. #
  78. # For details check
  79. # https://github.com/KeepSafe/aiohttp/issues/1177
  80. # See https://github.com/KeepSafe/aiohttp/issues/958 for details
  81. #
  82. # request should be a aiohttp.web.Request instance.
  83. # fobj should be an open file object.
  84. # count should be an integer > 0.
  85. transport = request.transport
  86. if (transport.get_extra_info("sslcontext") or
  87. transport.get_extra_info("socket") is None or
  88. self.compression):
  89. writer = await self._sendfile_fallback(request, fobj, count)
  90. else:
  91. writer = SendfileStreamWriter(
  92. request.protocol,
  93. request.loop
  94. )
  95. request._payload_writer = writer
  96. await super().prepare(request)
  97. await writer.sendfile(fobj, count)
  98. return writer
  99. async def _sendfile_fallback(self, request, fobj, count):
  100. # Mimic the _sendfile_system() method, but without using the
  101. # os.sendfile() system call. This should be used on systems
  102. # that don't support the os.sendfile().
  103. # To avoid blocking the event loop & to keep memory usage low,
  104. # fobj is transferred in chunks controlled by the
  105. # constructor's chunk_size argument.
  106. writer = await super().prepare(request)
  107. chunk_size = self._chunk_size
  108. chunk = fobj.read(chunk_size)
  109. while True:
  110. await writer.write(chunk)
  111. count = count - chunk_size
  112. if count <= 0:
  113. break
  114. chunk = fobj.read(min(chunk_size, count))
  115. await writer.drain()
  116. return writer
  117. if hasattr(os, "sendfile") and not NOSENDFILE: # pragma: no cover
  118. _sendfile = _sendfile_system
  119. else: # pragma: no cover
  120. _sendfile = _sendfile_fallback
  121. async def prepare(self, request):
  122. filepath = self._path
  123. gzip = False
  124. if 'gzip' in request.headers.get(hdrs.ACCEPT_ENCODING, ''):
  125. gzip_path = filepath.with_name(filepath.name + '.gz')
  126. if gzip_path.is_file():
  127. filepath = gzip_path
  128. gzip = True
  129. st = filepath.stat()
  130. modsince = request.if_modified_since
  131. if modsince is not None and st.st_mtime <= modsince.timestamp():
  132. self.set_status(HTTPNotModified.status_code)
  133. self._length_check = False
  134. # Delete any Content-Length headers provided by user. HTTP 304
  135. # should always have empty response body
  136. return await super().prepare(request)
  137. unmodsince = request.if_unmodified_since
  138. if unmodsince is not None and st.st_mtime > unmodsince.timestamp():
  139. self.set_status(HTTPPreconditionFailed.status_code)
  140. return await super().prepare(request)
  141. if hdrs.CONTENT_TYPE not in self.headers:
  142. ct, encoding = mimetypes.guess_type(str(filepath))
  143. if not ct:
  144. ct = 'application/octet-stream'
  145. should_set_ct = True
  146. else:
  147. encoding = 'gzip' if gzip else None
  148. should_set_ct = False
  149. status = HTTPOk.status_code
  150. file_size = st.st_size
  151. count = file_size
  152. start = None
  153. ifrange = request.if_range
  154. if ifrange is None or st.st_mtime <= ifrange.timestamp():
  155. # If-Range header check:
  156. # condition = cached date >= last modification date
  157. # return 206 if True else 200.
  158. # if False:
  159. # Range header would not be processed, return 200
  160. # if True but Range header missing
  161. # return 200
  162. try:
  163. rng = request.http_range
  164. start = rng.start
  165. end = rng.stop
  166. except ValueError:
  167. # https://tools.ietf.org/html/rfc7233:
  168. # A server generating a 416 (Range Not Satisfiable) response to
  169. # a byte-range request SHOULD send a Content-Range header field
  170. # with an unsatisfied-range value.
  171. # The complete-length in a 416 response indicates the current
  172. # length of the selected representation.
  173. #
  174. # Will do the same below. Many servers ignore this and do not
  175. # send a Content-Range header with HTTP 416
  176. self.headers[hdrs.CONTENT_RANGE] = 'bytes */{0}'.format(
  177. file_size)
  178. self.set_status(HTTPRequestRangeNotSatisfiable.status_code)
  179. return await super().prepare(request)
  180. # If a range request has been made, convert start, end slice
  181. # notation into file pointer offset and count
  182. if start is not None or end is not None:
  183. if start < 0 and end is None: # return tail of file
  184. start += file_size
  185. if start < 0:
  186. # if Range:bytes=-1000 in request header but file size
  187. # is only 200, there would be trouble without this
  188. start = 0
  189. count = file_size - start
  190. else:
  191. # rfc7233:If the last-byte-pos value is
  192. # absent, or if the value is greater than or equal to
  193. # the current length of the representation data,
  194. # the byte range is interpreted as the remainder
  195. # of the representation (i.e., the server replaces the
  196. # value of last-byte-pos with a value that is one less than
  197. # the current length of the selected representation).
  198. count = min(end if end is not None else file_size,
  199. file_size) - start
  200. if start >= file_size:
  201. # HTTP 416 should be returned in this case.
  202. #
  203. # According to https://tools.ietf.org/html/rfc7233:
  204. # If a valid byte-range-set includes at least one
  205. # byte-range-spec with a first-byte-pos that is less than
  206. # the current length of the representation, or at least one
  207. # suffix-byte-range-spec with a non-zero suffix-length,
  208. # then the byte-range-set is satisfiable. Otherwise, the
  209. # byte-range-set is unsatisfiable.
  210. self.headers[hdrs.CONTENT_RANGE] = 'bytes */{0}'.format(
  211. file_size)
  212. self.set_status(HTTPRequestRangeNotSatisfiable.status_code)
  213. return await super().prepare(request)
  214. status = HTTPPartialContent.status_code
  215. # Even though you are sending the whole file, you should still
  216. # return a HTTP 206 for a Range request.
  217. self.set_status(status)
  218. if should_set_ct:
  219. self.content_type = ct
  220. if encoding:
  221. self.headers[hdrs.CONTENT_ENCODING] = encoding
  222. if gzip:
  223. self.headers[hdrs.VARY] = hdrs.ACCEPT_ENCODING
  224. self.last_modified = st.st_mtime
  225. self.content_length = count
  226. self.headers[hdrs.ACCEPT_RANGES] = 'bytes'
  227. if status == HTTPPartialContent.status_code:
  228. self.headers[hdrs.CONTENT_RANGE] = 'bytes {0}-{1}/{2}'.format(
  229. start, start + count - 1, file_size)
  230. with filepath.open('rb') as fobj:
  231. if start: # be aware that start could be None or int=0 here.
  232. fobj.seek(start)
  233. return await self._sendfile(request, fobj, count)