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

fix terrible type converting, close #342 #343

Merged
merged 5 commits into from
Jul 7, 2024
Merged
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
11 changes: 6 additions & 5 deletions curl_cffi/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,18 @@ 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: "int*", # offset type
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*":
c_value = ffi.new("int*", value)
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)
self._write_handle = c_value
Expand Down
12 changes: 7 additions & 5 deletions ffi/shim.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "shim.h"

#define INTEGER_OPTION_MAX 10000
#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 (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);
}
Loading