From ede278079b707725f652e2eebe0655fe0bab5b4d Mon Sep 17 00:00:00 2001 From: qishipai <88886376+qishipai@users.noreply.github.com> Date: Sat, 6 Jul 2024 04:45:01 +0800 Subject: [PATCH 1/5] Update curl.py fix terrible type converting --- curl_cffi/curl.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/curl_cffi/curl.py b/curl_cffi/curl.py index 501a4082..63abb795 100644 --- a/curl_cffi/curl.py +++ b/curl_cffi/curl.py @@ -162,7 +162,7 @@ def setopt(self, option: CurlOpt, value: Any) -> int: 0: "int*", 10000: "char*", 20000: "void*", - 30000: "int*", # offset type + 30000: "off_t", # offset type } # print("option", option, "value", value) @@ -170,6 +170,8 @@ def setopt(self, option: CurlOpt, value: Any) -> int: value_type = input_option.get(int(option / 10000) * 10000) if value_type == "int*": c_value = ffi.new("int*", value) + elif value_type == "off_t": + c_value = ffi.cast("void*", value) elif option == CurlOpt.WRITEDATA: c_value = ffi.new_handle(value) self._write_handle = c_value From 31439bc5e980c0dad362a31bde808ab5878be072 Mon Sep 17 00:00:00 2001 From: qishipai <88886376+qishipai@users.noreply.github.com> Date: Sat, 6 Jul 2024 05:56:59 +0800 Subject: [PATCH 2/5] Update shim.c fix terrible type converting --- ffi/shim.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ffi/shim.c b/ffi/shim.c index 39a86000..a50e71fc 100644 --- a/ffi/shim.c +++ b/ffi/shim.c @@ -1,6 +1,7 @@ #include "shim.h" #define INTEGER_OPTION_MAX 10000 +#define POINTER_OPTION_MAX 30000 int _curl_easy_setopt(void* curl, int option, void* parameter) { // printf("****** hijack test begins: \n"); @@ -12,5 +13,8 @@ int _curl_easy_setopt(void* curl, int option, void* parameter) { if (option < INTEGER_OPTION_MAX) { return (int)curl_easy_setopt(curl, (CURLoption)option, *(int*)parameter); } + if (option >= POINTER_OPTION_MAX) { + return (int)curl_easy_setopt(curl, (CURLoption)option, *(curl_off_t*)parameter); + } return (int)curl_easy_setopt(curl, (CURLoption)option, parameter); } From ad63e354b56d7b7d46d8e7618450251df2c6f468 Mon Sep 17 00:00:00 2001 From: qishipai <88886376+qishipai@users.noreply.github.com> Date: Sat, 6 Jul 2024 06:00:44 +0800 Subject: [PATCH 3/5] Update curl.py --- curl_cffi/curl.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/curl_cffi/curl.py b/curl_cffi/curl.py index 63abb795..e816aee2 100644 --- a/curl_cffi/curl.py +++ b/curl_cffi/curl.py @@ -162,16 +162,14 @@ def setopt(self, option: CurlOpt, value: Any) -> int: 0: "int*", 10000: "char*", 20000: "void*", - 30000: "off_t", # offset type + 30000: "int64_t*", # offset type } # print("option", option, "value", value) # Convert value value_type = input_option.get(int(option / 10000) * 10000) - if value_type == "int*": - c_value = ffi.new("int*", value) - elif value_type == "off_t": - c_value = ffi.cast("void*", value) + if value_type == "int*" or value_type == "int64_t*": + c_value = ffi.new(value_type, value) elif option == CurlOpt.WRITEDATA: c_value = ffi.new_handle(value) self._write_handle = c_value From 6322c5f1f4d9937483341bcbbd517d502874e119 Mon Sep 17 00:00:00 2001 From: qishipai <88886376+qishipai@users.noreply.github.com> Date: Sat, 6 Jul 2024 15:09:10 +0800 Subject: [PATCH 4/5] improve macro using --- ffi/shim.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/ffi/shim.c b/ffi/shim.c index a50e71fc..0b2cc540 100644 --- a/ffi/shim.c +++ b/ffi/shim.c @@ -1,19 +1,17 @@ -#include "shim.h" -#define INTEGER_OPTION_MAX 10000 -#define POINTER_OPTION_MAX 30000 +#include "shim.h" int _curl_easy_setopt(void* curl, int option, void* parameter) { // printf("****** hijack test begins: \n"); // int val = curl_easy_setopt(instance->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); // printf("****** hijack test ends. opt: %d, val: %d, result is: %d\n", CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0, val); - CURLoption opt_value = (CURLoption) option; + // CURLoption opt_value = (CURLoption) option; // printf("option: %d, setopt parameter: %d\n", option, *(int*)parameter); // for integer options, we need to convert param from pointers to integers - if (option < INTEGER_OPTION_MAX) { - return (int)curl_easy_setopt(curl, (CURLoption)option, *(int*)parameter); + if (option < CURLOPTTYPE_OBJECTPOINT) { + return (int)curl_easy_setopt(curl, (CURLoption)option, *(long*)parameter); } - if (option >= POINTER_OPTION_MAX) { + if (CURLOPTTYPE_OFF_T <= option && option < CURLOPTTYPE_BLOB) { return (int)curl_easy_setopt(curl, (CURLoption)option, *(curl_off_t*)parameter); } return (int)curl_easy_setopt(curl, (CURLoption)option, parameter); From 0c8e1788444ba5a30355d735eee056076521ea72 Mon Sep 17 00:00:00 2001 From: qishipai <88886376+qishipai@users.noreply.github.com> Date: Sat, 6 Jul 2024 15:38:05 +0800 Subject: [PATCH 5/5] prepare for blob type support --- curl_cffi/curl.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/curl_cffi/curl.py b/curl_cffi/curl.py index e816aee2..e5a3bf6e 100644 --- a/curl_cffi/curl.py +++ b/curl_cffi/curl.py @@ -159,16 +159,17 @@ def setopt(self, option: CurlOpt, value: Any) -> int: input_option = { # this should be int in curl, but cffi requires pointer for void* # it will be convert back in the glue c code. - 0: "int*", + 0: "long*", 10000: "char*", 20000: "void*", 30000: "int64_t*", # offset type + 40000: "void*", # blob type } # print("option", option, "value", value) # Convert value - value_type = input_option.get(int(option / 10000) * 10000) - if value_type == "int*" or value_type == "int64_t*": + value_type = input_option.get((option // 10000) * 10000) + if value_type == "long*" or value_type == "int64_t*": c_value = ffi.new(value_type, value) elif option == CurlOpt.WRITEDATA: c_value = ffi.new_handle(value)