mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-13 14:30:02 -04:00
create session class and refactor js to use it
This commit is contained in:
parent
a282734c13
commit
0a5ace96fb
14 changed files with 359 additions and 192 deletions
|
@ -14,8 +14,6 @@
|
|||
void finish_request(CURLMsg *curl_msg);
|
||||
void forward_headers(struct RequestInfo *request_info);
|
||||
|
||||
CURLM *multi_handle;
|
||||
int request_active = 0;
|
||||
struct curl_blob cacert_blob;
|
||||
|
||||
size_t write_function(char *data, size_t size, size_t nmemb, struct RequestInfo *request_info) {
|
||||
|
@ -30,25 +28,7 @@ size_t write_function(char *data, size_t size, size_t nmemb, struct RequestInfo
|
|||
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* create_handle(const char* url, DataCallback data_callback, EndCallback end_callback, HeadersCallback headers_callback) {
|
||||
CURL* create_request(const char* url, DataCallback data_callback, EndCallback end_callback, HeadersCallback headers_callback) {
|
||||
CURL *http_handle = curl_easy_init();
|
||||
|
||||
//create request metadata struct
|
||||
|
@ -57,7 +37,6 @@ CURL* create_handle(const char* url, DataCallback data_callback, EndCallback end
|
|||
request_info->curl_msg = NULL;
|
||||
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;
|
||||
|
@ -73,10 +52,6 @@ CURL* create_handle(const char* url, DataCallback data_callback, EndCallback end
|
|||
return http_handle;
|
||||
}
|
||||
|
||||
void start_request(CURL* http_handle) {
|
||||
curl_multi_add_handle(multi_handle, http_handle);
|
||||
}
|
||||
|
||||
void forward_headers(struct RequestInfo *request_info) {
|
||||
request_info->headers_received = 1;
|
||||
(*request_info->headers_callback)();
|
||||
|
@ -96,19 +71,6 @@ void finish_request(CURLMsg *curl_msg) {
|
|||
curl_slist_free_all(request_info->headers_list);
|
||||
}
|
||||
(*request_info->end_callback)(error);
|
||||
if (request_info->prevent_cleanup) {
|
||||
return;
|
||||
}
|
||||
curl_multi_remove_handle(multi_handle, http_handle);
|
||||
curl_easy_cleanup(http_handle);
|
||||
free(request_info);
|
||||
}
|
||||
|
||||
void cleanup_handle(CURL* 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);
|
||||
}
|
||||
|
||||
unsigned char* get_cacert() {
|
||||
|
@ -117,8 +79,6 @@ unsigned char* get_cacert() {
|
|||
|
||||
void init_curl() {
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
multi_handle = curl_multi_init();
|
||||
|
||||
cacert_blob.data = _cacert_pem;
|
||||
cacert_blob.len = _cacert_pem_len;
|
||||
cacert_blob.flags = CURL_BLOB_NOCOPY;
|
3
client/libcurl/request.h
Normal file
3
client/libcurl/request.h
Normal file
|
@ -0,0 +1,3 @@
|
|||
#include "curl/multi.h"
|
||||
|
||||
void finish_request(CURLMsg *curl_msg);
|
52
client/libcurl/session.c
Normal file
52
client/libcurl/session.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "curl/multi.h"
|
||||
#include "curl/curl.h"
|
||||
|
||||
#include "types.h"
|
||||
#include "request.h"
|
||||
#include "util.h"
|
||||
|
||||
struct SessionInfo* session_create() {
|
||||
struct SessionInfo *session = malloc(sizeof(struct SessionInfo));
|
||||
session->multi_handle = curl_multi_init();
|
||||
session->request_active = 0;
|
||||
return session;
|
||||
}
|
||||
|
||||
void session_perform(struct SessionInfo *session) {
|
||||
CURLMcode mc;
|
||||
session->request_active = 0;
|
||||
mc = curl_multi_perform(session->multi_handle, &session->request_active);
|
||||
|
||||
int msgq = 0;
|
||||
struct CURLMsg *curl_msg;
|
||||
curl_msg = curl_multi_info_read(session->multi_handle, &msgq);
|
||||
if (curl_msg && curl_msg->msg == CURLMSG_DONE) {
|
||||
finish_request(curl_msg);
|
||||
}
|
||||
}
|
||||
|
||||
void session_set_options(struct SessionInfo *session, int connections_limit, int cache_limit) {
|
||||
curl_multi_setopt(session->multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, connections_limit);
|
||||
curl_multi_setopt(session->multi_handle, CURLMOPT_MAXCONNECTS, cache_limit);
|
||||
}
|
||||
|
||||
void session_add_request(struct SessionInfo *session, CURL* http_handle) {
|
||||
curl_multi_add_handle(session->multi_handle, http_handle);
|
||||
}
|
||||
|
||||
int session_get_active(struct SessionInfo *session) {
|
||||
return session->request_active;
|
||||
}
|
||||
|
||||
void session_remove_request(struct SessionInfo *session, CURL* http_handle) {
|
||||
struct RequestInfo *request_info = get_request_info(http_handle);
|
||||
curl_multi_remove_handle(session->multi_handle, http_handle);
|
||||
curl_easy_cleanup(http_handle);
|
||||
free(request_info);
|
||||
}
|
||||
|
||||
void session_cleanup(struct SessionInfo *session) {
|
||||
curl_multi_cleanup(session->multi_handle);
|
||||
}
|
|
@ -31,5 +31,4 @@ void tls_socket_set_options(CURL* http_handle, int verbose) {
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ typedef void(*HeadersCallback)();
|
|||
|
||||
struct RequestInfo {
|
||||
CURL* http_handle;
|
||||
int prevent_cleanup;
|
||||
int headers_received;
|
||||
struct CURLMsg *curl_msg;
|
||||
struct curl_slist* headers_list;
|
||||
|
@ -22,4 +21,9 @@ struct WSResult {
|
|||
int bytes_left;
|
||||
int is_text;
|
||||
char* buffer;
|
||||
};
|
||||
|
||||
struct SessionInfo {
|
||||
CURLM* multi_handle;
|
||||
int request_active;
|
||||
};
|
|
@ -40,7 +40,6 @@ void close_websocket(CURL* http_handle) {
|
|||
void websocket_set_options(CURL* 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;
|
||||
}
|
||||
|
||||
int get_result_size (const struct WSResult* result) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue