diff --git a/client/exported_funcs.txt b/client/exported_funcs.txt index 478d8ab..6fb892d 100644 --- a/client/exported_funcs.txt +++ b/client/exported_funcs.txt @@ -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 diff --git a/client/javascript/tls_socket.js b/client/javascript/tls_socket.js index a5249ac..aa1b8f5 100644 --- a/client/javascript/tls_socket.js +++ b/client/javascript/tls_socket.js @@ -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() { diff --git a/client/libcurl/http.c b/client/libcurl/http.c index 6e46711..ffdbc3e 100644 --- a/client/libcurl/http.c +++ b/client/libcurl/http.c @@ -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(); diff --git a/client/libcurl/main.c b/client/libcurl/main.c index 16419be..b1cc4f5 100644 --- a/client/libcurl/main.c +++ b/client/libcurl/main.c @@ -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); diff --git a/client/libcurl/tls_socket.c b/client/libcurl/tls_socket.c index c890769..f6a1802 100644 --- a/client/libcurl/tls_socket.c +++ b/client/libcurl/tls_socket.c @@ -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; @@ -21,4 +23,13 @@ int send_to_socket(CURL* http_handle, const char* data, int data_len) { size_t sent; CURLcode res = curl_easy_send(http_handle, data, data_len, &sent); return (int) res; -} \ No newline at end of file +} + +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; +} diff --git a/client/libcurl/util.c b/client/libcurl/util.c index 80d03f4..9687891 100644 --- a/client/libcurl/util.c +++ b/client/libcurl/util.c @@ -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; diff --git a/client/libcurl/util.h b/client/libcurl/util.h index f171ae2..c878f4b 100644 --- a/client/libcurl/util.h +++ b/client/libcurl/util.h @@ -2,4 +2,4 @@ int starts_with(const char *a, const char *b); -struct RequestInfo* get_handle_info(CURL* http_handle); \ No newline at end of file +struct RequestInfo* get_request_info(CURL* http_handle); \ No newline at end of file diff --git a/client/libcurl/websocket.c b/client/libcurl/websocket.c index e9ac026..4e2101b 100644 --- a/client/libcurl/websocket.c +++ b/client/libcurl/websocket.c @@ -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; }