parsers.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. """
  2. Parsers are used to parse the content of incoming HTTP requests.
  3. They give us a generic way of being able to handle various media types
  4. on the request, such as form content or json encoded data.
  5. """
  6. import codecs
  7. from django.conf import settings
  8. from django.core.files.uploadhandler import StopFutureHandlers
  9. from django.http import QueryDict
  10. from django.http.multipartparser import ChunkIter
  11. from django.http.multipartparser import \
  12. MultiPartParser as DjangoMultiPartParser
  13. from django.http.multipartparser import MultiPartParserError
  14. from rest_framework import renderers
  15. from rest_framework.compat import parse_header_parameters
  16. from rest_framework.exceptions import ParseError
  17. from rest_framework.settings import api_settings
  18. from rest_framework.utils import json
  19. class DataAndFiles:
  20. def __init__(self, data, files):
  21. self.data = data
  22. self.files = files
  23. class BaseParser:
  24. """
  25. All parsers should extend `BaseParser`, specifying a `media_type`
  26. attribute, and overriding the `.parse()` method.
  27. """
  28. media_type = None
  29. def parse(self, stream, media_type=None, parser_context=None):
  30. """
  31. Given a stream to read from, return the parsed representation.
  32. Should return parsed data, or a `DataAndFiles` object consisting of the
  33. parsed data and files.
  34. """
  35. raise NotImplementedError(".parse() must be overridden.")
  36. class JSONParser(BaseParser):
  37. """
  38. Parses JSON-serialized data.
  39. """
  40. media_type = 'application/json'
  41. renderer_class = renderers.JSONRenderer
  42. strict = api_settings.STRICT_JSON
  43. def parse(self, stream, media_type=None, parser_context=None):
  44. """
  45. Parses the incoming bytestream as JSON and returns the resulting data.
  46. """
  47. parser_context = parser_context or {}
  48. encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
  49. try:
  50. decoded_stream = codecs.getreader(encoding)(stream)
  51. parse_constant = json.strict_constant if self.strict else None
  52. return json.load(decoded_stream, parse_constant=parse_constant)
  53. except ValueError as exc:
  54. raise ParseError('JSON parse error - %s' % str(exc))
  55. class FormParser(BaseParser):
  56. """
  57. Parser for form data.
  58. """
  59. media_type = 'application/x-www-form-urlencoded'
  60. def parse(self, stream, media_type=None, parser_context=None):
  61. """
  62. Parses the incoming bytestream as a URL encoded form,
  63. and returns the resulting QueryDict.
  64. """
  65. parser_context = parser_context or {}
  66. encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
  67. return QueryDict(stream.read(), encoding=encoding)
  68. class MultiPartParser(BaseParser):
  69. """
  70. Parser for multipart form data, which may include file data.
  71. """
  72. media_type = 'multipart/form-data'
  73. def parse(self, stream, media_type=None, parser_context=None):
  74. """
  75. Parses the incoming bytestream as a multipart encoded form,
  76. and returns a DataAndFiles object.
  77. `.data` will be a `QueryDict` containing all the form parameters.
  78. `.files` will be a `QueryDict` containing all the form files.
  79. """
  80. parser_context = parser_context or {}
  81. request = parser_context['request']
  82. encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
  83. meta = request.META.copy()
  84. meta['CONTENT_TYPE'] = media_type
  85. upload_handlers = request.upload_handlers
  86. try:
  87. parser = DjangoMultiPartParser(meta, stream, upload_handlers, encoding)
  88. data, files = parser.parse()
  89. return DataAndFiles(data, files)
  90. except MultiPartParserError as exc:
  91. raise ParseError('Multipart form parse error - %s' % str(exc))
  92. class FileUploadParser(BaseParser):
  93. """
  94. Parser for file upload data.
  95. """
  96. media_type = '*/*'
  97. errors = {
  98. 'unhandled': 'FileUpload parse error - none of upload handlers can handle the stream',
  99. 'no_filename': 'Missing filename. Request should include a Content-Disposition header with a filename parameter.',
  100. }
  101. def parse(self, stream, media_type=None, parser_context=None):
  102. """
  103. Treats the incoming bytestream as a raw file upload and returns
  104. a `DataAndFiles` object.
  105. `.data` will be None (we expect request body to be a file content).
  106. `.files` will be a `QueryDict` containing one 'file' element.
  107. """
  108. parser_context = parser_context or {}
  109. request = parser_context['request']
  110. encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
  111. meta = request.META
  112. upload_handlers = request.upload_handlers
  113. filename = self.get_filename(stream, media_type, parser_context)
  114. if not filename:
  115. raise ParseError(self.errors['no_filename'])
  116. # Note that this code is extracted from Django's handling of
  117. # file uploads in MultiPartParser.
  118. content_type = meta.get('HTTP_CONTENT_TYPE',
  119. meta.get('CONTENT_TYPE', ''))
  120. try:
  121. content_length = int(meta.get('HTTP_CONTENT_LENGTH',
  122. meta.get('CONTENT_LENGTH', 0)))
  123. except (ValueError, TypeError):
  124. content_length = None
  125. # See if the handler will want to take care of the parsing.
  126. for handler in upload_handlers:
  127. result = handler.handle_raw_input(stream,
  128. meta,
  129. content_length,
  130. None,
  131. encoding)
  132. if result is not None:
  133. return DataAndFiles({}, {'file': result[1]})
  134. # This is the standard case.
  135. possible_sizes = [x.chunk_size for x in upload_handlers if x.chunk_size]
  136. chunk_size = min([2 ** 31 - 4] + possible_sizes)
  137. chunks = ChunkIter(stream, chunk_size)
  138. counters = [0] * len(upload_handlers)
  139. for index, handler in enumerate(upload_handlers):
  140. try:
  141. handler.new_file(None, filename, content_type,
  142. content_length, encoding)
  143. except StopFutureHandlers:
  144. upload_handlers = upload_handlers[:index + 1]
  145. break
  146. for chunk in chunks:
  147. for index, handler in enumerate(upload_handlers):
  148. chunk_length = len(chunk)
  149. chunk = handler.receive_data_chunk(chunk, counters[index])
  150. counters[index] += chunk_length
  151. if chunk is None:
  152. break
  153. for index, handler in enumerate(upload_handlers):
  154. file_obj = handler.file_complete(counters[index])
  155. if file_obj is not None:
  156. return DataAndFiles({}, {'file': file_obj})
  157. raise ParseError(self.errors['unhandled'])
  158. def get_filename(self, stream, media_type, parser_context):
  159. """
  160. Detects the uploaded file name. First searches a 'filename' url kwarg.
  161. Then tries to parse Content-Disposition header.
  162. """
  163. try:
  164. return parser_context['kwargs']['filename']
  165. except KeyError:
  166. pass
  167. try:
  168. meta = parser_context['request'].META
  169. disposition, params = parse_header_parameters(meta['HTTP_CONTENT_DISPOSITION'])
  170. if 'filename*' in params:
  171. return params['filename*']
  172. else:
  173. return params['filename']
  174. except (AttributeError, KeyError, ValueError):
  175. pass