Skip to content

Commit

Permalink
Added client cert support
Browse files Browse the repository at this point in the history
  • Loading branch information
perklet committed Jan 25, 2024
1 parent 76e16af commit 975a790
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions curl_cffi/requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def request(
debug: bool = False,
interface: Optional[str] = None,
multipart: Optional[CurlMime] = None,
cert: Optional[Union[str, Tuple[str, str]]] = None,
) -> Response:
"""Send an http request.
Expand Down Expand Up @@ -126,6 +127,7 @@ def request(
http_version=http_version,
interface=interface,
multipart=multipart,
cert=cert,
)


Expand Down
17 changes: 17 additions & 0 deletions curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def __init__(
http_version: Optional[CurlHttpVersion] = None,
debug: bool = False,
interface: Optional[str] = None,
cert: Optional[Union[str, Tuple[str, str]]] = None,
):
self.headers = Headers(headers)
self.cookies = Cookies(cookies)
Expand All @@ -196,6 +197,7 @@ def __init__(
self.http_version = http_version
self.debug = debug
self.interface = interface
self.cert = cert

if proxy and proxies:
raise TypeError("Cannot specify both 'proxy' and 'proxies'")
Expand Down Expand Up @@ -230,6 +232,7 @@ def _set_curl_options(
default_headers: Optional[bool] = None,
http_version: Optional[CurlHttpVersion] = None,
interface: Optional[str] = None,
cert: Optional[Union[str, Tuple[str, str]]] = None,
stream: bool = False,
max_recv_speed: int = 0,
multipart: Optional[CurlMime] = None,
Expand Down Expand Up @@ -428,6 +431,16 @@ def _set_curl_options(
if accept_encoding is not None:
c.setopt(CurlOpt.ACCEPT_ENCODING, accept_encoding.encode())

# cert
cert = cert or self.cert
if cert:
if isinstance(cert, str):
c.setopt(CurlOpt.SSLCERT, cert)
else:
cert, key = cert
c.setopt(CurlOpt.SSLCERT, cert)
c.setopt(CurlOpt.SSLKEY, key)

# impersonate
impersonate = impersonate or self.impersonate
default_headers = (
Expand Down Expand Up @@ -690,6 +703,7 @@ def request(
default_headers: Optional[bool] = None,
http_version: Optional[CurlHttpVersion] = None,
interface: Optional[str] = None,
cert: Optional[Union[str, Tuple[str, str]]] = None,
stream: bool = False,
max_recv_speed: int = 0,
multipart: Optional[CurlMime] = None,
Expand Down Expand Up @@ -731,6 +745,7 @@ def request(
stream=stream,
max_recv_speed=max_recv_speed,
multipart=multipart,
cert=cert,
queue_class=queue.Queue,
event_class=threading.Event,
)
Expand Down Expand Up @@ -953,6 +968,7 @@ async def request(
default_headers: Optional[bool] = None,
http_version: Optional[CurlHttpVersion] = None,
interface: Optional[str] = None,
cert: Optional[Union[str, Tuple[str, str]]] = None,
stream: bool = False,
max_recv_speed: int = 0,
multipart: Optional[CurlMime] = None,
Expand Down Expand Up @@ -987,6 +1003,7 @@ async def request(
stream=stream,
max_recv_speed=max_recv_speed,
multipart=multipart,
cert=cert,
queue_class=asyncio.Queue,
event_class=asyncio.Event,
)
Expand Down

0 comments on commit 975a790

Please sign in to comment.