Skip to content

Commit c71dca4

Browse files
author
Artturi Sipilä
authored
Merge pull request #1133 from Aiven-Open/rapu-header-changes
4.1.3 rapu: drop usage of deprecated cgi.parse_header
2 parents 22ff621 + 3803ef5 commit c71dca4

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

.github/workflows/container-smoke-test.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ jobs:
3232
- name: Run container
3333
run: docker compose --file=container/compose.yml up --build --wait --detach
3434

35+
- name: Check container status
36+
run: docker ps -a
37+
38+
- name: Show container logs (Kafka + others)
39+
run: docker compose --file=container/compose.yml logs --no-color
40+
3541
- name: Smoke test registry
36-
run: bin/smoke-test-registry.sh
42+
run: bin/smoke-test-registry.sh || (docker compose -f container/compose.yml logs && exit 1)
3743

3844
- name: Smoke test REST proxy
39-
run: bin/smoke-test-rest.sh
45+
run: bin/smoke-test-rest.sh || (docker compose -f container/compose.yml logs && exit 1)

container/compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
ZOOKEEPER_TICK_TIME: 2000
1111

1212
kafka:
13-
image: confluentinc/cp-kafka:latest
13+
image: confluentinc/cp-kafka:7.5.0
1414
depends_on:
1515
- zookeeper
1616
ports:

src/karapace/rapu.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import aiohttp.web
1919
import aiohttp.web_exceptions
2020
import asyncio
21-
import cgi # pylint: disable=deprecated-module
21+
import email
2222
import hashlib
2323
import logging
2424
import 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+
6068
class 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

Comments
 (0)