ActivityParser doesn't parse the JSON of incoming messages.

This is because when we validate the message, a little further
down the line, we need access to the exact content of the message.
Parsing it here would destroy the exact content, because Django
doesn't let you read request bodies twice.
status-serialisers
Marnanel Thurman 2020-09-21 17:43:57 +01:00
rodzic f95163dc04
commit 83b69f2fcb
1 zmienionych plików z 19 dodań i 6 usunięć

Wyświetl plik

@ -5,16 +5,29 @@
# Licensed under the GNU Public License v2.
"""
django-rest-framework provides a JSON parser, which
does what we need for parsing ActivityPub. However,
it baulks at the ActivityPub MIME type. So we have a
subclass here with only the accepted MIME type changed.
A custom django-rest-framework parser for ActivityPub.
"""
from rest_framework.parsers import JSONParser
import codecs
from django.conf import settings
from rest_framework.parsers import BaseParser
class ActivityParser(JSONParser):
class ActivityParser(BaseParser):
"""
django-rest-framework parser for application/activity+json.
"""
media_type = "application/activity+json"
def parse(self, stream, media_type=None, parser_context=None):
"""
Traditionally, we'd be parsing the JSON here. But despite
the name, we don't parse the incoming stream here at all.
This is because validate() needs to know the exact content
in order to test the signature. So we just pass it back
as a bytestring.
"""
result = stream.read()
return result