From: pradeep kumar B Date: Thu, 17 Mar 2016 13:42:59 +0000 (+0530) Subject: [capi-http] Handled aborted callback X-Git-Tag: submit/tizen/20160511.063207~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ebc6e7feba62238c95933193232aa65006ded3e3;p=platform%2Fcore%2Fapi%2Fhttp.git [capi-http] Handled aborted callback 1. Fixed memory leaks 2. Handle aborted callback for different error cases 3. Fixed the crash Change-Id: I0990195720bec321b83f05927d4513ddfc76c244 Signed-off-by: pradeep kumar B --- diff --git a/include/http.h b/include/http.h index c3bd308..c339428 100644 --- a/include/http.h +++ b/include/http.h @@ -204,7 +204,7 @@ typedef void (*http_transaction_completed_cb)(http_transaction_h transaction, vo * @details Called when the http transaction is aborted. * @param[in] reason aborted reason code */ -typedef void (*http_transaction_aborted_cb)(http_transaction_h transaction, int reason); +typedef void (*http_transaction_aborted_cb)(http_transaction_h transaction, int reason, void *user_data); /** * @internal @@ -420,6 +420,8 @@ int http_transaction_set_uploaded_cb(http_transaction_h transaction, http_transa int http_transaction_set_completed_cb(http_transaction_h transaction, http_transaction_completed_cb completed_cb, void* user_data); +int http_transaction_set_aborted_cb(http_transaction_h http_transaction, http_transaction_aborted_cb aborted_cb, void* user_data); + int http_transaction_close_all(http_session_h session); diff --git a/include/http_private.h b/include/http_private.h index b0e4803..bbed38d 100644 --- a/include/http_private.h +++ b/include/http_private.h @@ -142,6 +142,7 @@ typedef struct { http_transaction_completed_cb completed_cb; void *completed_user_data; http_transaction_aborted_cb aborted_cb; + void *aborted_user_data; /*Progress Callbacks */ http_transaction_upload_progress_cb upload_progress_cb; diff --git a/src/http_session.c b/src/http_session.c index 2876a16..b092a3b 100644 --- a/src/http_session.c +++ b/src/http_session.c @@ -39,14 +39,23 @@ void _check_curl_multi_status(gpointer user_data) DBG("Completed -%s: result(%d)\n", url, curl_code); - if (curl_code == CURLE_OK) { - transaction->completed_cb(transaction, transaction->completed_user_data); - } else { - + switch (curl_code) { + case CURLE_OK: + if (transaction->completed_cb) + transaction->completed_cb(transaction, transaction->completed_user_data); + break; + case CURLE_COULDNT_RESOLVE_HOST: + case CURLE_COULDNT_CONNECT: + case CURLE_SSL_CONNECT_ERROR: + case CURLE_OPERATION_TIMEDOUT: + if (transaction->aborted_cb) + transaction->aborted_cb(transaction, (int)curl_code, transaction->aborted_user_data); + break; + default: + break; } curl_multi_remove_handle(session->multi_handle, curl_easy); - g_main_loop_quit((GMainLoop*)transaction->thread_loop); } message = curl_multi_info_read(session->multi_handle, &count); } diff --git a/src/http_transaction.c b/src/http_transaction.c index 4660729..11ec436 100644 --- a/src/http_transaction.c +++ b/src/http_transaction.c @@ -276,8 +276,6 @@ void* thread_callback(void *user_data) g_main_loop_run(transaction->thread_loop); - g_main_loop_unref(transaction->thread_loop); - transaction->thread_loop = NULL; DBG("thread exited.\n"); return NULL; @@ -369,8 +367,6 @@ API int http_transaction_close(http_transaction_h http_transaction) session->active_transaction_count--; if (transaction) { - g_thread_join(transaction->thread); - transaction->thread = NULL; if (transaction->easy_handle != NULL) { curl_easy_cleanup(transaction->easy_handle); @@ -384,7 +380,11 @@ API int http_transaction_close(http_transaction_h http_transaction) transaction->timeout = 0; transaction->verify_peer = 0; - transaction->ca_path = '\0'; + + if (transaction->ca_path) { + free(transaction->ca_path); + transaction->ca_path = NULL; + } transaction->error[0] = '\0'; transaction->header_cb = NULL; @@ -438,6 +438,16 @@ API int http_transaction_close(http_transaction_h http_transaction) free(header); } + _remove_transaction_from_list(transaction); + + g_main_loop_quit((GMainLoop*)transaction->thread_loop); + + g_main_loop_unref(transaction->thread_loop); + transaction->thread_loop = NULL; + + g_thread_join(transaction->thread); + transaction->thread = NULL; + free(transaction); transaction = NULL; } @@ -550,6 +560,20 @@ API int http_transaction_set_completed_cb(http_transaction_h http_transaction, h return HTTP_ERROR_NONE; } +API int http_transaction_set_aborted_cb(http_transaction_h http_transaction, http_transaction_aborted_cb aborted_cb, void* user_data) +{ + _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(http_transaction) is NULL\n"); + _retvm_if(aborted_cb == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(aborted_cb) is NULL\n"); + + __http_transaction_h *transaction = (__http_transaction_h *)http_transaction; + + transaction->aborted_cb = aborted_cb; + transaction->aborted_user_data = user_data; + + return HTTP_ERROR_NONE; +} API int http_transaction_unset_progress_cb(http_transaction_h http_transaction) { diff --git a/test/http_test.c b/test/http_test.c index 72bd458..f3cf8de 100644 --- a/test/http_test.c +++ b/test/http_test.c @@ -72,18 +72,35 @@ void print_response_header(http_transaction_h transaction_handle) DBG("URI(%s) HTTP version (%d) Status Code (%d) Status message (%s)\n", uri, version, status_code, status_text); } +void close_transaction(http_transaction_h transaction_handle) +{ + remove_http_header(transaction_handle); + + http_transaction_close(transaction_handle); + + //transaction_handle = NULL; +} + +void delete_session(http_session_h session) +{ + http_transaction_close_all(session_handle); + + http_delete_session(session_handle); + session_handle = NULL; + http_deinit(); +} //////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////Callbacks//////////////////////////////////////////////////////////////////////////// void transaction_header_cb(http_transaction_h transaction_handle, char *header, size_t header_len, void *user_data) { - DBG("########################## transaction_header_cb#########################################\n"); + DBG("########################## transaction_header_cb(%p)#########################################\n", transaction_handle); } void transaction_body_cb(http_transaction_h transaction_handle, char *body, size_t size, size_t nmemb, void *user_data) { - DBG("########################## transaction_body_cb#########################################\n"); + DBG("########################## transaction_body_cb(%p)#########################################\n", transaction_handle); int written = size * nmemb; DBG("Received: %d\n", written); //if (written) { @@ -93,14 +110,14 @@ void transaction_body_cb(http_transaction_h transaction_handle, char *body, size void transaction_write_cb(http_transaction_h transaction_handle, int recommended_chunk_size, void *user_data) { - DBG("########################## transaction_write_cb#########################################\n"); + DBG("########################## transaction_write_cb(%p)#########################################\n", transaction_handle); DBG("recommended_chunk_size:%d\n", recommended_chunk_size); } void transaction_completed_cb(http_transaction_h transaction_handle, void *user_data) { - DBG("########################## transaction_completed_cb#########################################\n"); + DBG("########################## transaction_completed_cb(%p)#########################################\n", transaction_handle); char *uri = NULL; @@ -113,10 +130,15 @@ void transaction_completed_cb(http_transaction_h transaction_handle, void *user_ g_main_loop_quit((GMainLoop*)mainloop); } -void transaction_aborted_cb(int reason) +void transaction_aborted_cb(http_transaction_h transaction_handle, int reason, void *user_data) { - DBG("########################## transaction_aborted_cb#########################################\n"); + DBG("########################## transaction_aborted_cb(%p)#########################################\n", transaction_handle); + close_transaction(transaction_handle); + count--; + + if (count == 0) + g_main_loop_quit((GMainLoop*)mainloop); } http_transaction_h create_http_request(http_session_h session_handle, gchar* host_url) @@ -159,11 +181,13 @@ int main() http_transaction_set_received_body_cb(transaction_handle1, transaction_body_cb, NULL); http_transaction_set_uploaded_cb(transaction_handle1, transaction_write_cb, NULL); http_transaction_set_completed_cb(transaction_handle1, transaction_completed_cb, NULL); + http_transaction_set_aborted_cb(transaction_handle1, transaction_aborted_cb, NULL); http_transaction_set_received_header_cb(transaction_handle2, transaction_header_cb, NULL); http_transaction_set_received_body_cb(transaction_handle2, transaction_body_cb, NULL); http_transaction_set_uploaded_cb(transaction_handle2, transaction_write_cb, NULL); http_transaction_set_completed_cb(transaction_handle2, transaction_completed_cb, NULL); + http_transaction_set_aborted_cb(transaction_handle2, transaction_aborted_cb, NULL); DBG("transaction1(%p), transaction2(%p)\n", transaction_handle1, transaction_handle2); submit_http_request(transaction_handle1); @@ -171,24 +195,8 @@ int main() g_main_loop_run(mainloop); - remove_http_header(transaction_handle1); - //http_transaction_close(transaction_handle1); - - remove_http_header(transaction_handle2); - //http_transaction_close(transaction_handle2); http_transaction_close_all(session_handle); - - if (transaction_handle1) DBG("transaction_handle1 remained(%p)\n", transaction_handle1); - if (transaction_handle2) DBG("transaction_handle2 remained(%p)\n", transaction_handle2); - - transaction_handle1 = NULL; - transaction_handle2 = NULL; - - http_delete_session(session_handle); - session_handle = NULL; - http_deinit(); - - g_main_loop_unref(mainloop); + delete_session(session_handle); DBG("########################## main:Exit#########################################\n"); return 0;