Skip to content

Commit

Permalink
Check CDATA to not duplicate it
Browse files Browse the repository at this point in the history
  • Loading branch information
gschwind committed May 1, 2021
1 parent 62e3b46 commit 7840558
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions pywps/inout/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ def _json_reference(self, data):
def _json_data(self, data):
"""Return Data node
"""
# Match only data that are safe CDATA pattern.
CDATA_PATTERN = re.compile(r'^<!\[CDATA\[((?!\]\]>).)*\]\]>$')

data["type"] = "complex"

Expand All @@ -229,21 +231,33 @@ def _json_data(self, data):
# Try to inline data as text but if fail encode is in base64
if self.data_format.encoding == 'utf-8':
out = self.data.decode('utf-8')
if not re.search('\\]\\]>', out):
data["data"] = '<![CDATA[{}]]>'.format(out)
# If data is already enclose with CDATApatern do not add it twise
if CDATA_PATTERN.match(out):
data["data"] = out
else:
data['encoding'] = 'base64' # override the unsafe encoding
data["data"] = self.base64.decode('utf-8')
# Check if the data does not contain ]]> patern if is safe to use CDATA
# other wise we fallback to base64 encoding.
if not re.search('\\]\\]>', out):
data["data"] = '<![CDATA[{}]]>'.format(out)
else:
data['encoding'] = 'base64' # override the unsafe encoding
data["data"] = self.base64.decode('utf-8')
else:
data['encoding'] = 'base64' # override the unsafe encoding
data["data"] = self.base64.decode('utf-8')
else:
out = str(self.data)
if not re.search('\\]\\]>', out):
data["data"] = '<![CDATA[{}]]>'.format(out)
# If data is already enclose with CDATApatern do not add it twise
if CDATA_PATTERN.match(out):
data["data"] = out
else:
data['encoding'] = 'base64' # override the unsafe encoding
data["data"] = self.base64.decode('utf-8')
# Check if the data does not contain ]]> patern if is safe to use CDATA
# other wise we fallback to base64 encoding.
if not re.search('\\]\\]>', out):
data["data"] = '<![CDATA[{}]]>'.format(out)
else:
data['encoding'] = 'base64' # override the unsafe encoding
data["data"] = self.base64.decode('utf-8')

return data

Expand Down

0 comments on commit 7840558

Please sign in to comment.