Skip to content

Commit

Permalink
BaseSession._set_curl_options(): simplify code a little
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Apr 1, 2024
1 parent c8cafcb commit 0847c44
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,7 @@ def _set_curl_options(
c = curl

# method
if method == "POST":
c.setopt(CurlOpt.POST, 1)
elif method != "GET":
c.setopt(CurlOpt.CUSTOMREQUEST, method.encode())
c.setopt(CurlOpt.CUSTOMREQUEST, method.encode())

# url
if self.params:
Expand All @@ -296,14 +293,14 @@ def _set_curl_options(
c.setopt(CurlOpt.URL, url.encode())

# data/body/json
if isinstance(data, dict):
body = urlencode(data).encode()
elif isinstance(data, str):
body = data.encode()
elif isinstance(data, BytesIO):
body = data.read()
elif isinstance(data, bytes):
body = data
data_handlers = {
dict: lambda d: urlencode(d).encode(),
str: lambda s: s.encode(),
BytesIO: lambda b: b.read(),
bytes: lambda b: b,
}
if isinstance(data, (dict, str, BytesIO, bytes)):
body = data_handlers[type(data)](data)
elif data is None:
body = b""
else:
Expand All @@ -322,27 +319,19 @@ def _set_curl_options(
# headers
h = Headers(self.headers)
h.update(headers)

# remove Host header if it's unnecessary, otherwise curl maybe confused.
# Host header will be automatically added by curl if it's not present.
# https://github.com/yifeikong/curl_cffi/issues/119
host_header = h.get("Host")
if host_header is not None:
u = urlparse(url)
if host_header == u.netloc or host_header == u.hostname:
try:
del h["Host"]
except KeyError:
pass

header_lines = []
for k, v in h.multi_items():
header_lines.append(f"{k}: {v}")
h.pop("Host", None)
header_lines = [f"{k}: {v}" for k, v in h.multi_items()]
if json is not None:
_update_header_line(header_lines, "Content-Type", "application/json")
if isinstance(data, dict) and method != "POST":
_update_header_line(header_lines, "Content-Type", "application/x-www-form-urlencoded")
# print("header lines", header_lines)
c.setopt(CurlOpt.HTTPHEADER, [h.encode() for h in header_lines])

req = Request(url, h, method)
Expand Down

0 comments on commit 0847c44

Please sign in to comment.