You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2020/05/01 15:46:58 UTC

[GitHub] [trafficcontrol] zrhoffman commented on a change in pull request #4671: Ciab atstccfg

zrhoffman commented on a change in pull request #4671:
URL: https://github.com/apache/trafficcontrol/pull/4671#discussion_r418600654



##########
File path: infrastructure/cdn-in-a-box/ort/traffic_ops_ort/utils.py
##########
@@ -96,3 +97,83 @@ def getJSONResponse(uri:str, cookies:dict = None, verify:bool = True) -> dict:
 		logging.debug("Response: %r\n%r", response.headers, response.content)
 
 	return response.json()
+
+def parse_multipart(raw: str) -> typing.List[typing.Tuple[str, str]]:
+	"""
+	Parses a multipart/mixed-type payload and returns each contiguous chunk.
+
+	:param raw: The raw payload - without any HTTP status line.
+	:returns: A list where each element is a tuple where the first element is a chunk of the message. All headers are discarded except 'Path', which is the second element of each tuple if it was found in the chunk.
+	:raises: ValueError if the raw payload cannot be parsed as a multipart/mixed-type message.
+
+	>>> testdata = '''MIME-Version: 1.0\\r
+	... Content-Type: multipart/mixed; boundary="test"\\r
+	... \\r
+	... --test\\r
+	... Content-Type: text/plain; charset=us-ascii\\r
+	... Path: /path/to/ats/root/directory/etc/trafficserver/fname\\r
+	... \\r
+	... # A fake testing file that wasn't generated at all on some date
+	... CONFIG proxy.config.way.too.many.period.separated.words INT 1
+	...
+	... --test\\r
+	... Content-Type: text/plain; charset=utf8\\r
+	... Path: /path/to/ats/root/directory/etc/trafficserver/othername\\r
+	... \\r
+	... # The same header again
+	... CONFIG proxy.config.the.same.insane.chain.of.words.again.but.the.last.one.is.different INT 0
+	...
+	... --test--\\r
+	... '''
+	>>> output = parse_multipart(testdata)
+	>>> print(output[0][0])
+	# A fake testing file that wasn't generated at all on some date
+	CONFIG proxy.config.way.too.many.period.separated.words INT 1
+
+	>>> output[0][1]
+	'/path/to/ats/root/directory/etc/trafficserver/fname'
+	>>> print(output[1][0])
+	# The same header again
+	CONFIG proxy.config.the.same.insane.chain.of.words.again.but.the.last.one.is.different INT 0
+
+	>>> output[1][1]
+	'/path/to/ats/root/directory/etc/trafficserver/othername'
+	"""
+	try:
+		hdr_index = raw.index("\r\n\r\n")
+		headers = {line.split(':')[0].casefold(): line.split(':')[1] for line in raw[:hdr_index].splitlines()}
+	except (IndexError, ValueError) as e:
+		raise ValueError("Invalid or corrupt multipart header") from e
+
+	ctype = headers.get("content-type")
+	if not ctype:
+		raise ValueError("Message is missing 'Content-Type' header")
+
+	try:
+		param_index = ctype.index(";")
+		params = {param.split('=')[0].strip(): param.split('=')[1].strip() for param in ctype[param_index+1:].split(';')}

Review comment:
       TBH the existing way is more readable, it makes sense to stick with that for maintainability.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org