Skip to content

Commit

Permalink
[MOD] 增加share特性
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim Chow committed Apr 3, 2024
1 parent 418e452 commit 32494df
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Release
*.o
**/__pycache__
.venv
*.dll
*.lib
*.a
*.exe
*.in
*.rc

build/
dist/
Expand Down
7 changes: 5 additions & 2 deletions curl_cffi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
"Curl",
"AsyncCurl",
"CurlMime",
"CurlShare",
"CurlError",
"CurlInfo",
"CurlOpt",
"CURLSHoption",
"CurlLockData",
"CurlMOpt",
"CurlECode",
"CurlHttpVersion",
Expand All @@ -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
21 changes: 21 additions & 0 deletions curl_cffi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 23 additions & 1 deletion curl_cffi/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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} (.*)")
Expand Down Expand Up @@ -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()
6 changes: 6 additions & 0 deletions ffi/cdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 32494df

Please sign in to comment.