Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOD] 增加share特性 #288

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class CURLSHoption(IntEnum):
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 @@ -520,3 +520,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);
Loading