Skip to content

Commit

Permalink
Replace deprecated (in 3.12) cgi package with multipart external depe…
Browse files Browse the repository at this point in the history
…ndency
  • Loading branch information
benoit74 committed May 22, 2024
1 parent 05e6c52 commit 26fb639
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
29 changes: 10 additions & 19 deletions cdxj_indexer/postquery.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from multipart import MultipartParser
from warcio.utils import to_native_str

from urllib.parse import unquote_plus, urlencode
Expand All @@ -6,9 +7,9 @@
from cdxj_indexer.amf import amf_parse

import base64
import cgi
import json
import sys
import re

MAX_QUERY_LENGTH = 4096

Expand Down Expand Up @@ -94,27 +95,17 @@ def handle_binary(query_data):
query = handle_binary(query_data)

elif mime.startswith("multipart/"):
env = {
"REQUEST_METHOD": "POST",
"CONTENT_TYPE": mime,
"CONTENT_LENGTH": len(query_data),
}

args = dict(fp=BytesIO(query_data), environ=env, keep_blank_values=True)

args["encoding"] = "utf-8"

try:
data = cgi.FieldStorage(**args)
except ValueError:
# Content-Type multipart/form-data may lack "boundary" info
query = handle_binary(query_data)
else:
if boundary_match := re.match(r".*boundary=(\w*?)(?:\s|$|;).*", mime):
data = MultipartParser(
stream=BytesIO(query_data), boundary=boundary_match[1], charset="utf-8"
)
values = []
for item in data.list:
for item in data.parts():
values.append((item.name, item.value))

query = urlencode(values, True)
else:
# Content-Type multipart/form-data may lack "boundary" info
query = handle_binary(query_data)

elif mime.startswith("application/json"):
try:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def run_tests(self):
# temp fix for requests
"idna<3.0",
"py3amf",
"multipart",
],
zip_safe=True,
entry_points="""
Expand Down

0 comments on commit 26fb639

Please sign in to comment.