From 32494df3ba37012adb79ee90043ea5f686a0d12b Mon Sep 17 00:00:00 2001 From: Kim Chow Date: Wed, 3 Apr 2024 12:33:35 +0800 Subject: [PATCH] =?UTF-8?q?[MOD]=20=E5=A2=9E=E5=8A=A0share=E7=89=B9?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 ++++++ curl_cffi/__init__.py | 7 +++++-- curl_cffi/const.py | 21 +++++++++++++++++++++ curl_cffi/curl.py | 24 +++++++++++++++++++++++- ffi/cdef.c | 6 ++++++ 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index fbdc5ff6..1a390773 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,12 @@ Release *.o **/__pycache__ .venv +*.dll +*.lib +*.a +*.exe +*.in +*.rc build/ dist/ diff --git a/curl_cffi/__init__.py b/curl_cffi/__init__.py index 99a1a9a1..41fcb79a 100644 --- a/curl_cffi/__init__.py +++ b/curl_cffi/__init__.py @@ -2,9 +2,12 @@ "Curl", "AsyncCurl", "CurlMime", + "CurlShare", "CurlError", "CurlInfo", "CurlOpt", + "CURLSHoption", + "CurlLockData", "CurlMOpt", "CurlECode", "CurlHttpVersion", @@ -20,5 +23,5 @@ # This line includes _wrapper.so into the wheel from ._wrapper import ffi, lib from .aio import AsyncCurl -from .const import CurlECode, CurlHttpVersion, CurlInfo, CurlMOpt, CurlOpt, CurlWsFlag -from .curl import Curl, CurlError, CurlMime +from .const import CurlECode, CurlHttpVersion, CurlInfo, CURLSHoption, CurlLockData, CurlMOpt, CurlOpt, CurlWsFlag +from .curl import Curl, CurlError, CurlMime, CurlShare diff --git a/curl_cffi/const.py b/curl_cffi/const.py index a171efc4..82ecd7c3 100644 --- a/curl_cffi/const.py +++ b/curl_cffi/const.py @@ -551,3 +551,24 @@ class CurlWsFlag(IntEnum): CLOSE = 1 << 3 PING = 1 << 4 OFFSET = 1 << 5 + + +class CurlLockData(IntEnum): + NONE = 0 + SHARE = 1 + COOKIE = 2 + DNS = 3 + SSL_SESSION = 4 + CONNECT = 5 + PSL = 6 + LAST = 7 + + +class CURLSHoption(IntEnum): + NONE = 0 + SHARE = 1 + UNSHARE = 2 + LOCKFUNC = 3 + UNLOCKFUNC = 4 + USERDATA = 5 + LAST = 6 diff --git a/curl_cffi/curl.py b/curl_cffi/curl.py index c91480b7..6047b240 100644 --- a/curl_cffi/curl.py +++ b/curl_cffi/curl.py @@ -7,7 +7,7 @@ import certifi from ._wrapper import ffi, lib -from .const import CurlHttpVersion, CurlInfo, CurlOpt, CurlWsFlag +from .const import CurlHttpVersion, CurlInfo, CurlLockData, CurlOpt, CURLSHoption, CurlWsFlag DEFAULT_CACERT = certifi.where() REASON_PHRASE_RE = re.compile(rb"HTTP/\d\.\d [0-9]{3} (.*)") @@ -518,3 +518,25 @@ def close(self) -> None: def __del__(self) -> None: self.close() + +class CurlShare: + def __init__(self) -> None: + self._share = lib.curl_share_init() + + def close(self): + if self._share: + lib.curl_share_cleanup(self._share) + self._share = ffi.NULL + + def setopt(self, option: CURLSHoption, value: CurlLockData): + ret = lib.curl_share_setopt(self._share, option, value) + if ret: + self._check_error(ret) + return ret + + def _check_error(self, errornum): + errormsg = ffi.string(lib.curl_share_strerror(errornum)).decode('utf-8') + return ValueError(errormsg) + + def __del__(self): + self.close() diff --git a/ffi/cdef.c b/ffi/cdef.c index 9c22ce7d..c549e2fa 100644 --- a/ffi/cdef.c +++ b/ffi/cdef.c @@ -68,3 +68,9 @@ int curl_mime_type(void *field, char *type); int curl_mime_filename(void *field, char *filename); int curl_mime_filedata(void *field, char *filename); void curl_mime_free(void *form); + +// share +void *curl_share_init(); +int curl_share_setopt(void *curlsh, int option, int param); +int curl_share_cleanup(void *curlsh); +const char *curl_share_strerror(int errornum); \ No newline at end of file