From f76a8c5ca24592eb87486f8e50b5cbd80bdd4a05 Mon Sep 17 00:00:00 2001 From: zhengchao Date: Thu, 29 Nov 2018 16:24:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E5=A4=84=E5=86=85?= =?UTF-8?q?=E5=AD=98=E6=B3=84=E6=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/include/tfe_http.h | 4 +-- common/src/tfe_http.cpp | 1 + common/src/tfe_rpc.cpp | 34 +++++++++++++------ platform/src/key_keeper.cpp | 28 +++++++++------ platform/src/ssl_stream.cpp | 2 +- plugin/business/pangu-http/src/pangu_http.cpp | 3 +- plugin/protocol/http/src/http_half.cpp | 4 +-- 7 files changed, 48 insertions(+), 28 deletions(-) diff --git a/common/include/tfe_http.h b/common/include/tfe_http.h index 327d84d..774cd9d 100644 --- a/common/include/tfe_http.h +++ b/common/include/tfe_http.h @@ -291,8 +291,8 @@ typedef void (http_session_end_cb)(const struct tfe_stream * stream, /* Tools functions for standard HTTP header field name */ struct http_field_name * http_field_name_duplicate(const struct http_field_name * orig); int http_field_name_compare(const struct http_field_name * lvalue, const struct http_field_name * rvalue); -const char * http_field_to_string(const struct http_field_name * field); -void http_field_destory(struct http_field_name *); +const char * http_field_name_to_string(const struct http_field_name * field); +void http_field_name_destory(struct http_field_name *); /* Tools functions for standard HTTP method */ const char * http_std_method_to_string(enum tfe_http_std_method method); diff --git a/common/src/tfe_http.cpp b/common/src/tfe_http.cpp index 903d51e..234c96b 100644 --- a/common/src/tfe_http.cpp +++ b/common/src/tfe_http.cpp @@ -117,6 +117,7 @@ const char * http_field_to_string(const struct http_field_name * field) void http_field_destory(struct http_field_name * field) { + if(field->field_id == TFE_HTTP_UNKNOWN_FIELD) FREE(&field->field_name); free(field); } diff --git a/common/src/tfe_rpc.cpp b/common/src/tfe_rpc.cpp index 8056501..8496435 100644 --- a/common/src/tfe_rpc.cpp +++ b/common/src/tfe_rpc.cpp @@ -20,6 +20,7 @@ struct tfe_rpc_ctx { struct event_base * evbase; enum TFE_RPC_FLAG flag; + struct evhttp_connection* connection; }; /* @@ -41,6 +42,9 @@ int read_header_done_cb(struct evhttp_request* response, void* arg) static void tfe_rpc_promise_free_ctx(void* ctx) { + struct tfe_rpc_ctx* _ctx=(struct tfe_rpc_ctx*)ctx; + evhttp_connection_free(_ctx->connection); + _ctx->connection=NULL; free(ctx); ctx = NULL; return; @@ -190,13 +194,13 @@ void tfe_rpc_async_ask(struct future* f, const char* url, enum TFE_RPC_METHOD me if(NULL == uri) { _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "parse url failed!"); - return; + goto error_out; } const char* host = evhttp_uri_get_host(uri); if(!host) { _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "parse host failed!"); - return; + goto error_out; } int port = evhttp_uri_get_port(uri); if(port < 0) @@ -204,13 +208,13 @@ void tfe_rpc_async_ask(struct future* f, const char* url, enum TFE_RPC_METHOD me port = 80; } - struct evhttp_connection* connection = evhttp_connection_base_new(evbase, dnsbase, host, port); - if (!connection) + ctx->connection = evhttp_connection_base_new(evbase, dnsbase, host, port); + if (!ctx->connection) { _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "create connection failed!"); - return; + goto error_out; } - evhttp_connection_set_closecb(connection, connection_close_cb, evbase); + evhttp_connection_set_closecb(ctx->connection, connection_close_cb, evbase); struct evhttp_request* request = evhttp_request_new(get_response_cb, (void*)p); //evhttp_request_set_header_cb(request, read_header_done_cb); if(flag == CHUNK_CB) @@ -224,23 +228,31 @@ void tfe_rpc_async_ask(struct future* f, const char* url, enum TFE_RPC_METHOD me //printf("request url is %s\n", request_url); if(request_url == NULL) { - _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "get request url failed"); - return; + _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "get request url failed"); + goto error_out; } switch(method) { case GET: - evhttp_make_request(connection, request, EVHTTP_REQ_GET, request_url); + evhttp_make_request(ctx->connection, request, EVHTTP_REQ_GET, request_url); break; case POST: evbuffer_add(request->output_buffer, data, data_len); - evhttp_make_request(connection, request, EVHTTP_REQ_POST, request_url); + evhttp_make_request(ctx->connection, request, EVHTTP_REQ_POST, request_url); break; default: _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "method is invalid!"); - return; + goto error_out; } free(request_url); + evhttp_uri_free(uri); + return; + +error_out: + if(uri) evhttp_uri_free(uri); + promise_dettach_ctx(p); + tfe_rpc_promise_free_ctx(ctx); + return; } struct tfe_rpc_response_result* tfe_rpc_release(void* result) diff --git a/platform/src/key_keeper.cpp b/platform/src/key_keeper.cpp index ec17040..eaf98b0 100644 --- a/platform/src/key_keeper.cpp +++ b/platform/src/key_keeper.cpp @@ -300,12 +300,12 @@ static struct keyring_private* get_keyring_from_response(const char* data) STACK_OF(X509)* chain = NULL; if(data == NULL) { - return NULL; + goto error_out; } cJSON* data_json = cJSON_Parse(data); if(data_json == NULL) { - return NULL; + goto error_out; } cJSON* cert_json = NULL; cJSON* key_json = NULL; @@ -319,8 +319,7 @@ static struct keyring_private* get_keyring_from_response(const char* data) } if(cert == NULL) { - err_out(cert, key, chain); - return NULL; + goto error_out; } if (key_json && key_json->valuestring != NULL) { @@ -328,13 +327,11 @@ static struct keyring_private* get_keyring_from_response(const char* data) } if(key == NULL) { - err_out(cert, key, chain); - return NULL; + goto error_out; } if(chain_json == NULL) { - err_out(cert, key, chain); - return NULL; + goto error_out; } cJSON* chain_cert_json = NULL; chain = sk_X509_new_null(); @@ -347,8 +344,7 @@ static struct keyring_private* get_keyring_from_response(const char* data) } if(chain_cert == NULL) { - err_out(cert, key, chain); - return NULL; + goto error_out; } sk_X509_push(chain, chain_cert); ssl_x509_refcount_inc(chain_cert); @@ -359,8 +355,17 @@ static struct keyring_private* get_keyring_from_response(const char* data) keyring_set_chain(_kyr, chain); X509_free(cert); EVP_PKEY_free(key); - sk_X509_pop_free(chain, X509_free); + sk_X509_pop_free(chain, X509_free); + cJSON_free(data_json); return _kyr; + +error_out: + if(data_json!=NULL) cJSON_free(data_json); + if(cert) X509_free(cert); + if(key) EVP_PKEY_free(key); + if(chain) sk_X509_pop_free(chain, X509_free); + return NULL; + } static long keyring_local_cache_query_cb(void * data, const uchar * key, uint size, void * user_arg) @@ -600,6 +605,7 @@ char* url_escape(char* url) { _url = curl_easy_escape(curl, url, strlen(url)); } + curl_easy_cleanup(curl); return _url; } diff --git a/platform/src/ssl_stream.cpp b/platform/src/ssl_stream.cpp index 5e6c2a0..79e0e77 100644 --- a/platform/src/ssl_stream.cpp +++ b/platform/src/ssl_stream.cpp @@ -1434,7 +1434,7 @@ void ssl_connect_client_ctx_free(struct ssl_connect_client_ctx * ctx) { ssl_stream_free(ctx->downstream); } - + free(ctx); return; } diff --git a/plugin/business/pangu-http/src/pangu_http.cpp b/plugin/business/pangu-http/src/pangu_http.cpp index 1e8634f..f8553fc 100644 --- a/plugin/business/pangu-http/src/pangu_http.cpp +++ b/plugin/business/pangu-http/src/pangu_http.cpp @@ -463,6 +463,7 @@ void http_repl_ctx_free(struct replace_ctx* rep_ctx) FREE(&(rep_ctx->rule[i].find)); FREE(&(rep_ctx->rule[i].replace_with)); } + FREE(&(rep_ctx->rule)); if (rep_ctx->http_body) { evbuffer_free(rep_ctx->http_body); @@ -939,7 +940,7 @@ enum pangu_action http_scan(const struct tfe_http_session * session, enum tfe_ht break; } - const char * str_field_name = http_field_to_string(&field_name); + const char * str_field_name = http_field_name_to_string(&field_name); scan_ret = Maat_set_scan_status(g_pangu_rt->maat, &(ctx->scan_mid), MAAT_SET_SCAN_DISTRICT, str_field_name, strlen(str_field_name)); diff --git a/plugin/protocol/http/src/http_half.cpp b/plugin/protocol/http/src/http_half.cpp index 499fd61..d4200ec 100644 --- a/plugin/protocol/http/src/http_half.cpp +++ b/plugin/protocol/http/src/http_half.cpp @@ -709,7 +709,7 @@ void hf_private_destory(struct http_half_private * hf_private) TAILQ_FOREACH_SAFE(header_iter, &hf_private->header_list, next, header_titer) { TAILQ_REMOVE(&hf_private->header_list, header_iter, next); - free(header_iter->field); + http_field_name_destory(header_iter->field); free(header_iter->value); free(header_iter); } @@ -1110,7 +1110,7 @@ void hf_private_construct(struct http_half_private * hf_private) for (const char * str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name); str_value != NULL; str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name)) { - const char * str_field = http_field_to_string(&field_name); + const char * str_field = http_field_name_to_string(&field_name); evbuffer_add_printf(hf_private->evbuf_raw, "%s: %s\r\n", str_field, str_value); }