Skip to content

Commit

Permalink
Merge pull request scrapy#5002 from djikoSal/refactor-curl-to-request…
Browse files Browse the repository at this point in the history
…-kwargs

Refactor curl_to_request_kwargs scrapy#5001
  • Loading branch information
kmike committed Feb 24, 2021
2 parents bda5472 + 3894ebb commit f95ebd8
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions scrapy/utils/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ def error(self, message):
curl_parser.add_argument(*argument, action='store_true')


def _parse_headers_and_cookies(parsed_args):
headers = []
cookies = {}
for header in parsed_args.headers or ():
name, val = header.split(':', 1)
name = name.strip()
val = val.strip()
if name.title() == 'Cookie':
for name, morsel in SimpleCookie(val).items():
cookies[name] = morsel.value
else:
headers.append((name, val))

if parsed_args.auth:
user, password = parsed_args.auth.split(':', 1)
headers.append(('Authorization', basic_auth_header(user, password)))

return headers, cookies


def curl_to_request_kwargs(curl_command, ignore_unknown_options=True):
"""Convert a cURL command syntax to Request kwargs.
Expand Down Expand Up @@ -70,21 +90,7 @@ def curl_to_request_kwargs(curl_command, ignore_unknown_options=True):

result = {'method': method.upper(), 'url': url}

headers = []
cookies = {}
for header in parsed_args.headers or ():
name, val = header.split(':', 1)
name = name.strip()
val = val.strip()
if name.title() == 'Cookie':
for name, morsel in SimpleCookie(val).items():
cookies[name] = morsel.value
else:
headers.append((name, val))

if parsed_args.auth:
user, password = parsed_args.auth.split(':', 1)
headers.append(('Authorization', basic_auth_header(user, password)))
headers, cookies = _parse_headers_and_cookies(parsed_args)

if headers:
result['headers'] = headers
Expand Down

0 comments on commit f95ebd8

Please sign in to comment.