working response streaming

This commit is contained in:
ading2210 2024-03-08 17:00:20 -05:00
parent e211e8bf8c
commit 33ac41ae33
5 changed files with 159 additions and 47 deletions

View file

@ -14,6 +14,7 @@
#include "types.h"
void finish_request(CURLMsg *curl_msg);
void forward_headers(struct RequestInfo *request_info);
#define ERROR_REDIRECT_DISALLOWED -1
@ -21,11 +22,16 @@ CURLM *multi_handle;
int request_active = 0;
struct curl_blob cacert_blob;
size_t write_function(void *data, size_t size, size_t nmemb, DataCallback data_callback) {
size_t write_function(void *data, size_t size, size_t nmemb, struct RequestInfo *request_info) {
if (!request_info->headers_received) {
request_info->headers_received = 1;
forward_headers(request_info);
}
size_t real_size = size * nmemb;
char* chunk = malloc(real_size);
memcpy(chunk, data, real_size);
data_callback(chunk, real_size);
(*request_info->data_callback)(chunk, real_size);
free(chunk);
return real_size;
}
@ -48,7 +54,7 @@ void tick_request() {
}
}
CURL* start_request(const char* url, const char* json_params, DataCallback data_callback, EndCallback end_callback, const char* body, int body_length) {
CURL* start_request(const char* url, const char* json_params, DataCallback data_callback, EndCallback end_callback, HeadersCallback headers_callback, const char* body, int body_length) {
CURL *http_handle = curl_easy_init();
int abort_on_redirect = 0;
int prevent_cleanup = 0;
@ -56,10 +62,6 @@ CURL* start_request(const char* url, const char* json_params, DataCallback data_
curl_easy_setopt(http_handle, CURLOPT_URL, url);
curl_easy_setopt(http_handle, CURLOPT_CAINFO_BLOB , cacert_blob);
//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, data_callback);
//some default options
curl_easy_setopt(http_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(http_handle, CURLOPT_ACCEPT_ENCODING, "");
@ -129,36 +131,38 @@ CURL* start_request(const char* url, const char* json_params, DataCallback data_
curl_easy_setopt(http_handle, CURLOPT_POSTFIELDSIZE, body_length);
}
//create request metadata struct
struct RequestInfo *request_info = malloc(sizeof(struct RequestInfo));
request_info->http_handle = http_handle;
request_info->abort_on_redirect = abort_on_redirect;
request_info->curl_msg = NULL;
request_info->headers_list = headers_list;
request_info->end_callback = end_callback;
request_info->prevent_cleanup = prevent_cleanup;
request_info->headers_received = 0;
request_info->end_callback = end_callback;
request_info->data_callback = data_callback;
request_info->headers_callback = headers_callback;
//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, data_callback);
curl_easy_setopt(http_handle, CURLOPT_PRIVATE, request_info);
curl_easy_setopt(http_handle, CURLOPT_WRITEDATA, request_info);
curl_multi_add_handle(multi_handle, http_handle);
return http_handle;
}
void finish_request(CURLMsg *curl_msg) {
//get initial request info from the http handle
struct RequestInfo *request_info;
CURL *http_handle = curl_msg->easy_handle;
curl_easy_getinfo(http_handle, CURLINFO_PRIVATE, &request_info);
int error = (int) curl_msg->data.result;
long response_code;
curl_easy_getinfo(http_handle, CURLINFO_RESPONSE_CODE, &response_code);
if (request_info->abort_on_redirect && response_code / 100 == 3) {
error = ERROR_REDIRECT_DISALLOWED;
}
void forward_headers(struct RequestInfo *request_info) {
CURL *http_handle = request_info->http_handle;
//create new json object with response info
cJSON* response_json = cJSON_CreateObject();
long response_code;
curl_easy_getinfo(http_handle, CURLINFO_RESPONSE_CODE, &response_code);
cJSON* status_item = cJSON_CreateNumber(response_code);
cJSON_AddItemToObject(response_json, "status", status_item);
@ -188,10 +192,27 @@ void finish_request(CURLMsg *curl_msg) {
char* response_json_str = cJSON_Print(response_json);
cJSON_Delete(response_json);
(*request_info->headers_callback)(response_json_str);
}
void finish_request(CURLMsg *curl_msg) {
//get initial request info from the http handle
struct RequestInfo *request_info;
CURL *http_handle = curl_msg->easy_handle;
curl_easy_getinfo(http_handle, CURLINFO_PRIVATE, &request_info);
int error = (int) curl_msg->data.result;
long response_code;
curl_easy_getinfo(http_handle, CURLINFO_RESPONSE_CODE, &response_code);
if (request_info->abort_on_redirect && response_code / 100 == 3) {
error = ERROR_REDIRECT_DISALLOWED;
}
//clean up curl
curl_slist_free_all(request_info->headers_list);
(*request_info->end_callback)(error, response_json_str);
(*request_info->end_callback)(error);
if (request_info->prevent_cleanup) {
return;
}

View file

@ -1,12 +1,19 @@
#include "curl/curl.h"
typedef void(*DataCallback)(char* chunk_ptr, int chunk_size);
typedef void(*EndCallback)(int error, char* response_json);
typedef void(*EndCallback)(int error);
typedef void(*HeadersCallback)(char* response_json);
struct RequestInfo {
CURL* http_handle;
int abort_on_redirect;
int prevent_cleanup;
int headers_received;
struct CURLMsg *curl_msg;
struct curl_slist* headers_list;
DataCallback data_callback;
EndCallback end_callback;
HeadersCallback headers_callback;
};
struct WSResult {