Skip to content

Commit

Permalink
Replace deprecated cgi module with multipart for parsing multipart PO…
Browse files Browse the repository at this point in the history
…ST requests (#28)

* Replace deprecated cgi module with multipart

* Update to supported Python versions

Drop Python 3.7 and 3.8 from CI, as both had test failures due to
other packages dropping support for these now EOL versions.

Add Python 3.11 and 3.12 to CI.
  • Loading branch information
tw4l authored Nov 11, 2024
1 parent 3df3bbc commit 7d9e56b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
max-parallel: 3
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- name: checkout
Expand All @@ -20,7 +20,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -U black pytest pytest-cov
pip install -U black pytest pytest-cov setuptools[core]
python setup.py -q install
- name: Style Check
Expand Down
31 changes: 12 additions & 19 deletions cdxj_indexer/postquery.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from warcio.utils import to_native_str
import base64
import json
import sys

from urllib.parse import unquote_plus, urlencode
from io import BytesIO

from multipart import MultipartParser
from warcio.utils import to_native_str

from cdxj_indexer.amf import amf_parse

import base64
import cgi
import json
import sys

MAX_QUERY_LENGTH = 4096


# ============================================================================
def append_method_query_from_req_resp(req, resp):
len_ = req.http_headers.get_header("Content-Length")
Expand Down Expand Up @@ -93,25 +95,16 @@ 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:
boundary = mime.split("boundary=")[1]
parser = MultipartParser(BytesIO(query_data), boundary, charset="utf8")
except (ValueError, IndexError):
# Content-Type multipart/form-data may lack "boundary" info
query = handle_binary(query_data)
else:
values = []
for item in data.list:
values.append((item.name, item.value))
for part in parser:
values.append((part.name, part.value))

query = urlencode(values, True)

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 7d9e56b

Please sign in to comment.