fix tls sockets

This commit is contained in:
ading2210 2024-03-12 17:43:59 -04:00
parent 1748ca7dd9
commit 45b0084c79
8 changed files with 29 additions and 25 deletions

View file

@ -23,6 +23,7 @@ get_result_closed
get_result_bytes_left
get_result_is_text
tls_socket_set_options
recv_from_socket
send_to_socket

View file

@ -39,14 +39,10 @@ class TLSSocket {
this.cleanup(error);
}
}
let request_options = {
_connect_only: 1,
}
if (this.options.verbose) {
request_options._libcurl_verbose = 1;
}
this.http_handle = perform_request(this.url, request_options, data_callback, finish_callback, headers_callback, null);
this.http_handle = create_handle(this.url, data_callback, finish_callback, headers_callback);
_tls_socket_set_options(this.http_handle, +this.options.verbose);
start_request(this.http_handle);
}
recv() {

View file

@ -8,7 +8,7 @@
#include "util.h"
void http_set_options(CURL* http_handle, const char* json_params, const char* body, int body_length) {
struct RequestInfo *request_info = get_handle_info(http_handle);
struct RequestInfo *request_info = get_request_info(http_handle);
//some default options
curl_easy_setopt(http_handle, CURLOPT_FOLLOWLOCATION, 1);
@ -18,7 +18,7 @@ void http_set_options(CURL* http_handle, const char* json_params, const char* bo
//parse json options
cJSON* request_json = cJSON_Parse(json_params);
cJSON* item = NULL;
struct curl_slist* headers_list = malloc(sizeof(struct curl_slist));
struct curl_slist* headers_list = NULL;
cJSON_ArrayForEach(item, request_json) {
char* key = item->string;
@ -27,13 +27,6 @@ void http_set_options(CURL* http_handle, const char* json_params, const char* bo
curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
}
if (strcmp(key, "_connect_only") == 0) {
curl_easy_setopt(http_handle, CURLOPT_CONNECT_ONLY, 1L);
curl_easy_setopt(http_handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_easy_setopt(http_handle, CURLOPT_SSL_ENABLE_ALPN, 0L);
request_info->prevent_cleanup = 1;
}
if (strcmp(key, "method") == 0 && cJSON_IsString(item)) {
curl_easy_setopt(http_handle, CURLOPT_CUSTOMREQUEST, item->valuestring);
}
@ -73,7 +66,7 @@ void http_set_options(CURL* http_handle, const char* json_params, const char* bo
}
char* http_get_info(CURL* http_handle) {
struct RequestInfo *request_info = get_handle_info(http_handle);
struct RequestInfo *request_info = get_request_info(http_handle);
//create new json object with response info
cJSON* response_json = cJSON_CreateObject();

View file

@ -55,8 +55,9 @@ CURL* create_handle(const char* url, DataCallback data_callback, EndCallback end
struct RequestInfo *request_info = malloc(sizeof(struct RequestInfo));
request_info->http_handle = http_handle;
request_info->curl_msg = NULL;
request_info->prevent_cleanup = 0;
request_info->headers_list = NULL;
request_info->headers_received = 0;
request_info->prevent_cleanup = 0;
request_info->end_callback = end_callback;
request_info->data_callback = data_callback;
request_info->headers_callback = headers_callback;
@ -83,7 +84,7 @@ void forward_headers(struct RequestInfo *request_info) {
void finish_request(CURLMsg *curl_msg) {
CURL *http_handle = curl_msg->easy_handle;
struct RequestInfo *request_info = get_handle_info(http_handle);
struct RequestInfo *request_info = get_request_info(http_handle);
int error = (int) curl_msg->data.result;
if (!request_info->headers_received && error == 0) {
@ -91,7 +92,9 @@ void finish_request(CURLMsg *curl_msg) {
}
//clean up curl
curl_slist_free_all(request_info->headers_list);
if (request_info->headers_list != NULL) {
curl_slist_free_all(request_info->headers_list);
}
(*request_info->end_callback)(error);
if (request_info->prevent_cleanup) {
return;
@ -102,7 +105,7 @@ void finish_request(CURLMsg *curl_msg) {
}
void cleanup_handle(CURL* http_handle) {
struct RequestInfo *request_info = get_handle_info(http_handle);
struct RequestInfo *request_info = get_request_info(http_handle);
curl_multi_remove_handle(multi_handle, http_handle);
curl_easy_cleanup(http_handle);
free(request_info);

View file

@ -2,7 +2,9 @@
#include "curl/curl.h"
#include "curl/easy.h"
#include "types.h"
#include "util.h"
struct WSResult* recv_from_socket(CURL* http_handle, int buffer_size) {
size_t nread;
@ -22,3 +24,12 @@ int send_to_socket(CURL* http_handle, const char* data, int data_len) {
CURLcode res = curl_easy_send(http_handle, data, data_len, &sent);
return (int) res;
}
void tls_socket_set_options(CURL* http_handle, int verbose) {
struct RequestInfo *request_info = get_request_info(http_handle);
curl_easy_setopt(http_handle, CURLOPT_CONNECT_ONLY, 1L);
curl_easy_setopt(http_handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_easy_setopt(http_handle, CURLOPT_SSL_ENABLE_ALPN, 0L);
curl_easy_setopt(http_handle, CURLOPT_VERBOSE, (long) verbose);
request_info->prevent_cleanup = 1;
}

View file

@ -40,7 +40,7 @@ const char* get_error_str(CURLcode error_code) {
return curl_easy_strerror(error_code);
}
struct RequestInfo *get_handle_info(CURL* http_handle) {
struct RequestInfo *get_request_info(CURL* http_handle) {
struct RequestInfo *request_info;
curl_easy_getinfo(http_handle, CURLINFO_PRIVATE, &request_info);
return request_info;

View file

@ -2,4 +2,4 @@
int starts_with(const char *a, const char *b);
struct RequestInfo* get_handle_info(CURL* http_handle);
struct RequestInfo* get_request_info(CURL* http_handle);

View file

@ -38,7 +38,7 @@ void close_websocket(CURL* http_handle) {
}
void websocket_set_options(CURL* http_handle) {
struct RequestInfo *request_info = get_handle_info(http_handle);
struct RequestInfo *request_info = get_request_info(http_handle);
curl_easy_setopt(http_handle, CURLOPT_CONNECT_ONLY, 2L);
request_info->prevent_cleanup = 1;
}