Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_request_data(self, environ: dict) -> dict:
content_type = environ.get("CONTENT_TYPE", "")
content_type = content_type.split(";")[0]
if content_type == DATA_TYPE_JSON:
return self.extract_data_from_json_request(environ)
if content_type == DATA_TYPE_MULTIPART:
return self.extract_data_from_multipart_request(environ)
raise HttpBadRequestError(
"Posted content must be of type {} or {}".format(
DATA_TYPE_JSON, DATA_TYPE_MULTIPART
)
def extract_data_from_multipart_request(self, environ: dict) -> Any:
try:
form = FieldStorage(
fp=environ["wsgi.input"], environ=environ, keep_blank_values=True
)
except (TypeError, ValueError):
raise HttpBadRequestError("Malformed request data")
try:
operations = json.loads(form.getvalue("operations"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'operations' multipart field is not a valid JSON"
)
try:
files_map = json.loads(form.getvalue("map"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'map' multipart field is not a valid JSON"
)
return combine_multipart_data(operations, files_map, form)
def get_request_body(self, environ: dict, content_length: int) -> bytes:
if not environ.get("wsgi.input"):
raise HttpBadRequestError("Request body cannot be empty")
request_body = environ["wsgi.input"].read(content_length)
if not request_body:
raise HttpBadRequestError("Request body cannot be empty")
return request_body
def get_request_content_length(self, environ: dict) -> int:
try:
content_length = int(environ.get("CONTENT_LENGTH", 0))
if content_length < 1:
raise HttpBadRequestError(
"Content length header is missing or incorrect"
)
return content_length
except (TypeError, ValueError):
raise HttpBadRequestError("Content length header is missing or incorrect")
def extract_data_from_multipart_request(self, request: HttpRequest):
try:
operations = json.loads(request.POST.get("operations"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'operations' multipart field is not a valid JSON"
)
try:
files_map = json.loads(request.POST.get("map"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'map' multipart field is not a valid JSON"
)
return combine_multipart_data(operations, files_map, request.FILES)
def get_request_body(self, environ: dict, content_length: int) -> bytes:
if not environ.get("wsgi.input"):
raise HttpBadRequestError("Request body cannot be empty")
request_body = environ["wsgi.input"].read(content_length)
if not request_body:
raise HttpBadRequestError("Request body cannot be empty")
return request_body
form = FieldStorage(
fp=environ["wsgi.input"], environ=environ, keep_blank_values=True
)
except (TypeError, ValueError):
raise HttpBadRequestError("Malformed request data")
try:
operations = json.loads(form.getvalue("operations"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'operations' multipart field is not a valid JSON"
)
try:
files_map = json.loads(form.getvalue("map"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'map' multipart field is not a valid JSON"
)
return combine_multipart_data(operations, files_map, form)
def extract_data_from_multipart_request(self, environ: dict) -> Any:
try:
form = FieldStorage(
fp=environ["wsgi.input"], environ=environ, keep_blank_values=True
)
except (TypeError, ValueError):
raise HttpBadRequestError("Malformed request data")
try:
operations = json.loads(form.getvalue("operations"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'operations' multipart field is not a valid JSON"
)
try:
files_map = json.loads(form.getvalue("map"))
except (TypeError, ValueError):
raise HttpBadRequestError(
"Request 'map' multipart field is not a valid JSON"
)
return combine_multipart_data(operations, files_map, form)
def get_request_data(self, body: bytes) -> dict:
for header, value in self.scope["headers"]:
if header == b"content-type" and value == DATA_TYPE_JSON.encode("utf-8"):
break
else:
raise HttpBadRequestError(
"Posted content must be of type {}".format(DATA_TYPE_JSON)
)
data = self.parse_request_body(body)
if not isinstance(data, dict):
raise GraphQLError("Valid request body should be a JSON object")
return data