Skip to content

Commit dc206f5

Browse files
authored
bugfix: keep blank parameters in query string, close #411 (#412)
1 parent 8cdf5a8 commit dc206f5

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

.github/workflows/build-and-test.yaml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,24 @@ jobs:
3232
runs-on: ubuntu-latest
3333
steps:
3434
- uses: actions/checkout@v4
35-
- run: |
36-
make preprocess
37-
pipx run build --sdist
38-
- uses: actions/upload-artifact@v4 # https://github.com/actions/upload-artifact/issues/478
39-
with:
40-
path: ./dist/*.tar.gz
41-
overwrite: true
4235

4336
- uses: actions/setup-python@v5
4437
with:
4538
python-version: '3.10'
46-
- run: |
39+
40+
- name: build sdist
41+
run: |
42+
make preprocess
43+
pip install build
44+
python -m build --sdist
4745
pip install ./dist/*.tar.gz
4846
47+
- name: upload artifacts
48+
uses: actions/upload-artifact@v4 # https://github.com/actions/upload-artifact/issues/478
49+
with:
50+
path: ./dist/*.tar.gz
51+
overwrite: true
52+
4953
bdist:
5054
name: Build bdist wheels and test
5155
runs-on: ${{ matrix.os }}

curl_cffi/requests/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _is_absolute_url(url: str) -> bool:
109109
def _quote_path_and_params(url: str, quote_str: str = ""):
110110
safe = "".join(SAFE_CHARS - set(quote_str))
111111
parsed_url = urlparse(url)
112-
parsed_get_args = parse_qsl(parsed_url.query)
112+
parsed_get_args = parse_qsl(parsed_url.query, keep_blank_values=True)
113113
encoded_get_args = urlencode(parsed_get_args, doseq=True, safe=safe)
114114
return ParseResult(
115115
parsed_url.scheme,
@@ -141,7 +141,7 @@ def _update_url_params(url: str, params: Union[Dict, List, Tuple]) -> str:
141141
parsed_url = urlparse(url)
142142

143143
# Extracting URL arguments from parsed URL, NOTE the result is a list, not dict
144-
parsed_get_args = parse_qsl(parsed_url.query)
144+
parsed_get_args = parse_qsl(parsed_url.query, keep_blank_values=True)
145145

146146
# Merging URL arguments dict with new params
147147
old_args_counter = Counter(x[0] for x in parsed_get_args)

scripts/build.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# this is the upstream libcurl-impersonate version
1313
__version__ = "0.8.0"
1414

15-
tmpdir = None
16-
1715

1816
def detect_arch():
1917
with open(Path(__file__).parent.parent / "libs.json") as f:
@@ -38,8 +36,13 @@ def detect_arch():
3836
arch["libdir"] = os.path.expanduser(arch["libdir"])
3937
else:
4038
global tmpdir
41-
tmpdir = tempfile.TemporaryDirectory()
42-
arch["libdir"] = tmpdir.name
39+
if "CI" in os.environ:
40+
tmpdir = "./tmplibdir"
41+
os.makedirs(tmpdir, exist_ok=True)
42+
arch["libdir"] = tmpdir
43+
else:
44+
tmpdir = tempfile.TemporaryDirectory()
45+
arch["libdir"] = tmpdir.name
4346
return arch
4447
raise Exception(f"Unsupported arch: {uname}")
4548

@@ -69,8 +72,13 @@ def download_libcurl():
6972
os.makedirs(arch["libdir"], exist_ok=True)
7073
shutil.unpack_archive(file, arch["libdir"])
7174

75+
print("Files after unpacking")
76+
print(os.listdir(arch["libdir"]))
77+
7278

7379
def get_curl_archives():
80+
print("Files for linking")
81+
print(os.listdir(arch["libdir"]))
7482
if arch["system"] == "Linux" and arch.get("link_type") == "static":
7583
# note that the order of libraries matters
7684
# https://stackoverflow.com/a/36581865

tests/unittest/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ async def echo_path(scope, receive, send):
262262

263263

264264
async def echo_params(scope, receive, send):
265-
body = {"params": parse_qs(scope["query_string"].decode())}
265+
body = {"params": parse_qs(scope["query_string"].decode(), keep_blank_values=True)}
266266
await send(
267267
{
268268
"type": "http.response.start",

tests/unittest/test_requests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ def test_update_params(server):
149149
)
150150
assert r.content == b'{"params": {"a": ["1", "2"], "foo": ["z", "1", "2"]}}'
151151

152+
# empty values should be kept
153+
r = requests.get(
154+
str(server.url.copy_with(path="/echo_params?a=")),
155+
params=[("foo", "1"), ("foo", "2")],
156+
)
157+
assert r.content == b'{"params": {"a": [""], "foo": ["1", "2"]}}'
158+
152159

153160
def test_url_encode(server):
154161
# https://github.com/lexiforest/curl_cffi/issues/394
@@ -209,6 +216,11 @@ def test_url_encode(server):
209216
r = requests.get(url, quote=False)
210217
assert r.url == url
211218

219+
# empty values should be kept
220+
url = "http://127.0.0.1:8000/api?param1=value1&param2=&param3=value3"
221+
r = requests.get(url)
222+
assert r.url == url
223+
212224

213225
def test_headers(server):
214226
r = requests.get(str(server.url.copy_with(path="/echo_headers")), headers={"foo": "bar"})

0 commit comments

Comments
 (0)