1818import aiohttp .web
1919import aiohttp .web_exceptions
2020import asyncio
21- import cgi # pylint: disable=deprecated-module
21+ import email
2222import hashlib
2323import logging
2424import re
@@ -57,6 +57,14 @@ def is_success(http_status: HTTPStatus) -> bool:
5757 return http_status .value >= 200 and http_status .value < 300
5858
5959
60+ def parse_header (header_value : str ) -> tuple [str , dict [str , str ]]:
61+ msg = email .message .Message ()
62+ msg ["Content-Type" ] = header_value
63+ main_value = msg .get_content_type ()
64+ params = dict (msg .get_params ()[1 :])
65+ return main_value , params
66+
67+
6068class HTTPRequest :
6169 def __init__ (
6270 self ,
@@ -202,10 +210,8 @@ def check_rest_headers(self, request: HTTPRequest) -> dict:
202210 default_content = "application/vnd.kafka.json.v2+json"
203211 default_accept = "*/*"
204212 result = {"content_type" : default_content }
205- content_matcher = REST_CONTENT_TYPE_RE .search (
206- cgi .parse_header (request .get_header ("Content-Type" , default_content ))[0 ]
207- )
208- accept_matcher = REST_ACCEPT_RE .search (cgi .parse_header (request .get_header ("Accept" , default_accept ))[0 ])
213+ content_matcher = REST_CONTENT_TYPE_RE .search (parse_header (request .get_header ("Content-Type" , default_content ))[0 ])
214+ accept_matcher = REST_ACCEPT_RE .search (request .get_header ("Accept" , default_accept ))
209215 if method in {"POST" , "PUT" }:
210216 if not content_matcher :
211217 http_error (
@@ -230,7 +236,7 @@ def check_schema_headers(self, request: HTTPRequest):
230236 response_default_content_type = "application/vnd.schemaregistry.v1+json"
231237 content_type = request .get_header ("Content-Type" , JSON_CONTENT_TYPE )
232238
233- if method in {"POST" , "PUT" } and cgi . parse_header (content_type )[0 ] not in SCHEMA_CONTENT_TYPES :
239+ if method in {"POST" , "PUT" } and parse_header (content_type )[0 ] not in SCHEMA_CONTENT_TYPES :
234240 http_error (
235241 message = "HTTP 415 Unsupported Media Type" ,
236242 content_type = response_default_content_type ,
@@ -285,8 +291,8 @@ async def _handle_request(
285291 if not body :
286292 raise HTTPResponse (body = "Missing request JSON body" , status = HTTPStatus .BAD_REQUEST )
287293 try :
288- _ , options = cgi . parse_header (rapu_request .get_header ("Content-Type" ))
289- charset = options .get ("charset" , "utf-8" )
294+ _ , options = parse_header (rapu_request .get_header ("Content-Type" ))
295+ charset = email . utils . collapse_rfc2231_value ( options .get ("charset" , "utf-8" ) )
290296 body_string = body .decode (charset )
291297 rapu_request .json = json_decode (body_string )
292298 except UnicodeDecodeError :
0 commit comments