reorganize client code

This commit is contained in:
ading2210 2024-01-26 23:06:13 -05:00
parent 9cc0e4178b
commit 717331bfc1
9 changed files with 8 additions and 7 deletions

199
client/libcurl/main.c Normal file
View file

@ -0,0 +1,199 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <emscripten.h>
#include "curl/curl.h"
#include "curl/easy.h"
#include "curl/header.h"
#include "cjson/cJSON.h"
#include "cacert.h"
#include "curl/multi.h"
#include "util.h"
#include "types.h"
void finish_request(CURLMsg *curl_msg);
#define ERROR_REDIRECT_DISALLOWED -1
CURLM *multi_handle;
int request_active = 0;
int write_function(void *data, size_t size, size_t nmemb, DataCallback data_callback) {
long real_size = size * nmemb;
char* chunk = malloc(real_size);
memcpy(chunk, data, real_size);
data_callback(chunk, real_size);
free(chunk);
return real_size;
}
int active_requests() {
return request_active;
}
void tick_request() {
CURLMcode mc;
struct CURLMsg *curl_msg;
request_active = 1;
mc = curl_multi_perform(multi_handle, &request_active);
int msgq = 0;
curl_msg = curl_multi_info_read(multi_handle, &msgq);
if (curl_msg && curl_msg->msg == CURLMSG_DONE) {
finish_request(curl_msg);
}
}
CURL* start_request(const char* url, const char* json_params, DataCallback data_callback, EndCallback end_callback, const char* body, int body_length) {
CURL *http_handle = curl_easy_init();
int abort_on_redirect = 0;
int prevent_cleanup = 0;
curl_easy_setopt(http_handle, CURLOPT_URL, url);
curl_easy_setopt(http_handle, CURLOPT_CAINFO, "/cacert.pem");
curl_easy_setopt(http_handle, CURLOPT_CAPATH, "/cacert.pem");
//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, "");
curl_easy_setopt(http_handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
//if url is a websocket, tell curl that we should handle the connection manually
if (starts_with(url, "wss://") || starts_with(url, "ws://")) {
curl_easy_setopt(http_handle, CURLOPT_CONNECT_ONLY, 2L);
prevent_cleanup = 1;
}
//parse json options
cJSON* request_json = cJSON_Parse(json_params);
cJSON* item = NULL;
struct curl_slist* headers_list = NULL;
cJSON_ArrayForEach(item, request_json) {
char* key = item->string;
if (strcmp(key, "_libcurl_verbose") == 0) {
curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
}
if (strcmp(key, "method") == 0 && cJSON_IsString(item)) {
curl_easy_setopt(http_handle, CURLOPT_CUSTOMREQUEST, item->valuestring);
}
if (strcmp(key, "headers") == 0 && cJSON_IsObject(item)) {
cJSON* header = NULL;
cJSON_ArrayForEach(header, item) {
if (!cJSON_IsString(header)) continue;
int header_length = strlen(header->string) + strlen(header->valuestring) + 2;
char* header_str = malloc(header_length+1);
header_str[header_length] = 0;
sprintf(header_str, "%s: %s", header->string, header->valuestring);
headers_list = curl_slist_append(headers_list, header_str);
free(header_str);
}
curl_easy_setopt(http_handle, CURLOPT_HTTPHEADER, headers_list);
}
if (strcmp(key, "redirect") == 0 && cJSON_IsString(item)) {
if (strcmp(item->valuestring, "error") == 0) {
abort_on_redirect = 1;
curl_easy_setopt(http_handle, CURLOPT_FOLLOWLOCATION, 0);
}
else if (strcmp(item->valuestring, "manual") == 0) {
curl_easy_setopt(http_handle, CURLOPT_FOLLOWLOCATION, 0);
}
}
}
cJSON_Delete(request_json);
//add post data if specified
if (body != NULL) {
curl_easy_setopt(http_handle, CURLOPT_POSTFIELDS, body);
curl_easy_setopt(http_handle, CURLOPT_POSTFIELDSIZE, body_length);
}
struct RequestInfo *request_info = malloc(sizeof(struct RequestInfo));
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;
curl_easy_setopt(http_handle, CURLOPT_PRIVATE, 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;
}
//create new json object with response info
cJSON* response_json = cJSON_CreateObject();
cJSON* status_item = cJSON_CreateNumber(response_code);
cJSON_AddItemToObject(response_json, "status", status_item);
char* response_url;
curl_easy_getinfo(http_handle, CURLINFO_EFFECTIVE_URL, &response_url);
cJSON* url_item = cJSON_CreateString(response_url);
cJSON_AddItemToObject(response_json, "url", url_item);
cJSON* headers_item = cJSON_CreateObject();
struct curl_header *prev_header = NULL;
struct curl_header *header = NULL;
while ((header = curl_easy_nextheader(http_handle, CURLH_HEADER, -1, prev_header))) {
cJSON* header_entry = cJSON_CreateString(header->value);
cJSON_AddItemToObject(headers_item, header->name, header_entry);
prev_header = header;
}
cJSON_AddItemToObject(response_json, "headers", headers_item);
long redirect_count;
curl_easy_getinfo(http_handle, CURLINFO_REDIRECT_COUNT, &redirect_count);
cJSON* redirects_item = cJSON_CreateBool(redirect_count > 0);
cJSON_AddItemToObject(response_json, "redirected", redirects_item);
char* response_json_str = cJSON_Print(response_json);
cJSON_Delete(response_json);
//clean up curl
curl_slist_free_all(request_info->headers_list);
(*request_info->end_callback)(error, response_json_str);
if (request_info->prevent_cleanup) {
return;
}
curl_multi_remove_handle(multi_handle, http_handle);
curl_easy_cleanup(http_handle);
free(request_info);
}
void init_curl() {
curl_global_init(CURL_GLOBAL_DEFAULT);
multi_handle = curl_multi_init();
FILE* file = fopen("/cacert.pem", "wb");
fwrite(_cacert_pem, 1, _cacert_pem_len, file);
fclose(file);
}

19
client/libcurl/types.h Normal file
View file

@ -0,0 +1,19 @@
typedef void(*DataCallback)(char* chunk_ptr, int chunk_size);
typedef void(*EndCallback)(int error, char* response_json);
struct RequestInfo {
int abort_on_redirect;
int prevent_cleanup;
struct CURLMsg *curl_msg;
struct curl_slist* headers_list;
EndCallback end_callback;
};
struct WSResult {
int res;
int buffer_size;
int closed;
int bytes_left;
int is_text;
char* buffer;
};

5
client/libcurl/util.c Normal file
View file

@ -0,0 +1,5 @@
#include <string.h>
int starts_with(const char *a, const char *b) {
return strncmp(a, b, strlen(b)) == 0;
}

1
client/libcurl/util.h Normal file
View file

@ -0,0 +1 @@
int starts_with(const char *a, const char *b);

View file

@ -0,0 +1,66 @@
#include <stdlib.h>
#include "curl/curl.h"
#include "curl/websockets.h"
#include "types.h"
extern CURLM* multi_handle;
struct WSResult* recv_from_websocket(CURL* http_handle, int buffer_size) {
const struct curl_ws_frame* ws_meta;
char* buffer = malloc(buffer_size);
size_t result_len;
CURLcode res = curl_ws_recv(http_handle, buffer, buffer_size, &result_len, &ws_meta);
struct WSResult* result = malloc(sizeof(struct WSResult));
result->buffer_size = result_len;
result->buffer = buffer;
result->res = (int) res;
result->closed = (ws_meta->flags & CURLWS_CLOSE);
result->is_text = (ws_meta->flags & CURLWS_TEXT);
result->bytes_left = ws_meta->bytesleft;
return result;
}
int send_to_websocket(CURL* http_handle, const char* data, int data_len, int is_text) {
size_t sent;
unsigned int flags = CURLWS_BINARY;
if (is_text) flags = CURLWS_TEXT;
CURLcode res = curl_ws_send(http_handle, data, data_len, &sent, 0, flags);
return (int) res;
}
void close_websocket(CURL* http_handle) {
size_t sent;
curl_ws_send(http_handle, "", 0, &sent, 0, CURLWS_CLOSE);
}
//allow the main code to automatically clean up this websocket
void cleanup_websocket(CURL* http_handle) {
struct RequestInfo *request_info;
curl_easy_getinfo(http_handle, CURLINFO_PRIVATE, &request_info);
curl_multi_remove_handle(multi_handle, http_handle);
curl_easy_cleanup(http_handle);
free(request_info);
}
int get_result_size (const struct WSResult* result) {
return result->buffer_size;
}
char* get_result_buffer (const struct WSResult* result) {
return result->buffer;
}
int get_result_code (const struct WSResult* result) {
return result->res;
}
int get_result_closed (const struct WSResult* result) {
return result->closed;
}
int get_result_bytes_left (const struct WSResult* result) {
return result->bytes_left;
}
int get_result_is_text (const struct WSResult* result) {
return result->is_text;
}