curl: add support for parsing -b,--cookie (#6684)

This commit is contained in:
Matt Winter 2025-02-19 04:17:37 -05:00 committed by GitHub
parent a898331d14
commit 8c34e6d9a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View File

@ -36,6 +36,7 @@ curl_parser = CurlParser()
curl_parser.add_argument("url")
curl_parser.add_argument("-H", "--header", dest="headers", action="append")
curl_parser.add_argument("-X", "--request", dest="method")
curl_parser.add_argument("-b", "--cookie", dest="cookies", action="append")
curl_parser.add_argument("-d", "--data", "--data-raw", dest="data", action=DataAction)
curl_parser.add_argument("-u", "--user", dest="auth")
@ -68,6 +69,14 @@ def _parse_headers_and_cookies(
else:
headers.append((name, val))
for cookie_param in parsed_args.cookies or ():
# curl can treat this parameter as either "key=value; key2=value2" pairs, or a filename.
# Scrapy will only support key-value pairs.
if "=" not in cookie_param:
continue
for name, morsel in SimpleCookie(cookie_param).items():
cookies[name] = morsel.value
if parsed_args.auth:
user, password = parsed_args.auth.split(":", 1)
headers.append(("Authorization", basic_auth_header(user, password)))

View File

@ -49,8 +49,8 @@ class CurlToRequestKwargsTest(unittest.TestCase):
"ml,application/xhtml+xml,application/xml;q=0.9,image/webp,image/a"
"png,*/*;q=0.8' -H 'Referer: http://httpbin.org/' -H 'Cookie: _gau"
"ges_unique_year=1; _gauges_unique=1; _gauges_unique_month=1; _gau"
"ges_unique_hour=1; _gauges_unique_day=1' -H 'Connection: keep-ali"
"ve' --compressed"
"ges_unique_hour=1' -H 'Connection: keep-alive' --compressed -b '_"
"gauges_unique_day=1'"
)
expected_result = {
"method": "GET",