ftp works but it blocks the thread

This commit is contained in:
ading2210 2024-03-17 17:50:58 -04:00
parent 0a5ace96fb
commit 2a072ecee0
12 changed files with 165 additions and 184 deletions

View file

@ -17,18 +17,18 @@ void forward_headers(struct RequestInfo *request_info);
struct curl_blob cacert_blob;
size_t write_function(char *data, size_t size, size_t nmemb, struct RequestInfo *request_info) {
//this should be in the write callback rather than curl's header callback because
//the write function will only be called after redirects
if (!request_info->headers_received) {
forward_headers(request_info);
}
size_t real_size = size * nmemb;
(*request_info->data_callback)(data, real_size);
return real_size;
}
CURL* create_request(const char* url, DataCallback data_callback, EndCallback end_callback, HeadersCallback headers_callback) {
size_t header_function(char *data, size_t size, size_t nmemb, struct RequestInfo *request_info) {
size_t real_size = size * nmemb;
(*request_info->headers_callback)(data, real_size);
return real_size;
}
CURL* create_request(const char* url, DataCallback data_callback, EndCallback end_callback, DataCallback headers_callback) {
CURL *http_handle = curl_easy_init();
//create request metadata struct
@ -36,7 +36,6 @@ CURL* create_request(const char* url, DataCallback data_callback, EndCallback en
request_info->http_handle = http_handle;
request_info->curl_msg = NULL;
request_info->headers_list = NULL;
request_info->headers_received = 0;
request_info->end_callback = end_callback;
request_info->data_callback = data_callback;
request_info->headers_callback = headers_callback;
@ -48,23 +47,19 @@ CURL* create_request(const char* url, DataCallback data_callback, EndCallback en
//callbacks to pass the response data back to js
curl_easy_setopt(http_handle, CURLOPT_WRITEFUNCTION, &write_function);
curl_easy_setopt(http_handle, CURLOPT_WRITEDATA, request_info);
//callback which runs on every response header
curl_easy_setopt(http_handle, CURLOPT_HEADERFUNCTION, &header_function);
curl_easy_setopt(http_handle, CURLOPT_HEADERDATA, request_info);
return http_handle;
}
void forward_headers(struct RequestInfo *request_info) {
request_info->headers_received = 1;
(*request_info->headers_callback)();
}
void finish_request(CURLMsg *curl_msg) {
CURL *http_handle = curl_msg->easy_handle;
struct RequestInfo *request_info = get_request_info(http_handle);
int error = (int) curl_msg->data.result;
if (!request_info->headers_received && error == 0) {
forward_headers(request_info);
}
//clean up curl
if (request_info->headers_list != NULL) {