2011-05-10 13:49:28 +04:00
|
|
|
"""
|
|
|
|
Django supports parsing the content of an HTTP request, but only for form POST requests.
|
|
|
|
That behavior is sufficient for dealing with standard HTML forms, but it doesn't map well
|
2011-04-02 19:32:37 +04:00
|
|
|
to general HTTP requests.
|
2011-03-04 13:28:20 +03:00
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
We need a method to be able to:
|
2011-03-04 13:28:20 +03:00
|
|
|
|
2011-05-19 00:13:48 +04:00
|
|
|
1.) Determine the parsed content on a request for methods other than POST (eg typically also PUT)
|
|
|
|
|
|
|
|
2.) Determine the parsed content on a request for media types other than application/x-www-form-urlencoded
|
2011-04-02 19:32:37 +04:00
|
|
|
and multipart/form-data. (eg also handle multipart/json)
|
|
|
|
"""
|
2011-04-25 07:50:28 +04:00
|
|
|
|
2011-06-09 19:24:27 +04:00
|
|
|
from django.http import QueryDict
|
2011-05-10 13:49:28 +04:00
|
|
|
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
|
2011-06-30 01:02:00 +04:00
|
|
|
from django.http.multipartparser import MultiPartParserError
|
2011-05-10 13:49:28 +04:00
|
|
|
from django.utils import simplejson as json
|
2011-02-19 13:47:26 +03:00
|
|
|
from djangorestframework import status
|
2011-07-01 20:44:08 +04:00
|
|
|
from djangorestframework.compat import yaml
|
2011-05-10 13:49:28 +04:00
|
|
|
from djangorestframework.response import ErrorResponse
|
|
|
|
from djangorestframework.utils.mediatypes import media_type_matches
|
|
|
|
|
2011-06-26 03:34:52 +04:00
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
__all__ = (
|
|
|
|
'BaseParser',
|
|
|
|
'JSONParser',
|
|
|
|
'PlainTextParser',
|
|
|
|
'FormParser',
|
2011-05-19 00:13:48 +04:00
|
|
|
'MultiPartParser',
|
2011-06-26 03:34:52 +04:00
|
|
|
'YAMLParser',
|
2011-05-10 13:49:28 +04:00
|
|
|
)
|
2011-02-07 11:23:54 +03:00
|
|
|
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
class BaseParser(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
All parsers should extend :class:`BaseParser`, specifying a :attr:`media_type` attribute,
|
|
|
|
and overriding the :meth:`parse` method.
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 11:49:57 +04:00
|
|
|
|
2011-01-26 23:31:47 +03:00
|
|
|
media_type = None
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
def __init__(self, view):
|
|
|
|
"""
|
2011-05-10 13:49:28 +04:00
|
|
|
Initialize the parser with the ``View`` instance as state,
|
2011-05-19 00:13:48 +04:00
|
|
|
in case the parser needs to access any metadata on the :obj:`View` object.
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
|
|
|
self.view = view
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-05-12 15:55:13 +04:00
|
|
|
def can_handle_request(self, content_type):
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Returns :const:`True` if this parser is able to deal with the given *content_type*.
|
2011-05-10 13:49:28 +04:00
|
|
|
|
2011-05-19 00:13:48 +04:00
|
|
|
The default implementation for this function is to check the *content_type*
|
|
|
|
argument against the :attr:`media_type` attribute set on the class to see if
|
2011-05-10 13:49:28 +04:00
|
|
|
they match.
|
|
|
|
|
|
|
|
This may be overridden to provide for other behavior, but typically you'll
|
2011-05-19 00:13:48 +04:00
|
|
|
instead want to just set the :attr:`media_type` attribute on the class.
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
2011-05-24 16:29:30 +04:00
|
|
|
return media_type_matches(self.media_type, content_type)
|
2011-04-02 19:32:37 +04:00
|
|
|
|
|
|
|
def parse(self, stream):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Given a *stream* to read from, return the deserialized output.
|
2011-05-12 15:55:13 +04:00
|
|
|
Should return a 2-tuple of (data, files).
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-04-02 19:32:37 +04:00
|
|
|
raise NotImplementedError("BaseParser.parse() Must be overridden to be implemented.")
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
class JSONParser(BaseParser):
|
2011-05-19 11:36:55 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Parses JSON-serialized data.
|
2011-05-19 11:36:55 +04:00
|
|
|
"""
|
2011-05-19 11:49:57 +04:00
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
media_type = 'application/json'
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
def parse(self, stream):
|
2011-05-19 11:36:55 +04:00
|
|
|
"""
|
|
|
|
Returns a 2-tuple of `(data, files)`.
|
|
|
|
|
|
|
|
`data` will be an object which is the parsed content of the response.
|
|
|
|
`files` will always be `None`.
|
|
|
|
"""
|
2011-01-24 02:08:16 +03:00
|
|
|
try:
|
2011-05-12 15:55:13 +04:00
|
|
|
return (json.load(stream), None)
|
2011-01-24 02:08:16 +03:00
|
|
|
except ValueError, exc:
|
2011-05-10 13:49:28 +04:00
|
|
|
raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
|
|
|
|
{'detail': 'JSON parse error - %s' % unicode(exc)})
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-01-26 11:58:09 +03:00
|
|
|
|
2011-07-01 20:44:08 +04:00
|
|
|
if yaml:
|
|
|
|
class YAMLParser(BaseParser):
|
2011-06-26 03:34:52 +04:00
|
|
|
"""
|
2011-07-01 20:44:08 +04:00
|
|
|
Parses YAML-serialized data.
|
2011-06-26 03:34:52 +04:00
|
|
|
"""
|
2011-07-01 20:44:08 +04:00
|
|
|
|
|
|
|
media_type = 'application/yaml'
|
|
|
|
|
|
|
|
def parse(self, stream):
|
|
|
|
"""
|
|
|
|
Returns a 2-tuple of `(data, files)`.
|
|
|
|
|
|
|
|
`data` will be an object which is the parsed content of the response.
|
|
|
|
`files` will always be `None`.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return (yaml.safe_load(stream), None)
|
|
|
|
except ValueError, exc:
|
|
|
|
raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
|
|
|
|
{'detail': 'YAML parse error - %s' % unicode(exc)})
|
2011-07-02 21:19:45 +04:00
|
|
|
else:
|
|
|
|
YAMLParser = None
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-04-11 14:54:26 +04:00
|
|
|
class PlainTextParser(BaseParser):
|
|
|
|
"""
|
|
|
|
Plain text parser.
|
|
|
|
"""
|
2011-05-19 11:49:57 +04:00
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
media_type = 'text/plain'
|
2011-04-11 14:54:26 +04:00
|
|
|
|
|
|
|
def parse(self, stream):
|
2011-05-19 11:36:55 +04:00
|
|
|
"""
|
|
|
|
Returns a 2-tuple of `(data, files)`.
|
|
|
|
|
|
|
|
`data` will simply be a string representing the body of the request.
|
|
|
|
`files` will always be `None`.
|
|
|
|
"""
|
2011-05-12 15:55:13 +04:00
|
|
|
return (stream.read(), None)
|
2011-04-11 14:54:26 +04:00
|
|
|
|
|
|
|
|
2011-05-19 11:36:55 +04:00
|
|
|
class FormParser(BaseParser):
|
|
|
|
"""
|
|
|
|
Parser for form data.
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
2011-03-10 17:49:11 +03:00
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
media_type = 'application/x-www-form-urlencoded'
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
def parse(self, stream):
|
2011-05-19 11:36:55 +04:00
|
|
|
"""
|
|
|
|
Returns a 2-tuple of `(data, files)`.
|
|
|
|
|
2011-05-19 11:49:57 +04:00
|
|
|
`data` will be a :class:`QueryDict` containing all the form parameters.
|
|
|
|
`files` will always be :const:`None`.
|
2011-05-19 11:36:55 +04:00
|
|
|
"""
|
2011-06-09 19:24:27 +04:00
|
|
|
data = QueryDict(stream.read())
|
2011-05-12 15:55:13 +04:00
|
|
|
return (data, None)
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-03-04 18:23:18 +03:00
|
|
|
|
2011-05-19 11:36:55 +04:00
|
|
|
class MultiPartParser(BaseParser):
|
|
|
|
"""
|
|
|
|
Parser for multipart form data, which may include file data.
|
|
|
|
"""
|
2011-02-15 11:19:57 +03:00
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
media_type = 'multipart/form-data'
|
2011-03-04 13:28:20 +03:00
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
def parse(self, stream):
|
2011-05-19 11:36:55 +04:00
|
|
|
"""
|
|
|
|
Returns a 2-tuple of `(data, files)`.
|
|
|
|
|
2011-05-19 11:49:57 +04:00
|
|
|
`data` will be a :class:`QueryDict` containing all the form parameters.
|
|
|
|
`files` will be a :class:`QueryDict` containing all the form files.
|
2011-05-19 11:36:55 +04:00
|
|
|
"""
|
2011-04-02 19:32:37 +04:00
|
|
|
upload_handlers = self.view.request._get_upload_handlers()
|
2011-06-30 01:02:00 +04:00
|
|
|
try:
|
|
|
|
django_parser = DjangoMultiPartParser(self.view.request.META, stream, upload_handlers)
|
|
|
|
except MultiPartParserError, exc:
|
|
|
|
raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
|
|
|
|
{'detail': 'multipart parse error - %s' % unicode(exc)})
|
2011-05-19 11:36:55 +04:00
|
|
|
return django_parser.parse()
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-07-02 21:12:43 +04:00
|
|
|
DEFAULT_PARSERS = ( JSONParser,
|
|
|
|
FormParser,
|
|
|
|
MultiPartParser )
|
2011-07-02 21:03:26 +04:00
|
|
|
|
|
|
|
if YAMLParser:
|
2011-07-02 21:37:51 +04:00
|
|
|
DEFAULT_PARSERS += ( YAMLParser, )
|