contentviews: ignore empty data

This commit is contained in:
Maximilian Hils 2021-08-12 10:21:06 +02:00
parent f8b4c7bd44
commit a7f27259a7
10 changed files with 13 additions and 9 deletions

View File

@ -58,7 +58,7 @@ class ViewCSS(base.View):
return "CSS", base.format_text(beautified)
def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float:
return float(content_type == "text/css")
return float(bool(data) and content_type == "text/css")
if __name__ == "__main__": # pragma: no cover

View File

@ -45,7 +45,7 @@ class ViewGraphQL(base.View):
return "GraphQL", base.format_text(format_query_list(data))
def render_priority(self, data: bytes, *, content_type: typing.Optional[str] = None, **metadata) -> float:
if content_type != "application/json":
if content_type != "application/json" or not data:
return 0
data = parse_json(data)

View File

@ -59,4 +59,4 @@ class ViewJavaScript(base.View):
return "JavaScript", base.format_text(res)
def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float:
return float(content_type in self.__content_types)
return float(bool(data) and content_type in self.__content_types)

View File

@ -47,6 +47,8 @@ class ViewJSON(base.View):
return "JSON", format_json(data)
def render_priority(self, data: bytes, *, content_type: typing.Optional[str] = None, **metadata) -> float:
if not data:
return 0
if content_type in (
"application/json",
"application/json-rpc",

View File

@ -50,4 +50,4 @@ class ViewMsgPack(base.View):
return "MsgPack", format_msgpack(data)
def render_priority(self, data: bytes, *, content_type: typing.Optional[str] = None, **metadata) -> float:
return float(content_type in self.__content_types)
return float(bool(data) and content_type in self.__content_types)

View File

@ -21,4 +21,4 @@ class ViewMultipart(base.View):
return "Multipart form", self._format(v)
def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float:
return float(content_type == "multipart/form-data")
return float(bool(data) and content_type == "multipart/form-data")

View File

@ -80,4 +80,4 @@ class ViewProtobuf(base.View):
return "Protobuf", base.format_text(decoded)
def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float:
return float(content_type in self.__content_types)
return float(bool(data) and content_type in self.__content_types)

View File

@ -16,4 +16,4 @@ class ViewURLEncoded(base.View):
return "URLEncoded form", base.format_pairs(d)
def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float:
return float(content_type == "application/x-www-form-urlencoded")
return float(bool(data) and content_type == "application/x-www-form-urlencoded")

View File

@ -21,4 +21,4 @@ class ViewWBXML(base.View):
return None
def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float:
return float(content_type in self.__content_types)
return float(bool(data) and content_type in self.__content_types)

View File

@ -235,8 +235,10 @@ class ViewXmlHtml(base.View):
return t, pretty
def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float:
if not data:
return 0
if content_type in self.__content_types:
return 1
elif strutils.is_xml(data):
return 0.4
return float(content_type in self.__content_types)
return 0